gosu_tiled 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00bc74fe8d1bdea3996a255a4d88eda976f3ef4e
4
+ data.tar.gz: 7aa04c20004d37cd961689b8845490e10d986364
5
+ SHA512:
6
+ metadata.gz: d5c787058770680df7741054d66ce9be7b0ed910076ad5af568a3ee50e934aac634b9e25b6baa22e4403493d39f1a400c7017ef7227f0e1dcec88c3e797f1a27
7
+ data.tar.gz: c53327e4653a6c3c65cde33e923f03b0df497b75379e9c955e509e627101e768becdde13ea1448d3017f7888242a22a7753118953378bd749f46d595d94a39af
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --order random
2
+ --color
3
+ --format doc
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,7 @@
1
+ Before you make a pull request:
2
+
3
+ - Make sure your code is formatted using [this ruby style guide](https://github.com/bbatsov/ruby-style-guide)
4
+ - Trim all trailing whitespace
5
+ - Run `rspec` to see if all old tests pass
6
+ - Add new tests for your code
7
+ - Consult [betterspecs.org](http://betterspecs.org/) when writing new tests
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gosu_tiled.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, cmd: 'rspec --color --format doc' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tomas Varaneckas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tomas Varaneckas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # gosu-tiled
2
+
3
+ [Tiled](http://www.mapeditor.org/) map loader for [Gosu](http://www.libgosu.org) game development library.
4
+
5
+ # How to use it?
6
+
7
+ First, download [Tiled](http://www.mapeditor.org/) and create yourself a map.
8
+
9
+ ![Tiled Map](https://raw.githubusercontent.com/spajus/gosu-tiled/master/examples/screenshots/tiled.png)
10
+
11
+ Then export it as JSON and use with Gosu like this:
12
+
13
+ ```ruby
14
+ require 'gosu'
15
+ require 'gosu_tiled'
16
+
17
+ class GameWindow < Gosu::Window
18
+ def initialize
19
+ super(800, 600, false)
20
+ @map = Gosu::Tiled.load_json(self, 'path/to/map.json')
21
+ @x = @y = 0
22
+ end
23
+
24
+ def update
25
+ @factor = 3
26
+ @x -= @factor if button_down?(Gosu::KbLeft)
27
+ @x += @factor if button_down?(Gosu::KbRight)
28
+ @y -= @factor if button_down?(Gosu::KbUp)
29
+ @y += @factor if button_down?(Gosu::KbDown)
30
+ end
31
+
32
+ def draw
33
+ @map.draw(@x, @y)
34
+ end
35
+ end
36
+
37
+ GameWindow.new.show
38
+ ```
39
+
40
+ Run it and enjoy your game map:
41
+
42
+ ![Gosu Game](https://raw.githubusercontent.com/spajus/gosu-tiled/master/examples/screenshots/gosu_tiled.gif)
43
+
44
+ See [full example code](https://github.com/spajus/gosu-tiled/blob/master/examples/panorama.rb).
45
+
46
+ ## Contributing
47
+
48
+ 0. Read [CONTRIBUTING.md](https://github.com/spajus/gosu-tiled/blob/master/CONTRIBUTING.md)
49
+ 1. Fork it ( https://github.com/spajus/gosu-tiled/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,47 @@
1
+ ROOT_DIR = File.dirname(File.dirname(__FILE__))
2
+
3
+ # Add ../lib to load path
4
+ $:.unshift File.join(ROOT_DIR, 'lib')
5
+
6
+ require 'gosu'
7
+ require 'gosu_tiled'
8
+
9
+ class GameWindow < Gosu::Window
10
+ WIDTH = 800
11
+ HEIGHT = 600
12
+ def initialize
13
+ super(WIDTH, HEIGHT, false)
14
+ json_path = File.join(ROOT_DIR, 'spec', 'files', 'tiled_map.json')
15
+ @map = Gosu::Tiled.load_json(self, json_path)
16
+ @x = @y = 0
17
+ end
18
+
19
+ def update
20
+ @factor = 3
21
+ @x -= @factor if button_down?(Gosu::KbLeft)
22
+ @x += @factor if button_down?(Gosu::KbRight)
23
+ @y -= @factor if button_down?(Gosu::KbUp)
24
+ @y += @factor if button_down?(Gosu::KbDown)
25
+ self.caption = "#{Gosu.fps} FPS. Mem: #{memory_usage} KB. Loc: [#{@x}:#{@y}]. Use arrow keys"
26
+ end
27
+
28
+ def button_down(id)
29
+ close if id == Gosu::KbEscape
30
+ end
31
+
32
+ def draw
33
+ @map.draw(@x, @y)
34
+ end
35
+
36
+ private
37
+
38
+ def memory_usage
39
+ `ps -o rss= -p #{Process.pid}`
40
+ .chomp.gsub(/(\d)(?=(\d{3})+(\..*)?$)/,'\1,') + ' KB'
41
+ rescue
42
+ "Unavailable. Using Windows?"
43
+ end
44
+ end
45
+
46
+ GameWindow.new.show
47
+
Binary file
Binary file
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gosu_tiled/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gosu_tiled'
8
+ spec.version = Gosu::Tiled::VERSION
9
+ spec.authors = ['Tomas Varaneckas']
10
+ spec.email = ['tomas.varaneckas@gmail.com']
11
+ spec.summary = %q{Tiled map editor integration for Gosu game engine}
12
+ spec.description = %q{Makes it easy to load tile maps built with Tiled editor into Gosu games}
13
+ spec.homepage = 'https://github.com/spajus/gosu-tiled'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'gosu'
22
+ spec.add_dependency 'json'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ end
@@ -0,0 +1,8 @@
1
+ module Gosu
2
+ module Tiled
3
+ class EmptyTile
4
+ def draw(*args)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,110 @@
1
+ module Gosu
2
+ module Tiled
3
+ class Layer
4
+ def initialize(window, data, options)
5
+ @window = window
6
+ @data = data
7
+ @options = options
8
+ end
9
+
10
+ def visible?
11
+ @data['visible']
12
+ end
13
+
14
+ def type
15
+ @data['type']
16
+ end
17
+
18
+ def draw(x, y, tilesets)
19
+ if type == 'tilelayer'
20
+ draw_tiles(x, y, tilesets)
21
+ elsif type == 'objectgroup'
22
+ draw_objects(x, y, tilesets)
23
+ end
24
+ end
25
+
26
+ def screen_width_in_tiles
27
+ (@window.width / tile_width.to_f).ceil
28
+ end
29
+
30
+ def screen_height_in_tiles
31
+ (@window.height / tile_height.to_f).ceil
32
+ end
33
+
34
+ private
35
+
36
+ def offset_x_in_tiles(x)
37
+ x / tile_width
38
+ end
39
+
40
+ def offset_y_in_tiles(y)
41
+ y / tile_height
42
+ end
43
+
44
+ def draw_tiles(x, y, tilesets)
45
+ off_x = offset_x_in_tiles(x)
46
+ off_y = offset_y_in_tiles(y)
47
+ tile_range_x = (off_x..screen_width_in_tiles + off_x)
48
+ tile_range_y = (off_y..screen_height_in_tiles + off_y)
49
+ tile_range_x.each do |xx|
50
+ tile_range_y.each do |yy|
51
+ target_x = transpose_tile_x(xx, x)
52
+ target_y = transpose_tile_y(yy, y)
53
+ if within_map_range(x + target_x, y + target_y)
54
+ tilesets.get(tile_at(xx, yy)).draw(target_x, target_y, 0)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def within_map_range(x, y)
61
+ (0..map_width - 1).include?(x) && (0..map_height - 1).include?(y)
62
+ end
63
+
64
+ def within_screen_range(x, y)
65
+ range_x = (x - tile_width..@window.width + x + tile_width)
66
+ range_y = (y..@window.height + y + tile_height)
67
+ range_x.include?(x) && range_y.include?(y)
68
+ end
69
+
70
+ def transpose_tile_x(x, off_x)
71
+ x * tile_width - off_x
72
+ end
73
+
74
+ def transpose_tile_y(y, off_y)
75
+ y * tile_height - off_y
76
+ end
77
+
78
+ def draw_objects(x, y, tilesets)
79
+ @data['objects'].each do |obj|
80
+ obj_x = obj['x']
81
+ obj_y = obj['y']
82
+ if within_screen_range(obj_x, obj_y)
83
+ tilesets.get(obj['gid']).draw(obj_x - x, obj_y - y - tile_height, 10)
84
+ end
85
+ end
86
+ end
87
+
88
+ def tile_at(x, y)
89
+ @data['data'][y * @data['width'] + x]
90
+ end
91
+
92
+ def tile_width
93
+ @options[:tile_width]
94
+ end
95
+
96
+ def tile_height
97
+ @options[:tile_height]
98
+ end
99
+
100
+ def map_width
101
+ @options[:width]
102
+ end
103
+
104
+ def map_height
105
+ @options[:height]
106
+ end
107
+
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,35 @@
1
+ module Gosu
2
+ module Tiled
3
+ class Layers
4
+ include Enumerable
5
+ def initialize(window, data, options)
6
+ @window = window
7
+ @layers = data.map do |layer|
8
+ Layer.new(window, layer, options)
9
+ end
10
+ end
11
+
12
+ def tile
13
+ @layers.select { |l| l.type == 'tilelayer' }.select(&:visible?)
14
+ end
15
+
16
+ def object
17
+ @layers.select { |l| l.type == 'objectgroup' }.select(&:visible?)
18
+ end
19
+
20
+ def size
21
+ @layers.size
22
+ end
23
+
24
+ def each(&block)
25
+ @layers.each do |layer|
26
+ if block_given?
27
+ block.call(layer)
28
+ else
29
+ yield layer
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module Gosu
2
+ module Tiled
3
+ class Map
4
+ attr_reader :tilesets, :layers, :width, :height
5
+
6
+ def initialize(window, data, data_dir)
7
+ @window = window
8
+ @data = data
9
+ @data_dir = data_dir
10
+ @width = data['width'] * data['tilewidth']
11
+ @height = data['height'] * data['tileheight']
12
+ @tilesets = Tilesets.new(window, data['tilesets'], data_dir)
13
+ @layers = Layers.new(window,
14
+ data['layers'],
15
+ width: @width,
16
+ height: @height,
17
+ tile_width: data['tilewidth'],
18
+ tile_height: data['tileheight'])
19
+ end
20
+
21
+ def draw(offset_x, offset_y)
22
+ @layers.each do |layer|
23
+ layer.draw(offset_x, offset_y, tilesets)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ module Gosu
2
+ module Tiled
3
+ class Tilesets
4
+ def initialize(window, data, data_dir)
5
+ @root_dir = data_dir
6
+ @window = window
7
+ @data = data
8
+ @tilesets = {}
9
+ @data.each do |t|
10
+ tileset = Gosu::Image.load_tiles(
11
+ @window, File.join(data_dir, t['image']), t['tilewidth'], t['tileheight'], true)
12
+ @tilesets[t['firstgid']] = {
13
+ 'data' => t,
14
+ 'tiles' => tileset
15
+ }
16
+ end
17
+ end
18
+
19
+ def size
20
+ @data.size
21
+ end
22
+
23
+ def get(index)
24
+ return empty_tile if index == 0 || index.nil?
25
+ key = closest_key(index)
26
+ @tilesets[key]['tiles'][index - key]
27
+ end
28
+
29
+ def properties(index)
30
+ return {} if index == 0
31
+ key = closest_key(index)
32
+ props = @tilesets[key]['data']['tileproperties']
33
+ return {} unless props
34
+ props[(index - key).to_s]
35
+ end
36
+
37
+ private
38
+
39
+ def empty_tile
40
+ @empty_tile ||= EmptyTile.new
41
+ end
42
+
43
+ def closest_key(index)
44
+ @tilesets.keys.select { |k| k <= index }.max
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Gosu
2
+ module Tiled
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
data/lib/gosu_tiled.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'gosu'
2
+ require 'json'
3
+ require 'gosu_tiled/version'
4
+ require 'gosu_tiled/empty_tile'
5
+ require 'gosu_tiled/tilesets'
6
+ require 'gosu_tiled/layer'
7
+ require 'gosu_tiled/layers'
8
+ require 'gosu_tiled/map'
9
+
10
+ module Gosu
11
+ module Tiled
12
+ def self.load_json(window, json)
13
+ Map.new(window, JSON.load(File.open(json)), File.dirname(json))
14
+ end
15
+ end
16
+ end
Binary file