-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdde0f3
commit a2cb737
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package tech.fastj.example.behaviors; | ||
|
||
import tech.fastj.engine.FastJEngine; | ||
import tech.fastj.graphics.display.Display; | ||
import tech.fastj.graphics.game.Polygon2D; | ||
import tech.fastj.graphics.game.RenderStyle; | ||
import tech.fastj.graphics.util.DrawUtil; | ||
|
||
import tech.fastj.systems.behaviors.Behavior; | ||
import tech.fastj.systems.control.SimpleManager; | ||
|
||
import java.awt.Color; | ||
|
||
public class Main extends SimpleManager { | ||
|
||
@Override | ||
public void init(Display display) { | ||
|
||
/* Behaviors in FastJ */ | ||
|
||
/* FastJ's behavior system provides a simple and effective way to control GameObjects. | ||
* | ||
* A behavior: | ||
* - Is any class that implements the Behavior interface | ||
* - can be attached to any GameObject | ||
* - has instances, allowing behaviors to be used more than one GameObject at a time | ||
* - provides methods to control game objects through SimpleManager/Scene initialization | ||
* and updating */ | ||
|
||
|
||
/* Let's jump straight into it. Since a Behavior is just a class, we can simply create one. | ||
* From here, jump over to tech.fastj.example.behaviors.SimpleMovementBehavior.java to see | ||
* what makes up the behavior. */ | ||
SimpleMovementBehavior movementBehavior = new SimpleMovementBehavior(); | ||
|
||
|
||
/* To use the Behavior, we need to attach it to a game object. | ||
* For this demonstration, we'll create a simple box. | ||
* Its starting position will be (0, 0), and its rotation will be 0 degrees -- keep a close | ||
* eye on how the behavior affects these properties. */ | ||
Polygon2D box = Polygon2D.fromPoints(DrawUtil.createBox(0f, 0f, 50f)); | ||
|
||
/* And lastly, we need to attach the behavior to the game object. It's as simple as calling | ||
* gameObject.addBehavior(Behavior, SimpleManager/Scene) -- the SimpleManager/Scene is the | ||
* manager the behavior is in. */ | ||
box.addBehavior(movementBehavior, this); | ||
drawableManager.addGameObject(box); | ||
|
||
|
||
/* Pre-defined Behaviors */ | ||
|
||
/* FastJ also contains some pre-defined behaviors to avoid having users create an entire | ||
* Behavior just to have it constantly transform an object. | ||
* | ||
* There are three predefined behaviors: | ||
* - Behavior.simpleTranslation -- constantly translates a game object by the amount specified | ||
* - Behavior.simpleRotation -- constantly rotates a game object by the amount specified | ||
* - Behavior.simpleScale -- constantly scales a game object by the amount specified | ||
* | ||
* For this example, we'll just use simpleRotation. */ | ||
|
||
Polygon2D premadeBehaviorsBox = Polygon2D.create(DrawUtil.createBox(500f, 500f, 50f), RenderStyle.FillAndOutline) | ||
.withFill(Color.red) | ||
.withOutline(Polygon2D.DefaultOutlineStroke, Polygon2D.DefaultOutlineColor) | ||
.build(); | ||
|
||
premadeBehaviorsBox.addBehavior(Behavior.simpleRotation(3f), this); | ||
drawableManager.addGameObject(premadeBehaviorsBox); | ||
} | ||
|
||
@Override | ||
public void update(Display display) { | ||
// Empty -- this example does not make use of this method. | ||
} | ||
|
||
public static void main(String[] args) { | ||
FastJEngine.init("Hello, FastJ Behaviors!", new Main()); | ||
FastJEngine.run(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/example/java/tech/fastj/example/behaviors/SimpleMovementBehavior.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tech.fastj.example.behaviors; | ||
|
||
import tech.fastj.engine.FastJEngine; | ||
import tech.fastj.math.Pointf; | ||
import tech.fastj.graphics.game.GameObject; | ||
|
||
import tech.fastj.systems.behaviors.Behavior; | ||
|
||
public class SimpleMovementBehavior implements Behavior { | ||
|
||
/* Behaviors (any class which implements the Behavior interface) have a few useful methods: | ||
* - init(), which takes in the GameObject that uses the behavior and is called when the | ||
* behavior gets initialized (usually right after the GameObject gets initialized). | ||
* - update(), which takes in the same GameObject that is provided in init() and is called | ||
* after every SimpleManager/Scene update() call. | ||
* | ||
* You'll get to know these methods inside and out with time. */ | ||
|
||
private String gameObjectId; | ||
|
||
@Override | ||
public void init(GameObject gameObject) { | ||
/* Since this will only be called on first initialization, we can use it to accomplish | ||
* initial actions. | ||
* | ||
* In this case, we'll simply change the game object's translation to (50, 50). Since | ||
* translate(Pointf) is available to all GameObjects, this is general enough to be applied | ||
* to any sort of GameObject that could be used with this Behavior. */ | ||
|
||
gameObject.translate(new Pointf(50f, 50f)); | ||
|
||
/* We'll also log that the behavior has been initialized, to get a sense of when this | ||
* method is called. While doing so, we'll keep track of the game object's id for later | ||
* usage. */ | ||
FastJEngine.log("SimpleMovement behavior for GameObject " + gameObject.getID() + " has been initialized."); | ||
gameObjectId = gameObject.getID(); | ||
} | ||
|
||
@Override | ||
public void update(GameObject gameObject) { | ||
/* Since this will always be called right after the update() call on the | ||
* SimpleManager/Scene, we can use it to accomplish repeated actions. | ||
* | ||
* In this case, we'll rotate the game object by 5 degrees every time the method is called. | ||
* Since rotate(float) is available to all GameObjects, this is general enough to be applied | ||
* to any sort of GameObject that could be used with this Behavior. */ | ||
|
||
gameObject.rotate(5f); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
/* You can also implement this method -- destroy(). | ||
* When the behavior's game object is destroyed, the behavior gets destroyed as well. You | ||
* can choose to include specific behavior for when the behavior is destroyed using this | ||
* method. */ | ||
|
||
/* First, we'll log that the behavior was destroyed using the gameObjectId we kept from | ||
* before. */ | ||
FastJEngine.log("SimpleMovement behavior for GameObject " + gameObjectId + " has been destroyed."); | ||
|
||
/* Now that the id is no longer used, we'll set the gameObjectId we kept from before to | ||
* null. */ | ||
gameObjectId = null; | ||
} | ||
} |