Skip to content

Commit

Permalink
Add resting at the inn, and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ragesoss committed Mar 20, 2019
1 parent 8641645 commit 6d86557
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
21 changes: 21 additions & 0 deletions actions/action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Action
def self.success
Success.new
end

def self.failure
Failure.new
end

class Success
def success?
true
end
end

class Failure
def success?
false
end
end
end
13 changes: 13 additions & 0 deletions actions/inn_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative './action'

class InnActions < Action
def self.rest cost
if $adventurer.money >= cost
$adventurer.money -= cost
$adventurer.heal!
return success
else
return failure
end
end
end
37 changes: 14 additions & 23 deletions actions/market_actions.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
class MarketActions
require_relative './action'

class MarketActions < Action
def self.buy good, market
price = market.send(good)[:price]
if $adventurer.money >= price
$adventurer.inventory.add(good, 1)
$adventurer.money -= price
market.remove(good, 1)
return Success.new
if $adventurer.can_afford? price
$adventurer.inventory.add good, 1
$adventurer.pay price
market.remove good, 1
return success
else
return Failure.new
return failure
end
end

def self.sell good, market
price = market.send(good)[:price]
if $adventurer.inventory.goods[good] > 0
$adventurer.inventory.remove(good, 1)
$adventurer.money += price
market.add(good, 1)
return Success.new
$adventurer.inventory.remove good, 1
$adventurer.get_paid price
market.add good, 1
return success
else
return Failure.new
end
end

class Success
def success?
true
end
end
class Failure
def success?
false
return failure
end
end
end
17 changes: 17 additions & 0 deletions concepts/adventurer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ class Adventurer

def initialize name
@name = name
@max_life = 20
@life = 20
@money = 100
@inventory = Inventory.new(Hash.new 0)
end

def heal!
@life = @max_life
end

def can_afford? cost
money >= cost
end

def pay cost
@money -= cost
end

def get_paid amount
@money += amount
end
end
4 changes: 4 additions & 0 deletions concepts/town.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def describe
def market
@market ||= Market.new(self)
end

def inn_cost
@inn_cost ||= rand(5..15)
end
end
8 changes: 7 additions & 1 deletion interfaces/in_town.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../actions/market_actions'
require_relative '../actions/inn_actions'

class InTown < Interface
attr_reader :town
Expand Down Expand Up @@ -39,7 +40,12 @@ def set_overview
end

def rest
puts 'resting'
result = InnActions.rest town.inn_cost
if result.success?
@result = Gosu::Image.from_text("You have #{$adventurer.money} #{MONEY} left. You're at full life.", 30)
else
@result = Gosu::Image.from_text("Sorry, you don't have enough #{MONEY}.", 30)
end
end

SHOP_ACTIONS = {
Expand Down

0 comments on commit 6d86557

Please sign in to comment.