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
|
||||||
|
@ -18,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,9 +35,20 @@ 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 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:
|
else:
|
||||||
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.
|
||||||
|
@ -48,6 +59,22 @@ class HoverPlugin(Plugin):
|
||||||
game.add_pre_update_tasks(self._update)
|
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
|
||||||
|
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
class Hover:
|
class Hover:
|
||||||
"""
|
"""
|
||||||
Un composant qui marque une entitée comme etant survolée.
|
Un composant qui marque une entitée comme etant survolée.
|
||||||
|
|
|
@ -131,8 +131,8 @@ class Display:
|
||||||
Ressource qui represente la fenetre du jeu.
|
Ressource qui represente la fenetre du jeu.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
WIDTH = 1080.0
|
WIDTH = 1440.0
|
||||||
HEIGHT = 810.0
|
HEIGHT = 1080.0
|
||||||
RATIO = WIDTH / HEIGHT
|
RATIO = WIDTH / HEIGHT
|
||||||
INVERT_RATIO = HEIGHT / WIDTH
|
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 import *
|
||||||
from engine.math import Vec2
|
from engine.math import Vec2
|
||||||
from engine.plugins.animation import AnimatedSprite, AnimationPlugin
|
from engine.plugins.animation import AnimationPlugin
|
||||||
from engine.plugins.clickable import Clickable, ClickablePlugin
|
from engine.plugins.clickable import ClickablePlugin
|
||||||
from engine.plugins.hover import HoverPlugin
|
from engine.plugins.hover import HoverPlugin, Hoverable
|
||||||
from engine.plugins.render import (
|
from engine.plugins.render import (
|
||||||
Order,
|
Order,
|
||||||
RenderPlugin,
|
RenderPlugin,
|
||||||
|
@ -16,7 +16,6 @@ from engine.plugins.render import (
|
||||||
)
|
)
|
||||||
from engine.plugins.timing import Delta, TimePlugin
|
from engine.plugins.timing import Delta, TimePlugin
|
||||||
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
||||||
from random import random
|
|
||||||
|
|
||||||
|
|
||||||
# Initialisation
|
# Initialisation
|
||||||
|
@ -35,24 +34,32 @@ def spawn_sprites(world: World) -> None:
|
||||||
"""
|
"""
|
||||||
Ajoute des sprites au monde.
|
Ajoute des sprites au monde.
|
||||||
"""
|
"""
|
||||||
for i in range(100):
|
|
||||||
red = random() < 0.1
|
def new_button(position: Vec2, file_name: str) -> None:
|
||||||
entity = world.create_entity(
|
world.create_entity(
|
||||||
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
|
Position(position),
|
||||||
Texture("directory.png") if red else Texture("error.png"),
|
Order(0),
|
||||||
Order(1 if red else 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(
|
for i in range(4):
|
||||||
Clickable(
|
if i == 0:
|
||||||
lambda world, entity: entity.set(
|
file_name = "button_classique"
|
||||||
AnimatedSprite(
|
elif i == 1:
|
||||||
"search_directory",
|
file_name = "button_histoire"
|
||||||
lambda world, entity: print("finished !"),
|
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
|
# 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 |
Je ne comprend pas l'utilité de cette fonction.