Merge pull request 'Ajout du système d'objets cliquable et changement du système' (#21) from button into main

Reviewed-on: #21
This commit is contained in:
Tipragot 2023-10-27 15:48:35 +00:00 committed by Gitea
commit 7ac5ae0261
No known key found for this signature in database
285 changed files with 147 additions and 49 deletions

View file

@ -435,7 +435,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during pre-startup task: {e}")
world.apply()
world.apply()
# On execute les taches d'initialisation du monde
for task in self._startup_tasks:
@ -443,7 +443,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during startup task: {e}")
world.apply()
world.apply()
# On execute les taches de post initialisation du monde
for task in self._post_startup_tasks:
@ -451,7 +451,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during post-startup task: {e}")
world.apply()
world.apply()
while self._running:
# On execute les taches de pré mise à jour du monde
@ -460,7 +460,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during pre-update task: {e}")
world.apply()
world.apply()
# On exécute les taches de mise a jour du monde
for task in self._update_tasks:
@ -468,7 +468,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during update task: {e}")
world.apply()
world.apply()
# On execute les taches de post mise à jour du monde
for task in self._post_update_tasks:
@ -476,7 +476,15 @@ class Game:
task(world)
except Exception as e:
error(f"Error during post-update task: {e}")
world.apply()
world.apply()
# On execute les taches de pré rendu du jeu
for task in self._pre_render_tasks:
try:
task(world)
except Exception as e:
error(f"Error during pre-render task: {e}")
world.apply()
# On execute les taches de rendu du jeu
for task in self._render_tasks:
@ -484,7 +492,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during render task: {e}")
world.apply()
world.apply()
# On execute les taches de fin de rendu du jeu
for task in self._post_render_tasks:
@ -492,7 +500,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during post-render task: {e}")
world.apply()
world.apply()
# On execute les taches de pré fin de boucle
for task in self._pre_shutdown_tasks:
@ -500,7 +508,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during pre-shutdown task: {e}")
world.apply()
world.apply()
# On exécute les taches de fin du monde
for task in self._shutdown_tasks:
@ -508,7 +516,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during shutdown task: {e}")
world.apply()
world.apply()
# On execute les taches de post fin de boucle
for task in self._post_shutdown_tasks:
@ -516,7 +524,7 @@ class Game:
task(world)
except Exception as e:
error(f"Error during post-shutdown task: {e}")
world.apply()
world.apply()
# On retourne le monde
return world

View file

@ -0,0 +1,54 @@
"""
Definit un plugin qui verifie si un objet Hover est cliqué.
"""
from engine import *
from typing import Callable
from engine.plugins.hover import Hover
from engine.plugins.pygame import Mouse
class ClickablePlugin(Plugin):
"""
Plugin qui verifie si un objet Hover est cliqué.
"""
@staticmethod
def _update(world: World) -> None:
"""
Met à jour les objets cliquables du monde.
"""
for entity in world.query(Clickable):
if Hover in entity:
if world[Mouse].is_button_pressed(1):
entity[Clickable].callback(world, entity)
if world[Mouse].is_button(1):
entity.set(Clicked())
else:
entity.remove(Clicked)
else:
entity.remove(Clicked)
def apply(self, game: Game) -> None:
"""
Applique le plugin a un jeu.
Paramètres:
game: Le jeu auquel appliquer le plugin.
"""
game.add_pre_update_tasks(self._update)
class Clickable:
"""
Un composant qui marque une entitée comme pouvant etre cliquer.
"""
def __init__(self, callback: Callable[[World, Entity], None]) -> None:
self.callback = callback
class Clicked:
"""
Un composant qui marque une entitée comme en train d'être cliquée.
"""

View file

