Skip to content

Commit

Permalink
Merge 7613eec into c7091e0
Browse files Browse the repository at this point in the history
  • Loading branch information
nskins committed Jan 15, 2018
2 parents c7091e0 + 7613eec commit c5b3552
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 39 deletions.
12 changes: 6 additions & 6 deletions lib/goby/entity/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class Player < Entity
# @param [Integer] gold the currency used for economical transactions.
# @param [[BattleCommand]] battle_commands the commands that can be used in battle.
# @param [Hash] outfit the collection of equippable items currently worn.
# @param [Location] location at which the player should start.
# @param [Location] location the place at which the player should start.
# @param [Location] respawn_location the place at which the player respawns.
def initialize(name: "Player", stats: {}, inventory: [], gold: 0, battle_commands: [],
outfit: {}, location: nil)
outfit: {}, location: nil, respawn_location: nil)
super(name: name, stats: stats, inventory: inventory, gold: gold, outfit: outfit)
@saved_maps = Hash.new

Expand All @@ -45,6 +46,7 @@ def initialize(name: "Player", stats: {}, inventory: [], gold: 0, battle_command
add_battle_commands(battle_commands)

move_to(Location.new(new_map, new_coords))
@respawn_location = respawn_location || @location
@saved_maps = Hash.new
end

Expand Down Expand Up @@ -121,9 +123,7 @@ def choose_item_and_on_whom(enemy)
def die
sleep(2) unless ENV['TEST']

# TODO: fix next line. regen_coords could be nil or "bad."
@location = Location.new(@location.map, @location.map.regen_coords)

move_to(@respawn_location)
type("After being knocked out in battle,\n")
type("you wake up in #{@location.map.name}.\n\n")

Expand Down Expand Up @@ -294,7 +294,7 @@ def sample_gold
end

attr_reader :location, :saved_maps
attr_accessor :moved
attr_accessor :moved, :respawn_location

end

Expand Down
6 changes: 2 additions & 4 deletions lib/goby/map/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ class Map

# @param [String] name the name.
# @param [[Tile]] tiles the content of the map.
# @param [C(Integer, Integer)] regen_coords respawn-on-death coordinates.
def initialize(name: "Map", tiles: [[Tile.new]], regen_coords: C[0,0], music: nil)
def initialize(name: "Map", tiles: [[Tile.new]], music: nil)
@name = name
@tiles = tiles
@regen_coords = regen_coords
@music = music
end

Expand Down Expand Up @@ -40,7 +38,7 @@ def ==(rhs)
return @name == rhs.name
end

attr_accessor :name, :tiles, :regen_coords, :music
attr_accessor :name, :tiles, :music

end

Expand Down
2 changes: 1 addition & 1 deletion res/scaffold/simple/farm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# the Map - each point is referred to as a Tile.
class Farm < Map
def initialize
super(name: "Farm", regen_coords: C[1, 1])
super(name: "Farm")

# Define the main tiles on this map.
grass = Tile.new(description: "You are standing on some grass.")
Expand Down
7 changes: 4 additions & 3 deletions res/scaffold/simple/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

# No load? Create a new player.
if player.nil?
# A Location specifies the Map and (y,x) coordinates of a Player.
home = Location.new(Farm.new, C[1, 1])

# Use the Player constructor to set the
# initial Map, (y,x) location, stats,
# gold, inventory, and more.
player = Player.new(location: Location.new(Farm.new, C[1, 1]))
# location, stats, gold, inventory, and more.
player = Player.new(location: home)

end

Expand Down
2 changes: 1 addition & 1 deletion spec/goby/entity/monster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let!(:slime) { Monster.new(battle_commands: [Attack.new(success_rate: 0)],
gold: 5000, treasures: [C[slime_item, 1]]) }
let(:newb) { Player.new(battle_commands: [Attack.new(success_rate: 0)],
gold: 50) }
gold: 50, respawn_location: Location.new(Map.new, C[0, 0])) }

context "constructor" do
it "has the correct default parameters" do
Expand Down
47 changes: 29 additions & 18 deletions spec/goby/entity/player_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
# Constructs a map in the shape of a plus sign.
let!(:map) { Map.new(tiles: [[Tile.new(passable: false), Tile.new, Tile.new(passable: false)],
[Tile.new, Tile.new, Tile.new(monsters: [Monster.new(battle_commands: [Attack.new(success_rate: 0)])])],
[Tile.new(passable: false), Tile.new, Tile.new(passable: false)]],
regen_coords: C[1, 1]) }
let!(:center) { map.regen_coords }
[Tile.new(passable: false), Tile.new, Tile.new(passable: false)]]) }
let!(:center) { C[1, 1] }
let!(:passable) { Tile::DEFAULT_PASSABLE }
let!(:impassable) { Tile::DEFAULT_IMPASSABLE }

