Ajout du plugin Hover

This commit is contained in:
Rapahel Lauray 2023-10-27 15:16:58 +02:00
parent d9caf388a5
commit b00365d21b
2 changed files with 53 additions and 1 deletions

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.math import Vec2
from engine.plugins.hover import HoverPlugin
from engine.plugins.render import Order, RenderPlugin, Position, Texture
from engine.plugins.timing import Delta, TimePlugin
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
@ -12,7 +13,7 @@ from random import random
# Initialisation
game = Game(TimePlugin(), PygamePlugin(), RenderPlugin())
game = Game(TimePlugin(), PygamePlugin(), RenderPlugin(), HoverPlugin())
# On créer une tache pour afficher des sprites