Merge branch 'ecs-friendly' into smart-bot

This commit is contained in:
Timéo Cézard 2024-01-04 17:58:17 +01:00
commit 155bb3bf31

View file

@ -3,7 +3,6 @@ Le jeux principale.
""" """
from enum import Enum from enum import Enum
import math
from engine import Plugin, Scene from engine import Plugin, Scene
from engine.ecs import Entity, World from engine.ecs import Entity, World
from engine.math import Vec2 from engine.math import Vec2
@ -87,29 +86,29 @@ class Speed(int):
class Player1Score(int): class Player1Score(int):
""" """
Composant qui represente le score du joueur 1. Ressource qui represente le score du joueur 1.
""" """
class Player2Score(int): class Player2Score(int):
""" """
Composant qui represente le score du joueur 2. Ressource qui represente le score du joueur 2.
""" """
class Score: class Score:
""" """
Composant qui represente les scores affichés a l'ecran. Composant qui marque l'entité comme étant l'affichage du score.
""" """
class StartAnimation(float): class StartAnimation(float):
""" """
Composant qui represente un le moment auxquel on a lancé l'animation Composant qui represente un le moment auxquel on a lancé l'animation du compte a rebours
""" """
def __spawn_elements(world: World): def __spawn_ellements(world: World):
""" """
La fonction permet de initializer les ellements de la scene. La fonction permet de initializer les ellements de la scene.
""" """
@ -184,7 +183,6 @@ def __spawn_elements(world: World):
else None, else None,
) )
__spawn_ball(world)
__spawn_ball(world) __spawn_ball(world)
# Initialisation des scores # Initialisation des scores
@ -202,21 +200,12 @@ def __spawn_elements(world: World):
) )
def __angle_to_vec2(angle_degrees: float, magnitude: float):
# Convertir l'angle de degrés à radians
angle_radians = math.radians(angle_degrees)
# Calculer les coordonnées x et y
x = magnitude * math.cos(angle_radians)
y = magnitude * math.sin(angle_radians)
return Vec2(x, y)
def __spawn_ball(world: World): def __spawn_ball(world: World):
# random angle """
angle = random.randint(0, 360) Fonction qui fait apparaitre une balle avec une velocitée aleatoire
velocity = __angle_to_vec2(angle, 200) """
# random velocité
velocity = Vec2(2 * random.randint(100, 200), random.randint(100, 200))
# mouvement a droite ou a gauche # mouvement a droite ou a gauche
if random.randint(0, 1) == 0: if random.randint(0, 1) == 0:
@ -239,6 +228,9 @@ def __spawn_ball(world: World):
def __bounce_on_player(a: Entity, b: Entity): def __bounce_on_player(a: Entity, b: Entity):
"""
Fonction qui decrit se qui se passe lorque la ball entre en collision avec un joueur
"""
if Player1 in b or Player2 in b: if Player1 in b or Player2 in b:
speed = a[physics.Velocity].length speed = a[physics.Velocity].length
a[physics.Velocity] = a[physics.Velocity].normalized a[physics.Velocity] = a[physics.Velocity].normalized
@ -250,6 +242,9 @@ def __bounce_on_player(a: Entity, b: Entity):
def __bounce_on_left_wall(a: Entity, b: Entity): def __bounce_on_left_wall(a: Entity, b: Entity):
"""
Fonction qui decrit se qui se passe lorque la ball entre en collision avec le mur de gauche
"""
if Ball in b: if Ball in b:
world = a.world world = a.world
world[Player2Score] += 1 world[Player2Score] += 1
@ -259,6 +254,9 @@ def __bounce_on_left_wall(a: Entity, b: Entity):
def __bounce_on_right_wall(a: Entity, b: Entity): def __bounce_on_right_wall(a: Entity, b: Entity):
"""
Fonction qui decrit se qui se passe lorque la ball entre en collision avec le mur de droite
"""
if Ball in b: if Ball in b:
world = a.world world = a.world
world[Player1Score] += 1 world[Player1Score] += 1
@ -325,7 +323,7 @@ def __simulate_wall_position(entity: Entity, component_type: type):
def _update_bot(world: World): def _update_bot(world: World):
""" """
Test. Fonction qui update les mouvement du bot
""" """
ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler) ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler)
if ball_query == set(): if ball_query == set():
@ -373,6 +371,10 @@ def __animation(world: World, number: int):
def __update_animation(world: World): def __update_animation(world: World):
"""
Fonction qui permet de mettre a jour l'animation du compte a rebours.
"""
for animation in world.query(StartAnimation): for animation in world.query(StartAnimation):
time = world[Time] - animation[StartAnimation] time = world[Time] - animation[StartAnimation]
@ -390,7 +392,7 @@ def __update_animation(world: World):
__SCENE = Scene( __SCENE = Scene(
[__spawn_elements], [__spawn_ellements],
[__update_move, __update_animation], [__update_move, __update_animation],
[], [],
) )