Skip to content

Commit

Permalink
Merge pull request #1 from DoubleTroublePy/internal
Browse files Browse the repository at this point in the history
Internal
  • Loading branch information
DoubleTroublePy authored Apr 11, 2022
2 parents a677902 + ea39248 commit eed9006
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 27 deletions.
7 changes: 7 additions & 0 deletions src/com/game/BoolLinkedList2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.game;

import java.util.LinkedList;

public class BoolLinkedList2D extends LinkedList<LinkedList<Boolean>> {

}
125 changes: 125 additions & 0 deletions src/com/game/Field.java
Original file line number Diff line number Diff line change
@@ -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<LinkedList<Boolean>> field = new LinkedList<LinkedList<Boolean>>();

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<Boolean> tmpField = new LinkedList<Boolean>();
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<LinkedList<Boolean>> 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;
}
}
67 changes: 61 additions & 6 deletions src/com/game/main/Game.java → src/com/game/Game.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
Expand All @@ -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(){
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.game.main;
package com.game;

import java.awt.*;

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;
}

Expand All @@ -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;
}

}
10 changes: 3 additions & 7 deletions src/com/game/main/Handler.java → src/com/game/Handler.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.game.main;
package com.game;

import java.awt.*;
import java.util.LinkedList;
Expand All @@ -8,17 +8,13 @@ public class Handler {
LinkedList<GameObject> objects = new LinkedList<GameObject>();

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);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/com/game/ID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.game;

public enum ID {
Tile(),
}
36 changes: 36 additions & 0 deletions src/com/game/MyKeyListener.java
Original file line number Diff line number Diff line change
@@ -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) {

}
}
35 changes: 35 additions & 0 deletions src/com/game/MyMouseListener.java
Original file line number Diff line number Diff line change
@@ -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) { }

}
Loading

0 comments on commit eed9006

Please sign in to comment.