class FelFlame class Entities # Holds the unique ID of this entity # @return [Integer] attr_accessor :id # Creating a new Entity # @param components [Components] Can be any number of components, identical duplicates will be automatically purged however different components from the same component manager are allowed. # @return [Entity] def initialize(*components) # Assign new unique ID new_id = self.class.data.find_index { |i| i.nil? } new_id = self.class.data.size if new_id.nil? self.id = new_id # Add each component components.uniq.each do |component| add component end self.class.data[id] = self end # A hash that uses component manager constant names as keys, and where the values of those keys are arrays that contain the {FelFlame::Helper::ComponentManager#id IDs} of the components attached to this entity. # @return [Hash] def components @components ||= {} end # An alias for the {#id ID reader} # @return [Integer] def to_i id end # Removes this Entity from the list and purges all references to this Entity from other Components, as well as its {id ID} and data. # @return [Boolean] true. def delete components.each do |component_manager, component_array| component_array.each do |component_id| FelFlame.const_get( component_manager.to_s.delete_prefix('FelFlame::') )[component_id].entities.delete(id) # The following is neater, but doesnt work for some reason :/ #Object.const_get(component_manager)[component_id].entities.delete(id) end end FelFlame::Entities.data[id] = nil @id = nil @components = nil true end # Returns true when added, or false if it already belongs to the Entity # Add a component to the Entity # @param component [Component] A component created from any component manager # @return [Boolean] true if component is added, false if it already is attached def add(component) if components[component.class.to_s.to_sym].nil? components[component.class.to_s.to_sym] = [component.id] component.entities.push id true elsif !components[component.class.to_s.to_sym].include? component.id components[component.class.to_s.to_sym].push component.id component.entities.push id true else false end end # Remove a component from the Entity # @param component [Component] A component created from any component manager # @return [Boolean] true if component is removed, false if it wasnt attached to component def remove(component) components[component.class.to_s.to_sym].delete component.id if component.entities.delete id true else false end end # Export all data into a JSON String which can then be saved into a file # TODO: This function is not yet complete # @return [String] A JSON formatted String def to_json() end class <