Merge branch 'equipment' into doc

This commit is contained in:
Eichhornchen
2021-01-06 14:49:09 +01:00
8 changed files with 341 additions and 40 deletions

View File

@ -7,6 +7,8 @@ from random import choice, randint
from typing import List, Optional, Any, Dict, Tuple
from queue import PriorityQueue
from functools import reduce
from random import choice, randint, choices
from typing import List, Optional, Any
from .display.texturepack import TexturePack
from .translations import gettext as _
@ -152,7 +154,8 @@ class Map:
tile = self.tiles[y][x]
if tile.can_walk():
break
entity = choice(Entity.get_all_entity_classes())()
entity = choices(Entity.get_all_entity_classes(),\
weights = Entity.get_weights(), k=1)[0]()
entity.move(y, x)
self.add_entity(entity)
@ -421,11 +424,20 @@ class Entity:
"""
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
Rabbit, TeddyBear
Rabbit, TeddyBear, GiantSeaEagle
from squirrelbattle.entities.friendly import Merchant, Sunflower, \
Trumpet
Trumpet
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear,
Sunflower, Tiger, Merchant, Trumpet]
Sunflower, Tiger, Merchant, GiantSeaEagle, Trumpet]
@staticmethod
def get_weights() -> list:
"""
Returns a weigth list associated to the above function, to
be used to spawn random entities with a certain probability.
"""
return [3, 5, 6, 5, 5, 5,
5, 4, 4, 1]
@staticmethod
def get_all_entity_classes_in_a_dict() -> dict:
@ -434,11 +446,11 @@ class Entity:
"""
from squirrelbattle.entities.player import Player
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
TeddyBear
TeddyBear, GiantSeaEagle
from squirrelbattle.entities.friendly import Merchant, Sunflower, \
Trumpet
Trumpet
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, \
Heart, Sword
Heart, Sword, Shield, Chestplate, Helmet, RingCritical, RingXP
return {
"Tiger": Tiger,
"Bomb": Bomb,
@ -452,6 +464,12 @@ class Entity:
"Sunflower": Sunflower,
"Sword": Sword,
"Trumpet": Trumpet,
"Eagle": GiantSeaEagle,
"Shield": Shield,
"Chestplate": Chestplate,
"Helmet": Helmet,
"RingCritical": RingCritical,
"RingXP": RingXP,
}
def save_state(self) -> dict:
@ -478,11 +496,12 @@ class FightingEntity(Entity):
dexterity: int
constitution: int
level: int
critical: int
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
strength: int = 0, intelligence: int = 0, charisma: int = 0,
dexterity: int = 0, constitution: int = 0, level: int = 0,
*args, **kwargs) -> None:
critical: int = 0, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.maxhealth = maxhealth
self.health = maxhealth if health is None else health
@ -492,6 +511,7 @@ class FightingEntity(Entity):
self.dexterity = dexterity
self.constitution = constitution
self.level = level
self.critical = critical
@property
def dead(self) -> bool:
@ -505,21 +525,28 @@ class FightingEntity(Entity):
The entity deals damage to the opponent
based on their respective stats.
"""
diceroll = randint(0, 100)
damage = self.strength
string = " "
if diceroll <= self.critical: # It is a critical hit
damage *= 4
string = _(" It's a critical hit! ")
return _("{name} hits {opponent}.")\
.format(name=_(self.translated_name.capitalize()),
opponent=_(opponent.translated_name)) + " " + \
opponent.take_damage(self, self.strength)
opponent=_(opponent.translated_name)) + string + \
opponent.take_damage(self, damage)
def take_damage(self, attacker: "Entity", amount: int) -> str:
"""
The entity takes damage from the attacker
based on their respective stats.
"""
self.health -= amount
damage = max(0, amount - self.constitution)
self.health -= damage
if self.health <= 0:
self.die()
return _("{name} takes {amount} damage.")\
.format(name=self.translated_name.capitalize(), amount=str(amount))\
return _("{name} takes {damage} damage.")\
.format(name=self.translated_name.capitalize(), damage=str(damage))\
+ (" " + _("{name} dies.")
.format(name=self.translated_name.capitalize())
if self.health <= 0 else "")
@ -576,10 +603,10 @@ class InventoryHolder(Entity):
"""
for i in range(len(inventory)):
if isinstance(inventory[i], dict):
inventory[i] = self.dict_to_inventory(inventory[i])
inventory[i] = self.dict_to_item(inventory[i])
return inventory
def dict_to_inventory(self, item_dict: dict) -> Entity:
def dict_to_item(self, item_dict: dict) -> Entity:
"""
Translates a dictionnary that contains the state of an item
into an item object.
@ -602,13 +629,15 @@ class InventoryHolder(Entity):
"""
Adds an object to the inventory.
"""
self.inventory.append(obj)
if obj not in self.inventory:
self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None:
"""
Removes an object from the inventory.
"""
self.inventory.remove(obj)
if obj in self.inventory:
self.inventory.remove(obj)
def change_hazel_balance(self, hz: int) -> None:
"""