assets update + hoverable component #23

Merged
tipragot merged 9 commits from menu into main 2023-10-27 22:29:36 +00:00
Showing only changes of commit e6691fc9b7 - Show all commits

View file

@ -3,6 +3,7 @@ Definit un plugin qui verifie si la souris est sur un element (qui a une texture
"""
from engine import *
from typing import Callable
from engine.math import Vec2
from engine.plugins.pygame import Mouse
from engine.plugins.render import Position, Offset, Texture, TextureManager
@ -22,6 +23,12 @@ class HoverPlugin(Plugin):
textures = world[TextureManager]
mouse_pos = world[Mouse].position
for entity in entities:
# on execute les update de toutes les entities qui ont le composant Hover et qui sont hoverable
if Hover in entity and Hovarble in entity:
entity[Hovarble]._update(world, entity)
entity[Hovarble].callback(world, entity)
# Récupération de la position et de la taille de l'entité
entity_pos: Vec2 = entity[Position]
if Offset in entity:
@ -35,6 +42,10 @@ class HoverPlugin(Plugin):
and mouse_pos.y <= entity_pos.y + entity_size.y
):
entity.set(Hover())
# si notre entitée est aussi hoverable, on execute son initialisation
if Hovarble in entity:
entity[Hovarble].callback(world, entity)
else:
entity.remove(Hover)
@ -52,3 +63,15 @@ class Hover:
"""
Un composant qui marque une entitée comme etant survolée.
"""
class Hovarble:
"""
Un composant qui marque une entitée comme pouvant etre survolée.
"""
def __init__(self, callback: Callable[[World, Entity], None], update_callback: Callable[[World, Entity], None]) -> None:
self.update_callback = update_callback
self.callback = callback
def _update(self, world: World, entity: Entity) -> None:
self.callback = self.update_callback