SpriteAnimation

Namespace:

namespace Lenga\Engine\Core;

final class SpriteAnimation extends Component

SpriteAnimation plays a single clip or drives a sprite animation controller on a GameObject.

Use it when gameplay code needs to:

  • play or stop a clip directly
  • switch the active controller state
  • set animator parameters from gameplay code
  • fire one-shot animation triggers

Getting the Component

The usual workflow is:

  1. Add Sprite Renderer to a GameObject.
  2. Add Sprite Animation to the same GameObject.
  3. Assign either a clip or a controller in the Inspector.
  4. From PHP, call $this->getComponent(SpriteAnimation::class).
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\SpriteAnimation;

final class PlayerAnimator extends Behaviour
{
    private ?SpriteAnimation $animator = null;

    public function start(): void
    {
        $this->animator = $this->getComponent(SpriteAnimation::class);
    }
}

Properties

clipPath

public string $clipPath

Project-relative path to the clip that should play when you are not using a controller.

controllerPath

public string $controllerPath

Project-relative path to the animation controller asset.

state

public string $state

Gets or sets the currently selected state name.

This is most useful when you want to force a state directly instead of waiting for controller conditions.

playOnAwake

public bool $playOnAwake

Controls whether the animation starts automatically when the component starts.

speed

public float $speed

Playback speed multiplier for the current clip or controller output.

Methods

isPlaying

public function isPlaying(): bool

Returns whether the animation is currently playing.

play

public function play(): bool

Starts playback.

stop

public function stop(): bool

Stops playback.

getBool

public function getBool(string $parameterName): bool

Returns the current value of a bool controller parameter.

setBool

public function setBool(string $parameterName, bool $value): bool

Sets a bool controller parameter.

getInt

public function getInt(string $parameterName): int

Returns the current value of an int controller parameter.

setInt

public function setInt(string $parameterName, int $value): bool

Sets an int controller parameter.

getFloat

public function getFloat(string $parameterName): float

Returns the current value of a float controller parameter.

setFloat

public function setFloat(string $parameterName, float $value): bool

Sets a float controller parameter.

setTrigger

public function setTrigger(string $parameterName): bool

Fires a trigger parameter for one-shot transitions such as attacks, hits, and jumps.

resetTrigger

public function resetTrigger(string $parameterName): bool

Clears a trigger parameter.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Input;
use Lenga\Engine\Core\SpriteAnimation;

class CharacterAnimator extends Behaviour
{
    private ?SpriteAnimation $animator = null;

    public function start(): void
    {
        $this->animator = $this->getComponent(SpriteAnimation::class);
    }

    public function update(): void
    {
        if ($this->animator === null) {
            return;
        }

        $move = Input::getAxis('Horizontal');
        $speed = abs($move) * 8.0;

        $this->animator->setBool('isWalking', $speed > 0.05);
        $this->animator->setBool('isRunning', $speed > 6.0);
        $this->animator->setFloat('speed', $speed);

        if (Input::getButtonDown('Attack')) {
            $this->animator->setInt('comboStep', $this->animator->getInt('comboStep') + 1);
            $this->animator->setTrigger('lightAttack');
        }
    }
}

Parameter Notes

  • Parameter names must match the controller exactly.
  • Use bools for persistent conditions such as isGrounded.
  • Use ints for discrete steps such as comboStep.
  • Use floats for threshold- or comparison-based motion such as speed.
  • Use triggers for one-shot transitions such as lightAttack.