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