Sha256: 0af918ccacf981da500a671d4f593da67bad2878c279edfe8fb49ac0721e6c19

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module Lotu
  module Collidable

    attr_accessor :collision_tags

    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
      @collision_tags = self.class.behavior_options[Collidable]
      setup_tags
    end

    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

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

    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

    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

    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

    module ClassMethods
      def collides_as tag, opts={}
        default_opts = { :shape => :circle }
        opts = default_opts.merge!(opts)
        behavior_options[Collidable] ||= Hash.new
        behavior_options[Collidable][tag] = opts
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lotu-0.1.22 lib/lotu/behaviors/collidable.rb
lotu-0.1.21 lib/lotu/behaviors/collidable.rb
lotu-0.1.20 lib/lotu/behaviors/collidable.rb
lotu-0.1.19 lib/lotu/behaviors/collidable.rb
lotu-0.1.18 lib/lotu/behaviors/collidable.rb