diff options
Diffstat (limited to 'app/ECS')
| -rw-r--r-- | app/ECS/base_component.rb | 46 | ||||
| -rw-r--r-- | app/ECS/component_manager.rb | 20 | ||||
| -rw-r--r-- | app/ECS/components/00_renderable.rb | 16 | ||||
| -rw-r--r-- | app/ECS/components/01_sprite.rb | 44 | ||||
| -rw-r--r-- | app/ECS/components/02_label.rb | 27 | ||||
| -rw-r--r-- | app/ECS/components/03_player_control.rb | 21 | ||||
| -rw-r--r-- | app/ECS/components/04_map.rb | 21 | ||||
| -rw-r--r-- | app/ECS/components/05_interactable.rb | 16 | ||||
| -rw-r--r-- | app/ECS/components/06_level.rb | 25 | ||||
| -rw-r--r-- | app/ECS/components/07_collidable.rb | 22 | ||||
| -rw-r--r-- | app/ECS/components/debug_singleton.rb | 13 | ||||
| -rw-r--r-- | app/ECS/entity_manager.rb | 63 | ||||
| -rw-r--r-- | app/ECS/signatures.rb | 35 | ||||
| -rw-r--r-- | app/ECS/system_manager.rb | 5 | ||||
| -rw-r--r-- | app/ECS/systems/00_player.rb | 14 | ||||
| -rw-r--r-- | app/ECS/systems/99_render.rb | 37 | ||||
| -rw-r--r-- | app/ECS/test.rb | 18 |
17 files changed, 0 insertions, 443 deletions
diff --git a/app/ECS/base_component.rb b/app/ECS/base_component.rb deleted file mode 100644 index f32322b..0000000 --- a/app/ECS/base_component.rb +++ /dev/null @@ -1,46 +0,0 @@ -class BaseComponent - class <<self - def id - @id ||= ID.send(ComponentHelper.underscore(ancestors[0].name.split('::').last)) - 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 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 diff --git a/app/ECS/component_manager.rb b/app/ECS/component_manager.rb deleted file mode 100644 index f5261a2..0000000 --- a/app/ECS/component_manager.rb +++ /dev/null @@ -1,20 +0,0 @@ -#require 'app/ECS/base_component.rb' - -#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? - 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 - end -end diff --git a/app/ECS/components/00_renderable.rb b/app/ECS/components/00_renderable.rb deleted file mode 100644 index 4008691..0000000 --- a/app/ECS/components/00_renderable.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Components - # If an entity can be rendered on screen - class Renderable < BaseComponent - attr_accessor :z - - def initialize - @z = 0 - end - - def set(**opts) - opts.each do |key, value| - self.send "#{key}=", value - end - end - end -end diff --git a/app/ECS/components/01_sprite.rb b/app/ECS/components/01_sprite.rb deleted file mode 100644 index 5aa71ba..0000000 --- a/app/ECS/components/01_sprite.rb +++ /dev/null @@ -1,44 +0,0 @@ -class Components - # If an entity can be rendered on screen - class Sprite < BaseComponent - - attr_accessor :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 - - def set(x: @x, y: @y, w: @w, h: @h, path: @path, angle: @angle, a: @a, r: @r, g: @g, b: @b, - source_x: @source_x, source_y: @source_y, source_w: @source_w, source_h: @source_h, - tile_x: @tile_x, tile_y: @tile_y, tile_w: @tile_w, tile_h: @tile_h, - flip_horizontally: @flip_horizontally, flip_vertically: @flip_vertically, - angle_anchor_x: @angle_anchor_x, angle_anchor_y: @angle_anchor_y) - {x: @x = x, - y: @y = y, - w: @w = w, - h: @h = h, - path: @path = path, - angle: @angle = angle, - a: @a = a, - r: @r = r, - g: @g = g, - b: @b = b, - source_x: @source_x = source_x, - source_y: @source_y = source_y, - source_w: @source_w = source_w, - source_h: @source_h = source_h, - tile_x: @tile_x = tile_x, - tile_y: @tile_y = tile_y, - tile_w: @tile_w = tile_w, - tile_h: @tile_h = tile_h, - flip_horizontally: @flip_horizontally = flip_horizontally, - flip_vertically: @flip_vertically = flip_vertically, - angle_anchor_x: @angle_anchor_x = angle_anchor_x, - angle_anchor_y: @angle_anchor_y = angle_anchor_y} - end - - def primative_marker - :sprite - end - end -end diff --git a/app/ECS/components/02_label.rb b/app/ECS/components/02_label.rb deleted file mode 100644 index aed00ed..0000000 --- a/app/ECS/components/02_label.rb +++ /dev/null @@ -1,27 +0,0 @@ -class Components - # A dragonruby label wrapper - class Label < BaseComponent - - attr_accessor :x, :y, :text, :size_enum, :alignment_enum, - :a, :r, :g, :b, :font, :vertical_alignment_enum - - def set(x: @x, y: @y, text: @text, size_enum: @size_enum, alignment_enum: @alignment_enum, - a: @a, r: @r, g: @g, b: @b, font: @font, vertical_alignment_enum: @vertical_alignment_enum) - {x: @x = x, - y: @y = y, - text: @text = text, - size_enum: @size_enum = size_enum, - alignment_enum: @alignment_enum = alignment_enum, - r: @r = r, - g: @g = g, - b: @b = b, - a: @a = a, - font: @font = font, - vertical_alignment_enum: @vertical_alignment_enum = vertical_alignment_enum } - end - - def primative_marker - :label - end - end -end diff --git a/app/ECS/components/03_player_control.rb b/app/ECS/components/03_player_control.rb deleted file mode 100644 index 58d0102..0000000 --- a/app/ECS/components/03_player_control.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Components - # Gives control(keyboard or otherwise) over an object - class PlayerControl < BaseComponent - attr_accessor :north, :south, :east, :west, :interact, :menu - - def initialize - @north = 'w' - @south = 's' - @east = 'd' - @west = 'a' - @interact = 'space' - @menu = 'enter' - end - - def set(**opts) - opts.each do |key, value| - send "#{key}=", value - end - end - end -end diff --git a/app/ECS/components/04_map.rb b/app/ECS/components/04_map.rb deleted file mode 100644 index 804fc02..0000000 --- a/app/ECS/components/04_map.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Components - # dragonruby label wrapper - class Map < BaseComponent - - attr_accessor :json_name, :json, :x, :y, :tilewidth, :tileheight, :a, :r, :g, :b - - def set(json_name: @json_name, x: @x, y: @y, tilewidth: @tilewidth, - tileheight: @tileheight, a: @a, r: @r, g: @g, b: @b) - { json_name: @json_name = json_name, - json: @json = Helper.get_json_tiles(json_name), - x: @x = x, - y: @y = y, - tilewidth: @tilewidth = tilewidth, - tileheight: @tileheight = tileheight, - r: @r = r, - g: @g = g, - b: @b = b, - a: @a = a } - end - end -end diff --git a/app/ECS/components/05_interactable.rb b/app/ECS/components/05_interactable.rb deleted file mode 100644 index 305bb02..0000000 --- a/app/ECS/components/05_interactable.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Components - # If an entity can be rendered on screen - class Interactable < BaseComponent - attr_accessor :z - - def initialize - @z = z - end - - def set(**opts) - opts.each do |key, value| - self.send "#{key}=", value - end - end - end -end diff --git a/app/ECS/components/06_level.rb b/app/ECS/components/06_level.rb deleted file mode 100644 index 4d692e6..0000000 --- a/app/ECS/components/06_level.rb +++ /dev/null @@ -1,25 +0,0 @@ -class Components - # If an entity can be rendered on screen - class Level < BaseComponent - attr_accessor :z - - def initialize - @grid = [[]] #Array.new(1) { Array.new(1) } - end - - def set(x: x, y: y, **opts) - smart_array(x, y).merge(opts) - end - - # Things that the smart_array holds - # - # player: is the player in the square? - # hitbox: true/false - # interact: id/false - def smart_array(x, y) - @grid[x] = [] if @grid[x].nil? - @grix[x][y] = {} if @grid[x][y].nil? - @grid[x][y] - end - end -end diff --git a/app/ECS/components/07_collidable.rb b/app/ECS/components/07_collidable.rb deleted file mode 100644 index 207e138..0000000 --- a/app/ECS/components/07_collidable.rb +++ /dev/null @@ -1,22 +0,0 @@ -class Components - # If an entity can be rendered on screen - class Collidable < BaseComponent - class <<self - def add(entity_id) - super(entity_id) - #add to grid? - end - end - attr_accessor :grid - - def initialize - @grid = [[]] - end - - def set(**opts) - opts.each do |key, value| - self.send "#{key}=", value - end - end - end -end diff --git a/app/ECS/components/debug_singleton.rb b/app/ECS/components/debug_singleton.rb deleted file mode 100644 index f298172..0000000 --- a/app/ECS/components/debug_singleton.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Components - # If an entity can be rendered on screen - class DebugSingleton - class <<self - @data = false - attr_accessor :data - - def id - 0 - end - end - end -end diff --git a/app/ECS/entity_manager.rb b/app/ECS/entity_manager.rb deleted file mode 100644 index 7d0d1ff..0000000 --- a/app/ECS/entity_manager.rb +++ /dev/null @@ -1,63 +0,0 @@ -class Entity - attr_accessor :id - - def initialize(*signature) - final_signature = 0 - signature.each do |sig| - final_signature += sig - end - @id = Entity.generate_new_id - self.class.all.push self - self.class.signatures.push final_signature - Components.entity_created(@id) - end - - class <<self - # All entities that exist - def all - @all ||= [] - end - - def id_queue - @id_queue ||= [] - end - - def generate_new_id - if id_queue.empty? - all.size - else - id_queue.shift - end - end - - # What components a given entity uses - def signatures - @signatures ||= [] - end - - def destroy_entity(entity_id) - if all[entity_id].nil? - puts 'Entity can not be destroyed, id out of bounds' - elsif entity_id < all.size - 1 - Components.constants.each do |constant| - unless (signatures[entity_id] & Components::const_get(constant).id).zero? - Components::const_get(constant).delete(entity_id) - end - end - all[entity_id] = nil - signatures[entity_id] = nil - id_queue.push entity_id - elsif entity_id == all.size - 1 - Components.constants.each do |constant| - unless (signatures[entity_id] & Components::const_get(constant).id).zero? - Components::const_get(constant).delete(entity_id) - end - end - all.pop - signatures.pop - else - puts 'Unknown error with destroy_entity, entity not destroyed' - end - end - end -end diff --git a/app/ECS/signatures.rb b/app/ECS/signatures.rb deleted file mode 100644 index a6df0ac..0000000 --- a/app/ECS/signatures.rb +++ /dev/null @@ -1,35 +0,0 @@ -class ID - class <<self - 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 level - 0b0_001_000_000 - end - - def collidable - 0b0_010_000_000 - end - end -end diff --git a/app/ECS/system_manager.rb b/app/ECS/system_manager.rb deleted file mode 100644 index e63375c..0000000 --- a/app/ECS/system_manager.rb +++ /dev/null @@ -1,5 +0,0 @@ -#require 'app/ECS/systems/00_movement.rb' -#require 'app/ECS/systems/01_flying.rb' - -class Systems -end diff --git a/app/ECS/systems/00_player.rb b/app/ECS/systems/00_player.rb deleted file mode 100644 index 21dedfd..0000000 --- a/app/ECS/systems/00_player.rb +++ /dev/null @@ -1,14 +0,0 @@ -class Systems - class Player - def self.run - Components::PlayerControl.data.each do |id, data| - if !(Components::Sprite.id & Entity.signatures[id]).zero? - Components::Sprite.data[id].y -= 64 if $gtk.args.inputs.keyboard.key_down.send(data.north) - Components::Sprite.data[id].y += 64 if $gtk.args.inputs.keyboard.key_down.send(data.south) - Components::Sprite.data[id].x += 64 if $gtk.args.inputs.keyboard.key_down.send(data.east) - Components::Sprite.data[id].x -= 64 if $gtk.args.inputs.keyboard.key_down.send(data.west) - end - end - end - end -end diff --git a/app/ECS/systems/99_render.rb b/app/ECS/systems/99_render.rb deleted file mode 100644 index c71c6fc..0000000 --- a/app/ECS/systems/99_render.rb +++ /dev/null @@ -1,37 +0,0 @@ -class Systems - class Render - def self.run - Components::Renderable.data.sort_by { |v| v[1].z }.each do |key, data| - if !(Components::Sprite.id & Entity.signatures[key]).zero? - $gtk.args.outputs.sprites << Components::Sprite.data[key].set - elsif !(Components::Label.id & Entity.signatures[key]).zero? - $gtk.args.outputs.labels << Components::Label.data[key].set - elsif !(Components::Map.id & Entity.signatures[key]).zero? - Components::Map.data[key].json['layers'].each do |layer| - layer['chunks'].each do |chunk| - chunk['data'].each_slice(chunk['width']).with_index do |row, row_index| - row.each_with_index do |tile, column_index| - unless tile.zero? - iter = 0 - loop do - tile = Helper.get_tile(json_name: Components::Map.data[key].json['tilesets'][iter]['source'].split('/').last.delete('\\').delete_suffix('.tsx'), tile_index: tile) - break if tile.is_a?(Hash) - raise Exception.new "#{Components::Map.data[key].json['json_name']} not valid map, exceeded tile range" if (iter += 1) >= Components::Map.data[key].json['tilesets'].count - end - unless tile.empty? - tile[:x] = Components::Map.data[key].x + (Components::Map.data[key].tilewidth * column_index) + chunk['x'] - tile[:y] = Components::Map.data[key].y + (Components::Map.data[key].tileheight * row_index) + chunk['y'] - tile[:w] = Components::Map.data[key].tilewidth - tile[:h] = Components::Map.data[key].tileheight - $gtk.args.outputs.sprites << tile - end - end - end - end - end - end - end - end - end - end -end diff --git a/app/ECS/test.rb b/app/ECS/test.rb deleted file mode 100644 index 13fd401..0000000 --- a/app/ECS/test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative './entity_manager.rb' -require_relative './component_manager.rb' -require_relative './system_manager.rb' - -move = '0001'.to_i(2) -base = '0010'.to_i(2) -both = '0011'.to_i(2) -Entity.new(move) -Entity.new(base) -Entity.new(both) - -3.times do - Systems.constants.each do |constant| - puts "|----#{constant.to_s.upcase}----|" - Systems::const_get(constant).run - end - #ECS::Entity.destroy_entity(ECS::Entity.all.last.id) unless ECS::Entity.all.empty? -end |
