Sha256: e4b98069f03bf8472a878e09a4e65f07127ceb778ea71cfd9f351e6c96982463

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Lotu
  module Collidable

    def self.included base
      base.extend ClassMethods
    end

    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

    def init_behavior opts
      super if defined? super

      calc_radius
      class << self
        attr_accessor :collision_radius
      end

      @collision_tag = self.class.behavior_options[Collidable]
      # TODO: Change @parent for @manager (could be a Game or a Scene)
      @parent.systems[CollisionSystem].add_entity(self, @collision_tag) if @parent.systems[CollisionSystem]
    end

    def collides_with(other)
      return false if self.equal? other
      Gosu.distance(@x, @y, other.x, other.y) < @collision_radius + other.collision_radius
    end

    def die
      super
      @parent.systems[CollisionSystem].remove_entity(self, @collision_tag) if @parent.systems[CollisionSystem]
    end

    module ClassMethods
      def collides_as tag
        behavior_options[Collidable] = tag
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lotu-0.1.16 lib/lotu/behaviors/collidable.rb