Skip to content

Commit

Permalink
Show results from reach round of a fight
Browse files Browse the repository at this point in the history
  • Loading branch information
ragesoss committed Mar 24, 2019
1 parent eedb3b5 commit 5cffc85
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
19 changes: 13 additions & 6 deletions actions/action.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
class Action
def self.success
Success.new
def self.success text = nil
Success.new text
end

def self.failure
Failure.new
def self.failure text = nil
Failure.new text
end

class Success
class Result
attr_reader :text
def initialize text
@text = text
end
end

class Success < Result
def success?
true
end
end

class Failure
class Failure < Result
def success?
false
end
Expand Down
54 changes: 43 additions & 11 deletions actions/encounter_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@

class EncounterActions < Action
def self.fight enemy
if hit? enemy
enemy.take_damage $adventurer.damage
end
return success unless enemy.alive?
if hit? $adventurer
$adventurer.take_damage enemy.damage
end
return failure
FightRound.new(enemy).call
end

def self.run_from enemy
if hit? $adventurer
$adventurer.take_damage enemy.damage
return failure
damage = enemy.damage
$adventurer.take_damage damage
return failure("You can't get away! You were hit, and you lost #{damage} life.")
else
return success
return success("You got away!")
end
end

Expand All @@ -45,4 +39,42 @@ def self.loot_body character
$adventurer.inventory.add good, number
end
end

class FightRound
def initialize enemy
@enemy = enemy
end

def call
@hit_enemy = EncounterActions.hit? @enemy
if @hit_enemy
@damage_dealt = $adventurer.damage
@enemy.take_damage @damage_dealt
end
return EncounterActions.success(result) unless @enemy.alive?
@hit_adventurer = EncounterActions.hit? $adventurer
if @hit_adventurer
@damage_taken = @enemy.damage
$adventurer.take_damage @damage_taken
end
return EncounterActions.failure(result)
end

def result
@result = String.new
if @hit_enemy
@result += "You hit for #{@damage_dealt}. "
else
@result += "You missed. "
end

if @enemy.dead?
@result += "You killed #{@enemy.specific_name}!"
elsif @hit_adventurer
@result += "The enemy hit you for #{@damage_taken}."
else
@result += "The enemy missed."
end
end
end
end
3 changes: 2 additions & 1 deletion interfaces/encounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update_status

def fight
result = EncounterActions.fight @enemy

@result = Gosu::Image.from_text(result.text, 30)
return set_victory if result.success?
set_defeat if $adventurer.dead?
update_status
Expand All @@ -53,6 +53,7 @@ def draw
@description.draw 10, 50, 0
@status.draw 10, 100, 0
@enemy.image&.draw 800, 100, 0
@result&.draw 10, 640, 0
@options.each.with_index do |option, i|
style = @selected_option == i ? { bold: true } : {}
Gosu::Image.from_text(option[1], 30, style).draw 50, 160 + 50*i, 0
Expand Down

0 comments on commit 5cffc85

Please sign in to comment.