Les joueurs ne peuvent plus sortir de la scène #11

Merged
CoCo_Sol merged 1 commit from limit-players into main 2024-01-07 09:18:37 +00:00

View file

@ -25,6 +25,12 @@ from plugins.physics import CollisionHandler, Solid, Velocity
from scenes import game_over
class RestrictToScene:
"""
Composant marquant une entitée comme ne pouvant pas sortir de la scène.
"""
class GameMode(Enum):
"""
Ressource qui definit le game mode choisi par l'utilisateur.
@ -266,6 +272,7 @@ def __spawn_ellements(world: World):
DownKey("s"),
Speed(1000),
LastPlayerTurn(),
RestrictToScene(),
)
# Joueur 2
@ -282,6 +289,7 @@ def __spawn_ellements(world: World):
(UpKey("up"), DownKey("down"), Speed(1000))
if world[GameMode] == GameMode.TWO
else Speed(300),
RestrictToScene(),
)
__spawn_ball(world)
@ -541,6 +549,7 @@ def _update_bot(world: World):
better_distance = None
for offset in [-100, -50, 0, 50, 100]:
bot[Position].y = right_touch_height + offset
__restrict_to_scene(world)
player.remove(Solid)
left_wall_ball = __simulate_wall_position(ball, LeftWall)
player.set(Solid())
@ -561,6 +570,23 @@ def _update_bot(world: World):
if abs(diff) > 10:
bot[Position].y += (diff / abs(diff)) * bot[Speed] * world[Delta]
# On restrict l'entité a la scène
__restrict_to_scene(world)
def __restrict_to_scene(world: World):
"""
Restrinct dans la scène toutes les entitées ayant le composant RestrictToScene.
"""
for entity in world.query(RestrictToScene):
y = entity[Position].y - (entity[Origin].y * entity[Scale].y)
if y < 0:
entity[Position].y = entity[Origin].y * entity[Scale].y
if y + entity[Scale].y > render.HEIGHT:
entity[Position].y = (
render.HEIGHT - entity[Scale].y + (entity[Origin].y * entity[Scale].y)
)
def __check_bonus_collision(world: World):
"""
@ -663,7 +689,13 @@ def __update_bonus_time(world: World):
__SCENE = Scene(
[__spawn_ellements],
[__update_move, __check_bonus_collision, __update_animation, __update_bonus_time],
[
__update_move,
__check_bonus_collision,
__update_animation,
__update_bonus_time,
__restrict_to_scene,
],
[],
)