Skip to content

Commit

Permalink
Adds XP at the end of a battle and checks for leveling up
Browse files Browse the repository at this point in the history
  • Loading branch information
jrambold committed Jul 17, 2018
1 parent eb43c16 commit a4200c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
51 changes: 48 additions & 3 deletions lib/goby/battle/battle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ def determine_winner
end
end

#If @entity_a is dead return @entity_b, otherwise return @entity_a
entity_a.stats[:hp] <=0 ? entity_b : entity_a
# If @entity_a is dead set winner to @entity_b, otherwise @entity_a
winner = entity_a.stats[:hp] <=0 ? entity_b : entity_a
# Sets loser to entity with 0 or less hp
loser = entity_a.stats[:hp] <=0 ? entity_a : entity_b
if winner.class != Monster
# Gain XP based on opponent
gain_xp(winner, loser)
# Check if winner can level up
winner.check_level
# return winner
end
winner
end

private
Expand All @@ -64,7 +74,42 @@ def someone_dead?
entity_a.stats[:hp] <= 0 || entity_b.stats[:hp] <= 0
end

# Gain XP based on defeated opponent
#
# @param [Entity] the winner of the battle
# @param [Entity] the loser of the battle
# @return [Integer] the amount of xp gained
def gain_xp(winner, loser)
# Monsters should not level up fighting players
if winner.class != Monster
# Different Mechanics for monster vs pvp
if loser.class == Monster
# Gain monsters XP
xp = winner[:xp] + loser[:xp]
else
# Calculate XP gain based on level difference
level_diff = winner[:level] - loser[:level]
# About 10 victories to level up
if level_diff == 0
xp = (winner.nextLevel(winner[:level]) * 0.01).floor
# 10% more XP for each level down
elsif level_diff > 0
xp = (winner.nextLevel(winner[:level]) * 0.01 * level_diff).floor
else
amount = 0.01-0.001 * level_diff
# Minimum XP gain
if amount <= 0
amount = 0.001
end
xp = (winner.nextLevel(winner[:level]) * amount).floor
end
end
winner.set_stats(xp: xp)
end
xp
end

attr_reader :entity_a, :entity_b
end

end
end
2 changes: 2 additions & 0 deletions lib/goby/entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ def set_stats(passed_in_stats)
end

#calculates XP needed to reach the next level
#
# @param [Integer] :current level of the entity
def nextLevel(level)
#set for growth rate
exponent = 1.5
Expand Down

0 comments on commit a4200c2

Please sign in to comment.