diff --git a/build.gradle b/build.gradle index 89630e0f..e649b6ba 100644 --- a/build.gradle +++ b/build.gradle @@ -24,11 +24,11 @@ sourceSets { if (!project.hasProperty("example")) { throw new IllegalStateException(""" You need to specify which example using the "example" property! -E.G. \"./gradlew example -Pexample=helloworld\"""") +E.G. \"./gradlew example -Pexample=hellofastj\"""") } } - description('An example game using FastJ.') + description('Runs a FastJ example program.') classpath = files(sourceSets.main.output, sourceSets.example.runtimeClasspath) main = "io.github.lucasstarsz.fastj.example.${project.hasProperty("example") ? project.getProperty("example") : ""}.Main" } diff --git a/src/example/README.md b/src/example/README.md index 795e39d6..d05e50ac 100644 --- a/src/example/README.md +++ b/src/example/README.md @@ -12,9 +12,14 @@ Then, run the gradle task `example`. When you do this, you'll need to specify wh ``` _Having trouble using `gradlew`? Read [this][Terminals Are Different]._ -## Example Projects +## Example Programs -### Hello, World! +### Hello, FastJ! +This program is a remarkably simple introduction to starting work with FastJ. It initializes the engine and runs the engine, which results in an empty window. This also can serve as a project template for working with FastJ. + +Command to run: `./gradlew example -Pexample=hellofastj` + +### (Old) Hello, World! This project is an introduction to working with FastJ, and covers many of the essential topics: - Engine Initialization/Running - Window Creation @@ -22,7 +27,7 @@ This project is an introduction to working with FastJ, and covers many of the es - Drawing game objects and UI - Applying Behaviors to Game Objects -Command to run: `./gradlew example -Pexample=helloworld` +Command to run: `./gradlew example -Pexample=oldhelloworld` ### Bullet Hell This project takes the concepts we've learned so far and meshes them together into a game! diff --git a/src/example/java/io/github/lucasstarsz/fastj/example/hellofastj/Main.java b/src/example/java/io/github/lucasstarsz/fastj/example/hellofastj/Main.java new file mode 100644 index 00000000..f6ba3242 --- /dev/null +++ b/src/example/java/io/github/lucasstarsz/fastj/example/hellofastj/Main.java @@ -0,0 +1,22 @@ +package io.github.lucasstarsz.fastj.example.hellofastj; + +import io.github.lucasstarsz.fastj.engine.FastJEngine; +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.control.SimpleManager; + +public class Main extends SimpleManager { + + @Override + public void init(Display display) { + } + + @Override + public void update(Display display) { + } + + public static void main(String[] args) { + FastJEngine.init("Hello, FastJ!", new Main()); + FastJEngine.run(); + } +} diff --git a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/GameManager.java b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/GameManager.java similarity index 89% rename from src/example/java/io/github/lucasstarsz/fastj/example/helloworld/GameManager.java rename to src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/GameManager.java index d6e64aed..21d8dee7 100644 --- a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/GameManager.java +++ b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/GameManager.java @@ -1,4 +1,4 @@ -package io.github.lucasstarsz.fastj.example.helloworld; +package io.github.lucasstarsz.fastj.example.oldhelloworld; import io.github.lucasstarsz.fastj.graphics.Display; @@ -6,7 +6,7 @@ import java.awt.RenderingHints; -import io.github.lucasstarsz.fastj.example.helloworld.scenes.GameScene; +import io.github.lucasstarsz.fastj.example.oldhelloworld.scenes.GameScene; /** * Manages the game's overall state (in the background). diff --git a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/Main.java b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/Main.java similarity index 85% rename from src/example/java/io/github/lucasstarsz/fastj/example/helloworld/Main.java rename to src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/Main.java index af780e63..249ce8e7 100644 --- a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/Main.java +++ b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/Main.java @@ -1,4 +1,4 @@ -package io.github.lucasstarsz.fastj.example.helloworld; +package io.github.lucasstarsz.fastj.example.oldhelloworld; import io.github.lucasstarsz.fastj.engine.FastJEngine; diff --git a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/customscripts/PlayerScript.java b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/customscripts/PlayerScript.java similarity index 94% rename from src/example/java/io/github/lucasstarsz/fastj/example/helloworld/customscripts/PlayerScript.java rename to src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/customscripts/PlayerScript.java index 7c15af03..03ce0163 100644 --- a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/customscripts/PlayerScript.java +++ b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/customscripts/PlayerScript.java @@ -1,4 +1,4 @@ -package io.github.lucasstarsz.fastj.example.helloworld.customscripts; +package io.github.lucasstarsz.fastj.example.oldhelloworld.customscripts; import io.github.lucasstarsz.fastj.math.Pointf; import io.github.lucasstarsz.fastj.graphics.game.GameObject; @@ -7,7 +7,7 @@ import io.github.lucasstarsz.fastj.systems.input.keyboard.Keyboard; import io.github.lucasstarsz.fastj.systems.input.keyboard.Keys; -import io.github.lucasstarsz.fastj.example.helloworld.scenes.GameScene; +import io.github.lucasstarsz.fastj.example.oldhelloworld.scenes.GameScene; /** A custom script that moves the received game object using the WASD keys. */ public class PlayerScript implements Behavior { diff --git a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/scenes/GameScene.java b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/scenes/GameScene.java similarity index 97% rename from src/example/java/io/github/lucasstarsz/fastj/example/helloworld/scenes/GameScene.java rename to src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/scenes/GameScene.java index c3cf373f..094559b5 100644 --- a/src/example/java/io/github/lucasstarsz/fastj/example/helloworld/scenes/GameScene.java +++ b/src/example/java/io/github/lucasstarsz/fastj/example/oldhelloworld/scenes/GameScene.java @@ -1,4 +1,4 @@ -package io.github.lucasstarsz.fastj.example.helloworld.scenes; +package io.github.lucasstarsz.fastj.example.oldhelloworld.scenes; import io.github.lucasstarsz.fastj.engine.FastJEngine; import io.github.lucasstarsz.fastj.math.Pointf; @@ -15,7 +15,7 @@ import java.awt.Color; import java.awt.Font; -import io.github.lucasstarsz.fastj.example.helloworld.customscripts.PlayerScript; +import io.github.lucasstarsz.fastj.example.oldhelloworld.customscripts.PlayerScript; /** The game scene, where all the action happens! */ public class GameScene extends Scene { diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorHandler.java b/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorHandler.java new file mode 100644 index 00000000..1a59497e --- /dev/null +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorHandler.java @@ -0,0 +1,56 @@ +package io.github.lucasstarsz.fastj.systems.behaviors; + +import io.github.lucasstarsz.fastj.graphics.game.GameObject; + +import java.util.List; + +/** + * Interface denoting that the implementing classes directly interface with the {@link BehaviorManager} class. + * + * FOR IMPLEMENTORS: In order for these methods to work you need to call {@link + * BehaviorManager#addListenerList(BehaviorHandler)} upon construction. + */ +public interface BehaviorHandler { + + /** + * Gets the behavior listeners assigned to the behavior handler. + * + * @return The behavior listeners of the behavior handler. + */ + default List getBehaviorListeners() { + return BehaviorManager.getList(this); + } + + /** + * Adds the specified behavior listener to the behavior handler. + * + * @param listener The behavior listener to add. + */ + default void addBehaviorListener(GameObject listener) { + BehaviorManager.addListener(this, listener); + } + + /** + * Removes the specified behavior listener from the behavior handler. + * + * @param listener The behavior listener to remove. + */ + default void removeBehaviorListener(GameObject listener) { + BehaviorManager.removeListener(this, listener); + } + + /** Initializes all behavior listeners in the behavior handler. */ + default void initBehaviorListeners() { + BehaviorManager.initBehaviorListeners(this); + } + + /** Updates all behavior listeners in the behavior handler. */ + default void updateBehaviorListeners() { + BehaviorManager.updateBehaviorListeners(this); + } + + /** Removes all behavior listeners in the behavior handler. */ + default void clearBehaviorListeners() { + BehaviorManager.clearListenerList(this); + } +} diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorManager.java b/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorManager.java index ace71846..c2592ecf 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorManager.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/behaviors/BehaviorManager.java @@ -2,106 +2,105 @@ import io.github.lucasstarsz.fastj.graphics.game.GameObject; -import io.github.lucasstarsz.fastj.systems.control.Scene; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** - * Class to manage behavior listeners for all scenes. + * Class to manage behavior listeners for all {@link BehaviorHandler}s. * * @author Andrew Dey * @version 1.0.0 */ public class BehaviorManager { - private static final Map> BehaviorListenerLists = new HashMap<>(); + private static final Map> BehaviorListenerLists = new HashMap<>(); private BehaviorManager() { throw new java.lang.IllegalStateException(); } /** - * Gets the specified list of behavior listeners aliased to the specified {@code Scene}. + * Gets the specified list of behavior listeners aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} to get the list of behavior listeners for. + * @param behaviorHandler The {@code BehaviorHandler} to get the list of behavior listeners for. * @return The list of behavior listeners. */ - public static List getList(Scene scene) { - return BehaviorListenerLists.get(scene); + public static List getList(BehaviorHandler behaviorHandler) { + return BehaviorListenerLists.get(behaviorHandler); } /** - * Adds the specified behavior listener to the list aliased to the specified {@code Scene}. + * Adds the specified behavior listener to the list aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} used as the alias to add the specified behavior listener to. - * @param listener The behavior listener to add. + * @param behaviorHandler The {@code BehaviorHandler} used as the alias to add the specified behavior listener to. + * @param listener The behavior listener to add. */ - public static void addListener(Scene scene, GameObject listener) { - if (!BehaviorListenerLists.get(scene).contains(listener)) { - BehaviorListenerLists.get(scene).add(listener); + public static void addListener(BehaviorHandler behaviorHandler, GameObject listener) { + if (!BehaviorListenerLists.get(behaviorHandler).contains(listener)) { + BehaviorListenerLists.get(behaviorHandler).add(listener); } } /** - * Removes the specified behavior from the list aliased to the specified {@code Scene}. + * Removes the specified behavior from the list aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} used as the alias to remove the specified behavior listener from. - * @param listener The behavior listener to remove. + * @param behaviorHandler The {@code BehaviorHandler} used as the alias to remove the specified behavior listener + * from. + * @param listener The behavior listener to remove. */ - public static void removeListener(Scene scene, GameObject listener) { - BehaviorListenerLists.get(scene).remove(listener); + public static void removeListener(BehaviorHandler behaviorHandler, GameObject listener) { + BehaviorListenerLists.get(behaviorHandler).remove(listener); } /** - * Adds an alias for the specified {@code Scene}, if one does not already exist. + * Adds an alias for the specified {@link BehaviorHandler}, if one does not already exist. * - * @param scene The {@code Scene} to add a new alias for. + * @param behaviorHandler The {@code BehaviorHandler} to add a new alias for. */ - public static void addListenerList(Scene scene) { - if (!BehaviorListenerLists.containsKey(scene)) { - BehaviorListenerLists.put(scene, new ArrayList<>()); + public static void addListenerList(BehaviorHandler behaviorHandler) { + if (!BehaviorListenerLists.containsKey(behaviorHandler)) { + BehaviorListenerLists.put(behaviorHandler, new ArrayList<>()); } } /** - * Removes the list aliased to the specified {@code Scene}, and all elements inside. + * Removes the list aliased to the specified {@link BehaviorHandler}, and all elements inside. * - * @param scene The {@code Scene} to remove the alias for. + * @param behaviorHandler The {@code BehaviorHandler} to remove the alias for. */ - public static void removeListenerList(Scene scene) { - BehaviorListenerLists.remove(scene); + public static void removeListenerList(BehaviorHandler behaviorHandler) { + BehaviorListenerLists.remove(behaviorHandler); } /** - * Removes all elements from the list aliased to the specified {@code Scene}. + * Removes all elements from the list aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} used as the alias to remove all behavior listeners. + * @param behaviorHandler The {@code BehaviorHandler} used as the alias to remove all behavior listeners. */ - public static void clearListenerList(Scene scene) { - BehaviorListenerLists.get(scene).clear(); + public static void clearListenerList(BehaviorHandler behaviorHandler) { + BehaviorListenerLists.get(behaviorHandler).clear(); } /** - * Initializes the behavior listeners aliased to the specified {@code Scene}. + * Initializes the behavior listeners aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} used as the alias to initialize the behavior listeners for. + * @param behaviorHandler The {@code BehaviorHandler} used as the alias to initialize the behavior listeners for. */ - public static void initBehaviorListeners(Scene scene) { - for (GameObject listener : BehaviorListenerLists.get(scene)) { + public static void initBehaviorListeners(BehaviorHandler behaviorHandler) { + for (GameObject listener : BehaviorListenerLists.get(behaviorHandler)) { listener.initBehaviors(); } } /** - * Updates the behavior listeners aliased to the specified {@code Scene}. + * Updates the behavior listeners aliased to the specified {@link BehaviorHandler}. * - * @param scene The {@code Scene} used as the alias to update the behavior listeners for. + * @param behaviorHandler The {@code BehaviorHandler} used as the alias to update the behavior listeners for. */ - public static void updateBehaviorListeners(Scene scene) { - for (GameObject listener : BehaviorListenerLists.get(scene)) { + public static void updateBehaviorListeners(BehaviorHandler behaviorHandler) { + for (GameObject listener : BehaviorListenerLists.get(behaviorHandler)) { listener.updateBehaviors(); } } diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/control/Scene.java b/src/main/java/io/github/lucasstarsz/fastj/systems/control/Scene.java index ad485b1c..d1d51139 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/control/Scene.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/control/Scene.java @@ -2,25 +2,23 @@ import io.github.lucasstarsz.fastj.graphics.Camera; import io.github.lucasstarsz.fastj.graphics.Display; -import io.github.lucasstarsz.fastj.graphics.Drawable; -import io.github.lucasstarsz.fastj.graphics.game.GameObject; +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorHandler; import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorManager; import io.github.lucasstarsz.fastj.systems.input.InputManager; +import io.github.lucasstarsz.fastj.systems.tags.TagHandler; import io.github.lucasstarsz.fastj.systems.tags.TagManager; -import java.util.List; - /** * Class containing the logic for a specific section, or scene, of a game. *

- * The {@code LogicManager} of any game made with FastJ can store many scenes. Through this, the user can divide their + * A {@code SceneManager} of any game made with FastJ can store many scenes. Through this, the user can divide their * game into different sections. * * @author Andrew Dey * @version 1.0.0 */ -public abstract class Scene { +public abstract class Scene implements BehaviorHandler, TagHandler { private final String sceneName; private final Camera camera; @@ -84,24 +82,6 @@ public String getSceneName() { return sceneName; } - /** - * Gets the behavior listeners assigned to the scene. - * - * @return The behavior listeners of the scene. - */ - public List getBehaviorListeners() { - return BehaviorManager.getList(this); - } - - /** - * Gets the taggable entities assigned to the scene. - * - * @return The taggable entities of the scene. - */ - public List getTaggableEntities() { - return TagManager.getEntityList(this); - } - /** * Gets the camera of the scene. * @@ -129,77 +109,6 @@ public void setInitialized(boolean initialized) { isInitialized = initialized; } - /** - * Gets all taggable entities with the specified tag. - * - * @param tag The tag to check for. - * @return A list of all taggable entities with the specified tag. - */ - public List getAllWithTag(String tag) { - return TagManager.getAllInListWithTag(this, tag); - } - - /* Behavior Listeners */ - - /** - * Adds the specified behavior listener to the scene. - * - * @param listener The behavior listener to add. - */ - public void addBehaviorListener(GameObject listener) { - BehaviorManager.addListener(this, listener); - } - - /** - * Removes the specified behavior listener from the scene. - * - * @param listener The behavior listener to remove. - */ - public void removeBehaviorListener(GameObject listener) { - BehaviorManager.removeListener(this, listener); - } - - /** Initializes all behavior listeners in the scene. */ - public void initBehaviorListeners() { - BehaviorManager.initBehaviorListeners(this); - } - - /** Updates all behavior listeners in the scene. */ - public void updateBehaviorListeners() { - BehaviorManager.updateBehaviorListeners(this); - } - - /** Removes all behavior listeners in the scene. */ - public void clearBehaviorListeners() { - BehaviorManager.clearListenerList(this); - } - - /* Taggable Entities */ - - /** - * Adds the specified taggable entity, only if it extends the {@code Drawable} class. - * - * @param entity The taggable entity to add. - * @param The type of the taggable entity, which must extend the {@code Drawable} class. - */ - public void addTaggableEntity(T entity) { - TagManager.addTaggableEntity(this, entity); - } - - /** - * Removes the specified taggable entity. - * - * @param entity The taggable entity to remove. - */ - public void removeTaggableEntity(Drawable entity) { - TagManager.removeTaggableEntity(this, entity); - } - - /** Removes all taggable from the scene. */ - public void clearTaggableEntities() { - TagManager.clearEntityList(this); - } - /* Reset */ /** Removes all elements from the scene. */ diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/control/SceneManager.java b/src/main/java/io/github/lucasstarsz/fastj/systems/control/SceneManager.java index 8e0bf57f..1aa210b1 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/control/SceneManager.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/control/SceneManager.java @@ -24,21 +24,12 @@ public abstract class SceneManager implements LogicManager { private Scene currentScene; private boolean switchingScenes; - /** Processes all pending input events. */ - public void processInputEvents() { - currentScene.inputManager.processEvents(currentScene); - } - - @Override - public void receivedInputEvent(InputEvent inputEvent) { - currentScene.inputManager.receivedInputEvent(inputEvent); - } - /** * Updates the current scene, its behaviors, and listeners. * * @param display The {@code Display} that the game renders to. */ + @Override public void update(Display display) { updateCurrentScene(display); } @@ -48,11 +39,24 @@ public void update(Display display) { * * @param display The {@code Display} that the game renders to. */ + @Override public void render(Display display) { renderCurrentScene(display); } + /** Processes all pending input events. */ + @Override + public void processInputEvents() { + currentScene.inputManager.processEvents(); + } + + @Override + public void receivedInputEvent(InputEvent inputEvent) { + currentScene.inputManager.receivedInputEvent(inputEvent); + } + /** Resets the logic manager. */ + @Override public void reset() { for (Scene s : scenes.values()) { if (s.isInitialized()) { @@ -228,7 +232,7 @@ private void updateCurrentScene(Display display) { } /** - * Safely renders the current scene to the Display. + * Safely renders the current scene to the {@code Display}. * * @param display The {@code Display} that the game renders to. */ diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/control/SimpleManager.java b/src/main/java/io/github/lucasstarsz/fastj/systems/control/SimpleManager.java new file mode 100644 index 00000000..675dd13b --- /dev/null +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/control/SimpleManager.java @@ -0,0 +1,74 @@ +package io.github.lucasstarsz.fastj.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Camera; +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorHandler; +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorManager; +import io.github.lucasstarsz.fastj.systems.input.InputManager; +import io.github.lucasstarsz.fastj.systems.tags.TagHandler; +import io.github.lucasstarsz.fastj.systems.tags.TagManager; + +import java.awt.event.InputEvent; + +public abstract class SimpleManager implements LogicManager, BehaviorHandler, TagHandler { + + private final Camera camera; + public final InputManager inputManager; + public final DrawableManager drawableManager; + + /** + * Initializes the contents of the {@code SimpleManager}. + */ + public SimpleManager() { + camera = new Camera(); + + inputManager = new InputManager(); + drawableManager = new DrawableManager(); + + TagManager.addTaggableEntityList(this); + BehaviorManager.addListenerList(this); + } + + /** + * Renders the contents of the manager's {@code DrawableManager} to the {@code Display}. + * + * @param display The {@code Display} that the game renders to. + */ + @Override + public void render(Display display) { + display.render( + drawableManager.getGameObjects(), + drawableManager.getUIElements(), + camera + ); + } + + @Override + public void processInputEvents() { + inputManager.processEvents(); + } + + @Override + public void receivedInputEvent(InputEvent inputEvent) { + inputManager.receivedInputEvent(inputEvent); + } + + /** + * Gets the {@code Camera} of the manager. + * + * @return The manager's camera. + */ + public Camera getCamera() { + return camera; + } + + @Override + public void reset() { + camera.reset(); + inputManager.clearAllLists(); + drawableManager.clearAllLists(); + this.clearTaggableEntities(); + this.clearBehaviorListeners(); + } +} diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/input/InputManager.java b/src/main/java/io/github/lucasstarsz/fastj/systems/input/InputManager.java index ecb5a6be..d090633a 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/input/InputManager.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/input/InputManager.java @@ -1,6 +1,5 @@ package io.github.lucasstarsz.fastj.systems.input; -import io.github.lucasstarsz.fastj.systems.control.Scene; import io.github.lucasstarsz.fastj.systems.input.keyboard.Keyboard; import io.github.lucasstarsz.fastj.systems.input.keyboard.KeyboardActionListener; import io.github.lucasstarsz.fastj.systems.input.mouse.Mouse; @@ -149,13 +148,7 @@ public void fireKeyEvent(KeyEvent keyEvent) { KeyboardActionProcessor.get(keyEvent.getID()).accept(keyEvent, keyActionListeners); } - /** - * Fires a {@code keys down} event to all listening {@code KeyboardActionListeners}. - *

- * - * NOTE: When used by a FastJ {@code Scene}, this event gets fired every engine update - * call, if there are any keys pressed. - */ + /** Fires a {@code keys down} event to all listening {@code KeyboardActionListeners}. */ public void fireKeysDown() { if (Keyboard.areKeysDown()) { for (KeyboardActionListener listener : keyActionListeners) { @@ -206,7 +199,7 @@ public void fireMouseEvent(MouseEvent mouseEvent) { * backlog gets emptied into the main event list after all the events in that main list have been processed. * * @param event The event to be stored for processing later. - * @see #processEvents(Scene) + * @see #processEvents() */ public void receivedInputEvent(InputEvent event) { if (isProcessingEvents) { @@ -221,17 +214,15 @@ public void receivedInputEvent(InputEvent event) { *

* This method also empties the event backlog into the main event set after all the current events have been * processed and removed. - * - * @param current The scene to process events for. */ - public void processEvents(Scene current) { + public void processEvents() { isProcessingEvents = true; - for (InputEvent event : receivedInputEvents) { - if (event instanceof MouseEvent) { - Mouse.processEvent(current, (MouseEvent) event); - } else if (event instanceof KeyEvent) { - Keyboard.processEvent(current, (KeyEvent) event); + for (InputEvent inputEvent : receivedInputEvents) { + if (inputEvent instanceof MouseEvent) { + Mouse.processEvent(this, (MouseEvent) inputEvent); + } else if (inputEvent instanceof KeyEvent) { + Keyboard.processEvent(this, (KeyEvent) inputEvent); } } receivedInputEvents.clear(); diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/input/keyboard/Keyboard.java b/src/main/java/io/github/lucasstarsz/fastj/systems/input/keyboard/Keyboard.java index 82ef735a..55636060 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/input/keyboard/Keyboard.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/input/keyboard/Keyboard.java @@ -2,7 +2,7 @@ import io.github.lucasstarsz.fastj.engine.FastJEngine; -import io.github.lucasstarsz.fastj.systems.control.Scene; +import io.github.lucasstarsz.fastj.systems.input.InputManager; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -25,8 +25,8 @@ public class Keyboard implements KeyListener { private static String lastKeyPressed = ""; private static ScheduledExecutorService keyChecker; - private static final Map> KeyEventProcessor = Map.of( - KeyEvent.KEY_PRESSED, (scene, keyEvent) -> { + private static final Map> KeyEventProcessor = Map.of( + KeyEvent.KEY_PRESSED, (inputManager, keyEvent) -> { KeyDescription keyDescription = KeyDescription.get(keyEvent.getKeyCode(), keyEvent.getKeyLocation()); Key key = null; @@ -42,12 +42,12 @@ public class Keyboard implements KeyListener { if (!key.currentlyPressed) { key.setRecentPress(true); - scene.inputManager.fireKeyEvent(keyEvent); + inputManager.fireKeyEvent(keyEvent); } key.setCurrentPress(true); }, - KeyEvent.KEY_RELEASED, (scene, keyEvent) -> { + KeyEvent.KEY_RELEASED, (inputManager, keyEvent) -> { KeyDescription keyDescription = KeyDescription.get(keyEvent.getKeyCode(), keyEvent.getKeyLocation()); Key key = Keys.get(keyDescription); @@ -57,11 +57,11 @@ public class Keyboard implements KeyListener { key.setRecentRelease(true); } - scene.inputManager.fireKeyEvent(keyEvent); + inputManager.fireKeyEvent(keyEvent); }, - KeyEvent.KEY_TYPED, (scene, keyEvent) -> { + KeyEvent.KEY_TYPED, (inputManager, keyEvent) -> { lastKeyPressed = KeyEvent.getKeyText(keyEvent.getKeyCode()); - scene.inputManager.fireKeyEvent(keyEvent); + inputManager.fireKeyEvent(keyEvent); } ); @@ -245,13 +245,13 @@ public void keyTyped(KeyEvent e) { } /** - * Processes the specified key event for the specified scene, based on its event type. + * Processes the specified key event for the specified input manager, based on its event type. * - * @param scene The scene to fire the event to. - * @param event The key event to process. + * @param inputManager The input manager to fire the event to. + * @param event The key event to process. */ - public static void processEvent(Scene scene, KeyEvent event) { - KeyEventProcessor.get(event.getID()).accept(scene, event); + public static void processEvent(InputManager inputManager, KeyEvent event) { + KeyEventProcessor.get(event.getID()).accept(inputManager, event); /* Don't call the fireKeyEvent here! * KeyEvent.KEY_PRESSED only gets called under certain * conditions, so it cannot be abstracted to work here without some serious effort. */ diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/input/mouse/Mouse.java b/src/main/java/io/github/lucasstarsz/fastj/systems/input/mouse/Mouse.java index 2a18091a..553973a2 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/input/mouse/Mouse.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/input/mouse/Mouse.java @@ -5,7 +5,7 @@ import io.github.lucasstarsz.fastj.graphics.Display; import io.github.lucasstarsz.fastj.graphics.Drawable; -import io.github.lucasstarsz.fastj.systems.control.Scene; +import io.github.lucasstarsz.fastj.systems.input.InputManager; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; @@ -19,7 +19,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; +import java.util.function.Consumer; /** * Mouse class that takes mouse input from the {@code Display}, and uses it to store variables about the mouse's current @@ -44,8 +44,8 @@ public class Mouse implements MouseListener, MouseMotionListener, MouseWheelList private static boolean currentlyOnScreen; private static Pointf mouseLocation = new Pointf(); - private static final Map> MouseEventProcessor = Map.of( - MouseEvent.MOUSE_PRESSED, (scene, mouseEvent) -> { + private static final Map> MouseEventProcessor = Map.of( + MouseEvent.MOUSE_PRESSED, mouseEvent -> { if (!MouseAction.Press.recentAction) { createSleeperThread(MouseAction.Press); } @@ -58,7 +58,7 @@ public class Mouse implements MouseListener, MouseMotionListener, MouseWheelList buttonLastPressed = mouseEvent.getButton(); MouseButtons.get(mouseEvent.getButton()).currentlyPressed = true; }, - MouseEvent.MOUSE_RELEASED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_RELEASED, mouseEvent -> { if (!MouseAction.Release.recentAction) { createSleeperThread(MouseAction.Release); } @@ -69,14 +69,14 @@ public class Mouse implements MouseListener, MouseMotionListener, MouseWheelList buttonLastReleased = mouseEvent.getButton(); }, - MouseEvent.MOUSE_CLICKED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_CLICKED, mouseEvent -> { if (!MouseAction.Click.recentAction) { createSleeperThread(MouseAction.Click); } buttonLastClicked = mouseEvent.getButton(); }, - MouseEvent.MOUSE_MOVED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_MOVED, mouseEvent -> { if (!MouseAction.Move.recentAction) { createSleeperThread(MouseAction.Move); } @@ -86,7 +86,7 @@ public class Mouse implements MouseListener, MouseMotionListener, MouseWheelList FastJEngine.getDisplay().getResolutionScale() ); }, - MouseEvent.MOUSE_DRAGGED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_DRAGGED, mouseEvent -> { if (!MouseAction.Drag.recentAction) { createSleeperThread(MouseAction.Drag); } @@ -96,21 +96,21 @@ public class Mouse implements MouseListener, MouseMotionListener, MouseWheelList FastJEngine.getDisplay().getResolutionScale() ); }, - MouseEvent.MOUSE_ENTERED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_ENTERED, mouseEvent -> { if (MouseAction.Enter.recentAction) { createSleeperThread(MouseAction.Enter); } currentlyOnScreen = true; }, - MouseEvent.MOUSE_EXITED, (scene, mouseEvent) -> { + MouseEvent.MOUSE_EXITED, mouseEvent -> { if (MouseAction.Enter.recentAction) { createSleeperThread(MouseAction.Exit); } currentlyOnScreen = false; }, - MouseEvent.MOUSE_WHEEL, (scene, mouseEvent) -> { + MouseEvent.MOUSE_WHEEL, mouseEvent -> { if (!MouseAction.WheelScroll.recentAction) { createSleeperThread(MouseAction.WheelScroll); } @@ -300,14 +300,14 @@ public void mouseExited(MouseEvent e) { } /** - * Processes the specified mouse event for the specified scene, based on its event type. + * Processes the specified mouse event for the specified input manager, based on its event type. * - * @param scene The scene to fire the event to. - * @param event The mouse event to process. + * @param inputManager The input manager to fire the event to. + * @param event The mouse event to process. */ - public static void processEvent(Scene scene, MouseEvent event) { - MouseEventProcessor.get(event.getID()).accept(scene, event); - scene.inputManager.fireMouseEvent(event); + public static void processEvent(InputManager inputManager, MouseEvent event) { + MouseEventProcessor.get(event.getID()).accept(event); + inputManager.fireMouseEvent(event); } /** Private class to store the value of a mouse button, and whether it is currently pressed. */ diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagHandler.java b/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagHandler.java new file mode 100644 index 00000000..5382917e --- /dev/null +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagHandler.java @@ -0,0 +1,59 @@ +package io.github.lucasstarsz.fastj.systems.tags; + +import io.github.lucasstarsz.fastj.graphics.Drawable; + +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorManager; + +import java.util.List; + +/** + * Interface denoting that the implementing classes directly interface with the {@link BehaviorManager} class. + * + * FOR IMPLEMENTORS: In order for these methods to work you need to call {@link + * TagManager#addTaggableEntityList(TagHandler)} upon construction. + */ +public interface TagHandler { + + /** + * Gets the taggable entities assigned to the tag handler. + * + * @return The taggable entities of the tag handler. + */ + default List getTaggableEntities() { + return TagManager.getEntityList(this); + } + + /** + * Gets all taggable entities with the specified tag. + * + * @param tag The tag to check for. + * @return A list of all taggable entities with the specified tag. + */ + default List getAllWithTag(String tag) { + return TagManager.getAllInListWithTag(this, tag); + } + + /** + * Adds the specified taggable entity, only if it extends the {@code Drawable} class. + * + * @param entity The taggable entity to add. + * @param The type of the taggable entity, which must extend the {@code Drawable} class. + */ + default void addTaggableEntity(T entity) { + TagManager.addTaggableEntity(this, entity); + } + + /** + * Removes the specified taggable entity. + * + * @param entity The taggable entity to remove. + */ + default void removeTaggableEntity(Drawable entity) { + TagManager.removeTaggableEntity(this, entity); + } + + /** Removes all taggable entities from the tag handler. */ + default void clearTaggableEntities() { + TagManager.clearEntityList(this); + } +} diff --git a/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagManager.java b/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagManager.java index 3547a7b0..d0dde3f5 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagManager.java +++ b/src/main/java/io/github/lucasstarsz/fastj/systems/tags/TagManager.java @@ -2,8 +2,6 @@ import io.github.lucasstarsz.fastj.graphics.Drawable; -import io.github.lucasstarsz.fastj.systems.control.Scene; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -11,7 +9,7 @@ import java.util.stream.Collectors; /** - * Class to manage tags and taggable entities for all game scenes. + * Class to manage tags and taggable entities for all {@link TagHandler}s. * * @author Andrew Dey * @version 1.0.0 @@ -19,20 +17,20 @@ public class TagManager { private static final List MasterTagList = new ArrayList<>(); - private static final Map> EntityLists = new HashMap<>(); + private static final Map> EntityLists = new HashMap<>(); private TagManager() { throw new java.lang.IllegalStateException(); } /** - * Gets the list of taggable entities at the specified {@code Scene}. + * Gets the list of taggable entities at the specified {@link TagHandler}. * - * @param scene The scene to get the list of taggable entities from. + * @param tagHandler The tag handler to get the list of taggable entities from. * @return The list of taggable entities, as a {@code List}. */ - public static List getEntityList(Scene scene) { - return EntityLists.get(scene); + public static List getEntityList(TagHandler tagHandler) { + return EntityLists.get(tagHandler); } /** @@ -60,66 +58,67 @@ public static boolean doesTagExist(String tag) { } /** - * Adds the specified taggable entity to the list of taggable entities for the specified scene. + * Adds the specified taggable entity to the list of taggable entities for the specified tag handler. *

- * The taggable entity is only added if the specified scene does not already contain the specified taggable entity. + * The taggable entity is only added if the specified tag handler does not already contain the specified taggable + * entity. * - * @param scene The {@code Scene} which the taggable entity will be aliased with. + * @param tagHandler The {@link TagHandler} which the taggable entity will be aliased with. * @param taggableEntity The {@code Drawable} to add. */ - public static void addTaggableEntity(Scene scene, Drawable taggableEntity) { - if (!EntityLists.get(scene).contains(taggableEntity)) { - EntityLists.get(scene).add(taggableEntity); + public static void addTaggableEntity(TagHandler tagHandler, Drawable taggableEntity) { + if (!EntityLists.get(tagHandler).contains(taggableEntity)) { + EntityLists.get(tagHandler).add(taggableEntity); } } /** - * Removes the specified taggable entity from the list of taggable entities for the specified scene. + * Removes the specified taggable entity from the list of taggable entities for the specified tag handler. * - * @param scene The {@code Scene} that the taggable entity is aliased with. + * @param tagHandler The {@link TagHandler} that the taggable entity is aliased with. * @param taggableEntity The {@code Drawable} to remove. */ - public static void removeTaggableEntity(Scene scene, Drawable taggableEntity) { - EntityLists.get(scene).remove(taggableEntity); + public static void removeTaggableEntity(TagHandler tagHandler, Drawable taggableEntity) { + EntityLists.get(tagHandler).remove(taggableEntity); } /** - * Adds the specified {@code Scene} as an alias to store a list of taggable entities for. + * Adds the specified {@link TagHandler} as an alias to store a list of taggable entities for. *

- * The specified {@code Scene} is only added if it is not already in the tag manager. + * The specified {@link TagHandler} is only added if it is not already in the tag manager. * - * @param scene The scene to add. + * @param tagHandler The tag handler to add. */ - public static void addTaggableEntityList(Scene scene) { - if (!EntityLists.containsKey(scene)) { - EntityLists.put(scene, new ArrayList<>()); + public static void addTaggableEntityList(TagHandler tagHandler) { + if (!EntityLists.containsKey(tagHandler)) { + EntityLists.put(tagHandler, new ArrayList<>()); } } /** - * Removes the list of taggable entities aliased to the specified {@code Scene}. + * Removes the list of taggable entities aliased to the specified {@link TagHandler}. * - * @param scene The scene to remove. + * @param tagHandler The tag handler to remove. */ - public static void removeTaggableEntityList(Scene scene) { - EntityLists.remove(scene); + public static void removeTaggableEntityList(TagHandler tagHandler) { + EntityLists.remove(tagHandler); } /** - * Gets all taggable entities in the specified {@code Scene} with the specified tag. + * Gets all taggable entities in the specified {@link TagHandler} with the specified tag. * - * @param scene The scene to search through. - * @param tag The tag to search for. + * @param tagHandler The tag handler to search through. + * @param tag The tag to search for. * @return A list of taggable entities that have the specified tag. */ - public static List getAllInListWithTag(Scene scene, String tag) { - return EntityLists.get(scene).stream() + public static List getAllInListWithTag(TagHandler tagHandler, String tag) { + return EntityLists.get(tagHandler).stream() .filter(obj -> obj.hasTag(tag)) .collect(Collectors.toList()); } /** - * Gets all taggable entities from all {@code Scene}s with the specified tag. + * Gets all taggable entities from all {@link TagHandler}s with the specified tag. * * @param tag The tag to search for. * @return A list of taggable entities that have the specified tag. @@ -132,12 +131,12 @@ public static List getAllWithTag(String tag) { } /** - * Clears the taggable entity list aliased to the specified scene. + * Clears the taggable entity list aliased to the specified tag handler. * - * @param scene The scene to clear the list of taggable entities for. + * @param tagHandler The tag handler to clear the list of taggable entities for. */ - public static void clearEntityList(Scene scene) { - EntityLists.get(scene).clear(); + public static void clearEntityList(TagHandler tagHandler) { + EntityLists.get(tagHandler).clear(); } /** Wipes the {@code TagManager} of all aliases and tags. */