Skip to main content

Implementation of the ECS template for creating games

Project description

Implementation of the ECS template for creating games. Make a game instead of architecture for a game.

Документация на русском.

https://img.shields.io/pypi/dm/ecs_pattern.svg?style=social

Python version

3.5+

License

Apache-2.0

PyPI

https://pypi.python.org/pypi/ecs_pattern/

Intro

ECS - Entity-Component-System - it is an architectural pattern created for game development.

It is great for describing a dynamic virtual world.

Basic principles of ECS:

  • Composition over inheritance

  • Data separated from logic (Data Oriented Design)

Component - Property with object data
Entity - Container for properties
System - Data processing logic

Installation

$ pip install ecs-pattern

Guide

Component
Property with object data. Contains only data, no logic.
The component is used as a mixin in entities.
Use the ecs_pattern.component decorator to create components:
@component
class ComPosition:
    x: int = 0
    y: int = 0

@component
class ComPerson:
    name: str
    health: int
Entity
Container for properties. Consists of components only.
It is forbidden to add attributes to an entity dynamically.
Use the ecs_pattern.entity decorator to create entities:
@entity
class Player(ComPosition, ComPerson):
    pass

@entity
class Ball(ComPosition):
    pass
System
Data processing logic - components and entities.
Does not contain data about entities and components.
Use the ecs_pattern.System abstract class to create concrete systems:
class SysInit(System):
    def __init__(self, entities: EntityManager):
        self.entities = entities

    def start(self):
        self.entities.init(
            TeamScoredGoalEvent(Team.LEFT),
            Spark(spark_sprite(pygame.display.Info()), 0, 0, 0, 0)
        )
        self.entities.add(
            GameStateInfo(play=True, pause=False),
            WaitForBallMoveEvent(1000),
        )

class SysGravitation(System):
    def __init__(self, entities: EntityManager):
        self.entities = entities

    def update(self):
        for entity_with_pos in self.entities.get_with_component(ComPosition):
            if entity_with_pos.y > 0:
                entity_with_pos.y -= 1
EntityManager
Entity database.
A single point of access to all entities.
Use the ecs_pattern.EntityManager class to create systems.
entities.add - add entities.
entities.delete - delete entities.
entities.init - initialize entities (let manager know about entities).
entities.get_by_class - get all entities of the specified classes.
entities.get_with_component - Get all entities with the specified components.
entities = EntityManager()
entities.add(Player('Ivan', 20, 1, 2), Player('Vladimir', 30, 3, 4), Ball(0, 7))
for player_entity in entities.get_by_class(Player):
    print(player_entity.name)
for entity_with_pos in self.entities.get_with_component(ComPosition):
    print(entity_with_pos.x, entity_with_pos.y)
entities.delete(*tuple(next(entities.get_by_class(Ball), [])))
SystemManager
Container for systems.
Works with systems in a given order.
Use the ecs_pattern.SystemManager class to manage systems.
system_manager.start_systems - initialize systems. Call once before the main systems update cycle.
system_manager.update_systems - update systems status. Call in the main loop.
system_manager.stop_systems - stop systems. Call once after the main loop completes.
entities = EntityManager()
entities.add(Player('Ivan', 20, 1, 2), Player('Vladimir', 30, 3, 4), Ball(0, 7))
system_manager = SystemManager([SysPersonHealthRegeneration(entities), SysGravitation(entities)])
system_manager.start_systems()
while play:
    system_manager.update_systems()
    clock.tick(24)  # *pygame clock
system_manager.stop_systems()

Examples

Advantages

  • Weak code cohesion - easy to refactor and expand the codebase

  • Modularity and testability of logic - easy to test and reuse code in other projects

  • Hard to write bad code

  • Easy to follow Single Responsibility logic

  • Easy to combine entity properties

  • Easy to analyze performance

  • Easy to parallelize processing

  • Easy to work with clean data

Difficulties

It can take a lot of practice to learn how to cook ECS properly:

  • Data is available from anywhere - hard to find errors

  • Systems work strictly one after another

  • Recursive logic is not directly supported

Newbie mistakes

  • Inheritance of components, entities, systems

  • Ignoring the principles of ECS, such as storing data in the system

  • Raising ECS to the absolute, no one cancels the OOP

  • Adaptation of the existing project code under ECS “as is”

  • Use of recursive or reactive logic in systems

Good Practices

  • Use components - flags

  • Minimize component change locations

  • Use event entities and event systems

  • In large projects, placing ECS objects by type is not convenient (components.py, systems.py …). Group by responsibilities (movement.py …)

Releases

History of important changes: release_notes.rst

Help the project

Welcome :D

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

ecs-pattern-1.0.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

ecs_pattern-1.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file ecs-pattern-1.0.0.tar.gz.

File metadata

  • Download URL: ecs-pattern-1.0.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for ecs-pattern-1.0.0.tar.gz
Algorithm Hash digest
SHA256 64bee0d6337f15202278aba268b0b0953044938d4e0a2d563a939885e5ed173c
MD5 77dd192a85d44880b019d7a3f738622a
BLAKE2b-256 f096c38187b0dc29d6db4b70fb019e4a53cb4776ebfd0c9bfc4b8c018b5016e8

See more details on using hashes here.

File details

Details for the file ecs_pattern-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ecs_pattern-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for ecs_pattern-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61bf2b99286010fcec7c8281fa3c5835626be2d9efa063799b8a7809c1ab7cea
MD5 eb23618c4c230980ab8822f1d08f6eaf
BLAKE2b-256 178f3e3680f35e3f223f875e184d58f95198341b48280dd5826abc0dc4d59233

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