diff --git a/.gitignore b/.gitignore index ce9c3c5..0313bb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .env TODO.md +.rspec_status +*.gem diff --git a/.mdlrc b/.mdlrc index 68975e8..e350d3d 100644 --- a/.mdlrc +++ b/.mdlrc @@ -1 +1 @@ -rules "~MD029", "~MD033", "~MD013" \ No newline at end of file +rules "~MD029", "~MD033", "~MD013", "~MD034" diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3be69e2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Change log + +## [master] + +## [0.1.0] + +- Initial release 👾 + +[@fargelus]: https://github.com/fargelus \ No newline at end of file diff --git a/Gemfile b/Gemfile index 946445d..d394f1c 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,7 @@ source 'https://rubygems.org' # Specify your gem's dependencies in space_invaders.gemspec gemspec + +gem 'dotenv' +gem 'mdl' +gem 'rspec', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index f9f98f4..ab9c206 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,18 +2,20 @@ PATH remote: . specs: space_invaders (0.1.0) + gosu + mongo (~> 2.2.5) GEM remote: https://rubygems.org/ specs: - bson (4.8.2) - byebug (11.1.3) - chef-utils (17.9.46) + bson (4.15.0) + chef-utils (17.10.0) concurrent-ruby - concurrent-ruby (1.1.9) - diff-lcs (1.3) - dotenv (2.7.5) - kramdown (2.3.1) + concurrent-ruby (1.1.10) + diff-lcs (1.5.0) + dotenv (2.7.6) + gosu (1.4.3) + kramdown (2.4.0) rexml kramdown-parser-gfm (1.1.0) kramdown (~> 2.0) @@ -26,39 +28,34 @@ GEM mixlib-cli (2.1.8) mixlib-config (3.0.9) tomlrb - mixlib-shellout (3.2.5) + mixlib-shellout (3.2.7) chef-utils mongo (2.2.7) bson (~> 4.0) - rake (13.0.1) rexml (3.2.5) - rspec (3.9.0) - rspec-core (~> 3.9.0) - rspec-expectations (~> 3.9.0) - rspec-mocks (~> 3.9.0) - rspec-core (3.9.1) - rspec-support (~> 3.9.1) - rspec-expectations (3.9.1) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-mocks (3.9.1) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-support (3.9.2) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) tomlrb (2.0.1) PLATFORMS - ruby + arm64-darwin-21 DEPENDENCIES - bundler (~> 2.0) - byebug dotenv mdl - mongo (~> 2.2.5) - rake (~> 13.0) rspec (~> 3.0) space_invaders! BUNDLED WITH - 2.1.4 + 2.3.9 diff --git a/lib/space_invaders/aliens.rb b/lib/space_invaders/aliens.rb index a53d81f..5195176 100644 --- a/lib/space_invaders/aliens.rb +++ b/lib/space_invaders/aliens.rb @@ -109,10 +109,10 @@ def above_enemy_alien closest = find(@enemy.x + @enemy.w) return closest if closest - further_x = last_column_x(@aliens) + further_x = max_x_coord(@aliens) return find(further_x) if further_x < @enemy.x - find(first_column_x(@aliens)) + find(min_x_coord(@aliens)) end def can_move? @@ -146,8 +146,8 @@ def next_move_direction def available_directions extreme_right_aliens = WIDTH - ALIENS_WIDTH - ALIENS_MARGIN { - right: first_column_x(@aliens) < ALIENS_MARGIN, - left: last_column_x(@aliens) > extreme_right_aliens + right: min_x_coord(@aliens) < ALIENS_MARGIN, + left: max_x_coord(@aliens) > extreme_right_aliens } end diff --git a/lib/space_invaders/base/helpers.rb b/lib/space_invaders/base/helpers.rb index f049c90..43b4982 100644 --- a/lib/space_invaders/base/helpers.rb +++ b/lib/space_invaders/base/helpers.rb @@ -5,15 +5,11 @@ module SpaceInvaders module Helpers - def horizontal_center(obj) - Settings::WIDTH / 2 - obj.w / 2 - end - - def last_column_x(collection) + def max_x_coord(collection) collection.max_by(&:x).x end - def first_column_x(collection) + def min_x_coord(collection) collection.min_by(&:x).x end end diff --git a/space_invaders.gemspec b/space_invaders.gemspec index 4acfd53..0e02b32 100644 --- a/space_invaders.gemspec +++ b/space_invaders.gemspec @@ -14,25 +14,18 @@ Gem::Specification.new do |spec| spec.description = 'Space Invaders classic 2d game' spec.homepage = 'https://github.com/fargelus/space-invaders' spec.license = 'MIT' - - spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" + spec.required_ruby_version = '>= 2.6.0' spec.metadata['homepage_uri'] = spec.homepage + spec.metadata['changelog_uri'] = 'https://github.com/fargelus/space-invaders/CHANGELOG.md' # Specify which files should be added to the gem when it is released. - # The `git ls-files -z` loads the files in the RubyGem that have been added into git. - spec.files = Dir.chdir(File.expand_path(__dir__)) do - `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - end - spec.bindir = 'exe' - spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.files = Dir.glob('lib/**/*') + Dir.glob('bin/**/*') + %w[README.md CHANGELOG.md] + spec.bindir = 'bin' + spec.executables = 'space_invaders' spec.require_paths = ['lib'] - spec.add_development_dependency 'bundler', '~> 2.0' - spec.add_development_dependency 'dotenv' - spec.add_development_dependency 'byebug' - spec.add_development_dependency 'mongo', '~> 2.2.5' - spec.add_development_dependency 'rake', '~> 13.0' - spec.add_development_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'mdl' + spec.add_dependency 'gosu' + # TODO Change to SQLite + spec.add_dependency 'mongo', '~> 2.2.5' end diff --git a/spec/base/helpers_spec.rb b/spec/base/helpers_spec.rb new file mode 100644 index 0000000..b16ce34 --- /dev/null +++ b/spec/base/helpers_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'space_invaders/base/helpers' +require 'ostruct' + +RSpec.describe SpaceInvaders::Helpers do + include SpaceInvaders::Helpers + + let(:collection) do + [ + OpenStruct.new(x: 2, y: 35), + OpenStruct.new(x: 50, y: 0), + OpenStruct.new(x: -100, y: 200) + ] + end + + describe '#max_x_coord' do + subject { max_x_coord(collection) } + + it 'returns max x value' do + expect(subject).to eql 50 + end + end + + describe '#min_x_coord' do + subject { min_x_coord(collection) } + + it 'returns min x value' do + expect(subject).to eql(-100) + end + end +end diff --git a/spec/base/image_object_spec.rb b/spec/base/image_object_spec.rb new file mode 100644 index 0000000..ec55e58 --- /dev/null +++ b/spec/base/image_object_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'space_invaders/base/image_object' + +RSpec.describe SpaceInvaders::ImageObject do + let(:image_path) { File.join(Dir.pwd, 'spec', 'fixtures', 'files', 'ruby.png') } + + subject(:image_object) { described_class.new(0, 0, image_path) } + + describe '#set' do + before { image_object.set(100, 200) } + + it 'updates x and y coordinates' do + expect(image_object.x).to eql 100 + expect(image_object.y).to eql 200 + end + end + + describe '#draw' do + before do + allow_any_instance_of(Gosu::Image).to receive(:draw).and_return nil + end + + it 'draws an image at predefined coords' do + expect_any_instance_of(Gosu::Image).to receive(:draw).with(0, 0, 0) + image_object.draw + end + end +end diff --git a/spec/fixtures/files/ruby.png b/spec/fixtures/files/ruby.png new file mode 100644 index 0000000..9d88d4d Binary files /dev/null and b/spec/fixtures/files/ruby.png differ diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c6aed2f..e900f0a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'bundler/setup' -require 'space_invaders' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure diff --git a/spec/space_invaders_spec.rb b/spec/version_spec.rb similarity index 69% rename from spec/space_invaders_spec.rb rename to spec/version_spec.rb index b6b2f27..59946c2 100644 --- a/spec/space_invaders_spec.rb +++ b/spec/version_spec.rb @@ -1,11 +1,9 @@ # frozen_string_literal: true +require 'space_invaders/version' + RSpec.describe SpaceInvaders do it 'has a version number' do expect(SpaceInvaders::VERSION).not_to be nil end - - it 'does something useful' do - expect(false).to eq(true) - end end