assets update + hoverable component #23
|
@ -19,10 +19,9 @@ class HoverPlugin(Plugin):
|
|||
"""
|
||||
Met a jour les composants Hover des entités.
|
||||
"""
|
||||
entities = world.query(Texture, Position)
|
||||
textures = world[TextureManager]
|
||||
mouse_pos = world[Mouse].position
|
||||
for entity in entities:
|
||||
for entity in world.query(Hoverable, Texture, Position):
|
||||
# Récupération de la position et de la taille de l'entité
|
||||
entity_pos: Vec2 = entity[Position]
|
||||
if Offset in entity:
|
||||
|
@ -35,20 +34,21 @@ class HoverPlugin(Plugin):
|
|||
and mouse_pos.y >= entity_pos.y
|
||||
and mouse_pos.y <= entity_pos.y + entity_size.y
|
||||
):
|
||||
# si notre entitée est hoverable, on execute son initialisation
|
||||
# et pas encore hover (ce qui fait ce elle est executee une seul fois)
|
||||
if Hoverable in entity and not Hover in entity:
|
||||
entity[Hoverable].entry_callback(world, entity)
|
||||
|
||||
entity.set(Hover())
|
||||
|
||||
if Hoverable in entity:
|
||||
# Si l'entité n'était pas survolée on execute son callback
|
||||
if Hover not in entity:
|
||||
entity[Hoverable].enter_callback(world, entity)
|
||||
|
||||
# Si l'entité est survolée on execute son callback
|
||||
entity[Hoverable].update_callback(world, entity)
|
||||
else:
|
||||
if Hoverable in entity and Hover in entity:
|
||||
entity[Hoverable].exit_callback(world, entity)
|
||||
entity.remove(Hover)
|
||||
|
||||
# Si l'entité est survolée on execute son callback
|
||||
if Hover in entity:
|
||||
entity[Hoverable].exit_callback(world, entity)
|
||||
|
||||
def apply(self, game: Game) -> None:
|
||||
"""
|
||||
Applique le plugin a un jeu.
|
||||
|
@ -59,12 +59,6 @@ class HoverPlugin(Plugin):
|
|||
game.add_pre_update_tasks(self._update)
|
||||
|
||||
|
||||
class Hover:
|
||||
"""
|
||||
Un composant qui marque une entitée comme etant survolée.
|
||||
"""
|
||||
|
||||
|
||||
class Hoverable:
|
||||
"""
|
||||
Un composant qui marque une entitée comme pouvant etre survolée.
|
||||
|
@ -72,10 +66,16 @@ class Hoverable:
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
entry_callback: Callable[[World, Entity], None] = lambda _a, _b: None,
|
||||
update_callback: Callable[[World, Entity], None] = lambda _a, _b: None,
|
||||
exit_callback: Callable[[World, Entity], None] = lambda _a, _b: None,
|
||||
enter_callback: Callable[[World, Entity], None] = lambda _w, _e: None,
|
||||
update_callback: Callable[[World, Entity], None] = lambda _w, _e: None,
|
||||
exit_callback: Callable[[World, Entity], None] = lambda _w, _e: None,
|
||||
) -> None:
|
||||
self.update_callback = update_callback
|
||||
self.entry_callback = entry_callback
|
||||
self.enter_callback = enter_callback
|
||||
self.exit_callback = exit_callback
|
||||
|
||||
|
||||
class Hover:
|
||||
"""
|
||||
Un composant qui marque une entitée comme etant survolée.
|
||||
"""
|
||||
|
|
|
@ -41,7 +41,7 @@ def spawn_sprites(world: World) -> None:
|
|||
Order(0),
|
||||
Texture(file_name + ".png"),
|
||||
Hoverable(
|
||||
entry_callback=lambda _world, entity: entity.set(
|
||||
enter_callback=lambda _world, entity: entity.set(
|
||||
Texture(file_name + "_hover.png"),
|
||||
),
|
||||
exit_callback=lambda _world, entity: entity.set(
|
||||
|
|
Loading…
Reference in a new issue