82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
"""Player: stats, inventory, equipment, and leveling."""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
from data.constants import LEVEL_TABLE, LEVEL_CAP, PLAYER_BASE, STRENGTH_POTION_POWER
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Player:
|
|
x: int = 0
|
|
y: int = 0
|
|
name: str = "Adventurer"
|
|
sprite_key: str = "player"
|
|
blocks: bool = True
|
|
hp: int = PLAYER_BASE["hp"]
|
|
max_hp: int = PLAYER_BASE["max_hp"]
|
|
power: int = PLAYER_BASE["power"]
|
|
armor: int = PLAYER_BASE["armor"]
|
|
accuracy: int = PLAYER_BASE["accuracy"]
|
|
evasion: int = PLAYER_BASE["evasion"]
|
|
crit_chance: float = PLAYER_BASE["crit_chance"]
|
|
speed: int = PLAYER_BASE["speed"]
|
|
level: int = 1
|
|
xp: int = 0
|
|
inventory: list = field(default_factory=list)
|
|
equipped_weapon: Any = None
|
|
equipped_armor: Any = None
|
|
effect_timers: dict = field(default_factory=dict)
|
|
|
|
@property
|
|
def weapon_power(self) -> int:
|
|
# combat.roll_attack reads attacker.power + getattr(weapon_power, 0), so the
|
|
# equipped weapon and the strength-potion bonus must surface here for melee.
|
|
wp = 0
|
|
if self.equipped_weapon is not None:
|
|
wp += self.equipped_weapon.stats.get("power", 0)
|
|
if self.effect_timers.get("strength", 0) > 0:
|
|
wp += STRENGTH_POTION_POWER
|
|
return wp
|
|
|
|
def total_power(self) -> int:
|
|
return self.power + self.weapon_power
|
|
|
|
def total_armor(self) -> int:
|
|
a = self.armor
|
|
if self.equipped_armor is not None:
|
|
a += self.equipped_armor.stats.get("armor", 0)
|
|
return a
|
|
|
|
def equip(self, item: Any) -> None:
|
|
if item is None:
|
|
return
|
|
category = getattr(item, "category", "")
|
|
if category == "weapon":
|
|
if self.equipped_weapon is not None:
|
|
self.inventory.append(self.equipped_weapon)
|
|
self.equipped_weapon = item
|
|
elif category == "armor":
|
|
if self.equipped_armor is not None:
|
|
self.inventory.append(self.equipped_armor)
|
|
self.equipped_armor = item
|
|
else:
|
|
self.inventory.append(item)
|
|
|
|
def gain_xp(self, amount: int) -> int:
|
|
self.xp += amount
|
|
gained = 0
|
|
while self.level < LEVEL_CAP and self.level - 1 < len(LEVEL_TABLE):
|
|
need = LEVEL_TABLE[self.level - 1][0]
|
|
if self.xp < need:
|
|
break
|
|
_need, hp_gain, pow_gain = LEVEL_TABLE[self.level - 1]
|
|
self.level += 1
|
|
self.max_hp += hp_gain
|
|
self.power += pow_gain
|
|
gained += 1
|
|
if gained:
|
|
self.hp = self.max_hp
|
|
return gained
|