mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 00:46:01 +02:00
Add destroy method to entity and components
This commit is contained in:
@@ -44,4 +44,9 @@ public class ConveyorBehaviour extends Component {
|
||||
isConveyed = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,4 +26,9 @@ public class GeneratorBehaviour extends Component {
|
||||
this.parent.setData(dataKey, item.clone());
|
||||
System.out.println("generate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,15 @@ public class ItemBehaviour extends Component {
|
||||
System.out.println("Running item update");
|
||||
Platform.runLater(() -> {
|
||||
this.itemGroup.getChildren().clear();
|
||||
this.itemGroup.getChildren().add(item);
|
||||
if (item != null)
|
||||
this.itemGroup.getChildren().add(item);
|
||||
});
|
||||
System.out.println("Finished item update");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
parent.addListener(dataKey + "Changed", this::itemChanged);
|
||||
parent.addListener(dataKey + "Changed", this, this::itemChanged);
|
||||
|
||||
itemGroup = new VBox();
|
||||
itemGroup.setAlignment(Pos.CENTER);
|
||||
@@ -45,4 +46,9 @@ public class ItemBehaviour extends Component {
|
||||
public void update() {
|
||||
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();
|
||||
}
|
||||
|
||||
this.parent.addListener("itemAccepted", this::itemAccepted);
|
||||
this.parent.addListener("placed", this::placed);
|
||||
this.parent.addListener("itemAccepted", this, this::itemAccepted);
|
||||
this.parent.addListener("placed", this,this::placed);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,4 +60,10 @@ public class OfferBehaviour extends Component {
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ public class RecieveBehaviour extends Component {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
this.parent.addListener("offerItem", this::receive);
|
||||
this.parent.addListener("placed", this::placed);
|
||||
this.parent.addListener("offerItem", this, this::receive);
|
||||
this.parent.addListener("placed", this, this::placed);
|
||||
}
|
||||
|
||||
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
|
||||
public void initialize() {
|
||||
parent.addListener("placed", this::onNeighborPlaced);
|
||||
parent.addListener("placed", this, this::onNeighborPlaced);
|
||||
}
|
||||
|
||||
@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!");
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
GlobalEventHandler.addListener("timerTick", (__) -> {
|
||||
GlobalEventHandler.addListener("timerTick", this, (__) -> {
|
||||
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 update();
|
||||
public abstract void destroy();
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public abstract class Entity extends javafx.scene.Group {
|
||||
private int rotation;
|
||||
|
||||
public Entity() {
|
||||
GlobalEventHandler.addListener("timerTick", (__) -> update());
|
||||
GlobalEventHandler.addListener("timerTick", this, (__) -> update());
|
||||
}
|
||||
|
||||
public final void addBehaviour(Component behaviour) {
|
||||
@@ -21,9 +21,13 @@ public abstract class Entity extends javafx.scene.Group {
|
||||
|
||||
public final void removeBehaviour(Component behaviour) {
|
||||
ArrayList<Component> components = new ArrayList<>();
|
||||
for (Component b : behaviours) {
|
||||
if (!b.getClass().equals(behaviour.getClass())) {
|
||||
components.add(b);
|
||||
for (Component component : behaviours) {
|
||||
if (!component.getClass().equals(behaviour.getClass())) {
|
||||
components.add(component);
|
||||
}
|
||||
else
|
||||
{
|
||||
component.destroy();
|
||||
}
|
||||
}
|
||||
behaviours = components;
|
||||
@@ -41,6 +45,14 @@ public abstract class Entity extends javafx.scene.Group {
|
||||
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) {
|
||||
data.put(key, value);
|
||||
}
|
||||
@@ -49,12 +61,12 @@ public abstract class Entity extends javafx.scene.Group {
|
||||
return (T) data.get(key);
|
||||
}
|
||||
|
||||
public final <T> void addListener(String eventName, Consumer<T> function) {
|
||||
handler.addListener(eventName, function);
|
||||
public final <T> void addListener(String eventName, Object object, Consumer<T> function) {
|
||||
handler.addListener(eventName, object, function);
|
||||
}
|
||||
|
||||
public final <T> void removeListener(String eventName, Consumer<T> function) {
|
||||
handler.removeListener(eventName, function);
|
||||
public final <T> void removeListener(String eventName, Object object, Consumer<T> function) {
|
||||
handler.removeListener(eventName, object, function);
|
||||
}
|
||||
|
||||
public final void trigger(String eventName, Object parameter) {
|
||||
|
||||
@@ -1,34 +1,32 @@
|
||||
package com.yes.yes.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
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)) {
|
||||
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
|
||||
if (!events.containsKey(eventName)) {
|
||||
throw new IllegalArgumentException("Event " + eventName + " does not exist!");
|
||||
}
|
||||
//noinspection SuspiciousMethodCalls
|
||||
events.get(eventName).remove(function);
|
||||
events.get(eventName).remove(object);
|
||||
}
|
||||
|
||||
public <T> void trigger(String eventName, T parameter) {
|
||||
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() {
|
||||
}
|
||||
|
||||
public static <T> void addListener(String eventName, Consumer<T> function) {
|
||||
handler.addListener(eventName, function);
|
||||
public static <T> void addListener(String eventName, Object object, Consumer<T> function) {
|
||||
handler.addListener(eventName,object, function);
|
||||
}
|
||||
|
||||
public static <T> void removeListener(String eventName, Consumer<T> function) {
|
||||
handler.removeListener(eventName, function);
|
||||
public static <T> void removeListener(String eventName, Object object, Consumer<T> function) {
|
||||
handler.removeListener(eventName, object, function);
|
||||
}
|
||||
|
||||
public static void trigger(String eventName, Object parameter) {
|
||||
|
||||
@@ -45,7 +45,11 @@ public class Chunk extends GridPane {
|
||||
|
||||
public void removeEntity(Coordinate pos) {
|
||||
Entity entity = getEntity(pos);
|
||||
entity.setData("destroyed", true);
|
||||
|
||||
if (entity != null) {
|
||||
entity.setData("destroyed", true);
|
||||
entity.destroy();
|
||||
}
|
||||
this.getChildren().remove(entity);
|
||||
data[pos.x + pos.y * CHUNK_SIZE] = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user