Sha256: c947e5c273f1629312062e108ec1ba4c4e8ae65805f1dec8f09a97b39c1bf1c6

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

class SpatialTreeStagehand < Stagehand
  extend Forwardable
  def_delegators :@tree, :calculate_bb, :to_s, :each, :collisions, :query, :valid?

  attr_reader :moved_items

  def setup
    @dead_actors = {}
    @moved_items = {}
    @tree = AABBTree.new
  end

  def items
    @tree.items.keys
  end

  def add(actor)
    # TODO change these to one event? position_changed?
    # item.when :width_changed do |old_w, new_w|
    # item.when :height_changed do |old_h, new_h|

    actor.when :position_changed do move actor end
    actor.when :remove_me do
      remove actor
    end
    @dead_actors.delete actor
    if @tree.include? actor
      @tree.update actor
    else
      @tree.insert actor
    end
  end

  def remove(actor)
    @dead_actors[actor] = actor
    @moved_items.delete actor
    raise "remove #{actor} #{__FILE__}" unless @tree.valid?
  end

  def move(actor)
    @moved_items[actor] = actor
  end

  def reset
    @dead_actors.keys.each do |actor|
      @tree.remove actor
      @moved_items.delete actor
      actor.unsubscribe_all self
    end

    @moved_items.keys.each do |actor|
      @tree.update actor
    end

    @moved_items = {}
    @dead_actors = {}
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gamebox-0.4.0 lib/gamebox/stagehands/spatial_tree_stagehand.rb
gamebox-0.4.0.rc11 lib/gamebox/stagehands/spatial_tree_stagehand.rb
gamebox-0.4.0.rc5 lib/gamebox/stagehands/spatial_tree_stagehand.rb
gamebox-0.4.0.rc4 lib/gamebox/stagehands/spatial_tree_stagehand.rb
gamebox-0.4.0.rc3 lib/gamebox/stagehands/spatial_tree_stagehand.rb