From adf501a5e70d24f6c221af4c3ff2c72d83a35d6f Mon Sep 17 00:00:00 2001 From: David Henry Date: Mon, 13 Feb 2012 11:09:56 +0000 Subject: [PATCH] rename location to tile --- lib/game.rb | 2 +- lib/game/location/empty.rb | 11 --- lib/game/location/movement.rb | 23 ------- lib/game/map.rb | 8 +-- lib/game/modules/object_management.rb | 2 +- lib/game/object.rb | 2 +- lib/game/object/default.rb | 2 +- lib/game/object/exit.rb | 4 +- lib/game/object/healer.rb | 2 +- lib/game/object/location_modifier.rb | 9 --- lib/game/object/tile_modifier.rb | 9 +++ lib/game/player.rb | 2 +- lib/game/player/actions.rb | 6 +- lib/game/player/movements.rb | 18 ++--- lib/game/{location.rb => tile.rb} | 22 +++--- lib/game/{location => tile}/edge.rb | 6 +- lib/game/tile/empty.rb | 11 +++ lib/game/{location => tile}/modules.rb | 10 +-- lib/game/tile/movement.rb | 23 +++++++ lib/game/{location => tile}/passible.rb | 2 +- lib/game/{location => tile}/wall.rb | 6 +- lib/render/console/draw_map.rb | 4 +- lib/render/console/draw_tile.rb | 16 ++--- maps/level3 | 4 +- maps/level7_a | 10 +-- maps/level7_b | 10 +-- spec/game/engine_spec.rb | 4 +- spec/game/location_spec.rb | 50 -------------- spec/game/map_spec.rb | 12 ++-- spec/game/modules/object_management_spec.rb | 6 +- spec/game/object/healer_spec.rb | 6 +- spec/game/object/location_modifier_spec.rb | 16 ----- spec/game/object/tile_modifier_spec.rb | 16 +++++ spec/game/object_spec.rb | 4 +- spec/game/player/actions_spec.rb | 18 ++--- spec/game/player/movements_spec.rb | 38 +++++------ spec/game/player_spec.rb | 4 +- spec/game/{location => tile}/movement_spec.rb | 26 +++---- spec/game/{location => tile}/passible_spec.rb | 8 +-- spec/game/{location => tile}/wall_spec.rb | 18 ++--- spec/game/tile_spec.rb | 50 ++++++++++++++ spec/render/console/draw_map_spec.rb | 68 +++++++++---------- 42 files changed, 284 insertions(+), 284 deletions(-) delete mode 100644 lib/game/location/empty.rb delete mode 100644 lib/game/location/movement.rb delete mode 100644 lib/game/object/location_modifier.rb create mode 100644 lib/game/object/tile_modifier.rb rename lib/game/{location.rb => tile.rb} (56%) rename lib/game/{location => tile}/edge.rb (57%) create mode 100644 lib/game/tile/empty.rb rename lib/game/{location => tile}/modules.rb (70%) create mode 100644 lib/game/tile/movement.rb rename lib/game/{location => tile}/passible.rb (88%) rename lib/game/{location => tile}/wall.rb (81%) delete mode 100644 spec/game/location_spec.rb delete mode 100644 spec/game/object/location_modifier_spec.rb create mode 100644 spec/game/object/tile_modifier_spec.rb rename spec/game/{location => tile}/movement_spec.rb (52%) rename spec/game/{location => tile}/passible_spec.rb (66%) rename spec/game/{location => tile}/wall_spec.rb (82%) create mode 100644 spec/game/tile_spec.rb diff --git a/lib/game.rb b/lib/game.rb index 90cda9f..a6465b4 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -6,7 +6,7 @@ require 'lib/game/input' require 'lib/game/player' require 'lib/game/map' -require 'lib/game/location' +require 'lib/game/tile' require 'lib/game/object' require 'json' diff --git a/lib/game/location/empty.rb b/lib/game/location/empty.rb deleted file mode 100644 index fb21224..0000000 --- a/lib/game/location/empty.rb +++ /dev/null @@ -1,11 +0,0 @@ -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 \ No newline at end of file diff --git a/lib/game/location/movement.rb b/lib/game/location/movement.rb deleted file mode 100644 index acdf149..0000000 --- a/lib/game/location/movement.rb +++ /dev/null @@ -1,23 +0,0 @@ -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 \ No newline at end of file diff --git a/lib/game/map.rb b/lib/game/map.rb index 3e003ff..9a98b7e 100644 --- a/lib/game/map.rb +++ b/lib/game/map.rb @@ -34,18 +34,18 @@ def method_missing(method, *args, &blk) super end - def start_location + def start_tile at(0, 0) end private def build_data - Game::Location.clear + Game::Tile.clear map = [] @map_data['data'].each_with_index do |row, x| row_items = [] - row.each_with_index do |location, y| - row_items << Game::Location.build(location, x, y) + row.each_with_index do |tile, y| + row_items << Game::Tile.build(tile, x, y) end map << row_items end diff --git a/lib/game/modules/object_management.rb b/lib/game/modules/object_management.rb index 814874c..76c37f4 100644 --- a/lib/game/modules/object_management.rb +++ b/lib/game/modules/object_management.rb @@ -2,7 +2,7 @@ class Game module Modules module ObjectManagement def add(object) - object.location = self + object.tile = self @objects << object end diff --git a/lib/game/object.rb b/lib/game/object.rb index 84fcb71..74ffa49 100644 --- a/lib/game/object.rb +++ b/lib/game/object.rb @@ -5,7 +5,7 @@ require 'lib/game/object/exit' require 'lib/game/object/healer' require 'lib/game/object/inventry_item' -require 'lib/game/object/location_modifier' +require 'lib/game/object/tile_modifier' require 'lib/game/object/passage' require 'lib/game/object/switcher' require 'lib/game/object/setter' diff --git a/lib/game/object/default.rb b/lib/game/object/default.rb index b708a08..38244cc 100644 --- a/lib/game/object/default.rb +++ b/lib/game/object/default.rb @@ -2,7 +2,7 @@ class Game class Object module Default def self.included(base) - base.send :attr_accessor, :location + base.send :attr_accessor, :tile end def initialize(options) diff --git a/lib/game/object/exit.rb b/lib/game/object/exit.rb index 47fefef..29fbf73 100644 --- a/lib/game/object/exit.rb +++ b/lib/game/object/exit.rb @@ -4,7 +4,7 @@ module Exit def auto_process if @options['next_map'] skip_to next_map - move_to if @options["exit_location"] + move_to if @options["exit_tile"] else Game::Engine.instance.end('Congratulations you have Won.') end @@ -21,7 +21,7 @@ def skip_to(name) end def move_to - Game::Player.instance.move_to(Game::Location.at(*exit_location)) + Game::Player.instance.move_to(Game::Tile.at(*exit_tile)) end private :move_to end diff --git a/lib/game/object/healer.rb b/lib/game/object/healer.rb index e9a2b56..a12501f 100644 --- a/lib/game/object/healer.rb +++ b/lib/game/object/healer.rb @@ -3,7 +3,7 @@ class Object module Healer def auto_process self.health -= Game::Player.instance.heal(health) - self.location.remove(self) if self.health == 0 + self.tile.remove(self) if self.health == 0 end def health diff --git a/lib/game/object/location_modifier.rb b/lib/game/object/location_modifier.rb deleted file mode 100644 index ff06573..0000000 --- a/lib/game/object/location_modifier.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Game - class Object - module LocationModifier - def end_point - @options['end_point'] || raise('No end point set for location modifier') - end - end - end -end diff --git a/lib/game/object/tile_modifier.rb b/lib/game/object/tile_modifier.rb new file mode 100644 index 0000000..b80ceae --- /dev/null +++ b/lib/game/object/tile_modifier.rb @@ -0,0 +1,9 @@ +class Game + class Object + module TileModifier + def end_point + @options['end_point'] || raise('No end point set for tile modifier') + end + end + end +end diff --git a/lib/game/player.rb b/lib/game/player.rb index e6f5749..dbd56cf 100644 --- a/lib/game/player.rb +++ b/lib/game/player.rb @@ -8,7 +8,7 @@ class Player include Game::Player::Movements include Game::Modules::InstanceSetter - attr_accessor :location + attr_accessor :tile attr_reader :objects attr_reader :hp diff --git a/lib/game/player/actions.rb b/lib/game/player/actions.rb index a430d86..407a108 100644 --- a/lib/game/player/actions.rb +++ b/lib/game/player/actions.rb @@ -3,7 +3,7 @@ class Player module Actions def take_action objects_to_pick_up.each do |obj| - location.remove(obj) + tile.remove(obj) add(obj) end objects_to_responding_to(:process).each do |obj| @@ -18,12 +18,12 @@ def take_auto_action end def objects_to_pick_up - location.objects.select {|obj| obj.is_a?(Game::Object::InventryItem) } + tile.objects.select {|obj| obj.is_a?(Game::Object::InventryItem) } end private :objects_to_pick_up def objects_to_responding_to(method) - location.objects.select {|obj| obj.respond_to?(method) } + tile.objects.select {|obj| obj.respond_to?(method) } end private :objects_to_responding_to end diff --git a/lib/game/player/movements.rb b/lib/game/player/movements.rb index b0014de..ad03509 100644 --- a/lib/game/player/movements.rb +++ b/lib/game/player/movements.rb @@ -7,15 +7,15 @@ def self.included(base) def load_map(map) @map = map - @location = @map.start_location - @location.add(self) + @tile = @map.start_tile + @tile.add(self) end def move(direction) @direction = direction - new_location = @location.at(direction) - if new_location.passible?(objects) - move_to new_location + new_tile = @tile.at(direction) + if new_tile.passible?(objects) + move_to new_tile take_auto_action else # beep or something here @@ -23,10 +23,10 @@ def move(direction) end end - def move_to(location) - @location.remove(self) - @location = location - @location.add(self) + def move_to(tile) + @tile.remove(self) + @tile = tile + @tile.add(self) end end end diff --git a/lib/game/location.rb b/lib/game/tile.rb similarity index 56% rename from lib/game/location.rb rename to lib/game/tile.rb index e64bdfd..4a7dafd 100644 --- a/lib/game/location.rb +++ b/lib/game/tile.rb @@ -1,13 +1,13 @@ -require 'lib/game/location/modules' -require 'lib/game/location/movement' -require 'lib/game/location/passible' +require 'lib/game/tile/modules' +require 'lib/game/tile/movement' +require 'lib/game/tile/passible' -require 'lib/game/location/edge' -require 'lib/game/location/empty' -require 'lib/game/location/wall' +require 'lib/game/tile/edge' +require 'lib/game/tile/empty' +require 'lib/game/tile/wall' class Game - class Location + class Tile EMPTY_CELL = 0 WALL_90 = 1 WALL_CORNER_LEFT = 4 @@ -16,13 +16,13 @@ class Location WALL_0 = 3 class << self - def build(location_type, x, y) + def build(tile_type, x, y) @board ||= {} - @board[[x, y]] = class_for(location_type).new(location_type, x, y) + @board[[x, y]] = class_for(tile_type).new(tile_type, x, y) end - def class_for(location_type) - case location_type + def class_for(tile_type) + case tile_type when WALL_0, WALL_90, WALL_CORNER_RIGHT, WALL_CORNER_LEFT, WALL_CORNER Wall diff --git a/lib/game/location/edge.rb b/lib/game/tile/edge.rb similarity index 57% rename from lib/game/location/edge.rb rename to lib/game/tile/edge.rb index 62bcca7..70c66bd 100644 --- a/lib/game/location/edge.rb +++ b/lib/game/tile/edge.rb @@ -1,8 +1,8 @@ class Game - class Location + class Tile class Edge - include Game::Location::Base - include Game::Location::Impassible + include Game::Tile::Base + include Game::Tile::Impassible def self.instance @self ||= new diff --git a/lib/game/tile/empty.rb b/lib/game/tile/empty.rb new file mode 100644 index 0000000..43391a2 --- /dev/null +++ b/lib/game/tile/empty.rb @@ -0,0 +1,11 @@ +class Game + class Tile + class Empty + include Game::Modules::ObjectManagement + include Game::Tile::Base + include Game::Tile::Passible + include Game::Tile::Movement + + end + end +end \ No newline at end of file diff --git a/lib/game/location/modules.rb b/lib/game/tile/modules.rb similarity index 70% rename from lib/game/location/modules.rb rename to lib/game/tile/modules.rb index c47604b..894dc4c 100644 --- a/lib/game/location/modules.rb +++ b/lib/game/tile/modules.rb @@ -1,5 +1,5 @@ class Game - class Location + class Tile module Impassible def passible?(player_objects) false @@ -14,19 +14,19 @@ def passible?(player_objects) module Base def self.included(base) - base.send :attr_accessor, :location_type + base.send :attr_accessor, :tile_type base.send :attr_reader, :objects, :x, :y end - def initialize(location_type, x, y) - @location_type = location_type + def initialize(tile_type, x, y) + @tile_type = tile_type @x = x @y = y @objects = [] end def ==(val) - location_type == val.location_type && + tile_type == val.tile_type && x == val.x && y == val.y end diff --git a/lib/game/tile/movement.rb b/lib/game/tile/movement.rb new file mode 100644 index 0000000..e528354 --- /dev/null +++ b/lib/game/tile/movement.rb @@ -0,0 +1,23 @@ +class Game + class Tile + module Movement + def at(direction) + tile = Game::Tile.at(@x - 1, @y) if direction == :up + tile = Game::Tile.at(@x + 1, @y) if direction == :down + tile = Game::Tile.at(@x, @y - 1) if direction == :left + tile = Game::Tile.at(@x, @y + 1) if direction == :right + + tile.try(:end_point) || Game::Tile::Edge.instance + end + + def end_point + if has_object?(Game::Object::TileModifier) + Game::Tile.at(*get_object(Game::Object::TileModifier).end_point) + else + self + end + end + private :end_point + end + end +end \ No newline at end of file diff --git a/lib/game/location/passible.rb b/lib/game/tile/passible.rb similarity index 88% rename from lib/game/location/passible.rb rename to lib/game/tile/passible.rb index 6745a18..83b8f7f 100644 --- a/lib/game/location/passible.rb +++ b/lib/game/tile/passible.rb @@ -1,5 +1,5 @@ class Game - class Location + class Tile module Passible def passible?(player_objects) objects.all?(&:passible?) diff --git a/lib/game/location/wall.rb b/lib/game/tile/wall.rb similarity index 81% rename from lib/game/location/wall.rb rename to lib/game/tile/wall.rb index 60ceb92..a7c7ba1 100644 --- a/lib/game/location/wall.rb +++ b/lib/game/tile/wall.rb @@ -1,9 +1,9 @@ class Game - class Location + class Tile class Wall include Game::Modules::ObjectManagement - include Game::Location::Base - include Game::Location::Movement + include Game::Tile::Base + include Game::Tile::Movement def passible?(player_objects) return false unless has_object?(Game::Object::Passage) diff --git a/lib/render/console/draw_map.rb b/lib/render/console/draw_map.rb index 2fd8c35..dafc391 100644 --- a/lib/render/console/draw_map.rb +++ b/lib/render/console/draw_map.rb @@ -8,7 +8,7 @@ def draw_map(engine) draw_board(engine.map.data) draw_stats(engine.player) draw_player(engine.player) - draw_tile(engine.player.location) + draw_tile(engine.player.tile) system('clear') @io.puts @output @@ -66,7 +66,7 @@ def draw_player(player) def draw_tile(tile) objects = tile.objects.dup - append "Location: " + append "Tile: " append " ", tile.class append "Items: " objects.each do |obj| diff --git a/lib/render/console/draw_tile.rb b/lib/render/console/draw_tile.rb index f8b08e8..170b24a 100644 --- a/lib/render/console/draw_tile.rb +++ b/lib/render/console/draw_tile.rb @@ -9,7 +9,7 @@ def initialize(tile) end def draw - if tile.is_a?(Game::Location::Wall) + if tile.is_a?(Game::Tile::Wall) draw_player(draw_wall) else draw_player(draw_empty) @@ -39,7 +39,7 @@ def draw_empty Game::Object::Switcher => 'SSS', Game::Object::Setter => 'SSS', Game::Object::Exit => 'EEE', - Game::Object::LocationModifier => 'TTT', + Game::Object::TileModifier => 'TTT', Game::Object::Trap => '###', Game::Object::Enemy => '|:P', @@ -61,12 +61,12 @@ def draw_wall end base = { - Game::Location::WALL_90 => ' | ', - Game::Location::WALL_CORNER_RIGHT => ' +-', - Game::Location::WALL_CORNER_LEFT => '-+ ', - Game::Location::WALL_0 => '---', - Game::Location::WALL_CORNER => '-+-' - }[tile.location_type] || (debugger) + Game::Tile::WALL_90 => ' | ', + Game::Tile::WALL_CORNER_RIGHT => ' +-', + Game::Tile::WALL_CORNER_LEFT => '-+ ', + Game::Tile::WALL_0 => '---', + Game::Tile::WALL_CORNER => '-+-' + }[tile.tile_type] || (debugger) end end end diff --git a/maps/level3 b/maps/level3 index 7c44469..7ea90de 100644 --- a/maps/level3 +++ b/maps/level3 @@ -25,7 +25,7 @@ "x": 0, "y": 4, "details": { - "modules": ["LocationModifier"], + "modules": ["TileModifier"], "end_point": [4, 0] } }, @@ -34,7 +34,7 @@ "x": 4, "y": 0, "details": { - "modules": ["LocationModifier"], + "modules": ["TileModifier"], "end_point": [0, 4] } } diff --git a/maps/level7_a b/maps/level7_a index 2540009..a236781 100644 --- a/maps/level7_a +++ b/maps/level7_a @@ -27,7 +27,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_b", - "exit_location": [4, 0] + "exit_tile": [4, 0] } }, { @@ -37,7 +37,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_b", - "exit_location": [2, 2] + "exit_tile": [2, 2] } }, { @@ -47,7 +47,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_b", - "exit_location": [4, 2] + "exit_tile": [4, 2] } }, { @@ -57,7 +57,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_b", - "exit_location": [0, 4] + "exit_tile": [0, 4] } }, { @@ -67,7 +67,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_b", - "exit_location": [2, 4] + "exit_tile": [2, 4] } } ] diff --git a/maps/level7_b b/maps/level7_b index 5b455a5..bd855a8 100644 --- a/maps/level7_b +++ b/maps/level7_b @@ -18,7 +18,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_a", - "exit_location": [4, 0] + "exit_tile": [4, 0] } }, { @@ -28,7 +28,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_a", - "exit_location": [2, 2] + "exit_tile": [2, 2] } }, { @@ -38,7 +38,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_a", - "exit_location": [4, 2] + "exit_tile": [4, 2] } }, { @@ -48,7 +48,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_a", - "exit_location": [0, 4] + "exit_tile": [0, 4] } }, { @@ -58,7 +58,7 @@ "details": { "modules": ["Exit"], "next_map": "level7_a", - "exit_location": [2, 4] + "exit_tile": [2, 4] } } ] diff --git a/spec/game/engine_spec.rb b/spec/game/engine_spec.rb index 879fb4d..6c6697a 100644 --- a/spec/game/engine_spec.rb +++ b/spec/game/engine_spec.rb @@ -2,8 +2,8 @@ describe Game::Engine do let(:first_map) { mock(:first_map, :next => "next")} - let(:location) { mock(:location, :has_object? => true)} - let(:player) { mock(:player, :load_map => true, :location => location) } + let(:tile) { mock(:tile, :has_object? => true)} + let(:player) { mock(:player, :load_map => true, :tile => tile) } before do Game::Map.stub(:load_map => first_map) Game::Player.stub(:new => player) diff --git a/spec/game/location_spec.rb b/spec/game/location_spec.rb deleted file mode 100644 index 4e74bab..0000000 --- a/spec/game/location_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'spec_helper' - -describe Game::Location do - describe '#class_methods' do - before do - Game::Location.clear - end - - def board - Game::Location.instance_variable_get('@board') - end - - describe '#build' do - it 'creates a new insatnce of the class and stores it for retreival' do - Game::Location.build(0, 0, 0) - board.should == {[0, 0] => Game::Location::Empty.new(0, 0, 0)} - end - end - - describe "#class_for" do - it 'returns the appropriate class to for the location_type' do - Game::Location.class_for(Game::Location::EMPTY_CELL).should == Game::Location::Empty - Game::Location.class_for(Game::Location::WALL_0).should == Game::Location::Wall - Game::Location.class_for(Game::Location::WALL_90).should == Game::Location::Wall - Game::Location.class_for(Game::Location::WALL_CORNER_RIGHT).should == Game::Location::Wall - Game::Location.class_for(Game::Location::WALL_CORNER_LEFT).should == Game::Location::Wall - end - - it 'raises and error for an unknow location type' do - expect { Game::Location.class_for('unknown') }.to raise_error - end - end - - describe "#clear" do - it 'removes all items from the board' do - Game::Location.build(0, 0, 0) - Game::Location.clear - board.should == {} - end - end - - describe "at" do - it 'shorthand to get the value from the hash' do - Game::Location.build(0, 0, 0) - Game::Location.at(0, 0).should == board[[0, 0]] - end - end - end -end - diff --git a/spec/game/map_spec.rb b/spec/game/map_spec.rb index 6189e45..13bfbea 100644 --- a/spec/game/map_spec.rb +++ b/spec/game/map_spec.rb @@ -15,9 +15,9 @@ end describe '#data' do - it 'returns the board elements mapped to Game::Locations' do - subject.data.should == [[Game::Location::Empty.new(0, 0, 0), Game::Location::Empty.new(1, 0, 1)], - [Game::Location::Empty.new(2, 1, 0), Game::Location::Empty.new(3, 1, 1)]] + it 'returns the board elements mapped to Game::Tiles' do + subject.data.should == [[Game::Tile::Empty.new(0, 0, 0), Game::Tile::Empty.new(1, 0, 1)], + [Game::Tile::Empty.new(2, 1, 0), Game::Tile::Empty.new(3, 1, 1)]] end end @@ -33,9 +33,9 @@ end end - describe '#start_location' do + describe '#start_tile' do it 'returns the tile at 0,0 by default' do - subject.start_location.should == subject.at(0, 0) + subject.start_tile.should == subject.at(0, 0) end end @@ -53,7 +53,7 @@ subject end - it 'add an instance of the object to the map location' do + it 'add an instance of the object to the map tile' do subject.at(1, 0).should have_object(Game::Object::TestObject) end end diff --git a/spec/game/modules/object_management_spec.rb b/spec/game/modules/object_management_spec.rb index 481bf53..33ff807 100644 --- a/spec/game/modules/object_management_spec.rb +++ b/spec/game/modules/object_management_spec.rb @@ -2,7 +2,7 @@ describe Game::Modules::ObjectManagement do let(:exit) { Game::Object.instance('LevelExit', 'modules' => ['Exit']) } - subject { Game::Location.build(0, 0, 0) } + subject { Game::Tile.build(0, 0, 0) } describe '#add' do it 'adds an object to the object list for the tile' do @@ -26,9 +26,9 @@ end it 'returns true if an instance of a sub-class of the object class' do - object = Game::Object.instance('SubClassOfExit', 'modules' => ['LocationModifier']) + object = Game::Object.instance('SubClassOfExit', 'modules' => ['TileModifier']) subject.add(object) - subject.has_object?(Game::Object::LocationModifier).should be_true + subject.has_object?(Game::Object::TileModifier).should be_true end it 'returns false if tile doesnt have an instance of the object class' do diff --git a/spec/game/object/healer_spec.rb b/spec/game/object/healer_spec.rb index ca1365e..c5eeaa4 100644 --- a/spec/game/object/healer_spec.rb +++ b/spec/game/object/healer_spec.rb @@ -3,7 +3,7 @@ describe Game::Object::Healer do subject { Game::Object.instance('HealerTest', 'modules' => ['Healer'], 'health' => 50) } let(:player) { mock(:player, :heal => 25) } - let(:location) { Game::Location.build(0, 0, 0) } + let(:tile) { Game::Tile.build(0, 0, 0) } before do Game::Player.stub(:instance => player) @@ -20,9 +20,9 @@ end it 'removes the object once all healing power has been used up' do - location.add(subject) + tile.add(subject) player.stub(:heal => 50) subject.auto_process - location.should_not have_object(Game::Object::Healer) + tile.should_not have_object(Game::Object::Healer) end end diff --git a/spec/game/object/location_modifier_spec.rb b/spec/game/object/location_modifier_spec.rb deleted file mode 100644 index 89f001d..0000000 --- a/spec/game/object/location_modifier_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe Game::Object::LocationModifier do - subject { Game::Object.instance('LocationModifierTest', 'modules' => ['LocationModifier'] ,'end_point' => [1, 2]) } - it 'takes a details hash' do - subject - end - - it 'has an end-point method' do - subject.end_point.should == [1, 2] - end - - it 'raises and error is the end-point is not set' do - expect { LocationModifierTest.new({}).end_point }.to raise_error - end -end \ No newline at end of file diff --git a/spec/game/object/tile_modifier_spec.rb b/spec/game/object/tile_modifier_spec.rb new file mode 100644 index 0000000..7da238b --- /dev/null +++ b/spec/game/object/tile_modifier_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Game::Object::TileModifier do + subject { Game::Object.instance('TileModifierTest', 'modules' => ['TileModifier'] ,'end_point' => [1, 2]) } + it 'takes a details hash' do + subject + end + + it 'has an end-point method' do + subject.end_point.should == [1, 2] + end + + it 'raises and error is the end-point is not set' do + expect { TileModifierTest.new({}).end_point }.to raise_error + end +end \ No newline at end of file diff --git a/spec/game/object_spec.rb b/spec/game/object_spec.rb index 17e9ced..4ed447f 100644 --- a/spec/game/object_spec.rb +++ b/spec/game/object_spec.rb @@ -17,8 +17,8 @@ context 'takes optional details hash' do it 'sets the base class if parent details' do - instance = Game::Object.instance('UniqueTestObject', {'modules' => ['LocationModifier']}) - instance.class.ancestors.should include(Game::Object::LocationModifier) + instance = Game::Object.instance('UniqueTestObject', {'modules' => ['TileModifier']}) + instance.class.ancestors.should include(Game::Object::TileModifier) end end diff --git a/spec/game/player/actions_spec.rb b/spec/game/player/actions_spec.rb index 966b839..88fecd0 100644 --- a/spec/game/player/actions_spec.rb +++ b/spec/game/player/actions_spec.rb @@ -2,8 +2,8 @@ describe Game::Player::Actions do subject { Game::Player.new } - let(:map) { mock(:map, :start_location => location) } - let(:location) { Game::Location.build(0, 0, 0) } + let(:map) { mock(:map, :start_tile => tile) } + let(:tile) { Game::Tile.build(0, 0, 0) } before do subject.load_map(map) @@ -14,23 +14,23 @@ let(:stationary_object) { Game::Object.instance('TableTest') } let(:moveable_object) { Game::Object.instance('PlayerKeyTest', "modules" => ["InventryItem"]) } - it "picks up any inventry items in the current location and places them in the inventry" do - subject.location.add(moveable_object) + it "picks up any inventry items in the current tile and places them in the inventry" do + subject.tile.add(moveable_object) subject.take_action subject.should have_object(Game::Object::PlayerKeyTest) end it "can not picks up non inventry item" do subject.load_map(map) - subject.location.add(stationary_object) + subject.tile.add(stationary_object) subject.take_action subject.should_not have_object(Game::Object::TableTest) end it 'removes items from the map when they have been picked up' do - subject.location.add(moveable_object) + subject.tile.add(moveable_object) subject.take_action - subject.location.should_not have_object(Game::Object::PlayerKeyTest) + subject.tile.should_not have_object(Game::Object::PlayerKeyTest) end end @@ -38,7 +38,7 @@ let(:process_object) { Game::Object.instance('PlayerSwitcherTest', "modules" => ["Switcher"]) } it 'calls process on any objects that have a process method' do - subject.location.add(process_object) + subject.tile.add(process_object) process_object.should_receive(:process) subject.take_action end @@ -50,7 +50,7 @@ let(:process_object) { Game::Object.instance('ExitTest', "modules" => ["Exit"]) } it 'calls process on any objects that have a process method' do - subject.location.add(process_object) + subject.tile.add(process_object) process_object.should_receive(:auto_process) subject.take_auto_action end diff --git a/spec/game/player/movements_spec.rb b/spec/game/player/movements_spec.rb index 1e5793f..0a0ab85 100644 --- a/spec/game/player/movements_spec.rb +++ b/spec/game/player/movements_spec.rb @@ -2,42 +2,42 @@ describe Game::Player::Movements do subject { Game::Player.new } - let(:map) { mock(:map, :start_location => location) } - let(:location) { mock(:location, :add => true, :remove => true) } + let(:map) { mock(:map, :start_tile => tile) } + let(:tile) { mock(:tile, :add => true, :remove => true) } describe '#load_map' do - it 'sets the player start location' do + it 'sets the player start tile' do subject.load_map(map) - subject.location.should == location + subject.tile.should == tile end - it 'add the player to location objects' do - location.should_receive(:add).with(subject) + it 'add the player to tile objects' do + tile.should_receive(:add).with(subject) subject.load_map(map) end end describe '#move' do - let(:passible_location) { mock(:passible_location, :passible? => true, :add => true, :objects => []) } + let(:passible_tile) { mock(:passible_tile, :passible? => true, :add => true, :objects => []) } before do subject.load_map(map) - location.stub(:at => passible_location) + tile.stub(:at => passible_tile) end - it 'delegates the movement to the current location' do - location.should_receive(:at).with(:up) + it 'delegates the movement to the current tile' do + tile.should_receive(:at).with(:up) subject.move(:up) end - context 'the new location is passible' do + context 'the new tile is passible' do it 'moves the player' do subject.move(:up) - subject.location.should == passible_location + subject.tile.should == passible_tile end - it 'move the player object from the old location to the new location' do - location.should_receive(:remove).with(subject) - passible_location.should_receive(:add).with(subject) + it 'move the player object from the old tile to the new tile' do + tile.should_receive(:remove).with(subject) + passible_tile.should_receive(:add).with(subject) subject.move(:up) end @@ -47,16 +47,16 @@ end end - context 'the new location is not passible' do - let(:unpassible_location) { mock(:unpassible_location, :passible? => false) } + context 'the new tile is not passible' do + let(:unpassible_tile) { mock(:unpassible_tile, :passible? => false) } before do - location.stub(:at => unpassible_location) + tile.stub(:at => unpassible_tile) end it 'does not move the player' do subject.stub(:print => true) subject.move(:up) - subject.location.should == location + subject.tile.should == tile end it 'plays a beep' do diff --git a/spec/game/player_spec.rb b/spec/game/player_spec.rb index 3853973..31650d2 100644 --- a/spec/game/player_spec.rb +++ b/spec/game/player_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' describe Game::Player do - let(:map) { mock(:map, :start_location => location) } - let(:location) { mock(:location, :add => true, :remove => true) } + let(:map) { mock(:map, :start_tile => tile) } + let(:tile) { mock(:tile, :add => true, :remove => true) } it 'sets an initial health' do subject.hp.should == 100 diff --git a/spec/game/location/movement_spec.rb b/spec/game/tile/movement_spec.rb similarity index 52% rename from spec/game/location/movement_spec.rb rename to spec/game/tile/movement_spec.rb index d697756..2ad147f 100644 --- a/spec/game/location/movement_spec.rb +++ b/spec/game/tile/movement_spec.rb @@ -1,48 +1,48 @@ require 'spec_helper' -describe Game::Location::Movement do - subject { Game::Location.build(0, 1, 1) } +describe Game::Tile::Movement do + subject { Game::Tile.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) + Game::Tile.should_receive(:at).with(0, 1) subject.at(:up) end it 'for down' do - Game::Location.should_receive(:at).with(2, 1) + Game::Tile.should_receive(:at).with(2, 1) subject.at(:down) end it 'for left' do - Game::Location.should_receive(:at).with(1, 0) + Game::Tile.should_receive(:at).with(1, 0) subject.at(:left) end it 'for right' do - Game::Location.should_receive(:at).with(1, 2) + Game::Tile.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) + subject.at(:right).should be_a(Game::Tile::Edge) end context 'if the tile exists' do - context 'if a location effecting object exists' do + context 'if a tile 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'])) + end_point_tile = Game::Tile.build(0, 2, 2) + up_tile = Game::Tile.build(0, 0, 1) + up_tile.add(Game::Object.instance('Transport', "end_point" => [2, 2], 'modules' => ['TileModifier'])) subject.at(:up).should == end_point_tile end end - context 'if no location effecting object exists' do + context 'if no tile effecting object exists' do it 'returns the tile' do - up_tile = Game::Location.build(0, 0, 1) + up_tile = Game::Tile.build(0, 0, 1) subject.at(:up).should == up_tile end end diff --git a/spec/game/location/passible_spec.rb b/spec/game/tile/passible_spec.rb similarity index 66% rename from spec/game/location/passible_spec.rb rename to spec/game/tile/passible_spec.rb index 6691e7d..19a5e42 100644 --- a/spec/game/location/passible_spec.rb +++ b/spec/game/tile/passible_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Game::Location::Passible do - subject { Game::Location.build(0, 0, 0) } +describe Game::Tile::Passible do + subject { Game::Tile.build(0, 0, 0) } describe '#passible?' do context 'when no objects' do @@ -11,8 +11,8 @@ end context 'when objects' do - let(:passible) { mock(:object, :location= => true, :passible? => true) } - let(:impassible) { mock(:object, :location= => true, :passible? => false) } + let(:passible) { mock(:object, :tile= => true, :passible? => true) } + let(:impassible) { mock(:object, :tile= => true, :passible? => false) } it 'true when all objects are passible' do subject.add(passible) diff --git a/spec/game/location/wall_spec.rb b/spec/game/tile/wall_spec.rb similarity index 82% rename from spec/game/location/wall_spec.rb rename to spec/game/tile/wall_spec.rb index 1c6d721..f80660c 100644 --- a/spec/game/location/wall_spec.rb +++ b/spec/game/tile/wall_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' -describe Game::Location::Wall do +describe Game::Tile::Wall do describe '#passible?' do - subject { Game::Location::Wall.new(0, 0, 0) } + subject { Game::Tile::Wall.new(0, 0, 0) } let(:passage) { Game::Object.instance('TestWallPassage', 'modules' => ['Passage'], 'passible?' => false, 'id' => 'door_key') } let(:key) { Game::Object.instance('TestWallPassageKey', 'modules' => ['InventryItem'], 'id' => 'door_key') } @@ -40,11 +40,11 @@ end describe 'feature testing' do - let(:start) { Game::Location.build(0, 0, 0) } - let(:wall) { Game::Location.build(Game::Location::WALL_0, 1, 0) } - let(:stop) { Game::Location.build(0, 2, 0) } + let(:start) { Game::Tile.build(0, 0, 0) } + let(:wall) { Game::Tile.build(Game::Tile::WALL_0, 1, 0) } + let(:stop) { Game::Tile.build(0, 2, 0) } let(:player) { Game::Player.new } - let(:map) { mock(:map, :start_location => start) } + let(:map) { mock(:map, :start_tile => start) } let(:passage) { Game::Object.instance('FeaturePassage', 'modules' => ['Passage'], 'id' => 'feature_key') } let(:key) { Game::Object.instance('FeatureKey', 'modules' => ['InventryItem'], 'id' => 'feature_key') } @@ -57,16 +57,16 @@ player.add(key) player.load_map(map) player.move(:down) - player.location.should == wall + player.tile.should == wall player.move(:down) - player.location.should == stop + player.tile.should == stop end it 'passing the wall drops the key' do player.add(key) player.load_map(map) player.move(:down) - player.location.should == wall + player.tile.should == wall player.should_not have_object(Game::Object::FeatureKey) end diff --git a/spec/game/tile_spec.rb b/spec/game/tile_spec.rb new file mode 100644 index 0000000..b1c7ce4 --- /dev/null +++ b/spec/game/tile_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe Game::Tile do + describe '#class_methods' do + before do + Game::Tile.clear + end + + def board + Game::Tile.instance_variable_get('@board') + end + + describe '#build' do + it 'creates a new insatnce of the class and stores it for retreival' do + Game::Tile.build(0, 0, 0) + board.should == {[0, 0] => Game::Tile::Empty.new(0, 0, 0)} + end + end + + describe "#class_for" do + it 'returns the appropriate class to for the tile_type' do + Game::Tile.class_for(Game::Tile::EMPTY_CELL).should == Game::Tile::Empty + Game::Tile.class_for(Game::Tile::WALL_0).should == Game::Tile::Wall + Game::Tile.class_for(Game::Tile::WALL_90).should == Game::Tile::Wall + Game::Tile.class_for(Game::Tile::WALL_CORNER_RIGHT).should == Game::Tile::Wall + Game::Tile.class_for(Game::Tile::WALL_CORNER_LEFT).should == Game::Tile::Wall + end + + it 'raises and error for an unknow tile type' do + expect { Game::Tile.class_for('unknown') }.to raise_error + end + end + + describe "#clear" do + it 'removes all items from the board' do + Game::Tile.build(0, 0, 0) + Game::Tile.clear + board.should == {} + end + end + + describe "at" do + it 'shorthand to get the value from the hash' do + Game::Tile.build(0, 0, 0) + Game::Tile.at(0, 0).should == board[[0, 0]] + end + end + end +end + diff --git a/spec/render/console/draw_map_spec.rb b/spec/render/console/draw_map_spec.rb index ca7fc1f..c3fdccf 100644 --- a/spec/render/console/draw_map_spec.rb +++ b/spec/render/console/draw_map_spec.rb @@ -2,11 +2,11 @@ describe Render::Console::DrawMap do let(:string_io) { StringIO.new } - let(:location) { Game::Location.build(Game::Location::EMPTY_CELL, 0, 0) } - let(:wall_location) { Game::Location.build(Game::Location::WALL_0, 0, 1) } - let(:player) { mock(:player, :objects => [], :hp => 100, :location => location) } + let(:tile) { Game::Tile.build(Game::Tile::EMPTY_CELL, 0, 0) } + let(:wall_tile) { Game::Tile.build(Game::Tile::WALL_0, 0, 1) } + let(:player) { mock(:player, :objects => [], :hp => 100, :tile => tile) } let(:engine) { mock(:engine, :map => map, :player => player) } - let(:map) { mock(:map, :name => 'Test map', :goal => 'pass a test', :data => [[location],[wall_location]]) } + let(:map) { mock(:map, :name => 'Test map', :goal => 'pass a test', :data => [[tile],[wall_tile]]) } subject { Render::Console.new(string_io) } before do @@ -14,7 +14,7 @@ end def output(element=nil) - wall_location.location_type = element if element + wall_tile.tile_type = element if element subject.draw_map(engine) string_io.rewind @@ -46,29 +46,29 @@ def output(element=nil) end it 'draws a 90 degree wall' do - output(Game::Location::WALL_90).should include "| | |" + output(Game::Tile::WALL_90).should include "| | |" end it 'draws a 0 degree wall' do - output(Game::Location::WALL_0).should include "|---|" + output(Game::Tile::WALL_0).should include "|---|" end it 'draws a right corner wall' do - output(Game::Location::WALL_CORNER_RIGHT).should include "| +-|" + output(Game::Tile::WALL_CORNER_RIGHT).should include "| +-|" end it 'draws a left corner' do - output(Game::Location::WALL_CORNER_LEFT).should include "|-+ |" + output(Game::Tile::WALL_CORNER_LEFT).should include "|-+ |" end it 'draws a corner' do - output(Game::Location::WALL_CORNER).should include "|-+-|" + output(Game::Tile::WALL_CORNER).should include "|-+-|" end end context 'overwrites a tile with object' do let(:exit) { Game::Object.instance("LevelExit", 'modules' => ['Exit']) } - let(:transport) { Game::Object.instance("RenderTransport", 'modules' => ['LocationModifier']) } + let(:transport) { Game::Object.instance("RenderTransport", 'modules' => ['TileModifier']) } let(:closed_door) { Game::Object.instance("RenderClosedDoor", 'modules' => ['Passage'], 'passible?' => false)} let(:open_door) { Game::Object.instance("RenderClosedDoor", 'modules' => ['Passage'], 'passible?' => true)} let(:switcher) { Game::Object.instance("RenderSwitcher", 'modules' => ['Switcher'], 'passible?' => true)} @@ -76,78 +76,78 @@ def output(element=nil) let(:trap) { Game::Object.instance("RenderTrap", 'modules' => ['Trap'], 'damage' => 25)} it 'when player is on the tile' do - location.add(Game::Player.new) + tile.add(Game::Player.new) output.should include "| * |" end it 'when the tile is the exit' do - location.add(exit) + tile.add(exit) output.should include "|EEE|" end it 'when the tile is the exit and the player is on it' do - location.add(Game::Player.new) - location.add(exit) + tile.add(Game::Player.new) + tile.add(exit) output.should include "|E*E|" end it 'when the tile is a transport' do - location.add(transport) + tile.add(transport) output.should include "|TTT|" end it 'when the tile is a transport and the player is on it' do - location.add(Game::Player.new) - location.add(transport) + tile.add(Game::Player.new) + tile.add(transport) output.should include "|T*T|" end it 'when the tile is a door' do - wall_location.add(closed_door) - output(Game::Location::WALL_0).should include "|DDD|" + wall_tile.add(closed_door) + output(Game::Tile::WALL_0).should include "|DDD|" end it 'when the tile is an open door' do - wall_location.add(open_door) - output(Game::Location::WALL_0).should include "|D D|" + wall_tile.add(open_door) + output(Game::Tile::WALL_0).should include "|D D|" end it 'when the tile is an open door and the player is on it' do - wall_location.add(Game::Player.new) - wall_location.add(open_door) - output(Game::Location::WALL_0).should include "|D*D|" + wall_tile.add(Game::Player.new) + wall_tile.add(open_door) + output(Game::Tile::WALL_0).should include "|D*D|" end it 'when the tile is a switcher' do - location.add(switcher) + tile.add(switcher) output.should include "|SSS|" end it 'when the tile is a switcher and the player is on it' do - location.add(Game::Player.new) - location.add(switcher) + tile.add(Game::Player.new) + tile.add(switcher) output.should include "|S*S|" end it 'when the tile is a setter' do - location.add(setter) + tile.add(setter) output.should include "|SSS|" end it 'when the tile is a switcher and the player is on it' do - location.add(Game::Player.new) - location.add(setter) + tile.add(Game::Player.new) + tile.add(setter) output.should include "|S*S|" end it 'when the tile is a trap' do - location.add(trap) + tile.add(trap) output.should include "|###|" end it 'when the tile is a trap and the player is on it' do - location.add(Game::Player.new) - location.add(trap) + tile.add(Game::Player.new) + tile.add(trap) output.should include "|#*#|" end end