|
| 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 | +} |
0 commit comments