mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-10 07:36:05 +02:00
Add Eventsystem; Update Entity to reflect changes
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
package com.yes.yes.utils;
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class Entity extends javafx.scene.Group {
|
public abstract class Entity extends javafx.scene.Group {
|
||||||
private ArrayList<Component> behaviours = new ArrayList<>();
|
private ArrayList<Component> behaviours = new ArrayList<>();
|
||||||
|
private final HashMap<String, Object> data = new HashMap<String, Object>();
|
||||||
|
private final EventHandler handler = new EventHandler();
|
||||||
|
private int rotation;
|
||||||
|
|
||||||
public final void addBehaviour(Component behaviour) {
|
public final void addBehaviour(Component behaviour) {
|
||||||
behaviours.add(behaviour);
|
behaviours.add(behaviour);
|
||||||
@@ -26,4 +31,24 @@ public abstract class Entity extends javafx.scene.Group {
|
|||||||
behaviour.update();
|
behaviour.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void setData(String key, Object value) {
|
||||||
|
data.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final <T> T getData(String key) {
|
||||||
|
return (T) data.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void addListener(String eventName, Function<Object, Void> function) {
|
||||||
|
handler.addListener(eventName, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void removeListener(String eventName, Function<Object, Void> function) {
|
||||||
|
handler.removeListener(eventName, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void trigger(String eventName, Object parameter) {
|
||||||
|
handler.trigger(eventName, parameter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class EventHandler {
|
||||||
|
private final HashMap<String, ArrayList<Function<Object, Void>>> events = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void addListener(String eventName, Function<Object, Void> function) {
|
||||||
|
if (!events.containsKey(eventName)) {
|
||||||
|
events.put(eventName, new ArrayList<>());
|
||||||
|
}
|
||||||
|
events.get(eventName).add(function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeListener(String eventName, Function<Object, Void> function) throws IllegalArgumentException {
|
||||||
|
//NOTE: This needs to be called otherwise memory leak
|
||||||
|
if (!events.containsKey(eventName)) {
|
||||||
|
throw new IllegalArgumentException("Event " + eventName + " does not exist!");
|
||||||
|
}
|
||||||
|
events.get(eventName).remove(function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void trigger(String eventName, Object parameter) {
|
||||||
|
//TODO: Check if this needs null checking
|
||||||
|
if (events.containsKey(eventName)) {
|
||||||
|
events.get(eventName).forEach(e -> e.apply(parameter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public abstract class GlobalEventHandler {
|
||||||
|
private static final EventHandler handler = new EventHandler();
|
||||||
|
|
||||||
|
public static void addListener(String eventName, Function<Object, Void> function) {
|
||||||
|
handler.addListener(eventName, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeListener(String eventName, Function<Object, Void> function) {
|
||||||
|
handler.removeListener(eventName, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void trigger(String eventName, Object parameter) {
|
||||||
|
handler.trigger(eventName, parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user