annotate src/parpg/grease/world.py @ 27:09b581087d68

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