From 46db2834ccb4760815978869565ad7d2f3c7fc6d Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Wed, 17 Jul 2024 17:13:26 +0200 Subject: [PATCH] feat(#293): Added context to timer --- pokete.py | 11 ++++---- pokete_classes/inv/__init__.py | 2 +- pokete_classes/inv/buy.py | 49 +++++++++++++++++----------------- pokete_classes/movemap.py | 4 +-- pokete_classes/timer.py | 21 +++++++-------- 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/pokete.py b/pokete.py index 74b845dd..16fd0a24 100755 --- a/pokete.py +++ b/pokete.py @@ -22,7 +22,7 @@ import release from pokete_classes import animations, loops from pokete_classes.context import Context -from pokete_classes.inv import inv, Buy +from pokete_classes.inv import inv, buy from pokete_classes.menu import menu from pokete_classes.periodic_events import MovingGrassEvent, MovingWaterEvent, \ TreatNPCEvent @@ -342,7 +342,7 @@ def action(self, ob): mvp.movemap.text(mvp.movemap.bmap.inner.x - mvp.movemap.x + 9, 3, ["Welcome to the Pokete-Shop", "Wanna buy something?"]) - buy() + buy(Context(None, mvp.movemap, mvp.movemap, figure)) mvp.movemap.full_show(init=True) mvp.movemap.text(mvp.movemap.bmap.inner.x - mvp.movemap.x + 9, 3, ["Have a great day!"]) @@ -737,9 +737,9 @@ def _game(_map: PlayMap): Action.MAP: [RoadMap(), (ctx,)], Action.INVENTORY: [inv, (ctx,)], Action.POKEDEX: [Dex(), (ctx,)], - Action.CLOCK: [timer.clock, (mvp.movemap,)], + Action.CLOCK: [timer.clock, (ctx,)], Action.MENU: [menu, (ctx,)], - Action.HELP: [Help(), (mvp.movemap,)] + Action.HELP: [Help(), (ctx,)] } if _map.weather is not None: notifier.notify("Weather", "Info", _map.weather.info) @@ -1270,13 +1270,12 @@ def recogniser(): detail.detail = detail.Detail(tss.height - 1, tss.width) RoadMap.check_maps() deck.deck = deck.Deck(tss.height - 1, tss.width, figure, abb_funcs) - buy = Buy(figure, mvp.movemap) pokete_care.from_dict(session_info.get("pokete_care", { "entry": 0, "poke": None, })) timer.time = timer.Time(session_info.get("time", 0)) - timer.clock = timer.Clock(timer.time, mvp.movemap) + timer.clock = timer.Clock(timer.time) HighGrass.figure = figure Poketeball.figure = figure _ev.set_emit_fn(timer.time.emit_input) diff --git a/pokete_classes/inv/__init__.py b/pokete_classes/inv/__init__.py index ee7fd66e..1d81e781 100644 --- a/pokete_classes/inv/__init__.py +++ b/pokete_classes/inv/__init__.py @@ -1,3 +1,3 @@ from .inv import inv from .items import invitems -from .buy import Buy +from .buy import buy diff --git a/pokete_classes/inv/buy.py b/pokete_classes/inv/buy.py index 21e89ad4..90a650d3 100644 --- a/pokete_classes/inv/buy.py +++ b/pokete_classes/inv/buy.py @@ -2,32 +2,28 @@ import scrap_engine as se +from pokete_classes.context import Context from util import liner from .box import InvBox from .items import invitems +from ..ui import Overview from ..input import ACTION_UP_DOWN, Action, get_action from ..ui.elements import ChooseBox from .. import movemap as mvp, loops -class Buy: - """Menu to buy items in, is triggered in shop - Args: - figure: Figure object - _map: The se.Map the menu is shown on""" +class Buy(Overview): + """Menu to buy items in, is triggered in shop""" - def __init__(self, figure, _map): - self.map = _map - self.box = ChooseBox(_map.height - 3, 35, "Shop") + def __init__(self): + self.box = ChooseBox(50, 35, "Shop") self.box2 = InvBox(7, 21, overview=self) - self.fig = figure - self.map = _map self.items = [invitems.poketeball, invitems.superball, invitems.healing_potion, invitems.super_potion, invitems.ap_potion] self.box.add_c_obs([se.Text(f"{obj.pretty_name} : {obj.price}$") for obj in self.items]) - self.money_label = se.Text(f"{figure.get_money()}$") + self.money_label = se.Text("0$") self.desc_label = se.Text(" ") # adding self.box.add_ob(self.money_label, @@ -37,18 +33,21 @@ def __init__(self, figure, _map): def resize_view(self): """Manages recursive view resizing""" self.box.remove() - self.map.resize_view() - self.box.resize(self.map.height - 3, 35) - self.box.add(self.map, self.map.width - self.box.width, 0) + self.box.map.resize_view() + self.box.resize(self.box.map.height - 3, 35) + self.box.add(self.box.map, self.box.map.width - self.box.width, 0) mvp.movemap.full_show() - def __call__(self): + def __call__(self, ctx: Context): """Opens the buy menu""" - self.box.resize(self.map.height - 3, 35) - with self.box.add(self.map, self.map.width - 35, 0): - self.box2.add(self.map, self.box.x - 19, 3) + self.money_label.rechar(f"{ctx.figure.get_money()}$") + self.box.set_ob(self.money_label, + self.box.width - 2 - len(self.money_label.text), 0) + self.box.resize(ctx.map.height - 3, 35) + with self.box.add(ctx.map, ctx.map.width - 35, 0): + self.box2.add(ctx.map, self.box.x - 19, 3) self.rechar() - self.map.show() + ctx.map.show() while True: action = get_action() if action.triggers(*ACTION_UP_DOWN): @@ -58,11 +57,11 @@ def __call__(self): break elif action.triggers(Action.ACCEPT): obj = self.items[self.box.index.index] - if self.fig.get_money() - obj.price >= 0: - self.fig.add_money(-obj.price) - self.fig.give_item(obj.name) - loops.std(box=self.box2) - self.map.show() + if ctx.figure.get_money() - obj.price >= 0: + ctx.figure.add_money(-obj.price) + ctx.figure.give_item(obj.name) + loops.std(box=self.box2, pevm=ctx.pevm) + ctx.map.full_show() self.box2.remove() def rechar(self): @@ -72,5 +71,7 @@ def rechar(self): self.desc_label.rechar(liner(obj.desc, 19)) +buy = Buy() + if __name__ == "__main__": print("\033[31;1mDo not execute this!\033[0m") diff --git a/pokete_classes/movemap.py b/pokete_classes/movemap.py index 74a6e37f..833997da 100644 --- a/pokete_classes/movemap.py +++ b/pokete_classes/movemap.py @@ -8,12 +8,12 @@ from .classes import OutP from .color import Color from .input import _ev, Action -from .ui import notifier +from .ui import notifier, Overview from .tss import tss from . import loops, ob_maps as obmp, game_map as gm -class Movemap(gm.GameSubmap): +class Movemap(gm.GameSubmap, Overview): """Movemap class to remove bad code ARGS: height: Height of the map diff --git a/pokete_classes/timer.py b/pokete_classes/timer.py index 5d393ba6..66e9682a 100644 --- a/pokete_classes/timer.py +++ b/pokete_classes/timer.py @@ -4,6 +4,7 @@ import time as time_mod import scrap_engine as se from release import SPEED_OF_TIME +from .context import Context from .input import Action, get_action from .ui.elements import Box from . import loops @@ -99,24 +100,22 @@ def normalized(self): class Clock(Box): """Clock class to display the current time ARGS: - time_ob: Time object - overview: The overview this happens on""" + time_ob: Time object""" - def __init__(self, time_ob, overview): + def __init__(self, time_ob): self.time = time_ob super().__init__( 9, 28, "Clock", f"{Action.CANCEL.mapping}:close", - overview ) - def __call__(self, _map): - """Shows the clock - ARGS: - _map: The map to show on""" + def __call__(self, ctx: Context): + """Shows the clock""" + d_p = True letter_obs = self.draw_letters(d_p) raw_time = self.time.time - with self.center_add(_map): + self.overview = ctx.overview + with self.center_add(ctx.map): while True: if get_action().triggers(*(Action.CANCEL, Action.CLOCK)): break @@ -124,8 +123,8 @@ def __call__(self, _map): d_p = not d_p letter_obs = self.draw_letters(d_p, letter_obs) raw_time = self.time.time - self.map.show() - loops.std(box=self) + loops.std(box=self, pevm=ctx.pevm) + self.map.full_show() self.__rem_obs(letter_obs) def __rem_obs(self, letter_obs):