diff options
| author | realtradam <[email protected]> | 2021-05-18 04:49:47 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-18 04:49:47 -0400 |
| commit | 120b693ffd02bc5c7f41ff2b9657facc7117daae (patch) | |
| tree | 59e9f982dc2ee994cbbb65411065b35445048502 /app/ECS/base_component.rb | |
| parent | 24524ad0b1c7a2aeea0bad28092e946cef8026fa (diff) | |
| download | typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.tar.gz typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.zip | |
.
Diffstat (limited to 'app/ECS/base_component.rb')
| -rw-r--r-- | app/ECS/base_component.rb | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/app/ECS/base_component.rb b/app/ECS/base_component.rb index a40ef52..e374540 100644 --- a/app/ECS/base_component.rb +++ b/app/ECS/base_component.rb @@ -1,17 +1,47 @@ -class ECS - class BaseComponent - class <<self - def data - @data ||= {} - end +class BaseComponent + class <<self + def id + #puts underscore(self.ancestors[0].name.split('::').last) + @id ||= ID.send(ComponentHelper.underscore(ancestors[0].name.split('::').last)) + end - def add(entity_id, **args) - data[entity_id] = new(**args) - end + def data + @data ||= {} + end + + def add(entity_id) + data[entity_id] = new + end + + def delete(entity_id) + data.delete entity_id + end + end +end + +module ComponentHelper + class <<self + def up? char + char == char.upcase + end + + def down? char + char == char.downcase + end - def delete(entity_id) - data.delete entity_id + 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 end end |
