Class: RGame::Engine::Actions

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

Overview

An immutable per-frame snapshot of abstract action state. Game logic reads this, never physical keys. Built by ActionMapper (or constructed directly in tests with a fake).

Edge queries (pressed?/released?) compare against the previous frame's held state, so a one-shot action (menu confirm, jump) fires exactly once per press rather than every frame it's held.

Instance Method Summary collapse

Constructor Details

#initialize(held: {}, axes: {}, prev_held: {}) ⇒ Actions

held, axes and prev_held are mutable hashes the mapper updates in place each poll, so the snapshot stays a single reused, allocation-free object.



15
16
17
18
19
# File 'lib/rgame/engine/input/actions.rb', line 15

def initialize(held: {}, axes: {}, prev_held: {})
  @held = held
  @axes = axes
  @prev_held = prev_held
end

Instance Method Details

#axis(name) ⇒ Object

Analog value in [-1.0, 1.0]; 0.0 if unbound/neutral.



36
37
38
# File 'lib/rgame/engine/input/actions.rb', line 36

def axis(name)
  @axes.fetch(name, 0.0)
end

#held?(name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rgame/engine/input/actions.rb', line 21

def held?(name)
  @held.fetch(name, false)
end

#pressed?(name) ⇒ Boolean

True only on the frame the action transitions up→down.

Returns:

  • (Boolean)


26
27
28
# File 'lib/rgame/engine/input/actions.rb', line 26

def pressed?(name)
  @held.fetch(name, false) && !@prev_held.fetch(name, false)
end

#released?(name) ⇒ Boolean

True only on the frame the action transitions down→up.

Returns:

  • (Boolean)


31
32
33
# File 'lib/rgame/engine/input/actions.rb', line 31

def released?(name)
  !@held.fetch(name, false) && @prev_held.fetch(name, false)
end