Class: AdventureRL::Events::Mouse

Inherits:
Event
  • Object
show all
Defined in:
lib/AdventureRL/Events/Mouse.rb

Instance Method Summary collapse

Methods inherited from Event

#get_name, #get_objects, #on_trigger

Constructor Details

#initialize(*args) ⇒ Mouse

Returns a new instance of Mouse.



4
5
6
7
# File 'lib/AdventureRL/Events/Mouse.rb', line 4

def initialize *args
  super
  @quadtree = Quadtree.new
end

Instance Method Details

#add_object(object) ⇒ Object

Overwrite the #add_object method, so we can reset the object in the Quadtree if necessary, via the object’s #move_by method.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/AdventureRL/Events/Mouse.rb', line 12

def add_object object
  super
  [object].flatten.each do |obj|
    get_quadtree.add_object obj
    mouse_event = self
    obj.define_singleton_method :move_by do |*args|
      previous_position = get_position.dup
      super(*args)
      mouse_event.get_quadtree.reset_object self  if (get_position != previous_position)
    end
  end
end

#get_quadtreeObject



43
44
45
# File 'lib/AdventureRL/Events/Mouse.rb', line 43

def get_quadtree
  return @quadtree
end

#remove_object(object) ⇒ Object

Overwrite the #remove_object method, so we can also remove the object(s) from the Quadtree.



27
28
29
30
31
32
# File 'lib/AdventureRL/Events/Mouse.rb', line 27

def remove_object object
  super
  [object].flatten.each do |obj|
    get_quadtree.remove_object obj
  end
end

#trigger(*args) ⇒ Object

Overwrite the #trigger method, to perform a Quadtree query for objects colliding with the mouse pointer. For improved performance.



37
38
39
40
41
# File 'lib/AdventureRL/Events/Mouse.rb', line 37

def trigger *args
  get_colliding_objects.each do |object|
    @trigger_method.call object, *args
  end
end