Class: RGame::Engine::Actor

Inherits:
Object
  • Object
show all
Defined in:
lib/rgame/engine/actor.rb

Overview

A character in the world. Composes the pieces it owns — a collision box, an animator, a sprite id (the renderer maps it to a sheet), and a controller that decides movement — rather than baking them in. Pure logic; no graphics.

The controller responds to intent(dt, input) -> [ix, iy] with each axis in [-1, 1]; PlayerController reads input, AI/scripted controllers ignore it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, sprite_id:, collision_box:, animator:, controller:, speed: 120.0) ⇒ Actor

Returns a new instance of Actor.



15
16
17
18
19
20
21
22
23
# File 'lib/rgame/engine/actor.rb', line 15

def initialize(x:, y:, sprite_id:, collision_box:, animator:, controller:, speed: 120.0)
  @x = x
  @y = y
  @sprite_id = sprite_id
  @collision_box = collision_box
  @animator = animator
  @controller = controller
  @speed = speed
end

Instance Attribute Details

#animatorObject (readonly)

Returns the value of attribute animator.



13
14
15
# File 'lib/rgame/engine/actor.rb', line 13

def animator
  @animator
end

#collision_boxObject (readonly)

Returns the value of attribute collision_box.



13
14
15
# File 'lib/rgame/engine/actor.rb', line 13

def collision_box
  @collision_box
end

#sprite_idObject (readonly)

Returns the value of attribute sprite_id.



13
14
15
# File 'lib/rgame/engine/actor.rb', line 13

def sprite_id
  @sprite_id
end

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/rgame/engine/actor.rb', line 12

def x
  @x
end

#yObject

Returns the value of attribute y.



12
13
14
# File 'lib/rgame/engine/actor.rb', line 12

def y
  @y
end

Instance Method Details

#frameObject

[row, col, flip_x] for the current frame.



35
36
37
# File 'lib/rgame/engine/actor.rb', line 35

def frame
  @animator.frame
end

#update(dt, collision_system, input) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rgame/engine/actor.rb', line 25

def update(dt, collision_system, input)
  intent_x, intent_y = @controller.intent(dt, input)

  collision_system.move(self, intent_x * @speed * dt, intent_y * @speed * dt)

  @animator.play(walk_animation(intent_x, intent_y))
  @animator.update(dt)
end