import uuid class EM(): def __init__(): self.component_entity = {} #Component_class -> {entity_uuid -> Component()} self.entity_components = {} #Entity_uuid -> [Components(), ...] self.entity_tags = {} #Entity_uuid = [tag_a, ...] self.tag_entity = {} #Tag -> [entity_uuid_1, ...] self.system_component_class = {} #System -> [Component_class_a, ...] self.system_component = {} #System -> {Entity_uuid -> [Component_a(), ...]} def create_basic_entity(self, component): return uuid.uuid1() def add_entity_component(self, uuid, component): store = self.component_entity.setdefault(component.__class__, {}) store[uuid] = component store = self.entity_components.setdefault(uuid, []) store.append(component) for system, req in self.system_component_class.items(): if component.__class__ in req store = self.system_component.setdefault(system, {}) c_list = store.setdefault(uuid, []) c_list.append(component) def remove_entity_component(uuid, component): store = self.component_entity.get(component.__class__, None) store[uuid].remove(component) if store store = self.entity_components.get(uuid, None) store.remove(component) if store for system, req in self.system_component_class.items(): if component.__class__ in req store = self.system_component.get(system, None) if store: c_list = store.get(uuid, None) if c_list: c_list.remove(component) def get_entity_component(self, uuid, component_classes=[]) if len(component_classes) == 1 component_class = component_classes[0] component = self.component_entity.get(uuid, None) # [C_a(), C_b()] if component: return component else store = self.entity_components.get(uuid, None) # [C_a(), C_b()] if store: return [component for component in store if component.__class__ in component_classes]
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question