From 763430a63240bb7a1c81dda8d51c221489518f9f Mon Sep 17 00:00:00 2001 From: Tipragot Date: Wed, 1 Nov 2023 16:31:48 +0100 Subject: [PATCH] Ajout d'un plugin pour ajouter du texte --- src/plugins/defaults.py | 3 ++- src/plugins/text.py | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/plugins/text.py diff --git a/src/plugins/defaults.py b/src/plugins/defaults.py index 6496838..9f83fbc 100644 --- a/src/plugins/defaults.py +++ b/src/plugins/defaults.py @@ -2,7 +2,7 @@ Plugin qui rassemple tous les plugins globaux. """ -from plugins import animation, assets, display, inputs, render, timing +from plugins import animation, assets, display, inputs, render, text, timing PLUGIN = ( @@ -11,5 +11,6 @@ PLUGIN = ( + assets.PLUGIN + inputs.PLUGIN + animation.PLUGIN + + text.PLUGIN + render.PLUGIN ) diff --git a/src/plugins/text.py b/src/plugins/text.py new file mode 100644 index 0000000..fdd7099 --- /dev/null +++ b/src/plugins/text.py @@ -0,0 +1,49 @@ +""" +Un plugin permettant d'afficher du texte à l'écran. +""" + + +import pygame + +from engine import GlobalPlugin +from engine.ecs import World +from plugins.assets import Assets +from plugins.render import Sprite + + +class Text: + """ + Composant donnant le texte d'une entité, sa taille et sa couleur. + """ + + def __init__( + self, + text: str, + size: int = 50, + color: pygame.Color = pygame.Color(255, 255, 255), + ): + self.text = text + self.size = size + self.color = color + + +def __render_texts(world: World): + """ + Rend les textes à l'écran. + """ + assets = world[Assets] + for entity in world.query(Text): + text = entity[Text] + texture = assets.get_text(text.size, text.text, text.color) + if Sprite in entity: + entity[Sprite].texture = texture + else: + entity[Sprite] = Sprite(texture) + + +PLUGIN = GlobalPlugin( + [], + [], + [__render_texts], + [], +)