From 24524ad0b1c7a2aeea0bad28092e946cef8026fa Mon Sep 17 00:00:00 2001 From: realtradam Date: Sat, 15 May 2021 03:47:15 -0400 Subject: working ECS --- app/ECS/component_manager.rb | 6 ++-- app/ECS/components/00_renderable.rb | 33 ++++++++++++++++++++++ app/ECS/components/00_test_component.rb | 16 ----------- app/ECS/components/01_based.rb | 16 ----------- app/ECS/components/01_sprite.rb | 49 +++++++++++++++++++++++++++++++++ app/ECS/components/02_label.rb | 32 +++++++++++++++++++++ app/ECS/components/03_player_control.rb | 26 +++++++++++++++++ app/ECS/entity_manager.rb | 8 ++++-- app/ECS/signatures.rb | 23 ++++++++++++++++ app/ECS/system_manager.rb | 4 +-- app/ECS/systems/00_movement.rb | 25 ----------------- app/ECS/systems/00_player.rb | 17 ++++++++++++ app/ECS/systems/01_flying.rb | 19 ------------- app/ECS/systems/99_render.rb | 16 +++++++++++ app/main.rb | 18 ++++++++++++ app/tick.rb | 32 ++++++++------------- metadata/game_metadata.txt | 12 ++++---- 17 files changed, 242 insertions(+), 110 deletions(-) create mode 100644 app/ECS/components/00_renderable.rb delete mode 100644 app/ECS/components/00_test_component.rb delete mode 100644 app/ECS/components/01_based.rb create mode 100644 app/ECS/components/01_sprite.rb create mode 100644 app/ECS/components/02_label.rb create mode 100644 app/ECS/components/03_player_control.rb create mode 100644 app/ECS/signatures.rb delete mode 100644 app/ECS/systems/00_movement.rb create mode 100644 app/ECS/systems/00_player.rb delete mode 100644 app/ECS/systems/01_flying.rb create mode 100644 app/ECS/systems/99_render.rb diff --git a/app/ECS/component_manager.rb b/app/ECS/component_manager.rb index b07cf79..7c05bd5 100644 --- a/app/ECS/component_manager.rb +++ b/app/ECS/component_manager.rb @@ -1,7 +1,7 @@ -require 'app/ECS/base_component.rb' +#require 'app/ECS/base_component.rb' -require 'app/ECS/components/00_test_component.rb' -require 'app/ECS/components/01_based.rb' +#require 'app/ECS/components/00_test_component.rb' +#require 'app/ECS/components/01_based.rb' class ECS class Components diff --git a/app/ECS/components/00_renderable.rb b/app/ECS/components/00_renderable.rb new file mode 100644 index 0000000..bf944d0 --- /dev/null +++ b/app/ECS/components/00_renderable.rb @@ -0,0 +1,33 @@ +class ECS + class Components + # If an entity can be rendered on screen + class Renderable < ECS::BaseComponent + def self.id + @id ||= ECS::ID.renderable + end + + attr_accessor :z + + def initialize(z: 0) + @z = z + end +=begin + # 1. Create a serialize method that returns a hash with all of + # the values you care about. + def serialize + { z: z } + end + + # 2. Override the inspect method and return ~serialize.to_s~. + def inspect + serialize.to_s + end + + # 3. Override to_s and return ~serialize.to_s~. + def to_s + serialize.to_s + end +=end + end + end +end diff --git a/app/ECS/components/00_test_component.rb b/app/ECS/components/00_test_component.rb deleted file mode 100644 index 0906fc8..0000000 --- a/app/ECS/components/00_test_component.rb +++ /dev/null @@ -1,16 +0,0 @@ -class ECS - class Components - class TestComponent < ECS::BaseComponent - def self.id - @id = '0001'.to_i(2) - end - - attr_accessor :x, :y - - def initialize(x: 0, y: 0) - @x = x - @y = y - end - end - end -end diff --git a/app/ECS/components/01_based.rb b/app/ECS/components/01_based.rb deleted file mode 100644 index fe564ca..0000000 --- a/app/ECS/components/01_based.rb +++ /dev/null @@ -1,16 +0,0 @@ -class ECS - class Components - class Based < ECS::BaseComponent - def self.id - @id = '0010'.to_i(2) - end - - attr_accessor :x, :y - - def initialize(x: 0, y:0) - @x = x - @y = y - end - end - end -end diff --git a/app/ECS/components/01_sprite.rb b/app/ECS/components/01_sprite.rb new file mode 100644 index 0000000..8172ce5 --- /dev/null +++ b/app/ECS/components/01_sprite.rb @@ -0,0 +1,49 @@ +class ECS + class Components + # If an entity can be rendered on screen + class Sprite < ECS::BaseComponent + def self.id + @id ||= ECS::ID.sprite + end + + 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, + @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] + end + + def primative_marker + :sprite + end + end + end +end diff --git a/app/ECS/components/02_label.rb b/app/ECS/components/02_label.rb new file mode 100644 index 0000000..c6b8b0b --- /dev/null +++ b/app/ECS/components/02_label.rb @@ -0,0 +1,32 @@ +class ECS + class Components + # If an entity can be rendered on screen + class Label < ECS::BaseComponent + def self.id + @id ||= ECS::ID.label + end + + 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, + @y = y, + @text = text, + @size_enum = size_enum, + @alignment_enum = alignment_enum, + @r = r, + @g = g, + @b = b, + @a = a, + @font = font, + @vertical_alignment_enum = vertical_alignment_enum] + end + + def primative_marker + :label + end + end + end +end diff --git a/app/ECS/components/03_player_control.rb b/app/ECS/components/03_player_control.rb new file mode 100644 index 0000000..1e4a348 --- /dev/null +++ b/app/ECS/components/03_player_control.rb @@ -0,0 +1,26 @@ +class ECS + class Components + # If an entity can be rendered on screen + class PlayerControl < ECS::BaseComponent + def self.id + @id ||= ECS::ID.player_control + end + + attr_accessor :north, :south, :east, :west + + def initialize(north: 'w', south: 's', east: 'd', west: 'a') + @north = north + @south = south + @east = east + @west = west + end + + def set(north: 'w', south: 's', east: 'd', west: 'a') + @north = north + @south = south + @east = east + @west = west + end + end + end +end diff --git a/app/ECS/entity_manager.rb b/app/ECS/entity_manager.rb index 06f73cd..ba3561f 100644 --- a/app/ECS/entity_manager.rb +++ b/app/ECS/entity_manager.rb @@ -2,10 +2,14 @@ class ECS class Entity attr_accessor :id - def initialize(signature = 0) + def initialize(*signature) + final_signature = 0 + signature.each do |sig| + final_signature += sig + end @id = ECS::Entity.generate_new_id self.class.all.push self - self.class.signatures.push signature + self.class.signatures.push final_signature ECS::Components.entity_created(@id) end diff --git a/app/ECS/signatures.rb b/app/ECS/signatures.rb new file mode 100644 index 0000000..67464ad --- /dev/null +++ b/app/ECS/signatures.rb @@ -0,0 +1,23 @@ +class ECS + class ID + class <