Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SimpleManager (SceneManager alternative) #21

Merged
merged 9 commits into from
May 23, 2021
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
11 changes: 8 additions & 3 deletions src/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ 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
- Working with a Scene
- 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!
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.lucasstarsz.fastj.example.helloworld;
package io.github.lucasstarsz.fastj.example.oldhelloworld;

import io.github.lucasstarsz.fastj.graphics.Display;

import io.github.lucasstarsz.fastj.systems.control.SceneManager;

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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <b>FOR IMPLEMENTORS:</b> 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<GameObject> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scene, List<GameObject>> BehaviorListenerLists = new HashMap<>();
private static final Map<BehaviorHandler, List<GameObject>> 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<GameObject> getList(Scene scene) {
return BehaviorListenerLists.get(scene);
public static List<GameObject> 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();
}
}
Expand Down
Loading