This commit is contained in:
Raphaël 2024-01-06 18:11:50 +01:00
parent 344ed9d5f6
commit 6dbf733262
5 changed files with 70 additions and 8 deletions

View file

@ -5,8 +5,9 @@ lancement du jeu et le temps depuis la dernière frame.
from time import time from time import time
from typing import Callable
from engine import GlobalPlugin, KeepAlive from engine import GlobalPlugin, KeepAlive
from engine.ecs import World from engine.ecs import Entity, World
class GlobalTime(KeepAlive, float): class GlobalTime(KeepAlive, float):
@ -34,6 +35,16 @@ def __initialize(world: World):
world.set(GlobalTime(time()), Time(0.0)) 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): def __update(world: World):
""" """
Met à jour les ressources de temps. Met à jour les ressources de temps.
@ -43,6 +54,14 @@ def __update(world: World):
world[GlobalTime] = now world[GlobalTime] = now
world[Time] += delta 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( PLUGIN = GlobalPlugin(
[__initialize], [__initialize],

View file

@ -3,7 +3,7 @@ Definit un plugin qui crée un texte avec les touches frappées
""" """
from engine import Scene, World from engine import Scene, World
from plugins.inputs import Held, Pressed from plugins.inputs import Pressed
from plugins.render import Text from plugins.render import Text
from plugins.sound import Sound from plugins.sound import Sound
@ -26,7 +26,6 @@ def __update(world: World):
Met a jour les entitées contenant le composant Typing Met a jour les entitées contenant le composant Typing
""" """
pressed = world[Pressed] pressed = world[Pressed]
held = world[Held]
for entity in world.query(Writing, Text): for entity in world.query(Writing, Text):
writing = entity[Writing] writing = entity[Writing]
for key in pressed: for key in pressed:

View file

@ -12,7 +12,7 @@ from plugins.click import Clickable
from plugins.hover import HoveredTexture from plugins.hover import HoveredTexture
from plugins.render import SpriteBundle from plugins.render import SpriteBundle
from plugins.timing import Time 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): def __create_button(world: World, i: int, name: str):

View file

@ -1,14 +1,15 @@
from plugins import writing from plugins import writing
from engine import Plugin from engine import CurrentScene, Plugin
from engine.ecs import Entity, World from engine.ecs import Entity, World
from engine.math import Vec2 from engine.math import Vec2
from plugins import render from plugins import render
from plugins.click import Clickable from plugins.click import Clickable
from plugins.hover import HoveredTexture from plugins.hover import HoveredTexture
from plugins.render import SpriteBundle, Text, TextBundle from plugins.render import SpriteBundle, Text, TextBundle
from plugins.timing import TimedEvent
from plugins.writing import Writing from plugins.writing import Writing
import requests as rq import requests as rq
from scenes import game from scenes import game, thanks
IP = "pong.cocosol.fr" IP = "pong.cocosol.fr"
@ -21,6 +22,12 @@ def new_score(world: World, e: Entity):
post = {"name": name[Text], "score": world[game.Player1Score]} post = {"name": name[Text], "score": world[game.Player1Score]}
print(post) print(post)
rq.post(f"https://{IP}/new_score", post) rq.post(f"https://{IP}/new_score", post)
world.new_entity().set(
TimedEvent(
1,
lambda world, entity: world.set(CurrentScene(thanks.THANKS)),
)
)
except: except:
print("Error with the serveur") print("Error with the serveur")
@ -51,7 +58,7 @@ def __spawn_elements(world: World):
"...", "...",
0, 0,
50, 50,
position=Vec2(render.WIDTH / 2, 650), position=Vec2(render.WIDTH / 2, 475),
origin=Vec2(0.5), origin=Vec2(0.5),
), ),
Writing( Writing(
@ -64,7 +71,7 @@ def __spawn_elements(world: World):
world.new_entity().set( world.new_entity().set(
SpriteBundle( SpriteBundle(
f"button_one_player.png", f"button_one_player.png",
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2), position=Vec2(render.WIDTH / 2, 600),
order=1, order=1,
origin=Vec2(0.5), origin=Vec2(0.5),
), ),

37
src/scenes/thanks.py Normal file
View file

@ -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],
[],
[],
)