Ajout d'un systeme de Hover, savoir si la souris est au dessu du truc, de la texture, non ? #18

Merged
tipragot merged 6 commits from hover into main 2023-10-27 13:26:57 +00:00
2 changed files with 53 additions and 1 deletions
Showing only changes of commit b00365d21b - Show all commits

View file

@ -0,0 +1,51 @@
"""
Definit un plugin qui verifie si la souris est sur un element (qui a une texture).
"""
from engine import *
from engine.plugins.pygame import Mouse
from engine.plugins.render import Position, Texture, TextureManager
class HoverPlugin(Plugin):
"""
Plugin qui verifie si la souris est sur un element (qui a une texture).
"""
@staticmethod
def _update(world: World) -> None:
entities = world.query(Hoverable, Texture, Position)
textures = world[TextureManager]
mouse_pos = world[Mouse].position
for entity in entities:
entity_pos = entity[Position]
if (
entity_pos.x >= mouse_pos.x
and mouse_pos.x >= textures[entity[Texture]].get_width() + entity_pos.x
and entity_pos.y >= mouse_pos.y
and mouse_pos.y >= textures[entity[Texture]].get_height() + entity_pos.y
):
entity.set(Hover())
else:
entity.remove(Hover)
def apply(self, game: Game) -> None:
"""
Applique le plugin a un jeu.
Paramètres:
game: Le jeu auquel appliquer le plugin.
"""
game.add_update_tasks(self._update)
class Hoverable:
"""
Un composant qui marque une entitée comme pouvant etre survolée.
"""
class Hover:
"""
Un composant qui marque une entitée comme etant survolée.
"""

View file

@ -5,6 +5,7 @@ Ceci est un exemple de comment l'on peut utiliser le moteur du jeu.
from engine import * from engine import *
from engine.math import Vec2 from engine.math import Vec2
from engine.plugins.hover import HoverPlugin
from engine.plugins.render import Order, RenderPlugin, Position, Texture from engine.plugins.render import Order, RenderPlugin, Position, Texture
from engine.plugins.timing import Delta, TimePlugin from engine.plugins.timing import Delta, TimePlugin
from engine.plugins.pygame import Display, Keyboard, PygamePlugin from engine.plugins.pygame import Display, Keyboard, PygamePlugin
@ -12,7 +13,7 @@ from random import random
# Initialisation # Initialisation
game = Game(TimePlugin(), PygamePlugin(), RenderPlugin()) game = Game(TimePlugin(), PygamePlugin(), RenderPlugin(), HoverPlugin())
# On créer une tache pour afficher des sprites # On créer une tache pour afficher des sprites