Class: SpatialTree

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/gamebox/core/spatial_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage) ⇒ SpatialTree

Returns a new instance of SpatialTree.



7
8
9
10
11
12
# File 'lib/gamebox/core/spatial_tree.rb', line 7

def initialize(stage)
  @stage
  @dead_actors = {}
  @moved_items = {}
  @tree = AABBTree.new
end

Instance Attribute Details

#moved_itemsObject (readonly)

Returns the value of attribute moved_items.



5
6
7
# File 'lib/gamebox/core/spatial_tree.rb', line 5

def moved_items
  @moved_items
end

#stageObject (readonly)

Returns the value of attribute stage.



5
6
7
# File 'lib/gamebox/core/spatial_tree.rb', line 5

def stage
  @stage
end

Instance Method Details

#add(actor) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gamebox/core/spatial_tree.rb', line 18

def add(actor)
  # TODO change these to one event? position_changed?
  # item.when :width_changed do |old_w, new_w|
  # item.when :height_changed do |old_h, new_h|

  actor.when :position_changed do move actor end
  actor.when :remove_me do
    remove actor
  end
  @dead_actors.delete actor
  if @tree.include? actor
    @tree.update actor
  else
    @tree.insert actor
  end
end

#itemsObject



14
15
16
# File 'lib/gamebox/core/spatial_tree.rb', line 14

def items
  @tree.items.keys
end

#move(actor) ⇒ Object



41
42
43
# File 'lib/gamebox/core/spatial_tree.rb', line 41

def move(actor)
  @moved_items[actor] = actor
end

#remove(actor) ⇒ Object



35
36
37
38
39
# File 'lib/gamebox/core/spatial_tree.rb', line 35

def remove(actor)
  @dead_actors[actor] = actor
  @moved_items.delete actor
  raise "remove #{actor} #{__FILE__}" unless @tree.valid?
end

#resetObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gamebox/core/spatial_tree.rb', line 45

def reset
  @dead_actors.keys.each do |actor|
    @tree.remove actor
    @moved_items.delete actor
    actor.unsubscribe_all self
  end

  @moved_items.keys.each do |actor|
    @tree.update actor
  end

  @moved_items = {}
  @dead_actors = {}
end