In my twelfth video of the “Building a Game Development Framework” series, I introduce a Scripted Movement System. This allows developers to define a sequence of automated actions for Non-Player Characters (NPCs) or cinematic events without writing manual update logic for every frame.
Before diving into scripting, I made a few essential adjustments to the core framework:
- Window Resizing: Desktop window sizes are now correctly handled via LibGDX configurations and the
resizemethod, ensuring the game’s internal coordinate system matches the actual window dimensions. - Safe Sprite Removal: To avoid “Concurrent Modification Exceptions” (crashes when deleting a sprite while the game is still drawing or updating it), the framework now uses a “To Be Added” list and a
removedflag. Sprites are only physically added or removed from the main list between render cycles. - Clean Interfaces: Redundant methods were removed from the
Movementinterface, delegating all positional updates to theSpriteUpdateclass.
The scripting system is built around two main components:
ScriptMovement: The “brain” that holds a list of actions and executes them one by one. Once an action reports it is “done,” the script automatically transitions to the next one in the list.ScriptAction: The base class for specific behaviors (waiting, moving, turning). It includes logic for Gradual Direction Changes, allowing actors to make smooth arcs rather than “snapping” to a new heading.
I detail several specialized actions that can be chained together:
- Wait Action: Pauses the actor for a set duration. You can also specify a direction for the actor to face while waiting.
- Time Movement Action: Moves the actor in a specific direction at a set speed for a fixed amount of time.
- Change Speed Action: Gradually accelerates or decelerates the actor over time.
- Destination Action: Moves the actor to a specific
(X, Y)coordinate. It uses the Pythagorean theorem to calculate the distance and ensures the actor stops exactly on the target. - Easing Action: Provides advanced acceleration/deceleration curves (Linear, Quadratic, Cubic, etc.). This allows for “juicy” movement where an actor starts slow and “eases in” to a high speed.
- Go-To Action: A logic-based action that jumps to a previous step in the script, allowing for infinite loops or repeating patterns.
I then demonstrate how to combine these actions to create a complex patrol path:
- Ease In: The character starts from a standstill and slowly speeds up.
- Move to Destination: The character hits a series of four
(X, Y)coordinates to walk in a square. - Go-To: After the fourth corner, a
GoToActionsends the script back to the second step, creating a permanent walking loop.
This scripting system significantly reduces the complexity of AI behavior, allowing developers to “choreograph” NPCs using a simple list of commands.
