A simple attribute-based approach to ECS.
Project description
punyecs
punyecs is a tiny Entity Component System (ECS) inspired by tiny-ecs for Python. punyecs operates directly on class attributes as opposed to creating components along with querying mechanisms for fine grain control over which objects are operated on by systems similar to how tiny-ecs works on Lua tables.
What is it?
Instead of requiring inheritance, one can specify which attributes to operate on and any object (regardless of class) that has those attributes is operated on. That is, if a Player has an x and y attribute and an (unrelated) Enemy class has an x and y attribute you can have them both influenced by a World object. This avoids complicated inheritance hierarchies.
Here is a small example to illustrate the above:
from dataclasses import dataclass
from punyecs import World, requirements
w = World()
@dataclass
class Player:
x: float
y: float
@dataclass
class Enemy:
x: float
y: float
@requirements(w, {"x", "y"})
def move(e, dt):
e.x += 0.1
e.y += 0.1
player = Player(0.0, 0.0)
enemy = Enemy(1.0, 1.0)
w.add(player)
w.add(enemy)
w.update(1)
print(player.x)
# Prints 0.1
print(player.y)
# Prints 0.1
print(enemy.x)
# Prints 1.1
print(enemy.y)
# Prints 1.1
A Bit More Sophistication
We may also do exclusions for fine grain control. Returning to the example above, we may want various enemies to move like above but instead want to allow controller input for the player object. We can avoid influencing the player object by putting it in the excluded objects list. The function f becomes:
@requirements(w, {"x", "y"}, exclude_objs=[player])
def move(e, dt):
e.x += 0.1
e.y += 0.1
Then after every world.update(1), the player object will still remain at x=0.0, y=0.0.
Even More Sophistication!
It might be inconvenient to exclude individual objects if a large number of objects need to be excluded. punyecs provides a couple more filtering options. One way around this is to specify which attributes an object should not have.
For instance, we may have many different kinds of creatures. Most can follow the usual movement update function, but some creatures have a wiggle attribute. wiggle could be a Boolean, or even something more sophisticated like a function that describes how the creature wiggles.
To illustrate this consider:
from dataclasses import dataclass
from punyecs import World, requirements
w = World()
@dataclass
class Player:
x: float
y: float
@dataclass
class WalkingEnemy:
x: float
y: float
@dataclass
class Wiggler:
x: float
y: float
wiggle: lambda x: x + 2
@requirements(w, {"x", "y"}, exclude={"wiggle"})
def move(e, dt):
e.x += 0.1
e.y += 0.1
@requirements(w, {"wiggle", "x", "y"})
def wiggle(e, dt):
e.x = wiggle(e.x)
e.y = wiggle(e.y)
player = Player(0.0, 0.0)
enemy = Enemy(1.0, 1.0)
wiggler = Wiggle(3.0, 3.0)
w.add(player)
w.add(enemy)
w.add(wiggler)
w.update(1)
print(player.x)
# Prints 0.1
print(player.y)
# Prints 0.1
print(enemy.x)
# Prints 1.1
print(enemy.y)
# Prints 1.1
print(wiggler.x)
# Prints 5.0
print(wiggler.y)
# Prints 5.0
Thus, move does not operate on wiggler but wiggle does.
Documentation
Even more filtering options are available. To learn more, see the readthedocs.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file punyecs-0.1.0.tar.gz.
File metadata
- Download URL: punyecs-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a10c89f8eaa32fad8bf9ec916d263685c3f1ffcb5361c5a489b86d324eec575
|
|
| MD5 |
b3c9839de5c0426e68ccdaa49c1e9509
|
|
| BLAKE2b-256 |
9ef039fe4ce3328a20c936ea16c161f43262fbd386c198f44baa432ff079dc31
|
Provenance
The following attestation bundles were made for punyecs-0.1.0.tar.gz:
Publisher:
python-publish.yml on thyrgle/punyecs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
punyecs-0.1.0.tar.gz -
Subject digest:
5a10c89f8eaa32fad8bf9ec916d263685c3f1ffcb5361c5a489b86d324eec575 - Sigstore transparency entry: 1563936758
- Sigstore integration time:
-
Permalink:
thyrgle/punyecs@d592481b24188bedbbc24be5ba33e510c9fe6b05 -
Branch / Tag:
refs/tags/0.1.0-beta - Owner: https://github.com/thyrgle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d592481b24188bedbbc24be5ba33e510c9fe6b05 -
Trigger Event:
release
-
Statement type:
File details
Details for the file punyecs-0.1.0-py3-none-any.whl.
File metadata
- Download URL: punyecs-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4d25fd35720fd54622a48f37789150bb364e55293b6fd6f310bb423825a3cce
|
|
| MD5 |
8fbecdc4a6b430103329d611a38c92dc
|
|
| BLAKE2b-256 |
87f03076b56d093136ecff65a727c3a04f1bc13da0f691867807dc1bde4cec75
|
Provenance
The following attestation bundles were made for punyecs-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on thyrgle/punyecs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
punyecs-0.1.0-py3-none-any.whl -
Subject digest:
e4d25fd35720fd54622a48f37789150bb364e55293b6fd6f310bb423825a3cce - Sigstore transparency entry: 1563936766
- Sigstore integration time:
-
Permalink:
thyrgle/punyecs@d592481b24188bedbbc24be5ba33e510c9fe6b05 -
Branch / Tag:
refs/tags/0.1.0-beta - Owner: https://github.com/thyrgle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d592481b24188bedbbc24be5ba33e510c9fe6b05 -
Trigger Event:
release
-
Statement type: