Class: RGame::Engine::Components::DespawnOffscreen

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

Overview

Queues the node for removal once it has fully left the bounds (plus margin). From Body#offscreen?. Used by short-lived projectiles; removal is deferred via queue_free so it is safe to trigger from inside the update traversal.

Instance Attribute Summary

Attributes inherited from RGame::Engine::Component

#node

Instance Method Summary collapse

Methods inherited from RGame::Engine::Component

#context, #control, #draw, #on_attach, #on_detach, #sweep_freed

Methods included from Signal::DSL

#signal

Constructor Details

#initialize(width:, height:, margin: 0.0) ⇒ DespawnOffscreen

Returns a new instance of DespawnOffscreen.



10
11
12
13
14
15
# File 'lib/rgame/engine/components/despawn_offscreen.rb', line 10

def initialize(width:, height:, margin: 0.0)
  super()
  @width = width
  @height = height
  @margin = margin
end

Instance Method Details

#update(_dt) ⇒ Object



17
18
19
20
21
22
# File 'lib/rgame/engine/components/despawn_offscreen.rb', line 17

def update(_dt)
  x = node.x
  y = node.y
  offscreen = x < -@margin || x > @width + @margin || y < -@margin || y > @height + @margin
  node.queue_free if offscreen
end