ruby2d-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
+ SHA256:
3
+ metadata.gz: 216d93672f8fc45399d56b9711ef610880aee13e398822e6ebfb6817ca721a97
4
+ data.tar.gz: 1d26d7fece366fc146faf77f35d5406b222bbf0721da31984625c87efbfd9c4c
5
+ SHA512:
6
+ metadata.gz: cdd0f54326cd28c52277132bfac8bdf1a4f5cb823867123cc1cd220700b1ae1e94855e516aa70d2564e90d1b1c2564b2705e74cc7f0006fd12ad11d4ec418c5a
7
+ data.tar.gz: 32b96a7d9e2744532ddeae823694f05ab73125605d8c04921208ec6b7599871a2d304f6a74c8e157bf2545131a084975d181c283e13e6b37d6d5b4e3ea4234f7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-02-05
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at mario@mariovisic.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ruby2d-tiled.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby2d-tiled (0.1.0)
5
+ ruby2d (>= 0.11.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.5.0)
11
+ rake (13.0.6)
12
+ rspec (3.10.0)
13
+ rspec-core (~> 3.10.0)
14
+ rspec-expectations (~> 3.10.0)
15
+ rspec-mocks (~> 3.10.0)
16
+ rspec-core (3.10.2)
17
+ rspec-support (~> 3.10.0)
18
+ rspec-expectations (3.10.2)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.10.0)
21
+ rspec-mocks (3.10.3)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-support (3.10.3)
25
+ ruby2d (0.11.1)
26
+
27
+ PLATFORMS
28
+ arm64-darwin-20
29
+
30
+ DEPENDENCIES
31
+ rake (~> 13.0)
32
+ rspec (~> 3.0)
33
+ ruby2d-tiled!
34
+
35
+ BUNDLED WITH
36
+ 2.2.28
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Mario Visic
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Ruby2d::Tiled
2
+
3
+ A library to import LDTK worlds and levels into your [Ruby2D](https://www.ruby2d.com/) applications.
4
+
5
+ ## Installation
6
+
7
+ Install the gem on your comamnd line:
8
+
9
+ $ gem install ruby2d-tiled
10
+
11
+ ## Usage
12
+
13
+ Here is a small example of using the library in your ruby2d application:
14
+
15
+ ```ruby
16
+ require 'ruby2d'
17
+ require 'ruby2d/tiled'
18
+
19
+ world = Ruby2d::Tiled::LDTK.load('/path/to/ldtk/file')
20
+
21
+ world.show
22
+
23
+ show
24
+ ```
25
+
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mariovisic/ruby2d-tiled. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mariovisic/ruby2d-tiled/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Ruby2d::Tiled project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mariovisic/ruby2d-tiled/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :render_test do
9
+ require File.join(__dir__, 'lib/ruby2d/tiled')
10
+
11
+ set width: 1280
12
+ set height: 720
13
+
14
+ world_files = Dir.glob(File.join(__dir__, 'spec/fixtures/*.ldtk'))
15
+
16
+ puts "Press a key to view the next world"
17
+
18
+ on :key_down do |event|
19
+ case event.key
20
+ when 'return'
21
+ world_file = world_files.pop
22
+ if world_file
23
+ Window.clear
24
+ puts "Loading: #{File.basename(world_file)}"
25
+
26
+ @world = Ruby2d::Tiled::LDTK.load(world_file)
27
+ @world.show
28
+ else
29
+ close
30
+ end
31
+ when 'a' then @world.prev_level
32
+ when 'd' then @world.next_level
33
+ end
34
+ end
35
+
36
+ on :key do |event|
37
+ if @world
38
+ case event.key
39
+ when 'w' then @world.scale = @world.scale - 0.02
40
+ when 's' then @world.scale = @world.scale + 0.02
41
+ when 'left' then @world.x_offset = @world.x_offset - 5
42
+ when 'right' then @world.x_offset = @world.x_offset + 5
43
+ when 'up' then @world.y_offset = @world.y_offset - 5
44
+ when 'down' then @world.y_offset = @world.y_offset + 5
45
+ end
46
+ end
47
+ end
48
+
49
+ update do
50
+ if Window.frames % 60 == 0
51
+ puts "FPS: #{Window.fps.round}"
52
+ end
53
+ end
54
+
55
+ show
56
+ end
57
+
58
+ task default: [ :spec, :render_test ]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ruby2d/tiled"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ module Ruby2d
4
+ module Tiled
5
+ class LDTK
6
+ def self.load(ldtk_file)
7
+ ldtk_file_path = File.expand_path(File.dirname(ldtk_file))
8
+ json = JSON.parse(File.read(ldtk_file))
9
+
10
+ layer_data = json['defs']['layers']
11
+ levels = json.delete('levels').map { |level_data| Level.new(level_data, ldtk_file_path, layer_data) }
12
+
13
+ World.new(json, levels)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,101 @@
1
+ require 'securerandom'
2
+
3
+ module Ruby2d
4
+ module Tiled
5
+ class Level
6
+ attr_writer :x_offset, :y_offset, :angle
7
+
8
+ FLIP_MAP = {
9
+ 0 => nil,
10
+ 1 => :horizontal,
11
+ 2 => :vertical,
12
+ 3 => :both,
13
+ }
14
+
15
+ def initialize(data, tileset_relative_path, layer_data)
16
+ @scale = 1
17
+ @x_offset = 0
18
+ @y_offset = 0
19
+ @angle = 0
20
+
21
+ @data = data
22
+ @layer_data = layer_data
23
+ @tileset_relative_path = tileset_relative_path
24
+ @tilesets = {}
25
+
26
+ @layers = @data['layerInstances'].reverse.select do |layer|
27
+ ['Tiles', 'IntGrid', 'AutoLayer'].include?(layer['__type'])
28
+ end
29
+ @ruby2d_objects = []
30
+ end
31
+
32
+ def show
33
+ Window.set(background: @data['__bgColor'])
34
+
35
+ @layers.each_with_index do |layer, z_index|
36
+ grid_size = layer['__gridSize'] * @scale
37
+ grid_tiles = layer['gridTiles'] + layer['autoLayerTiles']
38
+
39
+ if grid_tiles.size > 0
40
+ tileset = @tilesets[layer['__identifier']]
41
+
42
+ if !tileset
43
+ tileset = Ruby2D::Tileset.new(
44
+ File.expand_path(File.join(@tileset_relative_path, layer['__tilesetRelPath'])),
45
+ tile_width: layer['__gridSize'],
46
+ tile_height: layer['__gridSize'],
47
+ scale: @scale,
48
+ z: z_index,
49
+ padding: 0, # FIXME: implement padding
50
+ spacing: 0, # FIXME: implement spacing
51
+ )
52
+
53
+ # FIXME: We use the gridSize and not the width and height, i'm not sure if that's OK
54
+ # Will need to fix this later
55
+ (grid_tiles.uniq { |tile| tile.slice('src', 'f') }).each do |tile|
56
+ tileset.define_tile("#{tile['t']}#{tile['f']}", tile['src'][0] / layer['__gridSize'], tile['src'][1] / layer['__gridSize'], flip: FLIP_MAP[tile['f']])
57
+ end
58
+
59
+ @tilesets[layer['__identifier']] = tileset
60
+ end
61
+
62
+ grid_tiles.each do |tile|
63
+ tileset.set_tile("#{tile['t']}#{tile['f']}", [{ x: (tile['px'][0] * @scale) + @x_offset, y: (tile['px'][1] * @scale) + @y_offset }])
64
+ end
65
+ elsif layer['intGridCsv'].size > 0
66
+ layer_data = @layer_data.detect { |layer_data| layer_data['uid'] == layer['layerDefUid'] }
67
+
68
+ layer['intGridCsv'].each_with_index do |int_grid_value, index|
69
+ if int_grid_value != 0
70
+ color = (layer_data['intGridValues'].detect { |value| value['value'] == int_grid_value })['color']
71
+
72
+ # FIXME: Need to support offsers here :)
73
+ x = (((index % layer['__cWid'])) * grid_size) + @x_offset
74
+ y = (((index + 1).to_i / layer['__cWid'].to_i) * grid_size) + @y_offset
75
+
76
+ @ruby2d_objects.push(Ruby2D::Square.new(
77
+ x: x,
78
+ y: y,
79
+ z: z_index,
80
+ size: grid_size,
81
+ color: color
82
+ ))
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def scale=(scale)
90
+ @scale = scale
91
+ @tilesets.each_value { |tileset| tileset.instance_variable_set('@scale', scale) }
92
+ end
93
+
94
+ def clear
95
+ @ruby2d_objects.each(&:remove)
96
+ @tilesets.each_value(&:clear_tiles)
97
+ @ruby2d_objects = []
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby2d
4
+ module Tiled
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+
3
+ module Ruby2d
4
+ module Tiled
5
+ class World
6
+ attr_reader :levels, :scale, :x_offset, :y_offset
7
+
8
+ def initialize(data, levels)
9
+ @data = data
10
+ @levels = levels
11
+ @current_level = 0
12
+ @scale = 1
13
+ @x_offset = 0
14
+ @y_offset = 0
15
+ @angle = 0
16
+ end
17
+
18
+ def show
19
+ clear
20
+ @levels[@current_level].scale = @scale
21
+ @levels[@current_level].x_offset = @x_offset
22
+ @levels[@current_level].y_offset = @y_offset
23
+ @levels[@current_level].show
24
+ end
25
+
26
+ def clear
27
+ @levels[@current_level].clear
28
+ end
29
+
30
+ def next_level
31
+ if @levels.size > @current_level + 1
32
+ clear
33
+ @current_level += 1
34
+ show
35
+ end
36
+ end
37
+
38
+ def prev_level
39
+ if @current_level > 0
40
+ clear
41
+ @current_level -= 1
42
+ show
43
+ end
44
+ end
45
+
46
+ def scale=(scale)
47
+ @scale = scale
48
+ show
49
+ end
50
+
51
+ def x_offset=(x_offset)
52
+ @x_offset = x_offset
53
+ show
54
+ end
55
+
56
+ def y_offset=(y_offset)
57
+ @y_offset = y_offset
58
+ show
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push(__dir__)
4
+
5
+ require 'ruby2d'
6
+
7
+ require "tiled/ldtk"
8
+ require "tiled/level"
9
+ require "tiled/world"
10
+ require "tiled/version"
11
+
12
+ module Ruby2d
13
+ module Tiled
14
+ class Error < StandardError; end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby2d-tiled
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mario Visic
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby2d
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.1
27
+ description: Load your worlds and levels into ruby2d using this convinent gem which
28
+ reads files written by the LDTK editor.
29
+ email:
30
+ - mario@mariovisic.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".ruby-version"
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - lib/ruby2d/tiled.rb
47
+ - lib/ruby2d/tiled/ldtk.rb
48
+ - lib/ruby2d/tiled/level.rb
49
+ - lib/ruby2d/tiled/version.rb
50
+ - lib/ruby2d/tiled/world.rb
51
+ homepage: https://github.com/mariovisic/ruby2d-tiled
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/mariovisic/ruby2d-tiled
56
+ source_code_uri: https://github.com/mariovisic/ruby2d-tiled
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.7.4
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.2.31
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: A 2d level loader for Ruby2D, compatible with LDTK files
76
+ test_files: []