Système d'entités et de composants #1
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||
}
|
||||
}
|
22
ecs.py
22
ecs.py
|
@ -28,7 +28,7 @@ class World:
|
|||
"""
|
||||
return Entity(self, *components)
|
||||
|
||||
def remove_entity(self, entity: 'Entity'):
|
||||
def remove_entity(self, entity: "Entity"):
|
||||
"""
|
||||
Supprime une entité du monde.
|
||||
|
||||
|
@ -81,11 +81,13 @@ class World:
|
|||
entity._to_apply.clear()
|
||||
self._to_apply.clear()
|
||||
for resource_type, resource in self._to_apply_resources:
|
||||
if resource is None: del self._resources[resource_type]
|
||||
else: self._resources[resource_type] = resource
|
||||
if resource is None:
|
||||
del self._resources[resource_type]
|
||||
else:
|
||||
self._resources[resource_type] = resource
|
||||
self._to_apply_resources.clear()
|
||||
|
||||
def query(self, *needed: type, without: tuple[type] = []) -> Iterator['Entity']:
|
||||
def query(self, *needed: type, without: tuple[type] = []) -> Iterator["Entity"]:
|
||||
"""
|
||||
Renvoie les entités qui ont les composants de *needed et sans les composants de *without.
|
||||
|
||||
|
@ -103,8 +105,10 @@ class World:
|
|||
yield entity
|
||||
else:
|
||||
for entity in self._mapping.get(needed[0], set()):
|
||||
if all(entity in self._mapping.get(component_type, set()) for component_type in needed[1:]) and \
|
||||
all(without_type not in entity for without_type in without):
|
||||
if all(
|
||||
entity in self._mapping.get(component_type, set())
|
||||
for component_type in needed[1:]
|
||||
) and all(without_type not in entity for without_type in without):
|
||||
yield entity
|
||||
|
||||
def __getitem__(self, resource_type: type) -> object:
|
||||
|
@ -131,10 +135,12 @@ class World:
|
|||
"""
|
||||
return resource_type in self._resources
|
||||
|
||||
|
||||
class Entity:
|
||||
"""
|
||||
Une entité du monde.
|
||||
"""
|
||||
|
||||
def __init__(self, world: World, *components):
|
||||
"""
|
||||
Créer une entité avec les composants en paramètres et l'ajoute au monde.
|
||||
|
@ -144,7 +150,9 @@ class Entity:
|
|||
*components: Les composants de l'entité.
|
||||
"""
|
||||
self._world = world
|
||||
self._to_apply: list[tuple[type, object]] = [(type(component), component) for component in components]
|
||||
self._to_apply: list[tuple[type, object]] = [
|
||||
(type(component), component) for component in components
|
||||
]
|
||||
self._components: dict[type, object] = {}
|
||||
self._deleted = False
|
||||
self._world._to_apply.add(self)
|
||||
|
|
16
main.py
16
main.py
|
@ -1,8 +1,14 @@
|
|||
from ecs import World
|
||||
|
||||
|
||||
# Création de composants pouvant être ajouté a des entitées
|
||||
class Name(str): pass
|
||||
class Age(int): pass
|
||||
class Name(str):
|
||||
pass
|
||||
|
||||
|
||||
class Age(int):
|
||||
pass
|
||||
|
||||
|
||||
# Création d'un monde
|
||||
world = World()
|
||||
|
@ -50,8 +56,11 @@ for entity in world.query():
|
|||
print(entity[Age], end=" ")
|
||||
print()
|
||||
|
||||
|
||||
# Création d'une ressource pouvant être ajoutée a un monde
|
||||
class Gravity(float): pass
|
||||
class Gravity(float):
|
||||
pass
|
||||
|
||||
|
||||
# On peut aussi ajouter des ressources globales
|
||||
world.set(Gravity(9.81))
|
||||
|
@ -79,4 +88,3 @@ world.apply()
|
|||
|
||||
print("On vérifie que la ressource Gravity n'existe plus")
|
||||
print(Gravity in world)
|
||||
|
||||
|
|
29
tasks.py
Normal file
29
tasks.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from typing import Callable
|
||||
from ecs import World
|
||||
|
||||
|
||||
class TaskManager:
|
||||
"""
|
||||
Contient des fonctions de logique s'appliquant sur un monde.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Créer un gestionnaire de taches vide.
|
||||
"""
|
||||
self.tasks: dict[int, list[Callable[[World], None]]] = {}
|
||||
|
||||
def add(self, priority: int, task: Callable[[World], None]):
|
||||
"""
|
||||
Ajoute une tache à un gestionnaire de taches.
|
||||
"""
|
||||
self.tasks.setdefault(priority, []).append(task)
|
||||
|
||||
def run(self, world: World):
|
||||
"""
|
||||
Exécute toutes les taches.
|
||||
"""
|
||||
priorities = sorted(list(self.tasks.keys()))
|
||||
for priority in priorities:
|
||||
for task in self.tasks[priority]:
|
||||
task(world)
|
Loading…
Reference in a new issue