Class: Lotu::CollisionSystem

Inherits:
BaseSystem show all
Defined in:
lib/lotu/systems/collision_system.rb

Defined Under Namespace

Modules: UserMethods

Instance Method Summary collapse

Methods inherited from BaseSystem

#draw, #dt

Constructor Details

#initialize(user, opts = {}) ⇒ CollisionSystem

Returns a new instance of CollisionSystem.



4
5
6
7
8
9
# File 'lib/lotu/systems/collision_system.rb', line 4

def initialize(user, opts={})
  super
  @user.extend UserMethods
  @entities = Hash.new{ |h,k| h[k] = [] }
  @actions = {}
end

Instance Method Details

#add_entity(obj, tag) ⇒ Object



11
12
13
# File 'lib/lotu/systems/collision_system.rb', line 11

def add_entity(obj, tag)
  @entities[tag] << obj
end

#remove_entity(obj, tag) ⇒ Object



15
16
17
# File 'lib/lotu/systems/collision_system.rb', line 15

def remove_entity(obj, tag)
  @entities[tag].delete(obj)
end

#updateObject



23
24
25
26
27
28
29
30
31
# File 'lib/lotu/systems/collision_system.rb', line 23

def update
  @actions.each do |tags, blk|
    @entities[tags[0]].each do |ent1|
      @entities[tags[1]].each do |ent2|
        blk.call(ent1, ent2) if ent1.collides_with?(ent2, tags[0])
      end
    end
  end
end

#when_colliding(type1, type2, &blk) ⇒ Object



19
20
21
# File 'lib/lotu/systems/collision_system.rb', line 19

def when_colliding(type1, type2, &blk)
  @actions[[type1, type2]] = blk
end