Reftored code; Added base for Components and Entities

+ Component Class
+ Entity Class
+ Manager Classes
This commit is contained in:
Stefan-5422
2022-02-11 12:20:57 +01:00
parent a4f5310aa6
commit 27065159a8
9 changed files with 113 additions and 35 deletions
@@ -1,17 +1,16 @@
package com.yes.yes; package com.yes.yes;
import com.yes.yes.world.World; import com.yes.yes.managers.GameManager;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
public class MainController { public class MainController {
@FXML @FXML
VBox root; VBox root;
public void initialize() { public void initialize() {
World chunk = new World(); GameManager gameManager = new GameManager(root);
gameManager.setup();
root.getChildren().add(chunk);
} }
} }
@@ -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);
}
}
@@ -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));
}
}
@@ -0,0 +1,4 @@
package com.yes.yes.managers;
public class PlayerManager {
}
@@ -1,4 +1,4 @@
package com.yes.yes.utils; package com.yes.yes.utils;
public abstract class Component extends javafx.scene.shape.Shape { public abstract class Component {
} }
@@ -10,7 +10,9 @@ public class Coordinate {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public Coordinate() {}
public Coordinate() {
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
+26 -1
View File
@@ -1,4 +1,29 @@
package com.yes.yes.utils; package com.yes.yes.utils;
public abstract class Entity { import java.util.ArrayList;
public abstract class Entity extends javafx.scene.Group {
private ArrayList<Component> behavours = new ArrayList<>();
public void addBehaviour(Component behaviour) {
behavours.add(behaviour);
}
public void removeBehaviour(Component behaviour) {
ArrayList<Component> 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());
}
}
} }
+23 -23
View File
@@ -1,50 +1,50 @@
package com.yes.yes.world; package com.yes.yes.world;
import com.yes.yes.utils.Component; import com.yes.yes.utils.Coordinate;
import javafx.scene.layout.ColumnConstraints; import com.yes.yes.utils.Entity;
import javafx.scene.layout.GridPane; import javafx.scene.layout.*;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
public class Chunk extends GridPane { public class Chunk extends GridPane {
private static final int CHUNK_SIZE = 16; private static final int CHUNK_SIZE = 8;
private static final double COMPONENT_SIZE = 50; private static final double ENTITY_SIZE = 50;
private final Component[] data = new Component[CHUNK_SIZE * CHUNK_SIZE]; private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE];
public Chunk() { public Chunk() {
super(); super();
this.setMaxSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE); this.setMaxSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE);
this.setMinSize(CHUNK_SIZE * COMPONENT_SIZE, CHUNK_SIZE * COMPONENT_SIZE); this.setMinSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE);
for (int i = 0; i < CHUNK_SIZE; i++) { for (int i = 0; i < CHUNK_SIZE; i++) {
for (int j = 0; j < CHUNK_SIZE; j++) { for (int j = 0; j < CHUNK_SIZE; j++) {
this.getColumnConstraints().add(new ColumnConstraints(COMPONENT_SIZE)); this.getColumnConstraints().add(new ColumnConstraints(ENTITY_SIZE));
this.getRowConstraints().add(new RowConstraints(COMPONENT_SIZE)); this.getRowConstraints().add(new RowConstraints(ENTITY_SIZE));
Rectangle rect = new Rectangle(COMPONENT_SIZE, COMPONENT_SIZE); 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(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.setStroke(Color.BLACK);
rect.strokeWidthProperty().set(0.5); rect.strokeWidthProperty().set(0.5);
this.add(rect, i, j); 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) { public void setEntity(Entity component, Coordinate pos) {
data[x + y * CHUNK_SIZE] = component; data[pos.x + pos.y * CHUNK_SIZE] = component;
this.add(component, x, y); this.add(component, pos.x, pos.y);
} }
public void removeComponent(int x, int y) { public void removeEntity(Coordinate pos) {
this.getChildren().remove(data[x + y * CHUNK_SIZE]); this.getChildren().remove(data[pos.x + pos.y * CHUNK_SIZE]);
data[x + y * CHUNK_SIZE] = null; data[pos.x + pos.y * CHUNK_SIZE] = null;
} }
public Component getComponent(int x, int y) { public Entity getEntity(Coordinate pos) {
return data[x + y * CHUNK_SIZE]; return data[pos.x + pos.y * CHUNK_SIZE];
} }
} }
+15 -4
View File
@@ -6,25 +6,36 @@ import javafx.scene.layout.GridPane;
import java.util.HashMap; import java.util.HashMap;
public class World extends GridPane { public class World extends GridPane {
private final HashMap<Coordinate,Chunk> chunks = new HashMap<>(); private final HashMap<Coordinate, Chunk> chunks = new HashMap<>();
public World() { public World() {
super(); super();
for (int x = 0; x < 4; x++) {//DEBUG CODE for (int x = 0; x < 3; x++) {//DEBUG CODE
for (int y = 0; y < 4; y++) { for (int y = 0; y < 3; y++) {
chunks.put(new Coordinate(x, y), new Chunk()); chunks.put(new Coordinate(x, y), new Chunk());
this.add(chunks.get(new Coordinate(x, y)), x, y); 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) { public void unload(Coordinate pos) {
this.getChildren().remove(chunks.get(pos)); this.getChildren().remove(chunks.get(pos));
} }
public void load(Coordinate pos) { public void load(Coordinate pos) {
this.add(chunks.get(pos), pos.x, pos.y); 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;
}
} }