项目作者: timfi

项目描述 :
An implementation of the Entity-Component-System pattern.
高级语言: Python
项目地址: git://github.com/timfi/pyecs.git
创建时间: 2019-08-31T07:26:08Z
项目社区:https://github.com/timfi/pyecs

开源协议:MIT License

下载


pyecs

A simple implementation of the Entity-Component pattern.

PyPI - Version
PyPI - Status
PyPI - Python Version
PyPI - License

Build Status
codecov
codebeat badge
Code style: black
Checked with mypy

Install

This project is available on PyPI so you can simply install it via

  1. pip install pyecs

Example

  1. from dataclasses import dataclass
  2. from typing import Tuple
  3. from pyecs import Store
  4. # 1. build your components
  5. @dataclass
  6. class Transform:
  7. position: Tuple[float, float] = (0.0, 0.0)
  8. @dataclass
  9. class Rigidbody:
  10. velocity: Tuple[float, float] = (0.0, 0.0)
  11. acceleration: Tuple[float, float] = (0.0, 0.0)
  12. if __name__ == "__main__":
  13. # 2. intialize entity-component store
  14. store = Store()
  15. # 3. add some entities
  16. scene = store.add_entity()
  17. scene.add_child(Transform(), Rigidbody(acceleration=(1.0, 0.0)))
  18. scene.add_child(Transform(), Rigidbody(acceleration=(0.0, 1.0)))
  19. scene.add_child(Transform(), Rigidbody(acceleration=(1.0, 1.0)))
  20. # 4. run everything
  21. while True:
  22. for entity in store.get_entities_with(Transform, Rigidbody):
  23. transform, rigidbody = entity.get_components(Transform, Rigidbody)
  24. rigidbody.velocity = (
  25. rigidbody.velocity[0] + rigidbody.acceleration[0],
  26. rigidbody.velocity[1] + rigidbody.acceleration[1],
  27. )
  28. transform.position = (
  29. transform.position[0] + rigidbody.velocity[0],
  30. transform.position[1] + rigidbody.velocity[1],
  31. )
  32. print(f"{transform=}\t{rigidbody=}")

Dev Setup

Simply install pipenv and run the following line:

  1. pipenv install --dev