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

Inherits:
RGame::Engine::Component show all
Includes:
RGame::Engine::Culling
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 WorldView 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 map's ground and canopy z bands, 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.



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

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

Instance Method Details

#draw(renderer, view) ⇒ Object

Top-left anchored, and sized by the sheet's frame — so the footprint to cull against is exactly the node's box.



46
47
48
49
50
51
52
53
# File 'lib/rgame/engine/components/animated_sprite.rb', line 46

def draw(renderer, view)
  return if culled?(view, node.abs_x, node.abs_y, node.width, node.height)

  # 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



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

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



39
40
41
42
# File 'lib/rgame/engine/components/animated_sprite.rb', line 39

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