diff --git a/src/main/java/com/yes/yes/MainController.java b/src/main/java/com/yes/yes/MainController.java index 25e099a..1650831 100644 --- a/src/main/java/com/yes/yes/MainController.java +++ b/src/main/java/com/yes/yes/MainController.java @@ -1,17 +1,16 @@ package com.yes.yes; -import com.yes.yes.world.World; +import com.yes.yes.managers.GameManager; import javafx.fxml.FXML; -import javafx.scene.control.Label; import javafx.scene.layout.VBox; + public class MainController { @FXML VBox root; public void initialize() { - World chunk = new World(); - - root.getChildren().add(chunk); + GameManager gameManager = new GameManager(root); + gameManager.setup(); } } \ No newline at end of file diff --git a/src/main/java/com/yes/yes/entities/machines/TestMachine.java b/src/main/java/com/yes/yes/entities/machines/TestMachine.java new file mode 100644 index 0000000..4ed20ec --- /dev/null +++ b/src/main/java/com/yes/yes/entities/machines/TestMachine.java @@ -0,0 +1,14 @@ +package com.yes.yes.entities.machines; + +import com.yes.yes.utils.Entity; +import javafx.scene.shape.Circle; + +public class TestMachine extends Entity { + + public TestMachine() { + super(); + Circle shape = new Circle(25); + shape.setStyle("-fx-fill: #ff0000"); + this.getChildren().add(shape); + } +} diff --git a/src/main/java/com/yes/yes/managers/GameManager.java b/src/main/java/com/yes/yes/managers/GameManager.java new file mode 100644 index 0000000..eb076f2 --- /dev/null +++ b/src/main/java/com/yes/yes/managers/GameManager.java @@ -0,0 +1,23 @@ +package com.yes.yes.managers; + +import com.yes.yes.entities.machines.TestMachine; +import com.yes.yes.utils.Coordinate; +import com.yes.yes.world.World; +import javafx.scene.layout.VBox; + +public class GameManager { + VBox root; + + public GameManager(VBox root) { + this.root = root; + } + + public void setup() { + World world = new World(); + + root.getChildren().add(world); + + world.getChunk(new Coordinate(1, 1)).setEntity(new TestMachine(), new Coordinate(1, 1)); + world.getChunk(new Coordinate(1, 1)).setEntity(new TestMachine(), new Coordinate(1, 2)); + } +} diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java new file mode 100644 index 0000000..0a32b4a --- /dev/null +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -0,0 +1,4 @@ +package com.yes.yes.managers; + +public class PlayerManager { +} diff --git a/src/main/java/com/yes/yes/utils/Component.java b/src/main/java/com/yes/yes/utils/Component.java index 25e9194..8412f52 100644 --- a/src/main/java/com/yes/yes/utils/Component.java +++ b/src/main/java/com/yes/yes/utils/Component.java @@ -1,4 +1,4 @@ package com.yes.yes.utils; -public abstract class Component extends javafx.scene.shape.Shape { +public abstract class Component { } diff --git a/src/main/java/com/yes/yes/utils/Coordinate.java b/src/main/java/com/yes/yes/utils/Coordinate.java index d709d18..38ae34f 100644 --- a/src/main/java/com/yes/yes/utils/Coordinate.java +++ b/src/main/java/com/yes/yes/utils/Coordinate.java @@ -10,7 +10,9 @@ public class Coordinate { this.x = x; this.y = y; } - public Coordinate() {} + + public Coordinate() { + } @Override public boolean equals(Object o) { diff --git a/src/main/java/com/yes/yes/utils/Entity.java b/src/main/java/com/yes/yes/utils/Entity.java index 2d68f32..5e463a3 100644 --- a/src/main/java/com/yes/yes/utils/Entity.java +++ b/src/main/java/com/yes/yes/utils/Entity.java @@ -1,4 +1,29 @@ package com.yes.yes.utils; -public abstract class Entity { +import java.util.ArrayList; + +public abstract class Entity extends javafx.scene.Group { + private ArrayList behavours = new ArrayList<>(); + + public void addBehaviour(Component behaviour) { + behavours.add(behaviour); + } + + public void removeBehaviour(Component behaviour) { + ArrayList components = new ArrayList<>(); + for (Component b : behavours) { + if (!b.getClass().equals(behaviour.getClass())) { + components.add(b); + } + } + behavours = components; + } + + public void update() { + for (Component behaviour : behavours) { + System.out.println("Updating " + behaviour.getClass().getSimpleName()); + } + } + + } diff --git a/src/main/java/com/yes/yes/world/Chunk.java b/src/main/java/com/yes/yes/world/Chunk.java index c97dfd6..5dece6e 100644 --- a/src/main/java/com/yes/yes/world/Chunk.java +++ b/src/main/java/com/yes/yes/world/Chunk.java @@ -1,50 +1,50 @@ package com.yes.yes.world; -import com.yes.yes.utils.Component; -import javafx.scene.layout.ColumnConstraints; -import javafx.scene.layout.GridPane; -import javafx.scene.layout.RowConstraints; +import com.yes.yes.utils.Coordinate; +import com.yes.yes.utils.Entity; +import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; public class Chunk extends GridPane { - private static final int CHUNK_SIZE = 16; - private static final double COMPONENT_SIZE = 50; - private final Component[] data = new Component[CHUNK_SIZE * CHUNK_SIZE]; + private static final int CHUNK_SIZE = 8; + private static final double ENTITY_SIZE = 50; + private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE]; public Chunk() { super(); - this.setMaxSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE); - this.setMinSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE); + this.setMaxSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE); + this.setMinSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE); for (int i = 0; i < CHUNK_SIZE; i++) { for (int j = 0; j < CHUNK_SIZE; j++) { - this.getColumnConstraints().add(new ColumnConstraints(COMPONENT_SIZE)); - this.getRowConstraints().add(new RowConstraints(COMPONENT_SIZE)); + this.getColumnConstraints().add(new ColumnConstraints(ENTITY_SIZE)); + this.getRowConstraints().add(new RowConstraints(ENTITY_SIZE)); - Rectangle rect = new Rectangle(COMPONENT_SIZE, COMPONENT_SIZE); - rect.setFill(Color.color((i+j*16)/257d,(i+j*16)/257d, (i+j*16)/257d,1)); //DEBUG CODE + Rectangle rect = new Rectangle(ENTITY_SIZE, ENTITY_SIZE); + //rect.setFill(Color.color((i+j*16)/257d,(i+j*16)/257d, (i+j*16)/257d,1)); //DEBUG CODE + rect.setFill(null); rect.setStroke(Color.BLACK); rect.strokeWidthProperty().set(0.5); this.add(rect, i, j); + + this.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT))); } } } - public void setComponent(Component component, int x, int y) { - data[x + y * CHUNK_SIZE] = component; - this.add(component, x, y); + public void setEntity(Entity component, Coordinate pos) { + data[pos.x + pos.y * CHUNK_SIZE] = component; + this.add(component, pos.x, pos.y); } - public void removeComponent(int x, int y) { - this.getChildren().remove(data[x + y * CHUNK_SIZE]); - data[x + y * CHUNK_SIZE] = null; + public void removeEntity(Coordinate pos) { + this.getChildren().remove(data[pos.x + pos.y * CHUNK_SIZE]); + data[pos.x + pos.y * CHUNK_SIZE] = null; } - public Component getComponent(int x, int y) { - return data[x + y * CHUNK_SIZE]; + public Entity getEntity(Coordinate pos) { + return data[pos.x + pos.y * CHUNK_SIZE]; } - - } diff --git a/src/main/java/com/yes/yes/world/World.java b/src/main/java/com/yes/yes/world/World.java index c5e789f..399c7ca 100644 --- a/src/main/java/com/yes/yes/world/World.java +++ b/src/main/java/com/yes/yes/world/World.java @@ -6,25 +6,36 @@ import javafx.scene.layout.GridPane; import java.util.HashMap; public class World extends GridPane { - private final HashMap chunks = new HashMap<>(); + private final HashMap chunks = new HashMap<>(); public World() { super(); - for (int x = 0; x < 4; x++) {//DEBUG CODE - for (int y = 0; y < 4; y++) { + for (int x = 0; x < 3; x++) {//DEBUG CODE + for (int y = 0; y < 3; y++) { chunks.put(new Coordinate(x, y), new Chunk()); this.add(chunks.get(new Coordinate(x, y)), x, y); } } - this.unload(new Coordinate(2, 2)); //DEBUG CODE + this.unload(new Coordinate(2, 2)); //END DEBUG CODE } public void unload(Coordinate pos) { this.getChildren().remove(chunks.get(pos)); } + public void load(Coordinate pos) { this.add(chunks.get(pos), pos.x, pos.y); } + + public Chunk getChunk(Coordinate pos) { + + Chunk c = chunks.get(pos); + if (c == null) { + c = new Chunk(); + chunks.put(pos, c); + } + return c; + } }