ecs #58
|
@ -221,9 +221,12 @@ def loading_scene(target: Scene, name: str, clear_cache: bool = True):
|
|||
else:
|
||||
file = asset_iterator.files.pop().replace("\\", "/")
|
||||
ressource_extension = file.split(".")[-1]
|
||||
ressource_name = file[
|
||||
len(f"assets/{name}/") : -len(ressource_extension) - 1
|
||||
]
|
||||
prefix = (
|
||||
"assets/global/"
|
||||
if file.startswith("assets/global/")
|
||||
else f"assets/{name}/"
|
||||
)
|
||||
ressource_name = file[len(prefix) : -len(ressource_extension) - 1]
|
||||
if ressource_extension in ("png", "jpg"):
|
||||
assets.load_texture(ressource_name, file)
|
||||
if ressource_extension in ("mp3", "wav", "ogg"):
|
||||
|
|
|
@ -3,10 +3,12 @@ Un plugin permettant de jouer des sons.
|
|||
"""
|
||||
|
||||
|
||||
import random
|
||||
from typing import Callable
|
||||
import pygame
|
||||
from engine import GlobalPlugin, KeepAlive
|
||||
from engine.ecs import Entity, World
|
||||
from plugins.assets import Assets
|
||||
|
||||
|
||||
class Channels(KeepAlive, dict[Entity, pygame.mixer.Channel]):
|
||||
|
@ -35,6 +37,32 @@ class Sound:
|
|||
self.callback = callback
|
||||
|
||||
|
||||
class MultiSound:
|
||||
"""
|
||||
Composant qui quand il est ajouté a une entité, il joue un son aléatoire
|
||||
parmis la liste de ses sons, il est ensuite retiré de l'entité.
|
||||
|
||||
Ce composant est fait pour être définis en tant que constante
|
||||
puis utilisé ensuite, pour cela il prend seulement le nom des
|
||||
sons au lieu des sons eux memes. Les sons seront donc récupérés
|
||||
a l'aide de `Assets`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*sound_names: str,
|
||||
loop: bool = False,
|
||||
volume: float = 1.0,
|
||||
fade_ms: int = 0,
|
||||
callback: Callable[[World, Entity], object] = lambda _w, _e: None,
|
||||
):
|
||||
self.sound_names = sound_names
|
||||
self.loop = loop
|
||||
self.volume = volume
|
||||
self.fade_ms = fade_ms
|
||||
self.callback = callback
|
||||
|
||||
|
||||
def __initialize(world: World):
|
||||
"""
|
||||
Ajoute les ressources utiles pour le plugin.
|
||||
|
@ -46,6 +74,20 @@ def __update_sounds(world: World):
|
|||
"""
|
||||
Met à jour les sons du jeu.
|
||||
"""
|
||||
# Ajout des sons aléatoires
|
||||
assets = world[Assets]
|
||||
for entity in world.query(MultiSound):
|
||||
multi_sound = entity[MultiSound]
|
||||
sound = assets.get_sound(random.choice(multi_sound.sound_names))
|
||||
entity[Sound] = Sound(
|
||||
sound,
|
||||
multi_sound.loop,
|
||||
multi_sound.volume,
|
||||
multi_sound.fade_ms,
|
||||
multi_sound.callback,
|
||||
)
|
||||
del entity[MultiSound]
|
||||
|
||||
# Ajout des sons non gérés
|
||||
channels = world[Channels]
|
||||
sound_entities = world.query(Sound)
|
||||
|
@ -64,6 +106,7 @@ def __update_sounds(world: World):
|
|||
callback = entity[Sound].callback
|
||||
del entity[Sound]
|
||||
callback(world, entity)
|
||||
channels_to_remove.append(entity)
|
||||
elif entity not in sound_entities:
|
||||
channel.stop()
|
||||
channels_to_remove.append(entity)
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
"""
|
||||
Module contenant toutes les scènes du jeu.
|
||||
"""
|
||||
|
||||
from plugins.sound import MultiSound
|
||||
|
||||
|
||||
CLICK_SOUND = MultiSound("click/0", "click/1", "click/2")
|
||||
|
|
|
@ -5,8 +5,8 @@ Définit 3 scènes de jeu basique
|
|||
from enum import Enum
|
||||
import random
|
||||
import pygame
|
||||
from engine import CurrentScene, Scene, Plugin
|
||||
from engine.ecs import World
|
||||
from engine import CurrentScene, KeepAlive, Scene, Plugin
|
||||
from engine.ecs import Entity, World
|
||||
from engine.math import Vec2
|
||||
from plugins import assets
|
||||
from plugins import render
|
||||
|
@ -18,7 +18,7 @@ from plugins.render import Sprite
|
|||
from plugins.text import Text
|
||||
from plugins.writing import Writing
|
||||
from plugins.click import Clickable
|
||||
from scenes import menu
|
||||
from scenes import CLICK_SOUND, menu
|
||||
|
||||
|
||||
class GameMode(Enum):
|
||||
|
@ -67,6 +67,14 @@ class PlayAgain:
|
|||
"""
|
||||
|
||||
|
||||
def __return_to_menu(world: World, _entity: Entity):
|
||||
"""
|
||||
Reviens sur le menu lorsque l'on clique sur la flèche de retour.
|
||||
"""
|
||||
world[CurrentScene] = menu.SCENE
|
||||
world.new_entity().set(KeepAlive(), CLICK_SOUND)
|
||||
|
||||
|
||||
def __initialize_world(world: World):
|
||||
world.set(Number(random.randint(0, 99)))
|
||||
world.set(IsRunning())
|
||||
|
@ -77,7 +85,7 @@ def __initialize_world(world: World):
|
|||
HoveredTexture(
|
||||
world[Assets].get_texture("arrow"), world[Assets].get_texture("arrow_hover")
|
||||
),
|
||||
Clickable(lambda world, entity: world.set(CurrentScene(menu.SCENE))),
|
||||
Clickable(__return_to_menu),
|
||||
)
|
||||
world.new_entity().set(
|
||||
Sprite(
|
||||
|
|
|
@ -3,8 +3,9 @@ La scène du menu principal du jeu.
|
|||
|
||||
Dans cette scène nous pouvons choisir le mode de jeu.
|
||||
"""
|
||||
from scenes import base_game
|
||||
from engine import CurrentScene, Scene
|
||||
from scenes import CLICK_SOUND, base_game
|
||||
from scenes.story import directory_search
|
||||
from engine import CurrentScene, KeepAlive, Scene
|
||||
from engine.ecs import Entity, World
|
||||
from engine.math import Vec2
|
||||
from plugins import render
|
||||
|
@ -13,8 +14,6 @@ from plugins.assets import Assets
|
|||
from plugins.click import Clickable
|
||||
from plugins.hover import HoveredTexture
|
||||
from plugins.render import Sprite
|
||||
from plugins.sound import Sound
|
||||
from scenes.story import directory_search
|
||||
|
||||
|
||||
def __create_button(world: World, assets: Assets, i: int, name: str):
|
||||
|
@ -40,7 +39,7 @@ def __on_click_butons(world: World, _entity: Entity, name: str):
|
|||
"""
|
||||
Fonction qui s'execute quand on clique sur un bouton.
|
||||
"""
|
||||
world.new_entity().set(Sound(world[Assets].get_sound("click")))
|
||||
world.new_entity().set(KeepAlive(), CLICK_SOUND)
|
||||
match name:
|
||||
case "classique":
|
||||
world[CurrentScene] = base_game.CLASSIC
|
||||
|
|
Loading…
Reference in a new issue