@ -3,8 +3,9 @@ Definit un plugin qui verifie si la souris est sur un element (qui a une texture
"""
from engine import *
from engine.math import Vec2
from engine.plugins.pygame import Mouse
from engine.plugins.render import Position, Texture, TextureManager
from engine.plugins.render import Position, Offset, Texture, TextureManager
class HoverPlugin(Plugin):
@ -17,16 +18,21 @@ class HoverPlugin(Plugin):
"""
Met a jour les composants Hover des entités.
"""
entities = world.query(Hoverable, Texture, Position)
entities = world.query(Texture, Position)
textures = world[TextureManager]
mouse_pos = world[Mouse].position
for entity in entities:
entity_pos = entity[Position]
# Récupération de la position et de la taille de l'entité
entity_pos: Vec2 = entity[Position]
if Offset in entity:
entity_pos = entity_pos + entity[Offset]
entity_size = Vec2(*textures[entity[Texture]].get_size())
if (
entity_pos.x >= mouse_pos.x
and mouse_pos.x >= textures[entity[Texture]].get_width() + entity_pos.x
and entity_pos.y >= mouse_pos.y
and mouse_pos.y >= textures[entity[Texture]].get_height() + entity_pos.y
mouse_pos.x >= entity_pos.x
and mouse_pos.x <= entity_pos.x + entity_size.x
and mouse_pos.y >= entity_pos.y
and mouse_pos.y <= entity_pos.y + entity_size.y
):
entity.set(Hover())
else:
@ -39,13 +45,7 @@ class HoverPlugin(Plugin):
Paramètres:
game: Le jeu auquel appliquer le plugin.
"""
game.add_update_tasks(self._update)
class Hoverable:
"""
Un composant qui marque une entitée comme pouvant etre survolée.
"""
game.add_pre_update_tasks(self._update)
class Hover:

View file

@ -35,7 +35,9 @@ class RenderPlugin(Plugin):
entities = sorted(world.query(Order, Position), key=lambda e: e[Order])
for entity in entities:
# Récupération de la position des entitées
position = entity[Position]
position: Vec2 = entity[Position]
if Offset in entity:
position = position + entity[Offset]
# Affichage de la texture
if Texture in entity:
@ -71,6 +73,12 @@ class Position(Vec2):
"""
class Offset(Vec2):
"""
Composant qui represente un décalage de la position d'une entité.
"""
class Order(int):
"""
Composant qui represente l'ordre d'affichage d'un objet.
@ -85,11 +93,13 @@ class TextureManager:
def __init__(self, display: Display) -> None:
self._textures: dict[str, pygame.Surface] = {}
for file in glob.iglob("textures/**/*.png", recursive=True):
self._textures[file[9:]] = pygame.image.load(file).convert_alpha(
display._surface
)
self._textures[file[9:].replace("\\", "/")] = pygame.image.load(
file
).convert_alpha(display._surface)
for file in glob.iglob("textures/**/*.jpg", recursive=True):
self._textures[file[9:]] = pygame.image.load(file).convert(display._surface)
self._textures[file[9:].replace("\\", "/")] = pygame.image.load(
file
).convert(display._surface)
error_texture = pygame.Surface((256, 256))
error_texture.fill((0, 0, 0))
pygame.draw.rect(error_texture, (255, 0, 255), (0, 0, 128, 128))

View file

@ -5,23 +5,27 @@ 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.hover import HoverPlugin
from engine.plugins.clickable import Clickable, ClickablePlugin
from engine.plugins.hover import Hover, HoverPlugin
from engine.plugins.render import (
Order,
RenderPlugin,
Position,
Text,
TextColor,
TextSize,
Offset,
Texture,
)
from engine.plugins.timing import Delta, TimePlugin
from engine.plugins.timing import Time, Delta, TimePlugin
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
from random import random
# Initialisation
game = Game(
TimePlugin(), PygamePlugin("Guess The Number"), RenderPlugin(), HoverPlugin()
TimePlugin(),
PygamePlugin("Guess The Number"),
RenderPlugin(),
HoverPlugin(),
ClickablePlugin(),
)
@ -32,20 +36,41 @@ def spawn_sprites(world: World) -> None:
"""
for i in range(100):
red = random() < 0.1
world.create_entity(
entity = world.create_entity(
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
# Texture("test2.png") if red else Texture("test.png"),
Text("Hello les gens"),
TextSize(50),
TextColor(255, 0, 0) if red else TextColor(255, 255, 255),
Texture("directory.png") if red else Texture("test.png"),
Order(1 if red else 0),
)
if red:
entity.set(Clickable(lambda world, entity: print("click")))
# On ajoutant la tache
game.add_startup_tasks(spawn_sprites)
# On créer un tache pour vérifier le système d'hover
def change_on_hover(world: World) -> None:
"""
Change la texture du sprite quand on passe la souris sur lui.
"""
for entity in world.query(Position, Texture):
if Hover in entity:
entity.set(
Texture(
f"animations/search_directory/{(int(world[Time] * 60) % 270):04}.png"
)
)
entity.set(Offset(-48, -176))
else:
entity.set(Texture("directory.png"))
entity.remove(Offset)
# On ajoute la tache
game.add_update_tasks(change_on_hover)
def move_sprites(world: World) -> None:
"""
Change la position des sprites.

View file

@ -1,8 +0,0 @@
{
"offset": {
"x": -48,
"y": -176
},
"frame_count" : 270,
"fps" : 60
}

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Some files were not shown because too many files have changed in this diff Show more