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/helpers/00_tileset.rb | |
| parent | 120b693ffd02bc5c7f41ff2b9657facc7117daae (diff) | |
| download | typemon-code-490b20123c75cf8eb5de7039993cb704e3d7a0c8.tar.gz typemon-code-490b20123c75cf8eb5de7039993cb704e3d7a0c8.zip | |
working json to sprite conversion
Diffstat (limited to 'app/helpers/00_tileset.rb')
| -rw-r--r-- | app/helpers/00_tileset.rb | 42 |
1 files changed, 42 insertions, 0 deletions
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 |
