Add Entity Registry; Add Template for Components

+ Refactored ChunkAlreadyExistsException (now AlreadyExistsException)
+ Removed debug console logs
This commit is contained in:
Stefan-5422
2022-02-25 10:52:24 +01:00
parent b3ac1077d7
commit 1c7b8dba1b
11 changed files with 130 additions and 45 deletions
@@ -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() {
}
}
@@ -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));
}
}
@@ -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();
}
}
}
@@ -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));
}
}
@@ -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();
}
+10 -10
View File
@@ -3,27 +3,27 @@ package com.yes.yes.utils;
import java.util.ArrayList;
public abstract class Entity extends javafx.scene.Group {
private ArrayList<Component> behavours = new ArrayList<>();
private ArrayList<Component> 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<Component> 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();
}
}
}
@@ -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<String, RegistryEntry> 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);
}
}
@@ -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;
}
}
@@ -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);
}
}
@@ -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);
}
}
+3 -6
View File
@@ -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");
}
}