diff --git a/src/com/game/BoolLinkedList2D.java b/src/com/game/BoolLinkedList2D.java new file mode 100644 index 0000000..23b3000 --- /dev/null +++ b/src/com/game/BoolLinkedList2D.java @@ -0,0 +1,7 @@ +package com.game; + +import java.util.LinkedList; + +public class BoolLinkedList2D extends LinkedList> { + +} diff --git a/src/com/game/Field.java b/src/com/game/Field.java new file mode 100644 index 0000000..9b956ad --- /dev/null +++ b/src/com/game/Field.java @@ -0,0 +1,125 @@ +package com.game; + +import java.awt.*; +import java.lang.reflect.Type; +import java.net.PortUnreachableException; +import java.sql.Time; +import java.util.LinkedList; + +public class Field{ + private int sx; + private int sy; + private int tileDim; + private boolean run = true; + + private long timer1 = System.currentTimeMillis(); + + private Handler handler; + private Game game; + + private LinkedList> field = new LinkedList>(); + + public Field(int x, int y, int width, int height, Handler handler) { + sx = x; + sy = y; + this.handler = handler; + + tileDim = Game.WIDTH/width; + + for (int tmpx = 0; tmpx < width; tmpx++) { + LinkedList tmpField = new LinkedList(); + for (int tmpy = 0; tmpy < height; tmpy++) { + tmpField.add(false); + handler.addObject(new Tile(tmpx*tileDim, tmpy*tileDim, tmpx, tmpy, tileDim, tileDim, false, ID.Tile)); + } + field.add(tmpField); + } + } + + public void tick() { + + } + + public void render(Graphics g) { + for (GameObject object : handler.objects){ + if (object.id == ID.Tile){ + Tile tile = (Tile)object; + tile.setAlive(field.get(tile.getIDx()).get(tile.getIDy())); + } + } + update(); + } + + private int neighbours(int x, int y, LinkedList> field) { + int ng = 0; + int[][] neighbor = { + {-1, 1}, + {-1, 0}, + {-1, -1}, + {1, 1}, + {1, 0}, + {1, -1}, + {0, 1}, + {0, -1}, + }; + + for (int[] ints : neighbor) { + int tmpx = x + ints[0]; + int tmpy = y + ints[1]; + + if (tmpx >= 0 && tmpx < field.size() && tmpy >= 0 && tmpy < field.size() && field.get(tmpx).get(tmpy)) { + ng += 1; + } + } + return ng; + } + + public void update(){ + if (run && System.currentTimeMillis() - timer1 > 1000){ + int ng; + for (int x = 0; x < field.size(); x++) { + for (int y = 0; y < field.size(); y++) { + ng = neighbours(x, y, field); + if (ng == 3) { + field.get(x).set(y, true); + } else if (ng > 3) { + field.get(x).set(y, !field.get(x).get(y)); + } else if (ng < 2) { + field.get(x).set(y, false); + } + } + } + timer1 = System.currentTimeMillis(); + } + } + + public void fieldReset() { + for (int x = 0; x < field.size(); x++) { + for (int y = 0; y < field.size(); y++) { + field.get(x).set(y, false); + } + } + } + public void fieldRnd() { + + for (int x = 0; x < field.size(); x++) { + for (int y = 0; y < field.size(); y++) { + double rnd = Math.random(); + System.out.println(rnd); + if (rnd > .9) + field.get(x).set(y, true); + } + } + } + + public void setTile(int x, int y, boolean status) { + field.get(x).set(y, status); + + } + public boolean getTile(int x, int y){ + return field.get(x).get(y); + } + public void play_stop(){ + run = !run; + } +} diff --git a/src/com/game/main/Game.java b/src/com/game/Game.java similarity index 51% rename from src/com/game/main/Game.java rename to src/com/game/Game.java index eeba9ca..fa16530 100644 --- a/src/com/game/main/Game.java +++ b/src/com/game/Game.java @@ -1,21 +1,38 @@ -package com.game.main; +package com.game; import java.awt.*; import java.awt.image.BufferStrategy; public class Game extends Canvas implements Runnable { - public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9; + public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9; + public static final int tileFieldWidth = 50, tileFieldHeight = 50; private Thread thread; private boolean running = false; - private Handler handler; + private final Handler handler; + private final Field field; - public Game(){ - new Window(WIDTH, HEIGHT, "game", this); + double interpolation = 0; + final int TICKS_PER_SECOND = 1; + final int SKIP_TICKS = 1000 / TICKS_PER_SECOND; + final int MAX_FRAMESKIP = 5; + + public Game() { handler = new Handler(); + + field = new Field(0, 0, tileFieldWidth, tileFieldHeight, handler); + + int magic = WIDTH / tileFieldWidth; + MyMouseListener mml = new MyMouseListener(field); + MyKeyListener mkl = new MyKeyListener(field); + + new Window(magic * tileFieldWidth, (HEIGHT / magic) * magic, "game", this); + this.addMouseListener(mml); + this.addKeyListener(mkl); } + public synchronized void start() { thread = new Thread(this); thread.start(); @@ -29,7 +46,12 @@ public synchronized void stop() { e.printStackTrace(); } } + public void run(){ + gameLoop2(); + } + + public void gameLoop1(){ long lastTime = System.nanoTime(); double amountOfTicks = 60.0; double ns = 1000000000 / amountOfTicks; @@ -56,8 +78,30 @@ public void run(){ stop(); } + public void gameLoop2() { + double next_game_tick = System.currentTimeMillis(); + int loops; + + while (running) { + loops = 0; + while (System.currentTimeMillis() > next_game_tick + && loops < MAX_FRAMESKIP) { + + tick(); + + next_game_tick += SKIP_TICKS; + loops++; + } + + interpolation = (System.currentTimeMillis() + SKIP_TICKS - next_game_tick + / (double) SKIP_TICKS); + render(); + } + } + private void tick(){ handler.tick(); + field.tick(); } private void render(){ @@ -68,13 +112,24 @@ private void render(){ } Graphics g = bs.getDrawGraphics(); - g.setColor(Color.BLACK); + g.setColor(Color.DARK_GRAY); g.fillRect(0, 0, WIDTH, HEIGHT); handler.render(g); + field.render(g); g.dispose(); bs.show(); + + } + + public static float clamp(float var, float min, float max){ + if (var >= max) + return max; + else if (var <= min) + return min ; + else + return var; } public static void main(String[] args) { diff --git a/src/com/game/main/GameObject.java b/src/com/game/GameObject.java similarity index 72% rename from src/com/game/main/GameObject.java rename to src/com/game/GameObject.java index 198ff53..f098cb1 100644 --- a/src/com/game/main/GameObject.java +++ b/src/com/game/GameObject.java @@ -1,4 +1,4 @@ -package com.game.main; +package com.game; import java.awt.*; @@ -6,11 +6,13 @@ public abstract class GameObject { protected int x, y; protected ID id; - protected int velx, vely; + protected int height, width; - public GameObject(int x, int y, ID id){ + public GameObject(int x, int y, int height, int width, ID id){ this.x = x; this.y = y; + this.height = height; + this.width = width; this.id = id; } @@ -36,16 +38,16 @@ public ID getId() { return id; } public void setVelx(int velx) { - this.velx = velx; + this.height = velx; } public void setVely(int vely) { - this.vely = vely; + this.width = vely; } public int getVelx() { - return velx; + return height; } public int getVely() { - return vely; + return width; } } diff --git a/src/com/game/main/Handler.java b/src/com/game/Handler.java similarity index 67% rename from src/com/game/main/Handler.java rename to src/com/game/Handler.java index 41a3375..bbbe0e8 100644 --- a/src/com/game/main/Handler.java +++ b/src/com/game/Handler.java @@ -1,4 +1,4 @@ -package com.game.main; +package com.game; import java.awt.*; import java.util.LinkedList; @@ -8,17 +8,13 @@ public class Handler { LinkedList objects = new LinkedList(); public void tick(){ - for (int i = 0; i < objects.size(); i++){ - GameObject tempObject = objects.get(i); - + for (GameObject tempObject : objects) { tempObject.tick(); } } public void render(Graphics g){ - for (int i = 0; i < objects.size(); i++){ - GameObject tempObject = objects.get(i); - + for (GameObject tempObject : objects) { tempObject.render(g); } } diff --git a/src/com/game/ID.java b/src/com/game/ID.java new file mode 100644 index 0000000..1af5be8 --- /dev/null +++ b/src/com/game/ID.java @@ -0,0 +1,5 @@ +package com.game; + +public enum ID { + Tile(), +} diff --git a/src/com/game/MyKeyListener.java b/src/com/game/MyKeyListener.java new file mode 100644 index 0000000..32e9673 --- /dev/null +++ b/src/com/game/MyKeyListener.java @@ -0,0 +1,36 @@ +package com.game; + + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +class MyKeyListener implements KeyListener { + private Field field; + + public MyKeyListener(Field field){ + this.field = field; + } + + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + char key = e.getKeyChar(); + if (key == KeyEvent.VK_SPACE) + field.play_stop(); + else if (key == 'r') + field.fieldRnd(); + else if (key == 'c') + field.fieldReset(); + } + + @Override + public void keyReleased(KeyEvent e) { + + } +} diff --git a/src/com/game/MyMouseListener.java b/src/com/game/MyMouseListener.java new file mode 100644 index 0000000..41dc525 --- /dev/null +++ b/src/com/game/MyMouseListener.java @@ -0,0 +1,35 @@ +package com.game; + + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +class MyMouseListener implements MouseListener { + private Field field; + + public MyMouseListener(Field field){ + this.field = field; + } + @Override + public void mouseClicked(MouseEvent e) { + int tileDim = Game.WIDTH/Game.tileFieldWidth; + + int x = e.getX()/tileDim; + int y = e.getY()/tileDim; + + field.setTile(x, y, !field.getTile(x, y)); + } + + @Override + public void mouseEntered(MouseEvent e) { } + + @Override + public void mouseExited(MouseEvent e) { } + + @Override + public void mousePressed(MouseEvent arg0) { } + + @Override + public void mouseReleased(MouseEvent arg0) { } + +} diff --git a/src/com/game/Tile.java b/src/com/game/Tile.java new file mode 100644 index 0000000..7de5b98 --- /dev/null +++ b/src/com/game/Tile.java @@ -0,0 +1,44 @@ +package com.game; + +import java.awt.*; + +public class Tile extends GameObject { + private boolean alive; + private int idx, idy; + + public Tile(int x, int y, int idx, int idy, int height, int tileDim, boolean alive, ID id) { + super(x, y, tileDim, tileDim, id); + this.alive = alive; + this.idx = idx; + this.idy = idy; + } + + @Override + public void tick() { + + } + + @Override + public void render(Graphics g) { + if (alive) + g.setColor(Color.BLACK); + else + g.setColor(Color.WHITE); + g.fillRect(x, y, width, height); + + g.setColor(Color.GRAY); + g.drawRect(x, y, width, height); + } + + public void setAlive(boolean status) { + this.alive = status; + } + + public int getIDx(){ + return idx; + } + + public int getIDy() { + return idy; + } +} diff --git a/src/com/game/main/Window.java b/src/com/game/Window.java similarity index 92% rename from src/com/game/main/Window.java rename to src/com/game/Window.java index 2f81a05..2c10ef2 100644 --- a/src/com/game/main/Window.java +++ b/src/com/game/Window.java @@ -1,4 +1,4 @@ -package com.game.main; +package com.game; import javax.swing.*; import java.awt.*; @@ -13,6 +13,7 @@ public Window(int width, int height, String title, Game game) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); + frame.setFocusable(true); frame.setLocationRelativeTo(null); frame.add(game); frame.setVisible(true); diff --git a/src/com/game/main/ID.java b/src/com/game/main/ID.java deleted file mode 100644 index 544172c..0000000 --- a/src/com/game/main/ID.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.game.main; - -public enum ID { - Player(), - Enemy(), -}