Add destroy method to entity and components

This commit is contained in:
Stone_Red
2022-05-07 13:12:08 +02:00
parent 8331272918
commit a297661118
13 changed files with 84 additions and 31 deletions
@@ -44,4 +44,9 @@ public class ConveyorBehaviour extends Component {
isConveyed = false; isConveyed = false;
} }
} }
@Override
public void destroy() {
}
} }
@@ -26,4 +26,9 @@ public class GeneratorBehaviour extends Component {
this.parent.setData(dataKey, item.clone()); this.parent.setData(dataKey, item.clone());
System.out.println("generate"); System.out.println("generate");
} }
@Override
public void destroy() {
}
} }
@@ -23,14 +23,15 @@ public class ItemBehaviour extends Component {
System.out.println("Running item update"); System.out.println("Running item update");
Platform.runLater(() -> { Platform.runLater(() -> {
this.itemGroup.getChildren().clear(); this.itemGroup.getChildren().clear();
this.itemGroup.getChildren().add(item); if (item != null)
this.itemGroup.getChildren().add(item);
}); });
System.out.println("Finished item update"); System.out.println("Finished item update");
} }
@Override @Override
public void initialize() { public void initialize() {
parent.addListener(dataKey + "Changed", this::itemChanged); parent.addListener(dataKey + "Changed", this, this::itemChanged);
itemGroup = new VBox(); itemGroup = new VBox();
itemGroup.setAlignment(Pos.CENTER); itemGroup.setAlignment(Pos.CENTER);
@@ -45,4 +46,9 @@ public class ItemBehaviour extends Component {
public void update() { public void update() {
System.out.println("Destroyed:" + parent.getData("destroyed")); System.out.println("Destroyed:" + parent.getData("destroyed"));
} }
@Override
public void destroy() {
parent.removeListener(dataKey + "Changed", this, this::itemChanged);
}
} }
@@ -47,8 +47,8 @@ public class OfferBehaviour extends Component {
e.printStackTrace(); e.printStackTrace();
} }
this.parent.addListener("itemAccepted", this::itemAccepted); this.parent.addListener("itemAccepted", this, this::itemAccepted);
this.parent.addListener("placed", this::placed); this.parent.addListener("placed", this,this::placed);
} }
@Override @Override
@@ -60,4 +60,10 @@ public class OfferBehaviour extends Component {
this.receiver.trigger("offerItem", item); this.receiver.trigger("offerItem", item);
} }
@Override
public void destroy() {
this.parent.removeListener("itemAccepted", this, this::itemAccepted);
this.parent.removeListener("placed", this,this::placed);
}
} }
@@ -30,4 +30,9 @@ public class PlaceBehaviour extends Component {
public void update() { public void update() {
} }
@Override
public void destroy() {
}
} }
@@ -36,8 +36,8 @@ public class RecieveBehaviour extends Component {
e.printStackTrace(); e.printStackTrace();
} }
this.parent.addListener("offerItem", this::receive); this.parent.addListener("offerItem", this, this::receive);
this.parent.addListener("placed", this::placed); this.parent.addListener("placed", this, this::placed);
} }
void receive(Item item) { void receive(Item item) {
@@ -54,4 +54,10 @@ public class RecieveBehaviour extends Component {
} }
@Override
public void destroy() {
this.parent.removeListener("offerItem", this, this::receive);
this.parent.removeListener("placed", this, this::placed);
}
} }
@@ -17,7 +17,7 @@ public class TestBehaviour extends Component {
@Override @Override
public void initialize() { public void initialize() {
parent.addListener("placed", this::onNeighborPlaced); parent.addListener("placed", this, this::onNeighborPlaced);
} }
@Override @Override
@@ -38,4 +38,9 @@ public class TestBehaviour extends Component {
} }
}*/ }*/
} }
@Override
public void destroy() {
parent.removeListener("placed", this, this::onNeighborPlaced);
}
} }
@@ -55,9 +55,9 @@ public class GameManager {
System.out.println("Finished tick!"); System.out.println("Finished tick!");
}, 0, 1, TimeUnit.SECONDS); }, 0, 1, TimeUnit.SECONDS);
GlobalEventHandler.addListener("timerTick", (__) -> { GlobalEventHandler.addListener("timerTick", this, (__) -> {
System.out.println("Tick!"); System.out.println("Tick!");
}); });
GlobalEventHandler.addListener("closing", (__) -> executor.shutdown()); GlobalEventHandler.addListener("closing", this, (__) -> executor.shutdown());
} }
} }
@@ -12,4 +12,5 @@ public abstract class Component {
public abstract void initialize(); public abstract void initialize();
public abstract void update(); public abstract void update();
public abstract void destroy();
} }
+20 -8
View File
@@ -12,7 +12,7 @@ public abstract class Entity extends javafx.scene.Group {
private int rotation; private int rotation;
public Entity() { public Entity() {
GlobalEventHandler.addListener("timerTick", (__) -> update()); GlobalEventHandler.addListener("timerTick", this, (__) -> update());
} }
public final void addBehaviour(Component behaviour) { public final void addBehaviour(Component behaviour) {
@@ -21,9 +21,13 @@ public abstract class Entity extends javafx.scene.Group {
public final void removeBehaviour(Component behaviour) { public final void removeBehaviour(Component behaviour) {
ArrayList<Component> components = new ArrayList<>(); ArrayList<Component> components = new ArrayList<>();
for (Component b : behaviours) { for (Component component : behaviours) {
if (!b.getClass().equals(behaviour.getClass())) { if (!component.getClass().equals(behaviour.getClass())) {
components.add(b); components.add(component);
}
else
{
component.destroy();
} }
} }
behaviours = components; behaviours = components;
@@ -41,6 +45,14 @@ public abstract class Entity extends javafx.scene.Group {
behaviours.forEach(Component::initialize); behaviours.forEach(Component::initialize);
} }
public final void destroy()
{
GlobalEventHandler.removeListener("timerTick",this, (__) -> update());
for (Component component : behaviours) {
component.destroy();
}
}
public final void setData(String key, Object value) { public final void setData(String key, Object value) {
data.put(key, value); data.put(key, value);
} }
@@ -49,12 +61,12 @@ public abstract class Entity extends javafx.scene.Group {
return (T) data.get(key); return (T) data.get(key);
} }
public final <T> void addListener(String eventName, Consumer<T> function) { public final <T> void addListener(String eventName, Object object, Consumer<T> function) {
handler.addListener(eventName, function); handler.addListener(eventName, object, function);
} }
public final <T> void removeListener(String eventName, Consumer<T> function) { public final <T> void removeListener(String eventName, Object object, Consumer<T> function) {
handler.removeListener(eventName, function); handler.removeListener(eventName, object, function);
} }
public final void trigger(String eventName, Object parameter) { public final void trigger(String eventName, Object parameter) {
@@ -1,34 +1,32 @@
package com.yes.yes.utils; package com.yes.yes.utils;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
public class EventHandler { public class EventHandler {
private final HashMap<String, ArrayList<Consumer<Object>>> events = new HashMap<>(); private final HashMap<String, HashMap<Object, Consumer<Object>>> events = new HashMap<>();
public <T> void addListener(String eventName, Consumer<T> function) { public <T> void addListener(String eventName, Object object, Consumer<T> function) {
if (!events.containsKey(eventName)) { if (!events.containsKey(eventName)) {
events.put(eventName, new ArrayList<>()); events.put(eventName, new HashMap<>());
} }
events.get(eventName).add((e) -> function.accept((T) e)); events.get(eventName).put(object, (e) -> function.accept((T) e));
} }
public <T> void removeListener(String eventName, Consumer<T> function) throws IllegalArgumentException { public <T> void removeListener(String eventName, Object object, Consumer<T> function) throws IllegalArgumentException {
//NOTE: This needs to be called otherwise memory leak //NOTE: This needs to be called otherwise memory leak
if (!events.containsKey(eventName)) { if (!events.containsKey(eventName)) {
throw new IllegalArgumentException("Event " + eventName + " does not exist!"); throw new IllegalArgumentException("Event " + eventName + " does not exist!");
} }
//noinspection SuspiciousMethodCalls //noinspection SuspiciousMethodCalls
events.get(eventName).remove(function); events.get(eventName).remove(object);
} }
public <T> void trigger(String eventName, T parameter) { public <T> void trigger(String eventName, T parameter) {
if (events.containsKey(eventName)) { if (events.containsKey(eventName)) {
events.get(eventName).forEach(e -> e.accept(parameter)); events.get(eventName).values().forEach(e -> e.accept(parameter));
} }
} }
} }
@@ -9,12 +9,12 @@ public class GlobalEventHandler {
private GlobalEventHandler() { private GlobalEventHandler() {
} }
public static <T> void addListener(String eventName, Consumer<T> function) { public static <T> void addListener(String eventName, Object object, Consumer<T> function) {
handler.addListener(eventName, function); handler.addListener(eventName,object, function);
} }
public static <T> void removeListener(String eventName, Consumer<T> function) { public static <T> void removeListener(String eventName, Object object, Consumer<T> function) {
handler.removeListener(eventName, function); handler.removeListener(eventName, object, function);
} }
public static void trigger(String eventName, Object parameter) { public static void trigger(String eventName, Object parameter) {
+5 -1
View File
@@ -45,7 +45,11 @@ public class Chunk extends GridPane {
public void removeEntity(Coordinate pos) { public void removeEntity(Coordinate pos) {
Entity entity = getEntity(pos); Entity entity = getEntity(pos);
entity.setData("destroyed", true);
if (entity != null) {
entity.setData("destroyed", true);
entity.destroy();
}
this.getChildren().remove(entity); this.getChildren().remove(entity);
data[pos.x + pos.y * CHUNK_SIZE] = null; data[pos.x + pos.y * CHUNK_SIZE] = null;
} }