An implementation of the Entity-Component-System pattern.
Project description
pyecs
An implementation of the Entity-Component-System pattern.
Install
This project is available on PyPI so you can simply install it via
pip install pyecs
Example
from dataclasses import dataclass
from typing import Tuple
from pyecs import ECSManager
# 1. build your components
@dataclass
class Transform:
position: Tuple[float, float] = (0.0, 0.0)
@dataclass
class Rigidbody:
velocity: Tuple[float, float] = (0.0, 0.0)
acceleration: Tuple[float, float] = (0.0, 0.0)
# 2. define a system
def physics(controller: ECSManager):
for entity in controller.get_entities_with(Transform, Rigidbody):
transform, rigidbody = entity.get_components(Transform, Rigidbody)
rigidbody.velocity = (
rigidbody.velocity[0] + rigidbody.acceleration[0],
rigidbody.velocity[1] + rigidbody.acceleration[1],
)
transform.position = (
transform.position[0] + rigidbody.velocity[0],
transform.position[1] + rigidbody.velocity[1],
)
print(f"{transform=}\t{rigidbody=}")
if __name__ == "__main__":
# 3. setup controller
controller = ECSManager()
controller.add_system(physics)
# 4. add some entities
controller.add_entity(Transform(), Rigidbody(acceleration=(1.0, 0.0)))
controller.add_entity(Transform(), Rigidbody(acceleration=(0.0, 1.0)))
controller.add_entity(Transform(), Rigidbody(acceleration=(1.0, 1.0)))
# 5. run everything
while True:
controller.tick_systems()
Dev Setup
Simply install pipenv and run the following line:
pipenv install --dev
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
pyecs-0.6.tar.gz
(6.2 kB
view hashes)