mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 00:46:01 +02:00
Reftored code; Added base for Components and Entities
+ Component Class + Entity Class + Manager Classes
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
public abstract class Component extends javafx.scene.shape.Shape {
|
||||
public abstract class Component {
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@ public class Coordinate {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
public Coordinate() {}
|
||||
|
||||
public Coordinate() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
@@ -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<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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,25 +6,36 @@ import javafx.scene.layout.GridPane;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class World extends GridPane {
|
||||
private final HashMap<Coordinate,Chunk> chunks = new HashMap<>();
|
||||
private final HashMap<Coordinate, Chunk> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user