activerpg/entities/player.rb

76 lines
2.2 KiB
Ruby

class Player < Entity
def initialize(mc, doc=nil)
super(mc, doc)
end
def get_default_doc()
q = {
'type' => :player,
'name' => nil,
'location' => nil,
'exp' => 0,
'last_line' => nil
}
return q
end
def gain_exp(amount)
self.set_exp(self.get('exp') + amount)
end
# be careful with this one. very rare use case when you want
# to use this directly from a bot or playground
def set_exp(amount)
self.set({'exp' => amount})
end
# yeah this needs a WHOLE LOT of cleanup and rethinking....
def resolve(foe)
result = nil
# call the resolve method for anything that's not a player.
if foe.get('type') != :player then
result = foe.resolve(self)
# Handle player v player here.... can't exactly recall resolve without
# recursing forever
# most of this should probably be somehow abstracted into Entity...
# I'm ok with this for now.
else
max_player_attack = self.get('exp')
max_foe_attack = foe.get('exp')
player_str = self.get('name')
player_str = player_str + "[" + max_player_attack.to_s + "str] "
result = player_str + "felt the need to fight "
result = result + foe.get('name')
result = result + "[" + max_foe_attack.to_s + "str]" + "! "
player_attack = 1+rand(max_player_attack)
foe_attack = 1+rand(foe.get('exp'))
result = result + "[" + player_attack.to_s + "dmg vs " + foe_attack.to_s + "dmg]! "
if player_attack > foe_attack then
# Max of 25% of foe exp, minimum of 1. Cannot pass a 0 into rand without
# getting a float, so this is a bit cludgy. if rand gets a 1 it always returns
# 0 so the floor is effectively 1 :/
max_xp_gain = (foe.get('exp').to_f * 0.25).to_i + 1
xp = 1 + rand(max_xp_gain)
result = result + foe.get('name') + " lost! "
result = result + self.get('name') + " wins and earns " + xp.to_s + "xp!"
gain_exp(xp)
elsif player_attack < foe_attack then
result = result + foe.get("name") + " wins and saunters away"
else
result = result + "They tied! They shake hands and agree to disagree and part ways."
end
end
return result
end
end