Package: ecs-pattern
Implementation of the ECS pattern (Entity Component System) for creating games.Make a game instead of architecture for a game.Документация на Русском.Python version3.3+LicenseApache-2.0PyPIhttps://pypi.python.org/pypi/ecs_pattern/Dependenciesdataclasses before 3.7, typing before 3.5ContentsIntroInstallationGuideComponentEntitySystemEntityManagerSystemManagerExamplesAdvantagesDifficultiesNewbie mistakesGood PracticesReleasesHelp the projectIntroECS - 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 inheritanceData separated from logic (Data Oriented Design)Component- Property with object dataEntity- Container for propertiesSystem- Data processing logicEntityManager- Entity databaseSystemManager- Container for systemsInstallation$ pip install ecs-patternGuidefromecs_patternimportcomponent,entity,EntityManager,System,SystemManagerDescribe components - componentDescribe entities based on components - entityDistribute the responsibility of processing entities by systems - SystemStore entities in entity manager - EntityManagerManage your systems with SystemManagerComponentProperty 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.Technically this is python dataclass.@componentclassComPosition:x:int=0y:int=0@componentclassComPerson:name:strhealth:intEntityContainer 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.Technically this is python dataclass with slots=True.@entityclassPlayer(ComPosition,ComPerson):pass@entityclassBall(ComPosition):passSystemEntity processing logic.Does not contain data about entities and components.Use the ecs_pattern.System abstract class to create concrete systems:classSysInit(System):def__init__(self,entities:EntityManager):self.entities=entitiesdefstart(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),)classSysGravitation(System):def__init__(self,entities:EntityManager):self.entities=entitiesdefupdate(self):forentity_with_posinself.entities.get_with_component(ComPosition):ifentity_with_pos.y>0:entity_with_pos.y-=1EntityManagerContainer for entities.Use the ecs_pattern class.EntityManager for creating an entity manager.Time complexity of get_by_class and get_with_component - like a dictentities.add- Add entities.entities.delete- Delete entities.entities.delete_buffer_add- Save entities to the delete buffer to delete later.entities.delete_buffer_purge- Delete all entities in the deletion buffer and clear the buffer.entities.init- Let manager know about entities. KeyError are raising on access to unknown entities.entities.get_by_class- Get all entities of the specified classes. Respects the order of entities.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))forentity_with_posinentities.get_with_component(ComPosition):print(entity_with_pos.x,entity_with_pos.y)forplayer_entityinentities.get_by_class(Player):print(player_entity.name)entities.delete_buffer_add(player_entity)entities.delete_buffer_purge()entities.delete(*tuple(entities.get_by_class(Ball)))# one line delSystemManagerContainer 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()whileplay:system_manager.update_systems()clock.tick(24)# *pygame clocksystem_manager.stop_systems()ExamplesPong: game - pygame + ecs_patternSnow day: scene - pygame + ecs_patternTrig fall: commercial game - pygame + ecs_pattern + numpyAdvantagesMemory efficient - Component and Entity use dataclassConvenient search for objects - by entity class and by entity componentsFlexibility - loose coupling in the code allows you to quickly expand the projectModularity - the code is easy to test, analyze performance, and reuseExecution control - systems work strictly one after anotherFollowing the principles of the pattern helps to write quality codeConvenient to parallelize processingCompact implementationDifficultiesIt can take a lot of practice to learn how to cook ECS properlyData is available from anywhere - hard to find errorsNewbie mistakesInheritance of components, entities, systemsIgnoring the principles of ECS, such as storing data in the systemRaising ECS to the absolute, no one cancels the OOPAdaptation of the existing project code under ECS “as is”Use of recursive or reactive logic in systemsUsing EntityManager.delete in get_by_class, get_with_component loopsGood PracticesUse “Singleton” components with data and flagsMinimize component change locationsDo not create methods in components and entitiesDivide the project into scenes, a scene can be considered a cycle for the SystemManager with its EntityManagerUse packages to separate scenesProject tree example:/common_tools
    __init__.py
    resources.py
    i18n.py
    gui.py
    consts.py
    components.py
    math.py
/menu_scene
    __init__.py
    entities.py
    main_loop.py
    surfaces.py
    systems.py
/game_scene
    __init__.py
    entities.py
    main_loop.py
    surfaces.py
    systems.py
main.pyReleasesHistory of important changes:release_notes.rstHelp the projectFound a bug or have a suggestion - issue / merge request 🎯There is nothing to help this project with - help another open project that you are using ✋Nowhere to put the money - spend it on family, friends, loved ones or people around you 💰Star the project ⭐