Skip to content

Commit

Permalink
feat(#267): refactored FightItems
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Nov 10, 2024
1 parent 4a7cfcf commit c47976d
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 126 deletions.
4 changes: 2 additions & 2 deletions pokete_classes/fight/attack_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, fightmap: FightMap):
self.fightmap: FightMap = fightmap

@staticmethod
def get_random_factor(attack, attacker) -> int:
def get_random_factor(attack, attacker) -> float:
return random.choices(
[0, 0.75, 1, 1.26],
weights=[attack.miss_chance
Expand All @@ -25,7 +25,7 @@ def get_random_factor(attack, attacker) -> int:
)[0]

@staticmethod
def get_base_effectivity(defender: Poke, attack: Attack) -> int:
def get_base_effectivity(defender: Poke, attack: Attack) -> float:
return (
1.3 if defender.type.name in attack.type.effective else 0.5
if defender.type.name in attack.type.ineffective else 1
Expand Down
9 changes: 5 additions & 4 deletions pokete_classes/fight/fight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from release import SPEED_OF_TIME
from .attack_process import AttackProcess
from .fight_decision import Result
from .fight_items import FightItems
from .fightmap import FightMap
from .providers import Provider
from .items import fight_items
from ..achievements import achievements
from ..attack import Attack
from ..audio import audio
Expand All @@ -22,7 +22,6 @@ class Fight:
def __init__(self):
self.fightmap: FightMap = FightMap(tss.height - 1, tss.width)
self.providers: list[Provider] = []
self.fight_items = FightItems()
self.attack_process = AttackProcess(self.fightmap)

def initial_player_index(self):
Expand Down Expand Up @@ -99,8 +98,10 @@ def __call__(self, ctx: Context, providers: list[Provider]):
return player
case Result.ITEM:
item: InvItem = attack_result.item_value
fight_func = getattr(self.fight_items, item.func)
match fight_func(self.fightmap, player, enem):
fight_item = fight_items.get(item.func, None)
if fight_item is None:
raise Exception(f"fight_item doesnt exist {item.func}")
match fight_item.use(self.fightmap, player, enem):
case 1:
continue # This is the sole reason for the while loop on top
case 2:
Expand Down
119 changes: 0 additions & 119 deletions pokete_classes/fight/fight_items.py

This file was deleted.

13 changes: 13 additions & 0 deletions pokete_classes/fight/items/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .item import FightItem
from .balls import PoketeBall, SuperBall, HyperBall
from .healing_potions import HealingPotion, SuperPotion
from .ap_potion import ApPotion

fight_items: dict[str|None, FightItem] = {
"heal_potion": HealingPotion(),
"super_potion": SuperPotion(),
"poketeball": PoketeBall(),
"superball": SuperBall(),
"hyperball": HyperBall(),
"ap_potion": ApPotion()
}
13 changes: 13 additions & 0 deletions pokete_classes/fight/items/ap_potion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging
from pokete_classes.fight.fightmap.fightmap import FightMap
from pokete_classes.fight.items.item import FightItem, RoundContinuation
from pokete_classes.fight.providers import Provider


class ApPotion(FightItem):
def use(self, fightmap: FightMap, obj, enem: Provider) -> RoundContinuation:
obj.remove_item("ap_potion")
for atc in obj.curr.attack_obs:
atc.set_ap(atc.max_ap)
logging.info("[Fighitem][ap_potion] Used")
return RoundContinuation.ENEMY_ATTACK
81 changes: 81 additions & 0 deletions pokete_classes/fight/items/balls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from abc import ABC
import logging
import random
import time

from pokete_classes.achievements import achievements
from pokete_classes.fight.fightmap.fightmap import FightMap
from pokete_classes.fight.providers import Provider
from pokete_classes.audio import audio
from ... import ob_maps as obmp
from pokete_classes.fight.providers import NatureProvider, Provider
from .item import FightItem, RoundContinuation
from ...asset_service.service import asset_service
from release import SPEED_OF_TIME


class GenericPokeBall(FightItem, ABC):
def __init__(self, chance:int, name: str):
self.chance = chance
self.name = name

def use(self, fightmap: FightMap, obj, enem:Provider) -> RoundContinuation:
"""Throws a ball
ARGS:
obj: The players Poke object
enem: The enemys Poke object
chance: The balls catch chance
name: The balls name
RETURNS:
1: The continue the attack round
2: The win the game
None: To let the enemy attack"""
if not isinstance(enem, NatureProvider):
fightmap.outp.outp("You can't do that in a duel!")
return RoundContinuation.CONTINUE_ATTACK
fightmap.outp.rechar(f"You threw a {self.name.capitalize()}!")
fightmap.fast_change(
[enem.curr.ico, fightmap.deadico1, fightmap.deadico2,
fightmap.pball], enem.curr.ico)
time.sleep(SPEED_OF_TIME * random.choice([1, 2, 3, 4]))
obj.remove_item(self.name)
catch_chance = 20 if obj.map == obmp.ob_maps.get("playmap_1", None) else 0
for effect in enem.curr.effects:
catch_chance += effect.catch_chance
if random.choices([True, False],
weights=[(enem.curr.full_hp / enem.curr.hp)
* self.chance + catch_chance,
enem.curr.full_hp], k=1)[0]:
audio.switch("xDeviruchi - Decisive Battle (End).mp3")
obj.add_poke(enem.curr, caught_with=self.name)
fightmap.outp.outp(f"You caught {enem.curr.name}!")
time.sleep(SPEED_OF_TIME * 2)
fightmap.pball.remove()
fightmap.clean_up(obj, enem)
obj.balls_label_rechar()
logging.info("[Fighitem][%s] Caught %s", self.name, enem.curr.name)
achievements.achieve("first_poke")
if all(poke in obj.caught_pokes for poke in
asset_service.get_base_assets().pokes):
achievements.achieve("catch_em_all")
return RoundContinuation.EXIT
fightmap.outp.outp("You missed!")
fightmap.show()
fightmap.pball.remove()
enem.curr.ico.add(fightmap, enem.curr.ico.x, enem.curr.ico.y)
fightmap.show()
logging.info("[Fighitem][%s] Missed", self.name)
return RoundContinuation.ENEMY_ATTACK

class PoketeBall(GenericPokeBall):
def __init__(self):
super().__init__(1, "poketeball")


class SuperBall(GenericPokeBall):
def __init__(self):
super().__init__(6, "superball")

class HyperBall(GenericPokeBall):
def __init__(self):
super().__init__(1000, "hyperball")
27 changes: 27 additions & 0 deletions pokete_classes/fight/items/healing_potions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from abc import ABC
import logging
from pokete_classes.fight.fightmap.fightmap import FightMap
from pokete_classes.fight.items.item import FightItem, RoundContinuation
from pokete_classes.fight.providers import Provider


class GenericeHealingPotion(FightItem, ABC):
def __init__(self, hp: int, name: str):
self.hp = hp
self.name = name

def use(self, fightmap: FightMap, obj, enem: Provider) -> RoundContinuation:
obj.remove_item(self.name)
obj.curr.oldhp = obj.curr.hp
obj.curr.hp = min(obj.curr.full_hp, obj.curr.hp + self.hp)
obj.curr.hp_bar.update(obj.curr.oldhp)
logging.info("[Fighitem][%s] Used", self.name)
return RoundContinuation.ENEMY_ATTACK

class HealingPotion(GenericeHealingPotion):
def __init__(self):
super().__init__(5, "healing_potion")

class SuperPotion(GenericeHealingPotion):
def __init__(self):
super().__init__(15, "super_potion")
18 changes: 18 additions & 0 deletions pokete_classes/fight/items/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from abc import ABC, abstractmethod
from enum import Enum, auto

from pokete_classes.fight.fightmap.fightmap import FightMap
from pokete_classes.fight.providers import Provider


class RoundContinuation(Enum):
CONTINUE_ATTACK = auto()
EXIT = auto()
ENEMY_ATTACK = auto()


class FightItem(ABC):

@abstractmethod
def use(self, fightmap: FightMap, obj, enem:Provider) -> RoundContinuation:
pass
2 changes: 1 addition & 1 deletion pokete_classes/inv/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class InvItem:
_fn: The associated method name in FightItems"""

def __init__(
self, name:str, pretty_name: str, desc: str, price: int | None, _fn=None
self, name:str, pretty_name: str, desc: str, price: int | None, _fn: str | None=None
):
self.name: str = name
self.pretty_name = pretty_name
Expand Down

0 comments on commit c47976d

Please sign in to comment.