Sha256: 582dd05f6e8bc3fe0e77716632e8b0d390848152987490d94f049b44a122dd8d

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module Baku
  class Entity
    include Baku::EventDispatcher
    
    attr_reader :components, :tags
    
    def initialize(tags = [])
      @tags = tags
      @components = {}
    end

    def add_component(component)
      if @components.has_key?(component.class)
        raise StandardError.
          new("Entity already has component: #{component.class}")
      end

      components[component.class] = component

      update_component_mask
      
      dispatch_event(:component_added, self, component)
    end

    def remove_component(component_class)
      if !@components.has_key?(component_class)
        raise StandardError.
          new("Entity does not have component: #{component_class}")
      end

      @components.delete(component_class)

      update_component_mask
      
      dispatch_event(:component_removed, self, @components[component_class])
    end
    
    def get_component(component_class)
      @components[component_class]
    end

    def component_mask
      @component_mask ||= ComponentMask.from_components(@components)
    end

    private

    def update_component_mask
      @component_mask = ComponentMask.from_components(@components)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
baku-0.2.4 lib/baku/entity.rb
baku-0.2.3 lib/baku/entity.rb
baku-0.2.2 lib/baku/entity.rb
baku-0.2.1 lib/baku/entity.rb
baku-0.2.0 lib/baku/entity.rb