diff options
| author | realtradam <[email protected]> | 2021-05-26 07:41:45 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-26 07:41:45 -0400 |
| commit | 1531c393a0b8b7c7a0451820fe3905b0b4e71079 (patch) | |
| tree | 2133bf8614d8cecab84b5fae37561710f9c11375 | |
| parent | 7bec71db2680e0503f39c31047f5f90ca89433df (diff) | |
| download | FelECS-1531c393a0b8b7c7a0451820fe3905b0b4e71079.tar.gz FelECS-1531c393a0b8b7c7a0451820fe3905b0b4e71079.zip | |
dynamic components can be created
| -rw-r--r-- | README.mdown | 5 | ||||
| -rw-r--r-- | component_manager.rb | 48 | ||||
| -rw-r--r-- | components/00_renderable.rb | 3 | ||||
| -rw-r--r-- | components/01_sprite.rb | 13 | ||||
| -rw-r--r-- | components/02_label.rb | 5 | ||||
| -rw-r--r-- | components/03_player_control.rb | 5 | ||||
| -rw-r--r-- | helpers/01_component.rb | 143 | ||||
| -rw-r--r-- | signatures.rb | 64 |
8 files changed, 140 insertions, 146 deletions
diff --git a/README.mdown b/README.mdown index a8f3b46..3324697 100644 --- a/README.mdown +++ b/README.mdown @@ -45,8 +45,9 @@ FF::Cmp::Name.new(param1: value1) FF::Cmp::Name.get_by_entity(entity_id) # gets array of components @component.set(param2: 'not default') @component.param2 = 'different not default' -FF::Cmp::Name.delete(component_id) -FF::Cmp::Name.delete_by_entity(entity_id) # deletes all related components attached to entity +FF::Cmp::Name.detach(entity_id: ent_id, component_id: cmp_id) +FF::Cmp::Name.remove_entity(entity_id) # Removes entity from any components +FF::Cmp::Name.delete_component(component_id) # deletes component and removes from all relevant entities @component.dump # returns hash of all variables!(and the id) FF::Cmp::Name.load @component_dump diff --git a/component_manager.rb b/component_manager.rb index f54c5f6..2f1b2e2 100644 --- a/component_manager.rb +++ b/component_manager.rb @@ -2,32 +2,36 @@ #require 'app/ECS/components/00_test_component.rb' #require 'app/ECS/components/01_based.rb' - -class Components - class <<self - def entity_destroyed(entity_id) - constants.each do |component| - component.delete(entity_id) unless (component.id & Entity.signatures[entity_id]).zero? +class FelFlame + class Components + @component_map = [] + class <<self + def entity_destroyed(entity_id) + @component_map.delete(entity_id) + constants.each do |component| #TODO change delete to remove + component.delete(entity_id) unless (component.signature & FelFlame::Entity.signatures[entity_id]).zero? + end end - end - def entity_created(entity_id) - constants.each do |component| - const_get(component.to_s).add(entity_id) unless (const_get(component.to_s).id & Entity.signatures[entity_id]).zero? - end - end - - def new(component_name, *attrs, **attrs_with_defaults) - const_set(component_name, Class.new(Helper::BaseComponent) {}) - attrs.each do |attr| - Components.const_get(component_name).attr_accessor attr + def entity_created(entity_id) + constants.each do |component| + const_get(component.to_s).add(entity_id) unless (const_get(component.to_s).signature & FelFlame::Entity.signatures[entity_id]).zero? + end + @component_map[entity_id] = [] end - attrs_with_defaults.each do |attr, default| - Components.const_get(component_name).attr_writer attr - Components.const_get(component_name).define_method(attr) do - return default unless instance_variable_defined? "@#{attr}" - instance_variable_get "@#{attr}" + def new(component_name, *attrs, **attrs_with_defaults) + const_set(component_name, Class.new(FelFlame::Helper::BaseComponent) {}) + attrs.each do |attr| + FelFlame::Components.const_get(component_name).attr_accessor attr + end + attrs_with_defaults.each do |attr, _default| + FelFlame::Components.const_get(component_name).attr_accessor attr + end + FelFlame::Components.const_get(component_name).define_method(:initialize) do + attrs_with_defaults.each do |attr, default| + instance_variable_set("@#{attr}", default) + end end end end diff --git a/components/00_renderable.rb b/components/00_renderable.rb index 3971c8c..1d34fbb 100644 --- a/components/00_renderable.rb +++ b/components/00_renderable.rb @@ -1,3 +1,5 @@ +FelFlame::Components.new('Renderable', z: 0) +=begin class Components # If an entity can be rendered on screen class Renderable < Helper::BaseComponent @@ -14,3 +16,4 @@ class Components end end end +=end diff --git a/components/01_sprite.rb b/components/01_sprite.rb index 8d30cdf..c02a51b 100644 --- a/components/01_sprite.rb +++ b/components/01_sprite.rb @@ -1,3 +1,15 @@ + +FelFlame::Components.new('Sprite', :x, :y, :w, :h, :path, :angle, :a, :r, :g, :b, + :source_x, :source_y, :source_w, :source_h, + :tile_x, :tile_y, :tile_w, :tile_h, + :flip_horizontally, :flip_vertically, + :angle_anchor_x, :angle_anchor_y, primative_marker: :sprite) + +#Components::Sprite.define_method('primative_marker') do +# :sprite +#end + +=begin class Components # If an entity can be rendered on screen class Sprite < Helper::BaseComponent @@ -42,3 +54,4 @@ class Components end end end +=end diff --git a/components/02_label.rb b/components/02_label.rb index 13544b7..f73b12c 100644 --- a/components/02_label.rb +++ b/components/02_label.rb @@ -1,3 +1,7 @@ + +FelFlame::Components.new :Label, :x, :y, :text, :size_enum, :alignment_enum, + :a, :r, :g, :b, :font, :vertical_alignment_enum, primative_marker: :label +=begin class Components # A dragonruby label wrapper class Label < Helper::BaseComponent @@ -25,3 +29,4 @@ class Components end end end +end diff --git a/components/03_player_control.rb b/components/03_player_control.rb index d5d8f7a..f48b155 100644 --- a/components/03_player_control.rb +++ b/components/03_player_control.rb @@ -1,3 +1,7 @@ + +FelFlame::Components.new :PlayerControl, north: 'up', south: 'down', east: 'right', + west: 'left', interact: 'space', menu: 'enter' +=begin class Components # Gives control(keyboard or otherwise) over an object class PlayerControl < Helper::BaseComponent @@ -19,3 +23,4 @@ class Components end end end +=end diff --git a/helpers/01_component.rb b/helpers/01_component.rb index ca4f86d..9ea90d9 100644 --- a/helpers/01_component.rb +++ b/helpers/01_component.rb @@ -1,101 +1,96 @@ -class Helper - class BaseComponent - class <<self - def id - @id ||= ID.create_new_id Helper::ComponentHelper.underscore(ancestors[0].name.split('::').last) - #@id ||= ID.send(Helper::ComponentHelper.underscore(ancestors[0].name.split('::').last)) - end +class FelFlame + class Helper + class BaseComponent + class <<self + def signature + @signature ||= FelFlame::Signature.create_new_signature FelFlame::Helper::ComponentHelper.underscore(ancestors[0].name.split('::').last) + end - def data - @data ||= {} - end + def data + @data ||= {} + end - def add(entity_id) - data[entity_id] = new - end + def add(entity_id) + data[entity_id] = new + end - def delete(entity_id) - data.delete entity_id + def delete(entity_id) + data.delete entity_id + end end - end - def self.inherited(subclass) #TODO this should automagically create the ID(handle it with ID) - #@id = ID.send(subclass) - # maybe overwrite this? - end - - def set(**opts) - opts.each do |key, value| - send "#{key}=", value + def set(**opts) + opts.each do |key, value| + send "#{key}=", value + end end - end - def create_data(name, default = nil) - #TODO fill this out - end + #def create_data(name, default = nil) + # #TODO fill this out + #end - def get #TODO maybe optimize removeing the @ symbol | doesnt get defaults - instance_variables.each_with_object({}) do |key, final| - final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key) + def get #TODO maybe optimize removeing the @ symbol + instance_variables.each_with_object({}) do |key, final| + final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key) + end end - end - def dump #TODO already does what get does? - # should return a json or hash of all data in this component + def dump #TODO Needs to get id and stuff? + # should return a json or hash of all data in this component + end end - end - class Level < Helper::BaseComponent - class <<self - def data - @data ||= { add: [], remove: [], grid: Helper::Array2D.new } - end + class Level < FelFlame::Helper::BaseComponent + class <<self + def data + @data ||= { add: [], remove: [], grid: FelFlame::Helper::Array2D.new } + end - def add(entity_id) - super - data[:add].push entity_id - end + def add(entity_id) + super + data[:add].push entity_id + end - def remove(entity_id) - data[:remove].push entity_id - super + def remove(entity_id) + data[:remove].push entity_id + super + end end end - end - class Array2D < Array - def [](val) - unless val.nil? - return self[val] = [] if super.nil? + class Array2D < Array + def [](val) + unless val.nil? + return self[val] = [] if super.nil? + end + super end - super end - end - - module ComponentHelper - class <<self - def up? char - char == char.upcase - end + module ComponentHelper + class <<self + def up? char + char == char.upcase + end - def down? char - char == char.downcase - end + def down? char + char == char.downcase + end - def underscore(input) - output = input[0].downcase - (1...(input.length - 1)).each do |iter| - if down?(input[iter]) && up?(input[iter + 1]) - output += "#{input[iter].downcase}_" - elsif up?(input[iter - 1]) && up?(input[iter]) && down?(input[iter + 1]) - output += "_#{input[iter].downcase}" - else - output += input[iter].downcase + def underscore(input) + output = input[0].downcase + (1...(input.length - 1)).each do |iter| + if down?(input[iter]) && up?(input[iter + 1]) + output += "#{input[iter].downcase}_" + elsif up?(input[iter - 1]) && up?(input[iter]) && down?(input[iter + 1]) + output += "_#{input[iter].downcase}" + else + output += input[iter].downcase + end end + output += input[-1].downcase unless input.length == 1 + output end - output += input[-1].downcase unless input.length == 1 - output end end end diff --git a/signatures.rb b/signatures.rb index c11c048..7827607 100644 --- a/signatures.rb +++ b/signatures.rb @@ -1,54 +1,22 @@ -class ID - class <<self - @next_id = 0b0_010_000_000_000 - - def create_new_id(name) - temp = @next_id - @next_id *= 2 - define_singleton_method(name) do - temp +class FelFlame + class Signature + class <<self + def next_signature + @next_signature ||= 1 end - send(name) - end - - def renderable - 0b0_001 - end - - def sprite - 0b0_010 - end - def label - 0b0_100 - end - - def player_control - 0b0_001_000 - end - - def map - 0b0_010_000 - end - - def interactable - 0b0_100_000 - end - - def collidable - 0b0_001_000_000 - end - - def overworld - 0b0_010_000_000 - end - - def indoor - 0b0_100_000_000 - end + def next_signature= num + @next_signature = num + end - def battle - 0b0_001_000_000_000 + def create_new_signature(name) + temp = self.next_signature + self.next_signature = 2 * self.next_signature + define_singleton_method(name) do + temp + end + send(name) + end end end end |
