Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.jacadev.lifegame;
- import static org.lwjgl.opengl.GL11.*;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.input.Mouse;
- import org.lwjgl.input.Keyboard;
- import pl.jacadev.lifegame.Entity.State;
- /**
- * @author JacaDev
- */
- public class Main {
- /**
- * Default frame dimensions declaration
- */
- public static final int FRAME_WIDTH = 800;
- public static final int FRAME_HEIGHT = 800;
- public static final int SIZE = 100;
- /**
- * Main method with ogl setup and main loop.
- *
- */
- public static void main(String[] args) throws FileNotFoundException,
- IOException {
- try {
- Display.setDisplayMode(new DisplayMode(FRAME_WIDTH, FRAME_HEIGHT));
- Display.setTitle("The Game of Life");
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, FRAME_WIDTH, FRAME_WIDTH, 0, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_TEXTURE_2D);
- EntityMap.spawnEntities();
- //The control by console.
- /*new Thread() {
- @Override
- public void run() {
- Scanner sc = new Scanner(System.in);
- while (true) {
- String s = sc.nextLine();
- if (s.split(" ")[0].equals("create")) {
- Entity en = EntityMap.getEntityAt(Integer.parseInt(s.split(" ")[1]),
- Integer.parseInt(s.split(" ")[2]));
- if(en == null){
- ;
- EntityMap.getEntities().add(
- new Entity(Integer.parseInt(s.split(" ")[1]),
- Integer.parseInt(s.split(" ")[2])));
- EntityMap.getEntityAt(Integer.parseInt(s.split(" ")[1]),
- Integer.parseInt(s.split(" ")[2])).makeExisting(true);
- }else{
- en.makeExisting(true);
- }
- }
- if(s.equals("next")){
- EntityMap.nextGen();
- System.out.println(EntityMap.getMapInString());
- }
- }
- }
- }.start();*/
- new Thread(){
- @Override
- public void run() {
- try {
- sleep(30);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
- EntityMap.nextGen();
- }
- run();
- };
- }.start();
- //Main loop
- while (!Display.isCloseRequested()) {
- glClear(GL_COLOR_BUFFER_BIT);
- EntityMap.draw();
- if(Mouse.isButtonDown(0)){
- for(int x = 0; x < EntityMap.getEntities().size(); x++){
- Entity en = EntityMap.getEntities().get(x);
- if(en.getRect().contains((Mouse.getX()), FRAME_HEIGHT - (Mouse.getY()))){
- en.setExisting(true);
- en.setState(State.NEW);
- }
- }
- }
- if(Mouse.isButtonDown(1)){
- for(int x = 0; x < EntityMap.getEntities().size(); x++){
- Entity en = EntityMap.getEntities().get(x);
- if(en.getRect().contains((Mouse.getX()), FRAME_HEIGHT - (Mouse.getY()))){
- en.setExisting(false);
- }
- }
- }
- Display.update();
- Display.sync(40);
- }
- Display.destroy();
- System.exit(0);
- }
- }
- package pl.jacadev.lifegame;
- import static org.lwjgl.opengl.GL11.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class EntityMap {
- /**
- * @param entities
- * - The list of all entities at game.
- */
- private static ArrayList<Entity> entities = new ArrayList<Entity>();
- public static ArrayList<Entity> getEntities() {
- return entities;
- }
- /**
- *
- * @return amount of neighbours of entity.
- */
- public static int countNeighbours(Entity entity) {
- int counter = 0;
- for (int x = 0; x < entities.size(); x++) {
- Entity en = entities.get(x);
- if ((entity.getX() - 1 == en.getX() && entity.getY() == en.getY() && en
- .isExisting())
- || (entity.getX() + 1 == en.getX()
- && entity.getY() == en.getY() && en.isExisting())
- || (entity.getX() == en.getX()
- && entity.getY() - 1 == en.getY() && en
- .isExisting())
- || (entity.getX() == en.getX()
- && entity.getY() + 1 == en.getY() && en
- .isExisting())
- || (entity.getX() + 1 == en.getX()
- && entity.getY() - 1 == en.getY() && en
- .isExisting())
- || (entity.getX() + 1 == en.getX()
- && entity.getY() + 1 == en.getY() && en
- .isExisting())
- || (entity.getX() - 1 == en.getX()
- && entity.getY() + 1 == en.getY() && en
- .isExisting())
- || (entity.getX() - 1 == en.getX() && entity.getY() - 1 == en
- .getY()) && en.isExisting()) {
- counter++;
- }
- }
- return counter;
- }
- public static List<Entity> getNeighbours(Entity en){
- ArrayList<Entity> toReturn = new ArrayList<Entity>();
- toReturn.addAll(Arrays.asList(new Entity[]{
- getEntityAt(en.getX(), en.getY() + 1),
- getEntityAt(en.getX(), en.getY() - 1),
- getEntityAt(en.getX() + 1, en.getY()),
- getEntityAt(en.getX() - 1, en.getY()),
- getEntityAt(en.getX() + 1, en.getY() + 1),
- getEntityAt(en.getX() - 1, en.getY() - 1),
- getEntityAt(en.getX() + 1, en.getY() - 1),
- getEntityAt(en.getX() - 1, en.getY() + 1)
- }));
- for(int i = 0; i < toReturn.size(); i++){
- if(toReturn.get(i) == null){
- toReturn.remove(toReturn.get(i));
- }
- }
- return toReturn;
- }
- /**
- * Start another generation.
- */
- public static void nextGen() {
- for (int x = 0; x < entities.size(); x++) {
- Entity en = entities.get(x);
- if(en.isExisting()){
- en.logic();
- for(Entity entity : getNeighbours(en)){
- entity.logic();
- }
- }
- }
- for (int x = 0; x < entities.size(); x++) {
- Entity en = entities.get(x);
- if(en.isExisting()){
- en.completeLogic();;
- for(Entity entity : getNeighbours(en)){
- entity.completeLogic();;
- }
- }
- }
- }
- /**
- * Spawn default entities.
- */
- public static void spawnEntities() {
- for (int x = 0; x < Main.SIZE + 6; x++) {
- for (int y = 0; y < Main.SIZE + 2; y++) {
- entities.add(new Entity(x - 3, y - 3));
- }
- }
- }
- /**
- *
- * Get entity with the same x and y as in the arguments.
- */
- public static Entity getEntityAt(int x, int y) {
- for (int b = 0; b < entities.size(); b++) {
- Entity en = entities.get(b);
- if (en.getX() == x && en.getY() == y) {
- return en;
- }
- }
- return null;
- }
- /**
- *
- * For the console form of control.
- *
- * @return Pretty, formated string with list of living entities.
- */
- public static String getMapInString() {
- String toReturn = "";
- for (int x = 0; x < entities.size(); x++) {
- Entity en = entities.get(x);
- if (en.isExisting()) {
- toReturn += "Entity at: x - " + en.getX() + " y - " + en.getY()
- + " " + "\n";
- }
- }
- return toReturn;
- }
- /**
- * Draw
- */
- public static void draw() {
- glColor3d(1, 1, 1);
- glRectd(0, 0, Main.FRAME_WIDTH, Main.FRAME_HEIGHT);
- for (int x = 0; x < Main.SIZE; x++) {
- glColor3d(0, 0, 0);
- glBegin(GL_LINES);
- glVertex2d(x * ((int)Main.FRAME_WIDTH / Main.SIZE), 0);
- glVertex2d(x * (int)(Main.FRAME_WIDTH / Main.SIZE), Main.FRAME_HEIGHT);
- glEnd();
- glBegin(GL_LINES);
- glVertex2d(0, x * (int)(Main.FRAME_HEIGHT / Main.SIZE));
- glVertex2d(Main.FRAME_WIDTH, x * (int)(Main.FRAME_HEIGHT / Main.SIZE));
- glEnd();
- }
- for (Entity e : entities) {
- e.draw();
- }
- }
- }
- package pl.jacadev.lifegame;
- import static org.lwjgl.opengl.GL11.*;
- import org.lwjgl.util.Rectangle;
- public class Entity {
- private int x;
- private int y;
- private Rectangle rect;
- private boolean exist;
- private State state;
- public static enum State{
- NEW, NORMAL;
- }
- public void setExisting(boolean canItLive) {
- exist = canItLive;
- }
- public boolean isExisting() {
- return exist;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setState(State state){
- this.state = state;
- }
- public State getState(){
- return state;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public Entity(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- rect = new Rectangle(x * (Main.FRAME_WIDTH / (int)(Main.SIZE)), y * (Main.FRAME_HEIGHT / (int)(Main.SIZE)),
- Main.FRAME_WIDTH / (int)(Main.SIZE), Main.FRAME_HEIGHT / (int)(Main.SIZE));
- }
- private boolean willexist;
- private boolean willntexist;
- public void logic() {
- this.state = State.NORMAL;
- int neighbours = EntityMap.countNeighbours(this);
- if (this.exist) {
- if (neighbours <= 1) {
- this.willntexist = true;
- }
- if (neighbours >= 4) {
- this.willntexist = true;
- }
- } else {
- if (neighbours == 3) {
- this.willexist = true;
- }
- }
- }
- public void completeLogic(){
- if(willexist){
- exist = true;
- state = State.NEW;
- willexist = false;
- }
- if(willntexist){
- exist = false;
- willntexist = false;
- }
- }
- public void draw() {
- if (this.exist) {
- switch(state){
- case NEW:
- glColor3d(0, 1, 0);
- break;
- case NORMAL:
- glColor3d(0, 0, 1);
- break;
- }
- glRectd(rect.getX() + 1, rect.getY(), rect.getX() + rect.getWidth(),
- rect.getY() + rect.getHeight() - 1);
- }
- }
- public Rectangle getRect() {
- return rect;
- }
- public void setRect(Rectangle rect){
- this.rect = rect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment