Merge pull request 'assets update + hoverable component' (#23) from menu into main
Reviewed-on: #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 typing import Callable
|
||||
from engine.math import Vec2
|
||||
from engine.plugins.pygame import Mouse
|
||||
from engine.plugins.render import Position, Offset, Texture, TextureManager
|
||||
|
@ -18,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,9 +35,20 @@ class HoverPlugin(Plugin):
|
|||
and mouse_pos.y <= entity_pos.y + entity_size.y
|
||||
):
|
||||
entity.set(Hover())
|
||||
|
||||
# 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:
|
||||
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.
|
||||
|
@ -48,6 +59,22 @@ class HoverPlugin(Plugin):
|
|||
game.add_pre_update_tasks(self._update)
|
||||
|
||||
|
||||
class Hoverable:
|
||||
"""
|
||||
Un composant qui marque une entitée comme pouvant etre survolée.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
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.enter_callback = enter_callback
|
||||
self.exit_callback = exit_callback
|
||||
|
||||
|
||||
class Hover:
|
||||
"""
|
||||
Un composant qui marque une entitée comme etant survolée.
|
||||
|
|
|
@ -131,8 +131,8 @@ class Display:
|
|||
Ressource qui represente la fenetre du jeu.
|
||||
"""
|
||||
|
||||
WIDTH = 1080.0
|
||||
HEIGHT = 810.0
|
||||
WIDTH = 1440.0
|
||||
HEIGHT = 1080.0
|
||||
RATIO = WIDTH / HEIGHT
|
||||
INVERT_RATIO = HEIGHT / WIDTH
|
||||
|
||||
|
|
49
src/main.py
|
@ -5,9 +5,9 @@ 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.animation import AnimatedSprite, AnimationPlugin
|
||||
from engine.plugins.clickable import Clickable, ClickablePlugin
|
||||
from engine.plugins.hover import HoverPlugin
|
||||
from engine.plugins.animation import AnimationPlugin
|
||||
from engine.plugins.clickable import ClickablePlugin
|
||||
from engine.plugins.hover import HoverPlugin, Hoverable
|
||||
from engine.plugins.render import (
|
||||
Order,
|
||||
RenderPlugin,
|
||||
|
@ -16,7 +16,6 @@ from engine.plugins.render import (
|
|||
)
|
||||
from engine.plugins.timing import Delta, TimePlugin
|
||||
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
||||
from random import random
|
||||
|
||||
|
||||
# Initialisation
|
||||
|
@ -35,24 +34,32 @@ def spawn_sprites(world: World) -> None:
|
|||
"""
|
||||
Ajoute des sprites au monde.
|
||||
"""
|
||||
for i in range(100):
|
||||
red = random() < 0.1
|
||||
entity = world.create_entity(
|
||||
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
|
||||
Texture("directory.png") if red else Texture("error.png"),
|
||||
Order(1 if red else 0),
|
||||
|
||||
def new_button(position: Vec2, file_name: str) -> None:
|
||||
world.create_entity(
|
||||
Position(position),
|
||||
Order(0),
|
||||
Texture(file_name + ".png"),
|
||||
Hoverable(
|
||||
enter_callback=lambda _world, entity: entity.set(
|
||||
Texture(file_name + "_hover.png"),
|
||||
),
|
||||
exit_callback=lambda _world, entity: entity.set(
|
||||
Texture(file_name + ".png"),
|
||||
),
|
||||
),
|
||||
)
|
||||
if red:
|
||||
entity.set(
|
||||
Clickable(
|
||||
lambda world, entity: entity.set(
|
||||
AnimatedSprite(
|
||||
"search_directory",
|
||||
lambda world, entity: print("finished !"),
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
for i in range(4):
|
||||
if i == 0:
|
||||
file_name = "button_classique"
|
||||
elif i == 1:
|
||||
file_name = "button_histoire"
|
||||
elif i == 2:
|
||||
file_name = "button_tricheur"
|
||||
else:
|
||||
file_name = "button_menteur"
|
||||
new_button(Vec2(Display.WIDTH / 4 * i, 20), file_name)
|
||||
|
||||
|
||||
# On ajoutant la tache
|
||||
|
|
BIN
textures/button_classique.png
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
textures/button_classique_hover.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
textures/button_histoire.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
textures/button_histoire_hover.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
textures/button_menteur.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
textures/button_menteur_hover.png
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
textures/button_tricheur.png
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
textures/button_tricheur_hover.png
Normal file
After Width: | Height: | Size: 27 KiB |