-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up location modules into separate files and improve passible logic
- Loading branch information
David Henry
committed
Feb 13, 2012
1 parent
7470530
commit a8388af
Showing
11 changed files
with
174 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Game | ||
class Location | ||
class Empty | ||
include Game::Modules::ObjectManagement | ||
include Game::Location::Base | ||
include Game::Location::Passible | ||
include Game::Location::Movement | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Game | ||
class Location | ||
module Movement | ||
def at(direction) | ||
tile = Game::Location.at(@x - 1, @y) if direction == :up | ||
tile = Game::Location.at(@x + 1, @y) if direction == :down | ||
tile = Game::Location.at(@x, @y - 1) if direction == :left | ||
tile = Game::Location.at(@x, @y + 1) if direction == :right | ||
|
||
tile.try(:end_point) || Game::Location::Edge.instance | ||
end | ||
|
||
def end_point | ||
if has_object?(Game::Object::LocationModifier) | ||
Game::Location.at(*get_object(Game::Object::LocationModifier).end_point) | ||
else | ||
self | ||
end | ||
end | ||
private :end_point | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Game | ||
class Location | ||
module Passible | ||
def passible?(player_objects) | ||
objects.all?(&:passible?) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
describe Game::Location::Movement do | ||
subject { Game::Location.build(0, 1, 1) } | ||
|
||
describe '#at' do | ||
context 'retrieves the tile via the class at method' do | ||
it 'for up' do | ||
Game::Location.should_receive(:at).with(0, 1) | ||
subject.at(:up) | ||
end | ||
|
||
it 'for down' do | ||
Game::Location.should_receive(:at).with(2, 1) | ||
subject.at(:down) | ||
end | ||
|
||
it 'for left' do | ||
Game::Location.should_receive(:at).with(1, 0) | ||
subject.at(:left) | ||
end | ||
|
||
it 'for right' do | ||
Game::Location.should_receive(:at).with(1, 2) | ||
subject.at(:right) | ||
end | ||
end | ||
|
||
it 'returns an edge element if the tile does not exist' do | ||
subject.at(:right).should be_a(Game::Location::Edge) | ||
end | ||
|
||
context 'if the tile exists' do | ||
context 'if a location effecting object exists' do | ||
it 'return the endpint instead' do | ||
end_point_tile = Game::Location.build(0, 2, 2) | ||
up_tile = Game::Location.build(0, 0, 1) | ||
up_tile.add(Game::Object.instance('Transport', "end_point" => [2, 2], 'modules' => ['LocationModifier'])) | ||
subject.at(:up).should == end_point_tile | ||
end | ||
end | ||
|
||
context 'if no location effecting object exists' do | ||
it 'returns the tile' do | ||
up_tile = Game::Location.build(0, 0, 1) | ||
subject.at(:up).should == up_tile | ||
end | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
describe Game::Location::Passible do | ||
subject { Game::Location.build(0, 0, 0) } | ||
|
||
describe '#passible?' do | ||
context 'when no objects' do | ||
it 'should be true' do | ||
subject.should be_passible([]) | ||
end | ||
end | ||
|
||
context 'when objects' do | ||
let(:passible) { mock(:object, :location= => true, :passible? => true) } | ||
let(:impassible) { mock(:object, :location= => true, :passible? => false) } | ||
|
||
it 'true when all objects are passible' do | ||
subject.add(passible) | ||
subject.should be_passible([]) | ||
end | ||
|
||
it 'false if any objects are impassible' do | ||
subject.add(impassible) | ||
subject.should_not be_passible([]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper' | ||
|
||
describe Game::Modules::ObjectManagement do | ||
let(:exit) { Game::Object.instance('LevelExit', 'modules' => ['Exit']) } | ||
subject { Game::Location.build(0, 0, 0) } | ||
|
||
describe '#add' do | ||
it 'adds an object to the object list for the tile' do | ||
subject.add(exit) | ||
subject.should have_object(Game::Object::Exit) | ||
end | ||
end | ||
|
||
describe '#remove' do | ||
it 'removes an object from the object list for the tile' do | ||
subject.add(exit) | ||
subject.remove(exit) | ||
subject.should_not have_object(Game::Object::Exit) | ||
end | ||
end | ||
|
||
describe '#has_object?' do | ||
it 'returns true if tile has an instance of the object class' do | ||
subject.add(exit) | ||
subject.has_object?(Game::Object::Exit).should be_true | ||
end | ||
|
||
it 'returns true if an instance of a sub-class of the object class' do | ||
object = Game::Object.instance('SubClassOfExit', 'modules' => ['LocationModifier']) | ||
subject.add(object) | ||
subject.has_object?(Game::Object::LocationModifier).should be_true | ||
end | ||
|
||
it 'returns false if tile doesnt have an instance of the object class' do | ||
subject.has_object?(Game::Player).should be_false | ||
end | ||
end | ||
end |