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

Add Simple Audio and Behavior Exmaples #101

Merged
merged 43 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
14ee623
begin work on sound example
lucasstarsz Aug 4, 2021
29db15f
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
05d8bb5
continue work on simple audio example
lucasstarsz Aug 9, 2021
4f72c97
begin work on sound example
lucasstarsz Aug 4, 2021
586345b
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
3c09497
continue work on simple audio example
lucasstarsz Aug 9, 2021
b0b80b0
begin work on sound example
lucasstarsz Aug 4, 2021
400b71c
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
1032410
continue work on simple audio example
lucasstarsz Aug 9, 2021
21cab17
Merge branch 'examples' of https://github.com/fastjengine/FastJ into …
lucasstarsz Aug 9, 2021
68f2b0c
(#3, #23) Add simple audio example
lucasstarsz Aug 9, 2021
b68622e
begin work on sound example
lucasstarsz Aug 4, 2021
153c6f3
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
256e618
rename sound example to audio example
lucasstarsz Aug 9, 2021
261cf1c
begin work on sound example
lucasstarsz Aug 4, 2021
106b299
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
ca55103
continue work on simple audio example
lucasstarsz Aug 9, 2021
d3fd288
begin work on sound example
lucasstarsz Aug 4, 2021
ab388d5
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
6771a0d
continue work on simple audio example
lucasstarsz Aug 9, 2021
5229826
(#20) added behaviors example
lucasstarsz Aug 10, 2021
d20751d
Merge branch 'examples' of https://github.com/fastjengine/FastJ into …
lucasstarsz Aug 10, 2021
19f239f
begin work on sound example
lucasstarsz Aug 4, 2021
20256fe
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
295c9cf
rename sound example to audio example
lucasstarsz Aug 9, 2021
f2792f4
begin work on sound example
lucasstarsz Aug 4, 2021
31630ad
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
3fbe319
continue work on simple audio example
lucasstarsz Aug 9, 2021
5acb1c3
begin work on sound example
lucasstarsz Aug 4, 2021
202c966
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
cdde0f3
continue work on simple audio example
lucasstarsz Aug 9, 2021
a2cb737
(#20) added behaviors example
lucasstarsz Aug 10, 2021
fdae62f
begin work on sound example
lucasstarsz Aug 4, 2021
47daa1a
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
00d284c
continue work on simple audio example
lucasstarsz Aug 9, 2021
270ca1a
begin work on sound example
lucasstarsz Aug 4, 2021
7852eb4
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
9530e65
continue work on simple audio example
lucasstarsz Aug 9, 2021
f126334
begin work on sound example
lucasstarsz Aug 4, 2021
151b235
add beginning description of MemoryAudio
lucasstarsz Aug 6, 2021
e024f0e
continue work on simple audio example
lucasstarsz Aug 9, 2021
cdf0010
Merge branch 'examples' of https://github.com/fastjengine/FastJ into …
lucasstarsz Aug 10, 2021
a620893
update example readme with new examples
lucasstarsz Aug 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ This program demonstrates how to use the `RenderSettings` class to change the wa

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

### [Audio](http://example.fastj.me/audio/Main.java)
This program demonstrates simple use of FastJ's audio engine, as well as file paths.

Command to run: `./gradlew example -PtoRun=audio`

### [GameObject Behaviors](http://example.fastj.me/behaviors/Main.java)
This program demonstrates how to use the `Behavior` class to control the state of `GameObject`s in FastJ.

Command to run: `./gradlew example -PtoRun=behaviors`

## Games
_Example programs to demonstrate FastJ through games._

Expand Down
80 changes: 80 additions & 0 deletions src/example/java/tech/fastj/example/behaviors/Main.java
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();
}
}
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;
}
}