Système d'entités et de composants #1

Merged
tipragot merged 9 commits from ecs-system into main 2023-10-23 14:04:18 +00:00
2 changed files with 52 additions and 0 deletions
Showing only changes of commit 980badc601 - Show all commits

22
ecs.py
View file

@ -6,6 +6,8 @@ class World:
self._to_apply: set[Entity] = set() self._to_apply: set[Entity] = set()
self._entities: set[Entity] = set() self._entities: set[Entity] = set()
self._mapping: dict[type, set[Entity]] = {} self._mapping: dict[type, set[Entity]] = {}
self._to_apply_resources: list[tuple[type, object]] = []
self._resources: dict[type, object] = {}
def create_entity(self, *components): def create_entity(self, *components):
return Entity(self, *components) return Entity(self, *components)
@ -14,6 +16,15 @@ class World:
entity._deleted = True entity._deleted = True
self._to_apply.add(entity) self._to_apply.add(entity)
def set(self, *resources):
for resource in resources:
self._to_apply_resources.append((type(resource), resource))
def remove(self, *resource_types):
for resource_type in resource_types:
if resource_type in self._resources:
self._to_apply_resources.append((resource_type, None))
def apply(self): def apply(self):
for entity in self._to_apply: for entity in self._to_apply:
if entity._deleted: if entity._deleted:
@ -30,6 +41,11 @@ class World:
entity._components[component_type] = component entity._components[component_type] = component
self._mapping.setdefault(component_type, set()).add(entity) self._mapping.setdefault(component_type, set()).add(entity)
entity._to_apply.clear() 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
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']:
if not needed: if not needed:
@ -42,6 +58,12 @@ class World:
all(without_type not in entity for without_type in without): all(without_type not in entity for without_type in without):
yield entity yield entity
def __getitem__(self, resource_type: type) -> object:
return self._resources[resource_type]
def __contains__(self, resource_type: type) -> bool:
return resource_type in self._resources
class Entity: class Entity:
def __init__(self, world: World, *components): def __init__(self, world: World, *components):
self._world = world self._world = world

30
main.py
View file

@ -50,3 +50,33 @@ for entity in world.query():
print(entity[Age], end=" ") print(entity[Age], end=" ")
print() print()
# Création d'une ressource pouvant être ajoutée a un monde
class Gravity(float): pass
# On peut aussi ajouter des ressources globales
world.set(Gravity(9.81))
print("On vérifie que la ressource Gravity existe")
print(Gravity in world)
# On applique les moddifications
world.apply()
print("On vérifie que la ressource Gravity existe après l'application")
print(Gravity in world)
print("Récupération de la ressource Gravity")
print(world[Gravity])
# On supprime la ressource Gravity
world.remove(Gravity)
print("On vérifie que la ressource Gravity n'existe plus")
print(Gravity in world)
# On applique les moddifications
world.apply()
print("On vérifie que la ressource Gravity n'existe plus")
print(Gravity in world)