summaryrefslogtreecommitdiffhomepage
path: root/app/ECS
diff options
context:
space:
mode:
Diffstat (limited to 'app/ECS')
-rw-r--r--app/ECS/base_component.rb52
-rw-r--r--app/ECS/component_manager.rb20
-rw-r--r--app/ECS/components/00_renderable.rb35
-rw-r--r--app/ECS/components/01_sprite.rb81
-rw-r--r--app/ECS/components/02_label.rb47
-rw-r--r--app/ECS/components/03_player_control.rb36
-rw-r--r--app/ECS/entity_manager.rb98
-rw-r--r--app/ECS/signatures.rb38
-rw-r--r--app/ECS/system_manager.rb4
-rw-r--r--app/ECS/systems/00_player.rb21
-rw-r--r--app/ECS/systems/99_render.rb37
-rw-r--r--app/ECS/test.rb10
12 files changed, 241 insertions, 238 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
diff --git a/app/ECS/component_manager.rb b/app/ECS/component_manager.rb
index 7c05bd5..f5261a2 100644
--- a/app/ECS/component_manager.rb
+++ b/app/ECS/component_manager.rb
@@ -3,19 +3,17 @@
#require 'app/ECS/components/00_test_component.rb'
#require 'app/ECS/components/01_based.rb'
-class ECS
- class Components
- class <<self
- def entity_destroyed(entity_id)
- constants.each do |component|
- component.delete(entity_id) unless (component.id & ECS::Entity.signatures[entity_id]).zero?
- end
+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 & ECS::Entity.signatures[entity_id]).zero?
- 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
diff --git a/app/ECS/components/00_renderable.rb b/app/ECS/components/00_renderable.rb
index bf944d0..18017cc 100644
--- a/app/ECS/components/00_renderable.rb
+++ b/app/ECS/components/00_renderable.rb
@@ -1,33 +1,10 @@
-class ECS
- class Components
- # If an entity can be rendered on screen
- class Renderable < ECS::BaseComponent
- def self.id
- @id ||= ECS::ID.renderable
- end
+class Components
+ # If an entity can be rendered on screen
+ class Renderable < BaseComponent
+ attr_accessor :z
- 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
+ def initialize(z: 0)
+ @z = z
end
end
end
diff --git a/app/ECS/components/01_sprite.rb b/app/ECS/components/01_sprite.rb
index 8172ce5..5aa71ba 100644
--- a/app/ECS/components/01_sprite.rb
+++ b/app/ECS/components/01_sprite.rb
@@ -1,49 +1,44 @@
-class ECS
- class Components
- # If an entity can be rendered on screen
- class Sprite < ECS::BaseComponent
- def self.id
- @id ||= ECS::ID.sprite
- end
+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
+ 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 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
+ def primative_marker
+ :sprite
end
end
end
diff --git a/app/ECS/components/02_label.rb b/app/ECS/components/02_label.rb
index c6b8b0b..2483d57 100644
--- a/app/ECS/components/02_label.rb
+++ b/app/ECS/components/02_label.rb
@@ -1,32 +1,27 @@
-class ECS
- class Components
- # If an entity can be rendered on screen
- class Label < ECS::BaseComponent
- def self.id
- @id ||= ECS::ID.label
- end
+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
+ 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 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
+ 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
index 1e4a348..22bc458 100644
--- a/app/ECS/components/03_player_control.rb
+++ b/app/ECS/components/03_player_control.rb
@@ -1,26 +1,20 @@
-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
+class Components
+ # Gives control(keyboard or otherwise) over an object
+ class PlayerControl < BaseComponent
+ attr_accessor :north, :south, :east, :west
- 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 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
+ def set(north: 'w', south: 's', east: 'd', west: 'a')
+ @north = north
+ @south = south
+ @east = east
+ @west = west
end
end
end
diff --git a/app/ECS/entity_manager.rb b/app/ECS/entity_manager.rb
index ba3561f..7d0d1ff 100644
--- a/app/ECS/entity_manager.rb
+++ b/app/ECS/entity_manager.rb
@@ -1,64 +1,62 @@
-class ECS
- class Entity
- attr_accessor :id
+class Entity
+ attr_accessor :id
- 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 final_signature
- ECS::Components.entity_created(@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
+ class <<self
+ # All entities that exist
+ def all
+ @all ||= []
+ end
- def id_queue
- @id_queue ||= []
- end
+ def id_queue
+ @id_queue ||= []
+ end
- def generate_new_id
- if id_queue.empty?
- all.size
- else
- id_queue.shift
- 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
+ # 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
- ECS::Components.constants.each do |constant|
- unless (signatures[entity_id] & ECS::Components::const_get(constant).id).zero?
- ECS::Components::const_get(constant).delete(entity_id)
- 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
- all[entity_id] = nil
- signatures[entity_id] = nil
- id_queue.push entity_id
- elsif entity_id == all.size - 1
- ECS::Components.constants.each do |constant|
- unless (signatures[entity_id] & ECS::Components::const_get(constant).id).zero?
- ECS::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
- all.pop
- signatures.pop
- else
- puts 'Unknown error with destroy_entity, entity not destroyed'
end
+ all.pop
+ signatures.pop
+ else
+ puts 'Unknown error with destroy_entity, entity not destroyed'
end
end
end
diff --git a/app/ECS/signatures.rb b/app/ECS/signatures.rb
index 67464ad..844c66d 100644
--- a/app/ECS/signatures.rb
+++ b/app/ECS/signatures.rb
@@ -1,23 +1,27 @@
-class ECS
- class ID
- class <<self
- def renderable
- @renderable ||= '0_001'.to_i(2)
- end
+class ID
+ class <<self
+ def renderable
+ @renderable ||= '0_001'.to_i(2)
+ end
- def sprite
- @sprite ||= '0_010'.to_i(2)
- end
+ def sprite
+ @sprite ||= '0_010'.to_i(2)
+ end
- def label
- @label ||= '0_100'.to_i(2)
- end
+ def label
+ @label ||= '0_100'.to_i(2)
+ end
- def player_control
- @player_control ||= '1_000'.to_i(2)
- end
+ def player_control
+ @player_control ||= '0_001_000'.to_i(2)
end
- end
-end
+ def map
+ @map ||= '0_010_000'.to_i(2)
+ end
+ def map
+ @map ||= '0_100_000'.to_i(2)
+ end
+ end
+end
diff --git a/app/ECS/system_manager.rb b/app/ECS/system_manager.rb
index e0e41da..e63375c 100644
--- a/app/ECS/system_manager.rb
+++ b/app/ECS/system_manager.rb
@@ -1,7 +1,5 @@
#require 'app/ECS/systems/00_movement.rb'
#require 'app/ECS/systems/01_flying.rb'
-class ECS
- class Systems
- end
+class Systems
end
diff --git a/app/ECS/systems/00_player.rb b/app/ECS/systems/00_player.rb
index a78137a..9b03def 100644
--- a/app/ECS/systems/00_player.rb
+++ b/app/ECS/systems/00_player.rb
@@ -1,15 +1,12 @@
-class ECS
- class Systems
- class Player
- def self.run
- ECS::Components::PlayerControl.data.each do |id, data|
- if !(ECS::Components::Renderable.id & ECS::Entity.signatures[id]).zero?
- ECS::Components::Sprite.data[id].y += 10 if $gtk.args.inputs.keyboard.key_held.send(data.north)
- ECS::Components::Sprite.data[id].y -= 10 if $gtk.args.inputs.keyboard.key_held.send(data.south)
- ECS::Components::Sprite.data[id].x += 10 if $gtk.args.inputs.keyboard.key_held.send(data.east)
- ECS::Components::Sprite.data[id].x -= 10 if $gtk.args.inputs.keyboard.key_held.send(data.west)
- #$gtk.args.outputs.labels << ECS::Components::Label.data[id].vars
- end
+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 += 10 if $gtk.args.inputs.keyboard.key_held.send(data.north)
+ Components::Sprite.data[id].y -= 10 if $gtk.args.inputs.keyboard.key_held.send(data.south)
+ Components::Sprite.data[id].x += 10 if $gtk.args.inputs.keyboard.key_held.send(data.east)
+ Components::Sprite.data[id].x -= 10 if $gtk.args.inputs.keyboard.key_held.send(data.west)
end
end
end
diff --git a/app/ECS/systems/99_render.rb b/app/ECS/systems/99_render.rb
index 0751bc2..37b562a 100644
--- a/app/ECS/systems/99_render.rb
+++ b/app/ECS/systems/99_render.rb
@@ -1,13 +1,30 @@
-class ECS
- class Systems
- class Render
- def self.run
- ECS::Components::Renderable.data.sort_by { |v| v[1].z }.each do |key, data|
- if !(ECS::Components::Sprite.id & ECS::Entity.signatures[key]).zero?
- #ECS::Components::Based.data[key].x += 2
- $gtk.args.outputs.sprites << ECS::Components::Sprite.data[key].set
- elsif !(ECS::Components::Label.id & ECS::Entity.signatures[key]).zero?
- $gtk.args.outputs.labels << ECS::Components::Label.data[key].set
+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?
+ #Components::Based.data[key].x += 2
+ $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?
+ #puts Components::Map.data[key].json.inspect
+ 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?
+ temp = Helper.get_tile(json_name: 'tileset_Room_Builder_16x16', tile_index: tile)
+ temp[:x] = Components::Map.data[key].x + (Components::Map.data[key].tilewidth * column_index) + chunk['x']
+ temp[:y] = Components::Map.data[key].y - (Components::Map.data[key].tileheight * (row_index + 1)) - chunk['y'] #REVERSED
+ temp[:w] = Components::Map.data[key].tilewidth
+ temp[:h] = Components::Map.data[key].tileheight
+ #puts60 temp.inspect
+ $gtk.args.outputs.sprites << temp
+ end
+ end
+ end
+ end
end
end
end
diff --git a/app/ECS/test.rb b/app/ECS/test.rb
index 2ab5be1..13fd401 100644
--- a/app/ECS/test.rb
+++ b/app/ECS/test.rb
@@ -5,14 +5,14 @@ require_relative './system_manager.rb'
move = '0001'.to_i(2)
base = '0010'.to_i(2)
both = '0011'.to_i(2)
-ECS::Entity.new(move)
-ECS::Entity.new(base)
-ECS::Entity.new(both)
+Entity.new(move)
+Entity.new(base)
+Entity.new(both)
3.times do
- ECS::Systems.constants.each do |constant|
+ Systems.constants.each do |constant|
puts "|----#{constant.to_s.upcase}----|"
- ECS::Systems::const_get(constant).run
+ Systems::const_get(constant).run
end
#ECS::Entity.destroy_entity(ECS::Entity.all.last.id) unless ECS::Entity.all.empty?
end