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 | |
| parent | 24524ad0b1c7a2aeea0bad28092e946cef8026fa (diff) | |
| download | typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.tar.gz typemon-code-120b693ffd02bc5c7f41ff2b9657facc7117daae.zip | |
.
Diffstat (limited to 'app')
| -rw-r--r-- | app/ECS/base_component.rb | 52 | ||||
| -rw-r--r-- | app/ECS/component_manager.rb | 20 | ||||
| -rw-r--r-- | app/ECS/components/00_renderable.rb | 35 | ||||
| -rw-r--r-- | app/ECS/components/01_sprite.rb | 81 | ||||
| -rw-r--r-- | app/ECS/components/02_label.rb | 47 | ||||
| -rw-r--r-- | app/ECS/components/03_player_control.rb | 36 | ||||
| -rw-r--r-- | app/ECS/entity_manager.rb | 98 | ||||
| -rw-r--r-- | app/ECS/signatures.rb | 38 | ||||
| -rw-r--r-- | app/ECS/system_manager.rb | 4 | ||||
| -rw-r--r-- | app/ECS/systems/00_player.rb | 21 | ||||
| -rw-r--r-- | app/ECS/systems/99_render.rb | 37 | ||||
| -rw-r--r-- | app/ECS/test.rb | 10 | ||||
| -rw-r--r-- | app/main.rb | 4 | ||||
| -rw-r--r-- | app/tick.rb | 76 |
14 files changed, 309 insertions, 250 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 diff --git a/app/main.rb b/app/main.rb index 99c10ae..a371d72 100644 --- a/app/main.rb +++ b/app/main.rb @@ -7,6 +7,10 @@ def require_all dir end end +require 'app/dragonruby-game-toolkit-contrib/dragon/grid.rb' + +require_all 'helpers' + require 'app/ECS/signatures.rb' require 'app/ECS/entity_manager.rb' diff --git a/app/tick.rb b/app/tick.rb index f6de238..0d07d73 100644 --- a/app/tick.rb +++ b/app/tick.rb @@ -1,17 +1,69 @@ +$gtk.args.grid.origin_top_left! +$gtk.args.grid.origin_bottom_left! -thing0 = ECS::Entity.new(ECS::ID.sprite, - ECS::ID.renderable, - ECS::ID.player_control) -ECS::Components::Sprite.data[thing0.id].set(x: 576, y: 280, w: 128, h: 101, path: 'dragonruby.png') -thing1 = ECS::Entity.new(ECS::ID.label, ECS::ID.renderable) -ECS::Components::Label.data[thing1.id].set(x: 640, y: 460, text: 'Based ECS', size_enum: 5, alignment_enum: 1) -thing2 = ECS::Entity.new(ECS::ID.label, ECS::ID.renderable) -ECS::Components::Label.data[thing2.id].set(x: 640, y: 420, text: 'It Werks', size_enum: 5, alignment_enum: 1) +thing0 = Entity.new(ID.sprite, + ID.renderable, + ID.player_control) +Components::Sprite.data[thing0.id].set(x: 0, y: 0, w: 128, h: 101, path: 'dragonruby.png') +#@thing0 = Entity.new(ID.sprite, +# ID.renderable, +# ID.player_control) +#@thing3 = Entity.new(ID.sprite, +# ID.renderable, +# ID.player_control) +#Components::Sprite.data[@thing3.id].set(x: 576, y: 580, +# w: 128, h: 101, +# path: 'assets/non-free/Modern_Interiors/1_Interiors/16x16/Room_Builder_16x16.png', +# source_x: 0, source_y: 0, source_w: 16, source_h: 16) + +#json_tiles = Helper.get_json_tiles('tileset_Room_Builder_16x16') +#tile_number = 100 +#source_height_tiles = (tile_number.to_i / json_tiles['columns'].to_i).to_i +#hash = { w: json_tiles['tilewidth'], +# h: json_tiles['tileheight'], +# path: 'assets/non-free/Modern_Interiors/1_Interiors/16x16/Room_Builder_16x16.png', +# source_x: ((tile_number % json_tiles['columns']) - 1) * json_tiles['tilewidth'], +# source_y: json_tiles['imageheight'] - ((source_height_tiles + 1) * json_tiles['tileheight']), +# source_w: json_tiles['tilewidth'], +# source_h: json_tiles['tileheight'] } +#puts hash.inspect + +#Helper.get_tile( +#Components::Sprite.data[thing0.id].set(Helper.get_tile('tileset_Room_Builder_16x16', 936)) +#Components::Sprite.data[thing0.id].set(x: 576, y:280, +# w:128, h: 101) +@thing1 = Entity.new(ID.label, ID.renderable) +Components::Label.data[@thing1.id].set(x: $gtk.args.grid.center[0], y: $gtk.args.grid.center[1], + text: $gtk.args.grid.center.inspect, + size_enum: 5, + alignment_enum: 1) +Components::Renderable.data[@thing1.id].z = 5 + +puts $gtk.args.grid.screen_y_direction.inspect + +@thing2 = Entity.new(ID.label, ID.renderable) +Components::Label.data[@thing2.id].set(x: 640, y: 420, + text: 'It Werks', + size_enum: 5, + alignment_enum: 1) +Components::Renderable.data[@thing2.id].z = 5 + +@map = Entity.new(ID.map, ID.renderable) +Components::Map.data[@map.id].set(x: 100, + y: 100, + tilewidth: 128, + tileheight: 128, + json: Helper.get_json_tiles('map_test_map')) + + +#Helper.get_json_tiles('map_test') +@x = 700 * 10 def tick args - puts60 '---' - ECS::Systems.constants.each do |constant| - ECS::Systems.const_get(constant).run + @x += 1 + @y = (@x / 10).to_i + Components::Label.data[@thing2.id].text = (@y).to_i + Systems.constants.each do |constant| + Systems.const_get(constant).run end - puts60 '---' end |
