A simple implementation of the Entity-Component pattern.
Project description
pyecs
A simple implementation of the Entity-Component 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 Store
# 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)
if __name__ == "__main__":
# 2. intialize entity-component store
store = Store()
# 3. add some entities
scene = store.add_entity()
scene.add_child(Transform(), Rigidbody(acceleration=(1.0, 0.0)))
scene.add_child(Transform(), Rigidbody(acceleration=(0.0, 1.0)))
scene.add_child(Transform(), Rigidbody(acceleration=(1.0, 1.0)))
# 4. run everything
while True:
for entity in store.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=}")
Dev Setup
Simply install pipenv and run the following line:
pipenv install --dev
MIT License
Copyright (c) 2020 Tim Fischer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.