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 ECSController, Component, preconfigure_system, register_system
# 1. build your components
@dataclass
class Transform(Component, identifier="transform"):
position: Tuple[float, float] = (0.0, 0.0)
@dataclass
class Rigidbody(Component, identifier="rigidbody"):
velocity: Tuple[float, float] = (0.0, 0.0)
acceleration: Tuple[float, float] = (0.0, 0.0)
# 2. define some systems
@preconfigure_system((Transform, Rigidbody))
def physics(delta_t, data, entities):
for entity in entities:
entity.rigidbody.velocity = (
entity.rigidbody.velocity[0] + entity.rigidbody.acceleration[0] * delta_t,
entity.rigidbody.velocity[1] + entity.rigidbody.acceleration[1] * delta_t,
)
entity.transform.position = (
entity.transform.position[0] + entity.rigidbody.velocity[0] * delta_t,
entity.transform.position[1] + entity.rigidbody.velocity[1] * delta_t,
)
@preconfigure_system((Transform,))
def introspect_position(delta_t, data, entities):
for entity in entities:
print(f"Entity {entity.uuid}: position = {entity.transform.position}")
if __name__ == "__main__":
# 3. setup controller
controller = ECSController()
register_system(controller, physics)
register_system(controller, introspect_position)
# 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
controller.run()
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.2.tar.gz
(8.1 kB
view hashes)