From 490b20123c75cf8eb5de7039993cb704e3d7a0c8 Mon Sep 17 00:00:00 2001 From: realtradam Date: Wed, 19 May 2021 02:23:17 -0400 Subject: working json to sprite conversion --- app/.gitignore | 2 ++ app/ECS/components/00_renderable.rb | 8 +++++- app/ECS/components/02_label.rb | 22 +++++++-------- app/ECS/components/03_player_control.rb | 23 ++++++++-------- app/ECS/components/04_map.rb | 21 ++++++++++++++ app/ECS/components/05_map_object.rb | 16 +++++++++++ app/ECS/components/06_grid_singleton.rb | 16 +++++++++++ app/ECS/signatures.rb | 18 +++++++----- app/ECS/systems/00_player.rb | 4 +-- app/ECS/systems/99_render.rb | 19 +++++++------ app/helpers/00_tileset.rb | 42 ++++++++++++++++++++++++++++ app/test.json | 49 +++++++++++++++++++++++++++++++++ app/tick.rb | 23 +++++++++++----- 13 files changed, 216 insertions(+), 47 deletions(-) create mode 100644 app/.gitignore create mode 100644 app/ECS/components/04_map.rb create mode 100644 app/ECS/components/05_map_object.rb create mode 100644 app/ECS/components/06_grid_singleton.rb create mode 100644 app/helpers/00_tileset.rb create mode 100644 app/test.json (limited to 'app') diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..52512c5 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,2 @@ +dragonruby-game-toolkit-contrib +dragonruby-game-toolkit-contrib/** diff --git a/app/ECS/components/00_renderable.rb b/app/ECS/components/00_renderable.rb index 18017cc..270e5f9 100644 --- a/app/ECS/components/00_renderable.rb +++ b/app/ECS/components/00_renderable.rb @@ -3,8 +3,14 @@ class Components class Renderable < BaseComponent attr_accessor :z - def initialize(z: 0) + 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/02_label.rb b/app/ECS/components/02_label.rb index 2483d57..aed00ed 100644 --- a/app/ECS/components/02_label.rb +++ b/app/ECS/components/02_label.rb @@ -7,17 +7,17 @@ class Components 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] + {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 diff --git a/app/ECS/components/03_player_control.rb b/app/ECS/components/03_player_control.rb index 22bc458..58d0102 100644 --- a/app/ECS/components/03_player_control.rb +++ b/app/ECS/components/03_player_control.rb @@ -1,20 +1,21 @@ 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, :interact, :menu - def initialize(north: 'w', south: 's', east: 'd', west: 'a') - @north = north - @south = south - @east = east - @west = west + def initialize + @north = 'w' + @south = 's' + @east = 'd' + @west = 'a' + @interact = 'space' + @menu = 'enter' end - def set(north: 'w', south: 's', east: 'd', west: 'a') - @north = north - @south = south - @east = east - @west = west + 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 new file mode 100644 index 0000000..804fc02 --- /dev/null +++ b/app/ECS/components/04_map.rb @@ -0,0 +1,21 @@ +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_map_object.rb b/app/ECS/components/05_map_object.rb new file mode 100644 index 0000000..270e5f9 --- /dev/null +++ b/app/ECS/components/05_map_object.rb @@ -0,0 +1,16 @@ +class Components + # If an entity can be rendered on screen + class Renderable < 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_grid_singleton.rb b/app/ECS/components/06_grid_singleton.rb new file mode 100644 index 0000000..392d770 --- /dev/null +++ b/app/ECS/components/06_grid_singleton.rb @@ -0,0 +1,16 @@ +class Components + # If an entity can be rendered on screen + class GridSingleton < 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/signatures.rb b/app/ECS/signatures.rb index 844c66d..d3ac390 100644 --- a/app/ECS/signatures.rb +++ b/app/ECS/signatures.rb @@ -1,27 +1,31 @@ class ID class < layer['chunks'].count + end + 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 #puts60 temp.inspect - $gtk.args.outputs.sprites << temp + $gtk.args.outputs.sprites << tile end end end diff --git a/app/helpers/00_tileset.rb b/app/helpers/00_tileset.rb new file mode 100644 index 0000000..d6e6cf3 --- /dev/null +++ b/app/helpers/00_tileset.rb @@ -0,0 +1,42 @@ +class Helper + # Returns a loaded map and its dependecies(images,json) + # If any are missing then it will load them from files + + @json_data = {} + class < json_tiles['tilecount'] + source_height_tiles = (tile_index.to_i / json_tiles['columns'].to_i).to_i# * json_tiles['tileheight'] + { w: json_tiles['tilewidth'], + h: json_tiles['tileheight'], + path: json_tiles['image'].split('mygame/').last.delete('\\'), + source_x: ((tile_index % json_tiles['columns']) - 1) * json_tiles['tilewidth'], + # source_y gets special treatment + source_y: json_tiles['imageheight'] - ((source_height_tiles + 1) * json_tiles['tileheight']), + source_w: json_tiles['tilewidth'], + source_h: json_tiles['tileheight'] } + end + end +end diff --git a/app/test.json b/app/test.json new file mode 100644 index 0000000..c9ca56a --- /dev/null +++ b/app/test.json @@ -0,0 +1,49 @@ +{ "compressionlevel":-1, + "editorsettings": + { + "export": + { + "format":"json", + "target":"room.json" + } + }, + "height":2, + "infinite":true, + "layers":[ + { + "chunks":[ + { + "data":[973, 974, 975, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1040, 1041, 1042, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":16, + "width":16, + "x":0, + "y":0 + }], + "height":16, + "id":1, + "name":"Floor", + "opacity":1, + "startx":0, + "starty":0, + "type":"tilelayer", + "visible":true, + "width":16, + "x":0, + "y":0 + }], + "nextlayerid":2, + "nextobjectid":1, + "orientation":"orthogonal", + "renderorder":"right-up", + "tiledversion":"1.6.0", + "tileheight":16, + "tilesets":[ + { + "firstgid":1, + "source":"Room_Builder_16x16.tsx" + }], + "tilewidth":16, + "type":"map", + "version":"1.6", + "width":3 +} diff --git a/app/tick.rb b/app/tick.rb index 0d07d73..41c95aa 100644 --- a/app/tick.rb +++ b/app/tick.rb @@ -1,10 +1,11 @@ $gtk.args.grid.origin_top_left! -$gtk.args.grid.origin_bottom_left! +#$gtk.args.grid.origin_bottom_left! 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') +#Components::PlayerControl.data[thing0.id].set(north: 's', south: 'w') #@thing0 = Entity.new(ID.sprite, # ID.renderable, @@ -49,12 +50,20 @@ Components::Label.data[@thing2.id].set(x: 640, y: 420, 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')) +#@map = Entity.new(ID.map, ID.renderable) +#Components::Map.data[@map.id].set(x: 0, +# y: 0, +# tilewidth: 128, +# tileheight: 128, +# json_name: 'map_test_map') + +@map2 = Entity.new(ID.map, ID.renderable) +Components::Map.data[@map2.id].set(x: 0, + y: 0, + tilewidth: 64, + tileheight: 64, + json_name: 'map_test2') +Components::Renderable.data[@map2.id].z = 4 #Helper.get_json_tiles('map_test') -- cgit v1.2.3