Skip to main content

A lightweight implementation of Entity Component System architecture

Project description

pyriak

A lightweight implementation of Entity Component System architecture for Python.

(Originally created August 2, 2022.)

Concepts

Usage

Entity creation

Any entity must first be created and populated with components.

A system can directly create and populate an entity. This system may decide itself to create an entity, or handle an event that directly tells it to create one.

player = space.entities.create(
  components.Player(),
  components.CameraFocus(),
  components.Sprite(),
)

A listening system can extend a created entity.

@bind(ComponentAdded, 200, RocketBooster):
def add_rocket_exhaust(space, event):
  event.entity.add(ParticleEmitter(rocket_particles))

Often, multiple systems will need to create a certain set of components that are not related enough to put under a single component, but common enough to necessitate code reuse. A dedicated module can provide functions that produce a "batch", a set of components, sometimes with parameters for customization.

# batches.py
def spaceship(radius=20):
  body = components.Body(radius)
  body.collision_type = 'spaceship'
  return body, components.Engine(), components.Sprite(spaceship_sprite)
...
# systems/player.py
import batches
...
player = space.entities.create(
  *batches.spaceship(),
  components.CameraFocus(),
  components.PlayerController()
)
enemy = space.entities.create(
  *batches.spaceship(),
  components.AIController()
)

It is also easy to customize or initialize the components after the entity and batch have been created as opposed to passing in customization parameters to the batch function.

Small, individual components may be created directly by systems. Batch functions should only be made when necessary, for when it is likely to be reused: repeated at least twice, lots of boilerplate. Batch functions should not be made for a large, unique set of components for a specific entity. It is preferable to have smaller batches to allow for more control in choosing which components to use.

A batch function that calls another batch function mimics inheritance, which can lead to avoidable problems. Batches should be considered large components, not a standalone pseudo-class. (However, a batch function that only calls other batch functions is fine because it does not create any components itself, so its use is not directly required by anything.)

Also note that the components should represent one, indivisible thing. Components can be created large and then later broken down into a batch of multiple components.

TO DO:

  • dynamic handlers?
  • 3.11 typing features
  • fix multibinding typing
  • system "new" method instantiation
  • __future__.annotations
  • type aliases
  • 'direct', 'indirect', 'strict', 'immediate' vocab docs
  • validate subclasses: hash, mro
  • sys mgr expose handlers + bind predicate/filter + _Binding public
  • __contains__ TypeError raise?
  • picklable __setstate__ __getstate__ __copy__ classes
  • 'processor' game pump generator yield event method, 'event loop'
  • discard method
  • keys method (for dict protocol)
  • improve error messages
  • raise from None bad
  • views, items methods: mappingproxy
  • __eq__, remove __hash__: mgrs
  • types(*types) method functionality all mgrs+entity ?
  • entities from ids: itertools helpers in entitymgr
  • 'add', 'remove' methods return value
  • entity mgr garbage collection behavior (currently undefined?)
  • game __call__ use ?
  • more container (set) dunder methods, operations
  • copy methods
  • more positional only arguments
  • more system config
  • str and repr methods all
  • place documentation in code, along with all rules (style guide first)
  • optimization through profiling, scalene (in a game)
  • make imported module variables private (consistency)
  • python version lower in poetry dependencies ? find min python version
  • review and rewrite readme.txt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyriak-0.2.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

pyriak-0.2.0-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file pyriak-0.2.0.tar.gz.

File metadata

  • Download URL: pyriak-0.2.0.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10

File hashes

Hashes for pyriak-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4049ef05ed688f162de7d5d8c3ec7ba40d55f5166a27a4638e8e17a05ce79246
MD5 716cfcdbcdbabad29d0c35d1f3f0c227
BLAKE2b-256 46123b8255ee02b8042902ef0d620011dab065faa27e01f232ec2671027023ad

See more details on using hashes here.

File details

Details for the file pyriak-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyriak-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10

File hashes

Hashes for pyriak-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 463447f831cf80fad82fe3465c2b04ca4828efe473ebfa427538b8840e8f8e9c
MD5 0702c49f42affe2f579b299b41bbbebe
BLAKE2b-256 59c2f1001fcc348285794380e26e07fe677ecde9b394f7465d872e07104849a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page