diff --git a/src/plugins/timing.py b/src/plugins/timing.py index cfdbdcf..7be3474 100644 --- a/src/plugins/timing.py +++ b/src/plugins/timing.py @@ -5,8 +5,9 @@ lancement du jeu et le temps depuis la dernière frame. from time import time +from typing import Callable from engine import GlobalPlugin, KeepAlive -from engine.ecs import World +from engine.ecs import Entity, World class GlobalTime(KeepAlive, float): @@ -34,6 +35,16 @@ def __initialize(world: World): world.set(GlobalTime(time()), Time(0.0)) +class TimedEvent: + """ + Composant permettant d'executer un callback après un certain temps. + """ + + def __init__(self, timer: float, callback: Callable[[World, Entity], object]): + self.timer = timer + self.callback = callback + + def __update(world: World): """ Met à jour les ressources de temps. @@ -43,6 +54,14 @@ def __update(world: World): world[GlobalTime] = now world[Time] += delta + # On met à jour les `TimedEvent` + for entity in world.query(TimedEvent): + event = entity[TimedEvent] + event.timer -= delta + if event.timer <= 0: + del entity[TimedEvent] + event.callback(world, entity) + PLUGIN = GlobalPlugin( [__initialize], diff --git a/src/plugins/writing.py b/src/plugins/writing.py index aea5f2f..982193a 100644 --- a/src/plugins/writing.py +++ b/src/plugins/writing.py @@ -3,7 +3,7 @@ Definit un plugin qui crée un texte avec les touches frappées """ from engine import Scene, World -from plugins.inputs import Held, Pressed +from plugins.inputs import Pressed from plugins.render import Text from plugins.sound import Sound @@ -26,7 +26,6 @@ def __update(world: World): Met a jour les entitées contenant le composant Typing """ pressed = world[Pressed] - held = world[Held] for entity in world.query(Writing, Text): writing = entity[Writing] for key in pressed: diff --git a/src/scenes/menu.py b/src/scenes/menu.py index e39005b..2575037 100644 --- a/src/scenes/menu.py +++ b/src/scenes/menu.py @@ -12,7 +12,7 @@ from plugins.click import Clickable from plugins.hover import HoveredTexture from plugins.render import SpriteBundle from plugins.timing import Time -from scenes import game, send_to_server +from scenes import game def __create_button(world: World, i: int, name: str): diff --git a/src/scenes/send_to_server.py b/src/scenes/send_to_server.py index 7f9c268..c0868cb 100644 --- a/src/scenes/send_to_server.py +++ b/src/scenes/send_to_server.py @@ -1,14 +1,15 @@ from plugins import writing -from engine import Plugin +from engine import CurrentScene, Plugin from engine.ecs import Entity, World from engine.math import Vec2 from plugins import render from plugins.click import Clickable from plugins.hover import HoveredTexture from plugins.render import SpriteBundle, Text, TextBundle +from plugins.timing import TimedEvent from plugins.writing import Writing import requests as rq -from scenes import game +from scenes import game, thanks IP = "pong.cocosol.fr" @@ -21,6 +22,12 @@ def new_score(world: World, e: Entity): post = {"name": name[Text], "score": world[game.Player1Score]} print(post) rq.post(f"https://{IP}/new_score", post) + world.new_entity().set( + TimedEvent( + 1, + lambda world, entity: world.set(CurrentScene(thanks.THANKS)), + ) + ) except: print("Error with the serveur") @@ -51,7 +58,7 @@ def __spawn_elements(world: World): "...", 0, 50, - position=Vec2(render.WIDTH / 2, 650), + position=Vec2(render.WIDTH / 2, 475), origin=Vec2(0.5), ), Writing( @@ -64,7 +71,7 @@ def __spawn_elements(world: World): world.new_entity().set( SpriteBundle( f"button_one_player.png", - position=Vec2(render.WIDTH / 2, render.HEIGHT / 2), + position=Vec2(render.WIDTH / 2, 600), order=1, origin=Vec2(0.5), ), diff --git a/src/scenes/thanks.py b/src/scenes/thanks.py new file mode 100644 index 0000000..0d58cf5 --- /dev/null +++ b/src/scenes/thanks.py @@ -0,0 +1,37 @@ +from engine import CurrentScene, Scene +from engine.ecs import World +from engine.math import Vec2 +from plugins import render +from plugins.render import SpriteBundle, TextBundle +from plugins.timing import TimedEvent +from scenes import menu + + +def __spawn_elements(world: World): + world.new_entity().set(SpriteBundle("background.jpg", -5)) + world.new_entity().set( + TextBundle( + "Merci,", + 0, + 150, + position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 - 75), + origin=Vec2(0.5), + ), + TimedEvent(3, lambda world, entity: world.set(CurrentScene(menu.MENU))), + ) + world.new_entity().set( + TextBundle( + "Votre score a bien été envoyé !", + 0, + 100, + position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 + 75), + origin=Vec2(0.5), + ), + ) + + +THANKS = Scene( + [__spawn_elements], + [], + [], +)