Expand All @@ -17,12 +16,12 @@
let!(:slime) { Monster.new(battle_commands: [Attack.new(success_rate: 0)],
gold: 5000, treasures: [C[Item.new, 1]]) }
let!(:newb) { Player.new(battle_commands: [Attack.new(success_rate: 0)],
gold: 50, location: Location.new(map, center)) }
gold: 50, location: Location.new(map, center),
respawn_location: Location.new(map, C[2, 1])) }
let!(:dragon) { Monster.new(stats: {attack: 50, agility: 10000},
battle_commands: [Attack.new(strength: 50)]) }
let!(:chest_map) { Map.new(name: "Chest Map",
tiles: [[Tile.new(events: [Chest.new(gold: 5)]), Tile.new(events: [Chest.new(gold: 5)])]],
regen_coords: C[0, 0]) }
tiles: [[Tile.new(events: [Chest.new(gold: 5)]), Tile.new(events: [Chest.new(gold: 5)])]]) }

context "constructor" do
it "has the correct default parameters" do
Expand All @@ -39,6 +38,8 @@
expect(player.battle_commands).to eq Array.new
expect(player.location.map).to eq Player::DEFAULT_MAP
expect(player.location.coords).to eq Player::DEFAULT_COORDS
expect(player.respawn_location.map).to eq Player::DEFAULT_MAP
expect(player.respawn_location.coords).to eq Player::DEFAULT_COORDS
end

it "correctly assigns custom parameters" do
Expand All @@ -59,7 +60,8 @@
BattleCommand.new(name: "Yell"),
BattleCommand.new(name: "Run")
],
location: Location.new(map, C[1, 1]))
location: Location.new(map, C[1, 1]),
respawn_location: Location.new(map, C[1, 2]))
expect(hero.name).to eq "Hero"
expect(hero.stats[:max_hp]).to eq 50
expect(hero.stats[:hp]).to eq 35
Expand All @@ -77,6 +79,14 @@
]
expect(hero.location.map).to eq map
expect(hero.location.coords).to eq C[1, 1]
expect(hero.respawn_location.map).to eq map
expect(hero.respawn_location.coords).to eq C[1, 2]
end

it "sets respawn to start location for no respawn_location" do
player = Player.new(location: Location.new(map, C[1, 1]))
expect(player.respawn_location.map).to eq map
expect(player.respawn_location.coords).to eq C[1, 1]
end

context "places the player in the default map & location" do
Expand Down Expand Up @@ -346,7 +356,7 @@
end
# Newb should die and go to respawn location.
expect(newb.gold).to eq 25
expect(newb.location.coords).to eq C[1, 1]
expect(newb.location.coords).to eq C[2, 1]
end

it "should allow the stronger player to win as the attacker" do
Expand All @@ -355,7 +365,7 @@
end
# Weaker Player should die and go to respawn location.
expect(newb.gold).to eq 25
expect(newb.location.coords).to eq C[1, 1]
expect(newb.location.coords).to eq C[2, 1]
# Stronger Player should get weaker Players gold
expect(dude.gold).to eq (35)
end
Expand All @@ -366,25 +376,26 @@
end
# Weaker Player should die and go to respawn location.
expect(newb.gold).to eq 25
expect(newb.location.coords).to eq C[1, 1]
expect(newb.location.coords).to eq C[2, 1]
# Stronger Player should get weaker Players gold
expect(dude.gold).to eq (35)
end

end

context "die" do
it "moves the player back to the map's regen location" do
dude.move_down
expect(dude.location.coords).to eq C[2, 1]
dude.die
expect(dude.location.coords).to eq map.regen_coords
it "moves the player back to his/her respawn location" do
newb.move_left
expect(newb.location.coords).to eq C[1, 0]
newb.die
expect(newb.location.map).to eq map
expect(newb.location.coords).to eq C[2, 1]
end

it "recovers the player's HP to max" do
dude.set_stats(hp: 0)
dude.die
expect(dude.stats[:hp]).to eq dude.stats[:max_hp]
newb.set_stats(hp: 0)
newb.die
expect(newb.stats[:hp]).to eq newb.stats[:max_hp]
end
end

Expand Down
5 changes: 1 addition & 4 deletions spec/goby/map/map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
RSpec.describe Map do

let(:lake) { Map.new(name: "Lake",
tiles: [ [ Tile.new, Tile.new(passable: false) ] ],
regen_coords: C[0,1]) }
tiles: [ [ Tile.new, Tile.new(passable: false) ] ] ) }

context "constructor" do
it "has the correct default parameters" do
map = Map.new
expect(map.name).to eq "Map"
expect(map.tiles[0][0].passable).to be true
expect(map.regen_coords).to eq C[0,0]
end

it "correctly assigns custom parameters" do
expect(lake.name).to eq "Lake"
expect(lake.tiles[0][0].passable).to be true
expect(lake.tiles[0][1].passable).to be false
expect(lake.regen_coords).to eq C[0,1]
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/goby/world_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
Tile.new(events: [NPC.new]),
Tile.new(events: [Event.new(visible: false)]) ],
[ Tile.new(events: [Shop.new, NPC.new]),
Tile.new(events: [Event.new(visible: false), Shop.new, NPC.new]) ] ],
regen_coords: C[0,0]) }
Tile.new(events: [Event.new(visible: false), Shop.new, NPC.new]) ] ] ) }

let!(:player) { Player.new(stats: { max_hp: 10, hp: 3 },
inventory: [ C[Food.new(name: "Banana", recovers: 5), 1],
Expand Down

0 comments on commit c5b3552

Please sign in to comment.