summaryrefslogtreecommitdiffhomepage
path: root/entity_manager.rb
diff options
context:
space:
mode:
Diffstat (limited to 'entity_manager.rb')
-rw-r--r--entity_manager.rb38
1 files changed, 25 insertions, 13 deletions
diff --git a/entity_manager.rb b/entity_manager.rb
index f699c95..5038630 100644
--- a/entity_manager.rb
+++ b/entity_manager.rb
@@ -4,8 +4,9 @@ class FelFlame
# @return [Integer]
attr_accessor :id
- # Creating a new component
- # @param components [Component] Can be any number of components, identical duplicated will be automatically purged however different components from the same component manager are allowed.
+ # 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? }
@@ -19,7 +20,7 @@ class FelFlame
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 IDs of components attached to this entity.
+ # 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 ||= {}
@@ -38,7 +39,9 @@ class FelFlame
component_array.each do |component_id|
FelFlame.const_get(
component_manager.to_s.delete_prefix('FelFlame::')
- )[component_id].linked_entities.delete(id)
+ )[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
@@ -51,14 +54,14 @@ class FelFlame
# 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
+ def add(component)
if components[component.class.to_s.to_sym].nil?
components[component.class.to_s.to_sym] = [component.id]
- component.linked_entities.push 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.linked_entities.push id
+ component.entities.push id
true
else
false
@@ -68,9 +71,9 @@ class FelFlame
# 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
+ def remove(component)
components[component.class.to_s.to_sym].delete component.id
- if component.linked_entities.delete id
+ if component.entities.delete id
true
else
false
@@ -80,8 +83,7 @@ class FelFlame
# 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
+ def to_json() end
class <<self
include Enumerable
@@ -91,7 +93,11 @@ class FelFlame
@data ||= []
end
- # Gets an Entity from the given unique ID. Usage is simular to how an Array lookup works
+ # Gets an Entity from the given {id unique ID}. Usage is simular to how an Array lookup works
+ #
+ # @example
+ # # This gets the Entity with ID 7
+ # FelFlame::Entities[7]
# @param entity_id [Integer]
# @return [Entity] returns the Entity that uses the given unique ID, nil if there is no Entity associated with the given ID
def [](entity_id)
@@ -99,11 +105,17 @@ class FelFlame
end
# Iterates over all entities. In general when using ECS the use of this method should never be neccassary unless you are doing something very wrong, however I will not stop you.
- # You also call other enumerable methods instead of each, such as `each_with_index` or `select`
+ # You also call other enumerable methods instead of each, such as +each_with_index+ or +select+
# @return [Enumerator]
def each(&block)
data.each(&block)
end
+
+ # Creates a new entity using the data from a JSON string
+ # TODO: This function is not yet complete
+ # @param json_string [String] A string that was exported originally using the {FelFlame::Entities#to_json to_json} function
+ # @param opts [Keywords] What values(its {FelFlame::Entities#id ID} or the {FelFlame::Helper::ComponentManager#id component IDs}) should be overwritten TODO: this might change
+ def from_json(json_string, **opts) end
end
end
end