Module: Lotu::Collidable

Defined in:
lib/lotu/behaviors/collidable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collision_tagsObject

Returns the value of attribute collision_tags.



4
5
6
# File 'lib/lotu/behaviors/collidable.rb', line 4

def collision_tags
  @collision_tags
end

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/lotu/behaviors/collidable.rb', line 6

def self.included base
  base.extend ClassMethods
end

Instance Method Details

#box(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/lotu/behaviors/collidable.rb', line 39

def box other
  # horizontal checks
  return false if @x > other.x + other.width * other.factor_x
  return false if @x + width * @factor_x < other.x

  # vertical checks
  return false if @y > other.y + other.height * other.factor_y
  return false if @y + height * @factor_y < other.y
  true
end

#calc_radiusObject



10
11
12
13
14
15
16
17
18
# File 'lib/lotu/behaviors/collidable.rb', line 10

def calc_radius
  if @width
    @collision_radius = @width/2.0 * @factor_x
  elsif @height
    @collision_radius = @height/2.0 * @factor_y
  else
    @collision_radius = 100
  end
end

#circle(other) ⇒ Object



50
51
52
53
# File 'lib/lotu/behaviors/collidable.rb', line 50

def circle other
  # distance between them is smaller than the sum of their radii?
  Gosu.distance(@x, @y, other.x, other.y) < collision_radius + other.collision_radius
end

#collides_with?(other, tag) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/lotu/behaviors/collidable.rb', line 33

def collides_with?(other, tag)
  return false if self.equal? other
  strategy = self.class.behavior_options[Collidable][tag][:shape]
  send(strategy, other)
end

#dieObject



55
56
57
58
59
60
# File 'lib/lotu/behaviors/collidable.rb', line 55

def die
  super if defined? super
  @collision_tags.each do |tag, options|
    @parent.systems[CollisionSystem].remove_entity(self, tag) if @parent.systems[CollisionSystem]
  end if @collision_tags
end

#init_behavior(opts) ⇒ Object



20
21
22
23
24
# File 'lib/lotu/behaviors/collidable.rb', line 20

def init_behavior opts
  super if defined? super
  @collision_tags = self.class.behavior_options[Collidable]
  setup_tags
end

#setup_tagsObject



26
27
28
29
30
31
# File 'lib/lotu/behaviors/collidable.rb', line 26

def setup_tags
  # TODO: Change @parent for @manager (could be a Game or a Scene)
  @collision_tags.each do |tag, opts|
    @parent.systems[CollisionSystem].add_entity(self, tag) if @parent.systems[CollisionSystem]
  end if @collision_tags
end