Git Product home page Git Product logo

entitas-python's Introduction

Entitas for Python

image

entitas-python is a port of Entitas ECS for C# and Unity.

Overview

Components

Position = namedtuple('Position', 'x y')
Health = namedtuple('Health', 'value')
Movable = namedtuple('Movable', '')

Entity

entity.add(Position, 3, 7)
entity.add(Health, 100)
entity.add(Movable)

entity.replace(Position, 10, 100)
entity.replace(Health, entity.get(Health).value - 1)

entity.remove(Position)

has_pos = entity.has(Position)
movable = entity.has(Movable)

Context

context = Context()
entity = context.create_entity()
entity.add(Movable)

entities = context.entities
for e in entities:
    # do something

Group

context.get_group(Matcher(Position)).entities

def move(entity, new_comp):
    # do something

context.get_group(Matcher(Position)).on_entity_added += move

Entity Collector

group = context.get_group(Matcher(Position))
collector = Collector()
collector.add(group, GroupEvent.added)

# later

for e in collector.collected_entities:
   # do something with all the entities
   # that have been collected to this point of time

collector.clear_collected_entities()

# stop observing
collector.deactivate()

Entity Index

Person = namedtuple('Person', 'name age')
group = context.get_group(Matcher(Person))

# get a set of 42-year-old Person
index = EntityIndex(Person, group, 'age')
context.add_entity_index(index)
entities = context.get_entity_index(Person).get_entities(42)

# get the Person named "John"
primary_index = PrimaryEntityIndex(Person, group, 'name')
context.add_entity_index(primary_index)
entity = context.get_entity_index(Person).get_entity('John')

Processors

class RenderDisplay(ExecuteProcessor):

    def execute(self):
        pygame.display.update()


# Initialize, Cleanup and TearDown are also available.


class Move(ReactiveProcessor):

    def __init__(self, context):
        super().__init__(context)
        self._context = context

    def get_trigger(self):
        return {Matcher(Position): GroupEvent.ADDED}

    def filter(self, entity):
        return entity.has(Position, Movable)

    def react(self, entities):
        for entity in entities:
            # use entity.get(Position).x & entity.get(Position).y

Setup example

context = Context()

processors = Processors()
processors.add(StartGame(context))
processors.add(InputProcessors(context))
processors.add(RenderDisplay())
processors.add(DestroyEntity(context))

processors.initialize()
processors.activate_reactive_processors()

# main loop
running = True
while running:
    processors.execute()
    processors.cleanup()

    if EmitInput.quit:
        break

processors.clear_reactive_processors()
processors.tear_down()

quit()

entitas-python's People

Contributors

aenyhm avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.