Class: RGame::Engine::Components::AnimatedSprite

Inherits:
RGame::Engine::Component show all
Defined in:
lib/rgame/engine/components/animated_sprite.rb

Overview

Draws a sprite-sheet animation for a walking actor, picking the animation from a CharacterBody sibling's movement intent: walk_left/right/up/down while moving (horizontal wins on a diagonal), stand when still. Owns its Animator + the pure AnimationSet built from the sheet's animation table.

Like Sprite, it passes NO angle and draws at the node's world origin (node.abs_x/abs_y): a CameraView ancestor wraps the draw in renderer.translated to map world → screen, so this component never touches the camera. z is the render layer (kept as @layer, distinct from the node's transform z); it must sit between the tile world's ground band and its overlay band so canopies draw in front.

sheet is the asset's relative path. The component resolves it from the game's asset manager on attach — via node.root.context.assets (the platform seam) — to build its animation table and to size the node to the sprite's frame, so siblings like CharacterBody can read node.width/height. The renderer resolves the same symbol when drawing, so nothing is registered or passed in by hand.

Instance Attribute Summary

Attributes inherited from RGame::Engine::Component

#node

Instance Method Summary collapse

Methods inherited from RGame::Engine::Component

#context, #control, #on_detach, #sweep_freed

Methods included from Signal::DSL

#signal

Constructor Details

#initialize(sheet:, z: 0) ⇒ AnimatedSprite

Returns a new instance of AnimatedSprite.



23
24
25
26
27
# File 'lib/rgame/engine/components/animated_sprite.rb', line 23

def initialize(sheet:, z: 0)
  super()
  @sheet = sheet
  @layer = z
end

Instance Method Details

#draw(renderer) ⇒ Object



42
43
44
45
46
47
# File 'lib/rgame/engine/components/animated_sprite.rb', line 42

def draw(renderer)
  # Read row/col/flip_x separately (not @animator.frame, which allocates an Array
  # every call) to keep the draw path allocation-free.
  renderer.sprite(@sheet, @animator.row, @animator.col, node.abs_x, node.abs_y,
                  flip_x: @animator.flip_x, z: @layer)
end

#on_attachObject



29
30
31
32
33
34
35
# File 'lib/rgame/engine/components/animated_sprite.rb', line 29

def on_attach
  sheet = context.assets.sheet(@sheet)
  @animator = Engine::Animator.new(Engine::AnimationSet.new(sheet.animations))
  node.width = sheet.frame_width
  node.height = sheet.frame_height
  @body = node.get_component(CharacterBody)
end

#update(dt) ⇒ Object



37
38
39
40
# File 'lib/rgame/engine/components/animated_sprite.rb', line 37

def update(dt)
  @animator.play(walk_animation(@body.move_x, @body.move_y))
  @animator.update(dt)
end