1 package de.lathanda.eos.game;
3 import java.util.Arrays;
4 import java.util.LinkedList;
5 import java.util.TreeMap;
6 import java.util.TreeSet;
8 import de.lathanda.eos.base.MutableColor;
9 import de.lathanda.eos.base.Picture2D;
10 import de.lathanda.eos.base.math.Point;
11 import de.lathanda.eos.game.geom.CollisionDetection;
27 public class Game implements Runnable {
47 private State state = State.PREPARE;
55 private long round = 0;
59 private final boolean[] keys =
new boolean[256];
63 private final boolean[] keysPressed =
new boolean[256];
67 private final boolean[] keysPress =
new boolean[256];
71 private final boolean[] mouseBtn =
new boolean[20];
75 private final boolean[] mouseBtnPress =
new boolean[20];
79 private final boolean[] mouseBtnPressed =
new boolean[20];
88 private final TreeSet<Sprite> drawingList =
new TreeSet<>();
92 private final TreeMap<Integer, Sprite> sprites =
new TreeMap<>();
96 private final LinkedList<Sprite> pendingDelete =
new LinkedList<>();
100 private final LinkedList<Sprite> pendingAdd =
new LinkedList<>();
107 private final Object DRAW_LOCK =
new Object();
109 private final Object CHANGE_LOCK =
new Object();
111 private final Object INPUT_LOCK =
new Object();
113 private GameFrame gameFrame;
139 public Game(
double width,
double height) {
143 public Game(
double width,
double height, String title) {
148 this(width, height, back, 30, title);
152 gameFrame =
new GameFrame(width, height, back,
this, title);
166 synchronized (INPUT_LOCK) {
168 keys[keyCode] =
true;
169 keysPress[keyCode] =
true;
170 }
catch (ArrayIndexOutOfBoundsException e) {
184 keys[keyCode] =
false;
185 }
catch (ArrayIndexOutOfBoundsException e) {
197 synchronized (INPUT_LOCK) {
199 mouseBtn[button] =
true;
200 mouseBtnPress[button] =
true;
201 }
catch (ArrayIndexOutOfBoundsException e) {
215 mouseBtn[button] =
false;
216 }
catch (ArrayIndexOutOfBoundsException e) {
235 state = State.RUNNING;
250 state = State.RUNNING;
251 (
new Thread(
this)).
start();
258 state = State.SHUTDOWN;
268 synchronized (DRAW_LOCK) {
269 for (
Sprite s : drawingList) {
275 }
catch (Throwable t) {
277 t.printStackTrace(System.err);
278 System.err.println(
"error occured while render, removing " + s);
291 for (
Sprite s : sprites.values()) {
296 s.update(round,
this);
299 }
catch (Throwable t) {
302 System.err.println(
"error occured while step execution, removing " + s);
306 synchronized (INPUT_LOCK) {
308 System.arraycopy(keysPress, 0, keysPressed, 0, keysPress.length);
309 Arrays.fill(keysPress,
false);
311 System.arraycopy(mouseBtnPress, 0, mouseBtnPressed, 0, mouseBtnPress.length);
312 Arrays.fill(mouseBtnPress,
false);
317 synchronized (CHANGE_LOCK) {
318 del =
new Sprite[pendingDelete.size()];
319 pendingDelete.toArray(del);
320 pendingDelete.clear();
321 add =
new Sprite[pendingAdd.size()];
322 pendingAdd.toArray(add);
325 if (del.length > 0 || add.length > 0) {
326 synchronized (DRAW_LOCK) {
327 for (
int i = 0; i < del.length; i++) {
329 drawingList.remove(del[i]);
330 sprites.remove(del[i].getID());
333 for (
int i = 0; i < add.length; i++) {
335 sprites.put(add[i].getID(), add[i]);
336 drawingList.add(add[i]);
352 long start = System.nanoTime();
356 while (state != State.SHUTDOWN) {
357 actual_time = System.nanoTime();
358 if (debit <= actual_time) {
362 start = actual_time - round * 1000000000 / fps;
366 start = actual_time - round * 1000000000 / fps;
373 debit =
start + round * 1000000000 / fps;
376 throw new RuntimeException(
"unknown state");
381 long pause = debit - actual_time;
382 Thread.sleep(
pause / 1000000, (
int) (
pause % 1000000));
383 }
catch (Exception e) {
394 synchronized (CHANGE_LOCK) {
396 pendingDelete.addAll(sprites.values());
406 return keys[keyCode];
407 }
catch (ArrayIndexOutOfBoundsException e) {
418 return keysPressed[keyCode];
419 }
catch (ArrayIndexOutOfBoundsException e) {
433 synchronized (CHANGE_LOCK) {
444 synchronized (CHANGE_LOCK) {
445 pendingDelete.add(obj);
446 pendingAdd.remove(obj);
456 return new Point(mousePos);
466 return mouseBtn[button];
467 }
catch (ArrayIndexOutOfBoundsException e) {
480 return mouseBtnPressed[button];
481 }
catch (ArrayIndexOutOfBoundsException e) {
static final MutableColor WHITE
boolean isKeyDown(int keyCode)
CollisionDetection colDetect
Kollisionserkennungsalgorithmus.
boolean isKeyPressed(int keyCode)
Game(double width, double height, MutableColor back, int fps, String title)
void removeSprite(Sprite obj)
void mouseDown(int button)
Game(double width, double height)
boolean isMouseDown(int button)
boolean isMousePressed(int button)
Game(double width, double height, MutableColor back, String title)
Game(double width, double height, String title)
void mousePosition(Point p)
void addSprite(Sprite obj)
void keyDown(int keyCode)
void removeObject(Sprite sprite)
void addObject(Sprite sprite)