Annotation-based asynchronous ECS library
Project description
ECS micro-engine
Library for python containing an interpretation of ECS design pattern.
Features
- Adding/removing entity's attributes causes dynamic addition/removal of the entity from the corresponding systems
- Systems with annotation-based syntax
- Asynchronous systems (yield skips a frame)
- Systems accept multiple arguments, preventing global mutable state issues
- Full compatibility with type hints
Interpretation
- Entity is a python object
- Component is entity's attribute
- System is an entity describing an interaction between cartesian product of entities
- Metasystem is a system that launches other systems
- MetasystemFacade encapsulates all general logic (add, remove, update)
Installation
pip install ecs-girvel
Usage
You can also see girvel/fallen, ecs is written for it.
from ecs import MetasystemFacade, System, Entity # TODO NEXT fix demo
import time
# 1. You create a metasystem
ms = MetasystemFacade()
dt = 0.04
# 2. You create systems and add them to metasystem
class VerticalSpeed:
vy: float
class GravityConstants:
g: float
@ms.add
@System
def gravity(target: VerticalSpeed, constants: GravityConstants):
target.vy += constants.g * dt
class Inert:
x: float
y: float
vx: float
vy: float
@ms.add
@System
def inertia(target: Inert):
target.x += target.vx * dt
target.y += target.vy * dt
class Displayable:
x: float
y: float
name: str
@ms.add
@System
def output(target: Displayable):
yield from range(int(1 / dt) - 1) # skips 24 out of 25 frames
print(f'{target.name}: {target.x:.2f}, {target.y:.2f}')
# 3. You create objects
class DynamicEntity(Entity):
def __init__(self, **attributes):
for key, value in attributes.items():
setattr(self, key, value)
ms.add(DynamicEntity(name='falling_guy1', x=0, y=0, vx=0, vy=0))
ms.add(DynamicEntity(name='falling_guy2', x=100, y=0, vx=0, vy=0))
ms.add(DynamicEntity(g=10))
# 4. Game loop
if __name__ == "__main__":
while True:
ms.update()
time.sleep(dt)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ecs-girvel-3.0.0.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file ecs-girvel-3.0.0.tar.gz
.
File metadata
- Download URL: ecs-girvel-3.0.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b107d6518db899e80808ee2d41e7290a00f7643918e4113b7fa867b63858603 |
|
MD5 | ab4f35eca386361d77deb21cbb323296 |
|
BLAKE2b-256 | 405da3e6292d6687eda6db94b122aa5312361aee9189c78f2c575476b72c771e |
File details
Details for the file ecs_girvel-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: ecs_girvel-3.0.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a61b041e9c8ae115a69a048e6ef10ed90db0cd161d566d791a61db2a4245639a |
|
MD5 | 78f80eb7a31c2cc9bed30a8d1511b744 |
|
BLAKE2b-256 | 9249acd11502d2d6f60652eb23b35b016459dbac26b2c05bfe2c1c260e02df3c |