Skip to content

Commit 6879f13

Browse files
authored
Add Simple Audio and Behavior Exmaples (#101)
* added simple audio example * added behavior example (uncovered issues #96 and #99) * update example readme with new examples
1 parent c236271 commit 6879f13

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

src/example/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ This program demonstrates how to use the `RenderSettings` class to change the wa
6767

6868
Command to run: `./gradlew example -PtoRun=rendersettings`
6969

70+
### [Audio](http://example.fastj.me/audio/Main.java)
71+
This program demonstrates simple use of FastJ's audio engine, as well as file paths.
72+
73+
Command to run: `./gradlew example -PtoRun=audio`
74+
75+
### [GameObject Behaviors](http://example.fastj.me/behaviors/Main.java)
76+
This program demonstrates how to use the `Behavior` class to control the state of `GameObject`s in FastJ.
77+
78+
Command to run: `./gradlew example -PtoRun=behaviors`
79+
7080
## Games
7181
_Example programs to demonstrate FastJ through games._
7282

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package tech.fastj.example.behaviors;
2+
3+
import tech.fastj.engine.FastJEngine;
4+
import tech.fastj.graphics.display.Display;
5+
import tech.fastj.graphics.game.Polygon2D;
6+
import tech.fastj.graphics.game.RenderStyle;
7+
import tech.fastj.graphics.util.DrawUtil;
8+
9+
import tech.fastj.systems.behaviors.Behavior;
10+
import tech.fastj.systems.control.SimpleManager;
11+
12+
import java.awt.Color;
13+
14+
public class Main extends SimpleManager {
15+
16+
@Override
17+
public void init(Display display) {
18+
19+
/* Behaviors in FastJ */
20+
21+
/* FastJ's behavior system provides a simple and effective way to control GameObjects.
22+
*
23+
* A behavior:
24+
* - Is any class that implements the Behavior interface
25+
* - can be attached to any GameObject
26+
* - has instances, allowing behaviors to be used more than one GameObject at a time
27+
* - provides methods to control game objects through SimpleManager/Scene initialization
28+
* and updating */
29+
30+
31+
/* Let's jump straight into it. Since a Behavior is just a class, we can simply create one.
32+
* From here, jump over to tech.fastj.example.behaviors.SimpleMovementBehavior.java to see
33+
* what makes up the behavior. */
34+
SimpleMovementBehavior movementBehavior = new SimpleMovementBehavior();
35+
36+
37+
/* To use the Behavior, we need to attach it to a game object.
38+
* For this demonstration, we'll create a simple box.
39+
* Its starting position will be (0, 0), and its rotation will be 0 degrees -- keep a close
40+
* eye on how the behavior affects these properties. */
41+
Polygon2D box = Polygon2D.fromPoints(DrawUtil.createBox(0f, 0f, 50f));
42+
43+
/* And lastly, we need to attach the behavior to the game object. It's as simple as calling
44+
* gameObject.addBehavior(Behavior, SimpleManager/Scene) -- the SimpleManager/Scene is the
45+
* manager the behavior is in. */
46+
box.addBehavior(movementBehavior, this);
47+
drawableManager.addGameObject(box);
48+
49+
50+
/* Pre-defined Behaviors */
51+
52+
/* FastJ also contains some pre-defined behaviors to avoid having users create an entire
53+
* Behavior just to have it constantly transform an object.
54+
*
55+
* There are three predefined behaviors:
56+
* - Behavior.simpleTranslation -- constantly translates a game object by the amount specified
57+
* - Behavior.simpleRotation -- constantly rotates a game object by the amount specified
58+
* - Behavior.simpleScale -- constantly scales a game object by the amount specified
59+
*
60+
* For this example, we'll just use simpleRotation. */
61+
62+
Polygon2D premadeBehaviorsBox = Polygon2D.create(DrawUtil.createBox(500f, 500f, 50f), RenderStyle.FillAndOutline)
63+
.withFill(Color.red)
64+
.withOutline(Polygon2D.DefaultOutlineStroke, Polygon2D.DefaultOutlineColor)
65+
.build();
66+
67+
premadeBehaviorsBox.addBehavior(Behavior.simpleRotation(3f), this);
68+
drawableManager.addGameObject(premadeBehaviorsBox);
69+
}
70+
71+
@Override
72+
public void update(Display display) {
73+
// Empty -- this example does not make use of this method.
74+
}
75+
76+
public static void main(String[] args) {
77+
FastJEngine.init("Hello, FastJ Behaviors!", new Main());
78+
FastJEngine.run();
79+
}
80+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package tech.fastj.example.behaviors;
2+
3+
import tech.fastj.engine.FastJEngine;
4+
import tech.fastj.math.Pointf;
5+
import tech.fastj.graphics.game.GameObject;
6+
7+
import tech.fastj.systems.behaviors.Behavior;
8+
9+
public class SimpleMovementBehavior implements Behavior {
10+
11+
/* Behaviors (any class which implements the Behavior interface) have a few useful methods:
12+
* - init(), which takes in the GameObject that uses the behavior and is called when the
13+
* behavior gets initialized (usually right after the GameObject gets initialized).
14+
* - update(), which takes in the same GameObject that is provided in init() and is called
15+
* after every SimpleManager/Scene update() call.
16+
*
17+
* You'll get to know these methods inside and out with time. */
18+
19+
private String gameObjectId;
20+
21+
@Override
22+
public void init(GameObject gameObject) {
23+
/* Since this will only be called on first initialization, we can use it to accomplish
24+
* initial actions.
25+
*
26+
* In this case, we'll simply change the game object's translation to (50, 50). Since
27+
* translate(Pointf) is available to all GameObjects, this is general enough to be applied
28+
* to any sort of GameObject that could be used with this Behavior. */
29+
30+
gameObject.translate(new Pointf(50f, 50f));
31+
32+
/* We'll also log that the behavior has been initialized, to get a sense of when this
33+
* method is called. While doing so, we'll keep track of the game object's id for later
34+
* usage. */
35+
FastJEngine.log("SimpleMovement behavior for GameObject " + gameObject.getID() + " has been initialized.");
36+
gameObjectId = gameObject.getID();
37+
}
38+
39+
@Override
40+
public void update(GameObject gameObject) {
41+
/* Since this will always be called right after the update() call on the
42+
* SimpleManager/Scene, we can use it to accomplish repeated actions.
43+
*
44+
* In this case, we'll rotate the game object by 5 degrees every time the method is called.
45+
* Since rotate(float) is available to all GameObjects, this is general enough to be applied
46+
* to any sort of GameObject that could be used with this Behavior. */
47+
48+
gameObject.rotate(5f);
49+
}
50+
51+
@Override
52+
public void destroy() {
53+
/* You can also implement this method -- destroy().
54+
* When the behavior's game object is destroyed, the behavior gets destroyed as well. You
55+
* can choose to include specific behavior for when the behavior is destroyed using this
56+
* method. */
57+
58+
/* First, we'll log that the behavior was destroyed using the gameObjectId we kept from
59+
* before. */
60+
FastJEngine.log("SimpleMovement behavior for GameObject " + gameObjectId + " has been destroyed.");
61+
62+
/* Now that the id is no longer used, we'll set the gameObjectId we kept from before to
63+
* null. */
64+
gameObjectId = null;
65+
}
66+
}

0 commit comments

Comments
 (0)