From fee80f42f0889f2d484e25f4366f14b68c65ba70 Mon Sep 17 00:00:00 2001 From: realtradam Date: Wed, 19 May 2021 05:42:53 -0400 Subject: fully working map import --- app/ECS/base_component.rb | 1 - app/ECS/components/00_renderable.rb | 2 +- app/ECS/components/05_interactable.rb | 16 ++++++++++++++++ app/ECS/components/05_map_object.rb | 16 ---------------- app/ECS/components/06_grid_singleton.rb | 16 ---------------- app/ECS/components/06_level.rb | 25 ++++++++++++++++++++++++ app/ECS/components/07_collidable.rb | 22 +++++++++++++++++++++ app/ECS/components/debug_singleton.rb | 13 +++++++++++++ app/ECS/signatures.rb | 22 ++++++++++++--------- app/ECS/systems/00_player.rb | 8 ++++---- app/ECS/systems/99_render.rb | 15 ++++++++------- app/helpers/00_tileset.rb | 15 ++++++++++----- app/tick.rb | 28 +++++++++++++-------------- assets/hitbox.png | Bin 0 -> 5178 bytes assets/json/hitbox.json | 14 ++++++++++++++ assets/json/map_test2.json | 33 +++++++++++++++++++++++++++++++- 16 files changed, 172 insertions(+), 74 deletions(-) create mode 100644 app/ECS/components/05_interactable.rb delete mode 100644 app/ECS/components/05_map_object.rb delete mode 100644 app/ECS/components/06_grid_singleton.rb create mode 100644 app/ECS/components/06_level.rb create mode 100644 app/ECS/components/07_collidable.rb create mode 100644 app/ECS/components/debug_singleton.rb create mode 100644 assets/hitbox.png create mode 100644 assets/json/hitbox.json diff --git a/app/ECS/base_component.rb b/app/ECS/base_component.rb index e374540..f32322b 100644 --- a/app/ECS/base_component.rb +++ b/app/ECS/base_component.rb @@ -1,7 +1,6 @@ class BaseComponent class < layer['chunks'].count + raise Exception.new "#{Components::Map.data[key].json['json_name']} not valid map, exceeded tile range" if (iter += 1) >= Components::Map.data[key].json['tilesets'].count + end + unless tile.empty? + 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 + $gtk.args.outputs.sprites << tile 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 << tile end end end diff --git a/app/helpers/00_tileset.rb b/app/helpers/00_tileset.rb index d6e6cf3..d64006d 100644 --- a/app/helpers/00_tileset.rb +++ b/app/helpers/00_tileset.rb @@ -7,12 +7,12 @@ class Helper attr_accessor :json_data def get_json_tiles(json_name) + return nil if json_name == 'hitbox' && !Components::DebugSingleton.data + 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')) @@ -25,16 +25,21 @@ class Helper end def get_tile(json_name:, tile_index:) + if json_name == 'hitbox' && !Components::DebugSingleton.data + return tile_index - 1 if tile_index > 1 + return {} + end + json_tiles = self.get_json_tiles(json_name) - return puts "Error, json file not a tileset" unless json_tiles['type'] == 'tileset' + raise Exception.new "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_x: [((tile_index % json_tiles['columns']) - 1) * json_tiles['tilewidth'], 0].max, # source_y gets special treatment - source_y: json_tiles['imageheight'] - ((source_height_tiles + 1) * json_tiles['tileheight']), + source_y: [json_tiles['imageheight'] - ((source_height_tiles + 1) * json_tiles['tileheight']), 0].max, source_w: json_tiles['tilewidth'], source_h: json_tiles['tileheight'] } end diff --git a/app/tick.rb b/app/tick.rb index 41c95aa..d1b95a7 100644 --- a/app/tick.rb +++ b/app/tick.rb @@ -1,10 +1,12 @@ $gtk.args.grid.origin_top_left! #$gtk.args.grid.origin_bottom_left! +#Components::DebugSingleton.data = true 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') + ID.renderable, + ID.player_control) +Components::Sprite.data[thing0.id].set(x: 256, y: 128, w: 64, h: 64, path: 'dragonruby.png') +Components::Renderable.data[thing0.id].z = 10 #Components::PlayerControl.data[thing0.id].set(north: 's', south: 'w') #@thing0 = Entity.new(ID.sprite, @@ -35,16 +37,14 @@ Components::Sprite.data[thing0.id].set(x: 0, y: 0, w: 128, h: 101, path: 'dragon #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 +#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 @thing2 = Entity.new(ID.label, ID.renderable) -Components::Label.data[@thing2.id].set(x: 640, y: 420, +Components::Label.data[@thing2.id].set(x: 740, y: 520, text: 'It Werks', size_enum: 5, alignment_enum: 1) @@ -67,11 +67,11 @@ Components::Renderable.data[@map2.id].z = 4 #Helper.get_json_tiles('map_test') -@x = 700 * 10 +@x = 0 def tick args @x += 1 - @y = (@x / 10).to_i - Components::Label.data[@thing2.id].text = (@y).to_i + @y = @x / 6 + Components::Label.data[@thing2.id].text = "Seconds Passed: #{(@y.to_i).to_s.insert(-2,'.')}" Systems.constants.each do |constant| Systems.const_get(constant).run end diff --git a/assets/hitbox.png b/assets/hitbox.png new file mode 100644 index 0000000..c06b1f4 Binary files /dev/null and b/assets/hitbox.png differ diff --git a/assets/json/hitbox.json b/assets/json/hitbox.json new file mode 100644 index 0000000..a1b7938 --- /dev/null +++ b/assets/json/hitbox.json @@ -0,0 +1,14 @@ +{ "columns":1, + "image":"..\/..\/Documents\/Ruby\/dragon\/dragonruby-linux-amd64\/mygame\/assets\/hitbox.png", + "imageheight":16, + "imagewidth":16, + "margin":0, + "name":"hitbox", + "spacing":0, + "tilecount":1, + "tiledversion":"1.6.0", + "tileheight":16, + "tilewidth":16, + "type":"tileset", + "version":"1.6" +} \ No newline at end of file diff --git a/assets/json/map_test2.json b/assets/json/map_test2.json index ef9c4a1..33640e5 100644 --- a/assets/json/map_test2.json +++ b/assets/json/map_test2.json @@ -43,8 +43,35 @@ "width":16, "x":0, "y":0 + }, + { + "chunks":[ + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 0, 0, 0, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 5048, 0, 5048, 0, 5048, 5048, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 5048, 0, 0, 0, 5048, 5048, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 0, 0, 5048, 0, 0, 0, 0, 0, 0, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 5048, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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":3, + "name":"Tile Layer 3", + "opacity":1, + "properties":[ + { + "name":"Hitbox", + "type":"bool", + "value":true + }], + "startx":0, + "starty":0, + "type":"tilelayer", + "visible":true, + "width":16, + "x":0, + "y":0 }], - "nextlayerid":3, + "nextlayerid":4, "nextobjectid":1, "orientation":"orthogonal", "renderorder":"right-down", @@ -58,6 +85,10 @@ { "firstgid":4088, "source":"..\/tileset_1_Generic_16x16.tsx" + }, + { + "firstgid":5048, + "source":"..\/hitbox.tsx" }], "tilewidth":16, "type":"map", -- cgit v1.2.3