Ajout d'un plugin pour ajouter du texte

This commit is contained in:
Tipragot 2023-11-01 16:31:48 +01:00
parent ff3a08a992
commit 763430a632
2 changed files with 51 additions and 1 deletions

View file

@ -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
)

49
src/plugins/text.py Normal file
View file

@ -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],
[],
)