assets update + hoverable component #23

Merged
tipragot merged 9 commits from menu into main 2023-10-27 22:29:36 +00:00
2 changed files with 22 additions and 22 deletions
Showing only changes of commit 6ad8580196 - Show all commits

View file

@ -19,10 +19,9 @@ class HoverPlugin(Plugin):
""" """
Met a jour les composants Hover des entités. Met a jour les composants Hover des entités.
""" """
entities = world.query(Texture, Position)
textures = world[TextureManager] textures = world[TextureManager]
mouse_pos = world[Mouse].position 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é # 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,20 +34,21 @@ class HoverPlugin(Plugin):
and mouse_pos.y >= entity_pos.y and mouse_pos.y >= entity_pos.y
and mouse_pos.y <= entity_pos.y + entity_size.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()) entity.set(Hover())
if Hoverable in entity: # Si l'entité n'était pas survolée on execute son callback
entity[Hoverable].update_callback(world, entity) 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: else:
if Hoverable in entity and Hover in entity:
entity[Hoverable].exit_callback(world, entity)
entity.remove(Hover) 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: def apply(self, game: Game) -> None:
""" """
Applique le plugin a un jeu. Applique le plugin a un jeu.
@ -59,12 +59,6 @@ class HoverPlugin(Plugin):
game.add_pre_update_tasks(self._update) game.add_pre_update_tasks(self._update)
class Hover:
"""
Un composant qui marque une entitée comme etant survolée.
"""
class Hoverable: class Hoverable:
""" """
Un composant qui marque une entitée comme pouvant etre survolée. Un composant qui marque une entitée comme pouvant etre survolée.
@ -72,10 +66,16 @@ class Hoverable:
def __init__( def __init__(
self, self,
entry_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 _a, _b: None, update_callback: Callable[[World, Entity], None] = lambda _w, _e: None,
exit_callback: Callable[[World, Entity], None] = lambda _a, _b: None, exit_callback: Callable[[World, Entity], None] = lambda _w, _e: None,
) -> None: ) -> None:
self.update_callback = update_callback self.update_callback = update_callback
self.entry_callback = entry_callback self.enter_callback = enter_callback
self.exit_callback = exit_callback self.exit_callback = exit_callback
class Hover:
"""
Un composant qui marque une entitée comme etant survolée.
"""

View file

@ -41,7 +41,7 @@ def spawn_sprites(world: World) -> None:
Order(0), Order(0),
Texture(file_name + ".png"), Texture(file_name + ".png"),
Hoverable( Hoverable(
entry_callback=lambda _world, entity: entity.set( enter_callback=lambda _world, entity: entity.set(
Texture(file_name + "_hover.png"), Texture(file_name + "_hover.png"),
), ),
exit_callback=lambda _world, entity: entity.set( exit_callback=lambda _world, entity: entity.set(