squirrel-battle/squirrelbattle/entities/player.py

96 lines
3.4 KiB
Python
Raw Normal View History

2020-11-27 15:33:17 +00:00
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
# SPDX-License-Identifier: GPL-3.0-or-later
2020-11-10 23:50:47 +00:00
from random import randint
from ..interfaces import FightingEntity, InventoryHolder
2020-11-06 14:33:26 +00:00
class Player(InventoryHolder, FightingEntity):
"""
The class of the player.
"""
2020-11-06 20:23:17 +00:00
current_xp: int = 0
max_xp: int = 10
2020-11-06 17:12:17 +00:00
def __init__(self, name: str = "player", maxhealth: int = 20,
strength: int = 5, intelligence: int = 1, charisma: int = 1,
dexterity: int = 1, constitution: int = 1, level: int = 1,
current_xp: int = 0, max_xp: int = 10, inventory: list = None,
2021-01-07 15:34:12 +00:00
hazel: int = 42, vision: int = 5, *args, **kwargs) \
-> None:
super().__init__(name=name, maxhealth=maxhealth, strength=strength,
2020-11-18 23:10:37 +00:00
intelligence=intelligence, charisma=charisma,
dexterity=dexterity, constitution=constitution,
level=level, *args, **kwargs)
self.current_xp = current_xp
self.max_xp = max_xp
self.inventory = self.translate_inventory(inventory or [])
self.paths = dict()
self.hazel = hazel
self.vision = vision
2020-11-10 21:02:41 +00:00
def move(self, y: int, x: int) -> None:
"""
Moves the view of the map (the point on which the camera is centered)
according to the moves of the player.
2020-11-10 21:02:41 +00:00
"""
super().move(y, x)
self.map.currenty = y
self.map.currentx = x
self.recalculate_paths()
self.map.compute_visibility(self.y, self.x, self.vision)
2020-11-10 21:02:41 +00:00
2020-11-06 20:15:09 +00:00
def level_up(self) -> None:
"""
Add as many levels as possible to the player.
"""
2020-11-06 20:23:17 +00:00
while self.current_xp > self.max_xp:
2020-11-06 20:15:09 +00:00
self.level += 1
2020-11-06 20:23:17 +00:00
self.current_xp -= self.max_xp
2020-11-06 20:15:09 +00:00
self.max_xp = self.level * 10
2020-11-10 23:50:47 +00:00
self.health = self.maxhealth
self.strength = self.strength + 1
# TODO Remove it, that's only fun
2020-11-11 15:23:27 +00:00
self.map.spawn_random_entities(randint(3 * self.level,
10 * self.level))
2020-11-06 20:15:09 +00:00
def add_xp(self, xp: int) -> None:
"""
Adds some experience to the player.
If the required amount is reached, the player levels up.
"""
2020-11-06 20:15:09 +00:00
self.current_xp += xp
2020-11-06 17:12:17 +00:00
self.level_up()
2020-11-10 23:50:47 +00:00
2020-11-11 15:47:19 +00:00
# noinspection PyTypeChecker,PyUnresolvedReferences
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
-> bool:
2020-11-10 23:50:47 +00:00
"""
If the player tries to move but a fighting entity is there,
the player fights this entity.
If the entity dies, the player is rewarded with some XP
2020-11-10 23:50:47 +00:00
"""
2020-11-11 00:17:00 +00:00
# Don't move if we are dead
if self.dead:
return False
2020-11-10 23:50:47 +00:00
for entity in self.map.entities:
2020-11-11 15:47:19 +00:00
if entity.y == y and entity.x == x:
if entity.is_fighting_entity():
self.map.logs.add_message(self.hit(entity))
2020-11-11 15:47:19 +00:00
if entity.dead:
self.add_xp(randint(3, 7))
return True
elif entity.is_item():
entity.hold(self)
return super().check_move(y, x, move_if_possible)
def save_state(self) -> dict:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["current_xp"] = self.current_xp
d["max_xp"] = self.max_xp
return d