Created
November 23, 2023 17:55
-
-
Save asiletto/bc79fdac8bf6410e3787bd9895a7d910 to your computer and use it in GitHub Desktop.
ECS example with esper - RTS game python skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import IntEnum | |
from dataclasses import dataclass as component | |
class SpeciesEnum(IntEnum): | |
TOMATO = 1 | |
BEECH = 2 | |
CAT = 1001 | |
DOG = 1002 | |
E_COLI = 10001 | |
C_ALBICANS = 10002 | |
@component | |
class UUID: | |
uuid: str = "" | |
@component | |
class Position: | |
x: int = 0 | |
y: int = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import esper | |
import json | |
from commons import SpeciesEnum, UUID, Position | |
from trees import TreeSize, TreeSpecies | |
from trees import GrowthProcessor | |
from trees import setTreeHandlers | |
def loadWorld(): | |
f = open('world.json') | |
world = json.load(f) | |
f.close() | |
for treeData in world['trees']: | |
print(treeData) | |
tree = esper.create_entity() | |
esper.add_component(tree, UUID(uuid=treeData['id'])) | |
esper.add_component(tree, TreeSize(years=treeData['years'], size=treeData['size'])) | |
esper.add_component(tree, Position(x=treeData['x'], y=treeData['y'])) | |
esper.add_component(tree, TreeSpecies(name=treeData['species'])) | |
def main(): | |
loadWorld() | |
esper.add_processor(GrowthProcessor(), 1) | |
setTreeHandlers(esper) | |
try: | |
while True: | |
esper.process() | |
time.sleep(1) | |
except KeyboardInterrupt: | |
return | |
if __name__ == '__main__': | |
print("\nHeadless Example. Press Ctrl+C to quit!\n") | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import esper | |
from dataclasses import dataclass as component | |
from commons import SpeciesEnum, UUID | |
@component | |
class TreeSize: | |
years: float = 0.0 | |
size: float = 0.0 | |
@component | |
class TreeSpecies: | |
name: SpeciesEnum = SpeciesEnum.TOMATO | |
class GrowthProcessor: | |
def process(self): | |
print("") | |
print("") | |
for ent, (size, species) in esper.get_components(TreeSize, TreeSpecies): | |
uuid = esper.component_for_entity(ent, UUID) | |
print("UUID: "+uuid.uuid+" entity: " + str(ent)) | |
print(size) | |
print(species) | |
size.years += 0.001 | |
if species.name == SpeciesEnum.TOMATO: | |
size.size += 0.1 | |
if size.size > 3: | |
esper.delete_entity(ent) | |
esper.dispatch_event('tree_death', ent, species.name, uuid.uuid) | |
if species.name == SpeciesEnum.BEECH: | |
size.size += 0.5 | |
if size.size > 15: | |
esper.delete_entity(ent) | |
esper.dispatch_event('tree_death', ent, species.name, uuid.uuid) | |
print("==========") | |
def logTreeDeath(ent, species, uuid): | |
print("dead tree:" + str(species)+" "+uuid) | |
def setTreeHandlers(esper): | |
esper.set_handler('tree_death', logTreeDeath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"trees":[ | |
{ | |
"id":"aaaa-bbbb-cccc", | |
"x":10, | |
"y":5, | |
"years":10, | |
"size":1, | |
"species": 1 | |
}, | |
{ | |
"id":"dddd-eeee-ffff", | |
"x":2, | |
"y":2, | |
"years":1, | |
"size":10, | |
"species": 2 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment