Système d'animations #22

Merged
tipragot merged 3 commits from animations into main 2023-10-27 16:20:43 +00:00
287 changed files with 203 additions and 30 deletions
Showing only changes of commit ae5b86963d - Show all commits

BIN
icon.png Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 379 KiB

After

Width:  |  Height:  |  Size: 379 KiB

BIN
krita/desktop.kra Normal file

Binary file not shown.

BIN
krita/directory.kra Normal file

Binary file not shown.

BIN
krita/edmond_angry.kra Normal file

Binary file not shown.

BIN
krita/edmond_happy.kra Normal file

Binary file not shown.

BIN
krita/edmond_normal.kra Normal file

Binary file not shown.

BIN
krita/search_directory.kra Normal file

Binary file not shown.

View file

@ -478,6 +478,14 @@ class Game:
error(f"Error during post-update task: {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 # On execute les taches de rendu du jeu
for task in self._render_tasks: for task in self._render_tasks:
try: try:

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

@ -0,0 +1,54 @@
"""
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, Offset, Texture, TextureManager
class HoverPlugin(Plugin):
"""
Plugin qui verifie si la souris est sur un element (qui a une texture).
"""
@staticmethod
def _update(world: World) -> None:
"""
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:
# 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 (
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:
entity.remove(Hover)
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 Hover:
"""
Un composant qui marque une entitée comme etant survolée.
"""

View file

@ -4,7 +4,7 @@ Définit un plugin qui gère les évenements pygame.
from engine import * from engine import *
from engine.math import Vec2 from engine.math import Vec2
import pygame import pygame, os
class PygamePlugin(Plugin): class PygamePlugin(Plugin):
@ -12,6 +12,9 @@ class PygamePlugin(Plugin):
Plugin qui gère les évenements pygame. Plugin qui gère les évenements pygame.
""" """
def __init__(self, title: str = "Game") -> None:
self.title = title
@staticmethod @staticmethod
def _find_surface_rect() -> tuple[float, float, float, float]: def _find_surface_rect() -> tuple[float, float, float, float]:
width, height = pygame.display.get_surface().get_size() width, height = pygame.display.get_surface().get_size()
@ -26,11 +29,14 @@ class PygamePlugin(Plugin):
return rect return rect
@staticmethod @staticmethod
def _initialize(world: World) -> None: def _initialize(world: World, title: str) -> None:
""" """
Initialize pygame et les ressources. Initialize pygame et les ressources.
""" """
pygame.init() pygame.init()
if os.path.exists("icon.png"):
pygame.display.set_icon(pygame.image.load("icon.png"))
pygame.display.set_caption(title)
pygame.display.set_mode((800, 600), pygame.RESIZABLE) pygame.display.set_mode((800, 600), pygame.RESIZABLE)
# Initialisation des ressources # Initialisation des ressources
@ -112,7 +118,9 @@ class PygamePlugin(Plugin):
Paramètres: Paramètres:
game: Le jeu auquel appliquer le plugin. game: Le jeu auquel appliquer le plugin.
""" """
game.add_pre_startup_tasks(self._initialize) game.add_pre_startup_tasks(
lambda world: PygamePlugin._initialize(world, self.title)
)
game.add_pre_update_tasks(self._check_events) game.add_pre_update_tasks(self._check_events)
game.add_post_render_tasks(self._update_display) game.add_post_render_tasks(self._update_display)
game.add_post_shutdown_tasks(self._terminate) game.add_post_shutdown_tasks(self._terminate)

View file

@ -4,7 +4,7 @@ Définis un plugin permettant d'afficher des choses a l'écran.
from engine import * from engine import *
from engine.math import Vec2 from engine.math import Vec2
import pygame, os import pygame, glob
from engine.plugins.pygame import Display from engine.plugins.pygame import Display
@ -35,7 +35,9 @@ class RenderPlugin(Plugin):
entities = sorted(world.query(Order, Position), key=lambda e: e[Order]) entities = sorted(world.query(Order, Position), key=lambda e: e[Order])
for entity in entities: for entity in entities:
# Récupération de la position des entitées # 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 # Affichage de la texture
if Texture in entity: 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): class Order(int):
""" """
Composant qui represente l'ordre d'affichage d'un objet. Composant qui represente l'ordre d'affichage d'un objet.
@ -84,10 +92,14 @@ class TextureManager:
def __init__(self, display: Display) -> None: def __init__(self, display: Display) -> None:
self._textures: dict[str, pygame.Surface] = {} self._textures: dict[str, pygame.Surface] = {}
for file in os.listdir("textures"): for file in glob.iglob("textures/**/*.png", recursive=True):
self._textures[file] = pygame.image.load(f"textures/{file}").convert( self._textures[file[9:].replace("\\", "/")] = pygame.image.load(
display._surface file
) ).convert_alpha(display._surface)
for file in glob.iglob("textures/**/*.jpg", recursive=True):
self._textures[file[9:].replace("\\", "/")] = pygame.image.load(
file
).convert(display._surface)
error_texture = pygame.Surface((256, 256)) error_texture = pygame.Surface((256, 256))
error_texture.fill((0, 0, 0)) error_texture.fill((0, 0, 0))
pygame.draw.rect(error_texture, (255, 0, 255), (0, 0, 128, 128)) pygame.draw.rect(error_texture, (255, 0, 255), (0, 0, 128, 128))

View file

@ -5,21 +5,28 @@ 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.clickable import Clickable, ClickablePlugin
from engine.plugins.hover import Hover, HoverPlugin
from engine.plugins.render import ( from engine.plugins.render import (
Order, Order,
RenderPlugin, RenderPlugin,
Position, Position,
Text, Offset,
TextColor, Texture,
TextSize,
) )
from engine.plugins.timing import Delta, TimePlugin from engine.plugins.timing import Time, Delta, TimePlugin
from engine.plugins.pygame import Display, Keyboard, PygamePlugin from engine.plugins.pygame import Display, Keyboard, PygamePlugin
from random import random from random import random
# Initialisation # Initialisation
game = Game(TimePlugin(), PygamePlugin(), RenderPlugin()) game = Game(
TimePlugin(),
PygamePlugin("Guess The Number"),
RenderPlugin(),
HoverPlugin(),
ClickablePlugin(),
)
# On créer une tache pour afficher des sprites # On créer une tache pour afficher des sprites
@ -29,20 +36,41 @@ def spawn_sprites(world: World) -> None:
""" """
for i in range(100): for i in range(100):
red = random() < 0.1 red = random() < 0.1
world.create_entity( entity = world.create_entity(
Position(random() * Display.WIDTH, random() * Display.HEIGHT), Position(random() * Display.WIDTH, random() * Display.HEIGHT),
# Texture("test2.png") if red else Texture("test.png"), Texture("directory.png") if red else Texture("test.png"),
Text("Hello les gens"),
TextSize(50),
TextColor(255, 0, 0) if red else TextColor(255, 255, 255),
Order(1 if red else 0), Order(1 if red else 0),
) )
if red:
entity.set(Clickable(lambda world, entity: print("click")))
# On ajoutant la tache # On ajoutant la tache
game.add_startup_tasks(spawn_sprites) 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: def move_sprites(world: World) -> None:
""" """
Change la position des sprites. Change la position des sprites.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

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