diff options
| author | realtradam <[email protected]> | 2021-05-19 02:23:17 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-05-19 02:23:17 -0400 |
| commit | 490b20123c75cf8eb5de7039993cb704e3d7a0c8 (patch) | |
| tree | 302caf2be626efd0ac14baed36b8201bc49d0795 /app | |
| parent | 120b693ffd02bc5c7f41ff2b9657facc7117daae (diff) | |
| download | typemon-code-490b20123c75cf8eb5de7039993cb704e3d7a0c8.tar.gz typemon-code-490b20123c75cf8eb5de7039993cb704e3d7a0c8.zip | |
working json to sprite conversion
Diffstat (limited to 'app')
| -rw-r--r-- | app/.gitignore | 2 | ||||
| -rw-r--r-- | app/ECS/components/00_renderable.rb | 8 | ||||
| -rw-r--r-- | app/ECS/components/02_label.rb | 22 | ||||
| -rw-r--r-- | app/ECS/components/03_player_control.rb | 23 | ||||
| -rw-r--r-- | app/ECS/components/04_map.rb | 21 | ||||
| -rw-r--r-- | app/ECS/components/05_map_object.rb | 16 | ||||
| -rw-r--r-- | app/ECS/components/06_grid_singleton.rb | 16 | ||||
| -rw-r--r-- | app/ECS/signatures.rb | 18 | ||||
| -rw-r--r-- | app/ECS/systems/00_player.rb | 4 | ||||
| -rw-r--r-- | app/ECS/systems/99_render.rb | 19 | ||||
| -rw-r--r-- | app/helpers/00_tileset.rb | 42 | ||||
| -rw-r--r-- | app/test.json | 49 | ||||
| -rw-r--r-- | app/tick.rb | 23 |
13 files changed, 216 insertions, 47 deletions
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 <<self def renderable - @renderable ||= '0_001'.to_i(2) + @renderable ||= 0b0_001 #'0_001'.to_i(2) end def sprite - @sprite ||= '0_010'.to_i(2) + @sprite ||= 0b0_010 #'0_010'.to_i(2) end def label - @label ||= '0_100'.to_i(2) + @label ||= 0b0_100 #'0_100'.to_i(2) end def player_control - @player_control ||= '0_001_000'.to_i(2) + @player_control ||= 0b0_001_000 #'0_001_000'.to_i(2) end def map - @map ||= '0_010_000'.to_i(2) + @map ||= 0b0_010_000 #'0_010_000'.to_i(2) end - def map - @map ||= '0_100_000'.to_i(2) + def map_object + @map_object ||= 0b0_100_000 + end + + def grid_singleton + @grid_singleton ||= 0b0_001_000_000 end end end diff --git a/app/ECS/systems/00_player.rb b/app/ECS/systems/00_player.rb index 9b03def..0a0492a 100644 --- a/app/ECS/systems/00_player.rb +++ b/app/ECS/systems/00_player.rb @@ -3,8 +3,8 @@ class Systems 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].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 diff --git a/app/ECS/systems/99_render.rb b/app/ECS/systems/99_render.rb index 37b562a..a2f697b 100644 --- a/app/ECS/systems/99_render.rb +++ b/app/ECS/systems/99_render.rb @@ -3,24 +3,27 @@ class Systems 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 + 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) > 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 <<self + attr_accessor :json_data + + def get_json_tiles(json_name) + if self.json_data[json_name].nil? + self.json_data[json_name] = $gtk.parse_json_file "assets/json/#{json_name}.json" + raise Exception.new "#{json_name} is null and not loaded. Cannot get json tile" if self.json_data[json_name].nil? + + #puts self.json_data[json_name].inspect + #puts json_name + if self.json_data[json_name]['type'] == 'map' #json_name.split("_").first == 'map' + self.json_data[json_name]['tilesets'].each do |tileset| + tileset = Helper.get_json_tiles(tileset['source'].split('/').last.delete_suffix('.tsx')) + # download tileset here + # $gtk.args.gtk.http_get 'https://mysite.net/#{tileset['name']}.png' + end + end + end + self.json_data[json_name] + end + + def get_tile(json_name:, tile_index:) + json_tiles = self.get_json_tiles(json_name) + return puts "Error, json file not a tileset" unless json_tiles['type'] == 'tileset' + return tile_index - json_tiles['tilecount'] if tile_index > 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') |
