diff --git a/src/main/java/com/yes/yes/behaviours/TestBehaviour.java b/src/main/java/com/yes/yes/behaviours/TestBehaviour.java new file mode 100644 index 0000000..bbed637 --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/TestBehaviour.java @@ -0,0 +1,22 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.Component; +import com.yes.yes.utils.Entity; +import com.yes.yes.utils.EntityRegistry; + +public class TestBehaviour extends Component { + + public TestBehaviour(Entity entity) { + super(entity); + } + + @Override + public void initialize() { + System.out.println(EntityRegistry.getEntity("test").toString()); + } + + @Override + public void update() { + + } +} diff --git a/src/main/java/com/yes/yes/entities/machines/TestMachine.java b/src/main/java/com/yes/yes/entities/machines/TestMachine.java index f8e9e15..3723605 100644 --- a/src/main/java/com/yes/yes/entities/machines/TestMachine.java +++ b/src/main/java/com/yes/yes/entities/machines/TestMachine.java @@ -1,7 +1,11 @@ package com.yes.yes.entities.machines; +import com.yes.yes.behaviours.TestBehaviour; import com.yes.yes.utils.Coordinate; import com.yes.yes.utils.Entity; +import com.yes.yes.utils.EntityRegistry; +import com.yes.yes.utils.RegistryEntry; +import com.yes.yes.utils.exceptions.AlreadyExistsException; import javafx.scene.shape.Circle; public class TestMachine extends Entity { @@ -9,5 +13,7 @@ public class TestMachine extends Entity { public TestMachine() { super(); this.getChildren().add(new Circle(25)); + + this.addBehaviour(new TestBehaviour(this)); } } diff --git a/src/main/java/com/yes/yes/managers/GameManager.java b/src/main/java/com/yes/yes/managers/GameManager.java index 93fcd6a..a798f0c 100644 --- a/src/main/java/com/yes/yes/managers/GameManager.java +++ b/src/main/java/com/yes/yes/managers/GameManager.java @@ -2,6 +2,9 @@ package com.yes.yes.managers; import com.yes.yes.entities.machines.TestMachine; import com.yes.yes.utils.Coordinate; +import com.yes.yes.utils.EntityRegistry; +import com.yes.yes.utils.RegistryEntry; +import com.yes.yes.utils.exceptions.AlreadyExistsException; import com.yes.yes.world.World; import javafx.scene.layout.VBox; @@ -17,9 +20,15 @@ public class GameManager { this.root = root; } - public void setup() { + public void initialize() { world = new World(); root.getChildren().add(world); + + try { + EntityRegistry.register(new RegistryEntry("test","test machine", TestMachine.class)); + } catch (AlreadyExistsException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java index 8ea2476..91e6b58 100644 --- a/src/main/java/com/yes/yes/managers/PlayerManager.java +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -1,5 +1,6 @@ package com.yes.yes.managers; +import com.yes.yes.entities.machines.TestMachine; import com.yes.yes.utils.Coordinate; import com.yes.yes.world.Chunk; import com.yes.yes.world.World; @@ -23,7 +24,7 @@ public class PlayerManager { this.world = world; } - public void setup() { + public void initialize() { Scene scene = world.getScene(); scene.setOnKeyPressed(this::ProcessKeyPress); @@ -49,14 +50,11 @@ public class PlayerManager { double offset = +Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE; - System.out.println("World pos: X: " + (-world.getTranslateX() + offset) + "; Y: " + (-world.getTranslateY() + offset)); - //NOTE: WHAT??? int chunkX = -(int) Math.floor((world.getTranslateX() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE); int chunkY = -(int) Math.floor((world.getTranslateY() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE); - System.out.println("Chunk pos: X: " + chunkX + "; Y: " + chunkY); if (chunkX < chunkPos.x) { for (int i = 0; i < 6; i++) { @@ -88,7 +86,5 @@ public class PlayerManager { chunkPos = new Coordinate(chunkX, chunkY); - - //world.load(new Coordinate(chunkX, chunkY)); } } diff --git a/src/main/java/com/yes/yes/utils/Component.java b/src/main/java/com/yes/yes/utils/Component.java index 8412f52..fa7570d 100644 --- a/src/main/java/com/yes/yes/utils/Component.java +++ b/src/main/java/com/yes/yes/utils/Component.java @@ -1,4 +1,13 @@ package com.yes.yes.utils; public abstract class Component { + protected Entity parent; + + + public Component(Entity entity) { + this.parent = entity; + } + + public abstract void initialize(); + public abstract void update(); } diff --git a/src/main/java/com/yes/yes/utils/Entity.java b/src/main/java/com/yes/yes/utils/Entity.java index 5e463a3..5ef5fc2 100644 --- a/src/main/java/com/yes/yes/utils/Entity.java +++ b/src/main/java/com/yes/yes/utils/Entity.java @@ -3,27 +3,27 @@ package com.yes.yes.utils; import java.util.ArrayList; public abstract class Entity extends javafx.scene.Group { - private ArrayList behavours = new ArrayList<>(); + private ArrayList behaviours = new ArrayList<>(); - public void addBehaviour(Component behaviour) { - behavours.add(behaviour); + public final void addBehaviour(Component behaviour) { + behaviours.add(behaviour); + behaviour.initialize(); } - public void removeBehaviour(Component behaviour) { + public final void removeBehaviour(Component behaviour) { ArrayList components = new ArrayList<>(); - for (Component b : behavours) { + for (Component b : behaviours) { if (!b.getClass().equals(behaviour.getClass())) { components.add(b); } } - behavours = components; + behaviours = components; } - public void update() { - for (Component behaviour : behavours) { + public final void update() { + for (Component behaviour : behaviours) { System.out.println("Updating " + behaviour.getClass().getSimpleName()); + behaviour.update(); } } - - } diff --git a/src/main/java/com/yes/yes/utils/EntityRegistry.java b/src/main/java/com/yes/yes/utils/EntityRegistry.java new file mode 100644 index 0000000..0197e1c --- /dev/null +++ b/src/main/java/com/yes/yes/utils/EntityRegistry.java @@ -0,0 +1,23 @@ +package com.yes.yes.utils; + +import com.yes.yes.utils.exceptions.AlreadyExistsException; + +import java.util.HashMap; + +public abstract class EntityRegistry { + static HashMap entities = new HashMap<>(); + + public static void register(RegistryEntry entry) throws AlreadyExistsException { + if (entities.containsKey(entry.getName())) { + throw new AlreadyExistsException("Name already registered"); + } else { + entities.put(entry.getName(), entry); + } + } + + public static RegistryEntry getEntity(String name) { + return entities.get(name); + } + +} + diff --git a/src/main/java/com/yes/yes/utils/RegistryEntry.java b/src/main/java/com/yes/yes/utils/RegistryEntry.java new file mode 100644 index 0000000..241b60b --- /dev/null +++ b/src/main/java/com/yes/yes/utils/RegistryEntry.java @@ -0,0 +1,25 @@ +package com.yes.yes.utils; + +public class RegistryEntry { + String name; + String displayName; + Class entity; + + public RegistryEntry(String name, String displayName, Class entity) { + this.name = name; + this.displayName = displayName; + this.entity = entity; + } + + public String getName() { + return name; + } + + public String getDisplayName() { + return displayName; + } + + public Class getEntity() { + return entity; + } +} diff --git a/src/main/java/com/yes/yes/utils/exceptions/AlreadyExistsException.java b/src/main/java/com/yes/yes/utils/exceptions/AlreadyExistsException.java new file mode 100644 index 0000000..84bf40c --- /dev/null +++ b/src/main/java/com/yes/yes/utils/exceptions/AlreadyExistsException.java @@ -0,0 +1,20 @@ +package com.yes.yes.utils.exceptions; + +public class AlreadyExistsException extends Exception { + + public AlreadyExistsException(String message) { + super(message); + } + + public AlreadyExistsException(String message, Throwable cause) { + super(message, cause); + } + + public AlreadyExistsException(Throwable cause) { + super(cause); + } + + public AlreadyExistsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/yes/yes/utils/exceptions/ChunkAlreadyExistsException.java b/src/main/java/com/yes/yes/utils/exceptions/ChunkAlreadyExistsException.java deleted file mode 100644 index 5a2f67c..0000000 --- a/src/main/java/com/yes/yes/utils/exceptions/ChunkAlreadyExistsException.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.yes.yes.utils.exceptions; - -public class ChunkAlreadyExistsException extends Exception { - public ChunkAlreadyExistsException() { - } - - public ChunkAlreadyExistsException(String message) { - super(message); - } - - public ChunkAlreadyExistsException(String message, Throwable cause) { - super(message, cause); - } - - public ChunkAlreadyExistsException(Throwable cause) { - super(cause); - } - - public ChunkAlreadyExistsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/src/main/java/com/yes/yes/world/World.java b/src/main/java/com/yes/yes/world/World.java index bfa57c4..a0fa631 100644 --- a/src/main/java/com/yes/yes/world/World.java +++ b/src/main/java/com/yes/yes/world/World.java @@ -1,7 +1,7 @@ package com.yes.yes.world; import com.yes.yes.utils.Coordinate; -import com.yes.yes.utils.exceptions.ChunkAlreadyExistsException; +import com.yes.yes.utils.exceptions.AlreadyExistsException; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.RowConstraints; @@ -26,8 +26,6 @@ public class World extends GridPane { Chunk chunk = getChunk(pos); this.getChildren().remove(chunk); loadedChunks.remove(pos); - - System.out.println("Unloading: " + pos.x + "; " + pos.y); } public void load(Coordinate pos) { @@ -36,14 +34,13 @@ public class World extends GridPane { Chunk chunk = getChunk(pos); this.add(chunk, pos.x, pos.y); loadedChunks.put(pos, chunk); - System.out.println("Loading: " + pos.x + "; " + pos.y); } - public void addChunk(Coordinate pos) throws ChunkAlreadyExistsException { + public void addChunk(Coordinate pos) throws AlreadyExistsException { if (chunks.get(pos) == null) { chunks.put(pos, new Chunk()); } else { - throw new ChunkAlreadyExistsException(); + throw new AlreadyExistsException("Chunk already exists"); } }