annotate grease/world.py @ 38:b30a72c41f90

Added components to the World class
author KarstenBock@gmx.net
date Mon, 05 Sep 2011 14:46:43 +0200
parents bc88f7d5ca8b
children
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13 """Worlds are environments described by a configuration of components, systems and
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 renderers. These parts describe the data, behavioral and presentation aspects
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 of the world respectively.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17 The world environment is the context within which entities exist. A typical
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 application consists of one or more worlds containing entities that evolve
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 over time and react to internal and external interaction.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 See :ref:`an example of world configuration in the tutorial <tut-world-example>`.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 __version__ = '$Id$'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 import itertools
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 from grease import mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 from grease.component import ComponentError
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 from grease.entity import Entity, ComponentEntitySet
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 class BaseWorld(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 """A coordinated collection of components, systems and entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 A world is also a mode that may be pushed onto a
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 :class:`grease.mode.Manager`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 components = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 """:class:`ComponentParts` object containing all world components.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 :class:`grease.component.Component` objects define and contain all entity data
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 systems = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 """:class:`Parts` object containing all world systems.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 :class:`grease.System` objects define world and entity behavior
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 renderers = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 """:class:`Parts` object containing all world renderers.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 :class:`grease.Renderer` objects define world presentation
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 entities = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 """Set of all entities that exist in the world"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 def __init__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 self.components = ComponentParts(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 self.systems = Parts(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 self.renderers = Parts(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 self.new_entity_id = itertools.count().next
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 self.new_entity_id() # skip id 0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 self.entities = WorldEntitySet(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 self._full_extent = EntityExtent(self, self.entities)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65 self._extents = {}
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 self.configure()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 def configure(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 """Hook to configure the world after construction. This method
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 is called immediately after the world is initialized. Override
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 in a subclass to configure the world's components, systems,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 and renderers.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74 The default implementation does nothing.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 def __getitem__(self, entity_class):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78 """Return an :class:`EntityExtent` for the given entity class. This extent
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 can be used to access the set of entities of that class in the world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 or to query these entities via their components.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82 Examples::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84 world[MyEntity]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 world[...]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87 :param entity_class: The entity class for the extent.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 May also be a tuple of entity classes, in which case
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 the extent returned contains union of all entities of the classes
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 in the world.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 May also be the special value ellipsis (``...``), which
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 returns an extent containing all entities in the world. This allows
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 you to conveniently query all entities using ``world[...]``.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 if isinstance(entity_class, tuple):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98 entities = set()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 for cls in entity_class:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 if cls in self._extents:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101 entities |= self._extents[cls].entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 return EntityExtent(self, entities)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 elif entity_class is Ellipsis:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 return self._full_extent
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 return self._extents[entity_class]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 extent = self._extents[entity_class] = EntityExtent(self, set())
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 return extent
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111 def draw_renderers(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 """Draw all renderers"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 for renderer in self.renderers:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114 renderer.draw()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116 class WorldEntitySet(set):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 """Entity set for a :class:`World`"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119 def __init__(self, world):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 self.world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122 def add(self, entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 """Add the entity to the set and all necessary class sets
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124 Return the unique entity id for the entity, creating one
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 as needed.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 super(WorldEntitySet, self).add(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 for cls in entity.__class__.__mro__:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 if issubclass(cls, Entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 self.world[cls].entities.add(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132 def remove(self, entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 """Remove the entity from the set and, world components,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134 and all necessary class sets
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 super(WorldEntitySet, self).remove(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 for component in self.world.components:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139 del component[entity]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142 for cls in entity.__class__.__mro__:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143 if issubclass(cls, Entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
144 self.world[cls].entities.discard(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
145
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
146 def discard(self, entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
147 """Remove the entity from the set if it exists, if not,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
148 do nothing
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
149 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
150 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
151 self.remove(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
152 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
153 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
154
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
155
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
156 class EntityExtent(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
157 """Encapsulates a set of entities queriable by component. Extents
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
158 are accessed by using an entity class as a key on the :class:`World`::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
159
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
160 extent = world[MyEntity]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
161 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
162
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
163 entities = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
164 """The full set of entities in the extent"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
165
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
166 def __init__(self, world, entities):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
167 self.__world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
168 self.entities = entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
169
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
170 def __getattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
171 """Return a queriable :class:`ComponentEntitySet` for the named component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
172
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
173 Example::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
174
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
175 world[MyEntity].movement.velocity > (0, 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
176
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
177 Returns a set of entities where the value of the :attr:`velocity` field
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
178 of the :attr:`movement` component is greater than ``(0, 0)``.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
179 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
180 component = getattr(self.__world.components, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
181 return ComponentEntitySet(component, self.entities & component.entities)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
182
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
183
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
184 class Parts(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
185 """Maps world parts to attributes. The parts are kept in the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
186 order they are set. Parts may also be inserted out of order.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
187
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
188 Used for:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
189
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
190 - :attr:`World.systems`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
191 - :attr:`World.renderers`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
192 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
193
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
194 _world = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
195 _parts = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
196 _reserved_names = ('entities', 'entity_id', 'world')
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
197
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
198 def __init__(self, world):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
199 self._world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
200 self._parts = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
201
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
202 def _validate_name(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
203 if (name in self._reserved_names or name.startswith('_')
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
204 or hasattr(self.__class__, name)):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
205 raise ComponentError('illegal part name: %s' % name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
206 return name
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
207
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
208 def __setattr__(self, name, part):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
209 if not hasattr(self.__class__, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
210 self._validate_name(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
211 if not hasattr(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
212 self._parts.append(part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
213 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
214 old_part = getattr(self, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
215 self._parts[self._parts.index(old_part)] = part
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
216 super(Parts, self).__setattr__(name, part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
217 if hasattr(part, 'set_world'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
218 part.set_world(self._world)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
219 elif name.startswith("_"):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
220 super(Parts, self).__setattr__(name, part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
221 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
222 raise AttributeError("%s attribute is read only" % name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
223
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
224 def __delattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
225 self._validate_name(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
226 part = getattr(self, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
227 self._parts.remove(part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
228 super(Parts, self).__delattr__(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
229
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
230 def insert(self, name, part, before=None, index=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
231 """Add a part with a particular name at a particular index.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
232 If a part by that name already exists, it is replaced.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
233
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
234 :arg name: The name of the part.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
235 :type name: str
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
236
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
237 :arg part: The component, system, or renderer part to insert
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
238
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
239 :arg before: A part object or name. If specified, the part is
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
240 inserted before the specified part in order.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
241
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
242 :arg index: If specified, the part is inserted in the position
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
243 specified. You cannot specify both before and index.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
244 :type index: int
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
245 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
246 assert before is not None or index is not None, (
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
247 "Must specify a value for 'before' or 'index'")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
248 assert before is None or index is None, (
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
249 "Cannot specify both 'before' and 'index' arguments when inserting")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
250 self._validate_name(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
251 if before is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
252 if isinstance(before, str):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
253 before = getattr(self, before)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
254 index = self._parts.index(before)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
255 if hasattr(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
256 old_part = getattr(self, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
257 self._parts.remove(old_part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
258 self._parts.insert(index, part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
259 super(Parts, self).__setattr__(name, part)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
260 if hasattr(part, 'set_world'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
261 part.set_world(self._world)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
262
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
263 def __iter__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
264 """Iterate the parts in order"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
265 return iter(tuple(self._parts))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
266
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
267 def __len__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
268 return len(self._parts)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
269
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
270
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
271 class ComponentParts(Parts):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
272 """Maps world components to attributes. The components are kept in the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
273 order they are set. Components may also be inserted out of order.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
274
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
275 Used for: :attr:`World.components`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
276 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
277
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
278 def join(self, *component_names):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
279 """Join and iterate entity data from multiple components together.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
280
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
281 For each entity in all of the components named, yield a tuple containing
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
282 the entity data from each component specified.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
283
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
284 This is useful in systems that pull data from multiple components.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
285
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
286 Typical Usage::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
287
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
288 for position, movement in world.components.join("position", "movement"):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
289 # Do something with each entity's position and movement data
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
290 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
291 if component_names:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
292 components = [getattr(self, self._validate_name(name))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
293 for name in component_names]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
294 if len(components) > 1:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
295 entities = components[0].entities & components[1].entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
296 for comp in components[2:]:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
297 entities &= comp.entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
298 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
299 entities = components[0].entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
300 for entity in entities:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
301 yield tuple(comp[entity] for comp in components)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
302