annotate bGrease/entity.py @ 72:783dc57eca7c

Fixed old equipable values not reset when equippin another equipable in its slot. Added Exceptions for the Equip components.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 19:51:37 +0200
parents ff3e395abf91
children a9cc5559ec2a
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 """Grease entities are useful as actionable, interactive
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 game elements that are often visible to the player.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 You might use entities to represent:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 - Characters
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 - Bullets
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 - Particles
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 - Pick-ups
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 - Space Ships
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 - Weapons
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 - Trees
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 - Planets
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 - Explosions
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 See :ref:`an example entity class in the tutorial <tut-entity-example>`.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 """
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 __version__ = '$Id$'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 __all__ = ('Entity', 'EntityComponentAccessor', 'ComponentEntitySet')
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
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 class EntityMeta(type):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 """The entity metaclass enforces fixed slots of `entity_id` and `world`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 for all subclasses. This prevents accidental use of other entity instance
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 attributes, which may not be saved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 Class attributes are not affected by this restriction, but subclasses
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 should be careful not to cause name collisions with world components,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 which are exposed as entity attributes. Using a naming convention for
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 class attributes, such as UPPER_CASE_WITH_UNDERSCORES is recommended to
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 avoid name clashes.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 Note as a result of this, entity subclasses are not allowed to define
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 `__slots__`, and doing so will cause a `TypeError` to be raised.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 def __new__(cls, name, bases, clsdict):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 if '__slots__' in clsdict:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 raise TypeError('__slots__ may not be defined in Entity subclasses')
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 clsdict['__slots__'] = ('world', 'entity_id')
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 return type.__new__(cls, name, bases, clsdict)
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
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 class Entity(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 """Base class for grease entities.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 Entity objects themselves are merely identifiers within a :class:`grease.world.World`.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 They also provide a facade for convenient entity-wise access of component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 data. However, they do not contain any data themselves other than an
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 entity id.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 Entities must be instantiated in the context of a world. To instantiate an
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67 entity, you must pass the world as the first argument to the constructor.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 Subclasses that implement the :meth:`__init__()` method, must accept the world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 as their first argument (after ``self``). Other constructor arguments can be
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 specified arbitarily by the subclass.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 __metaclass__ = EntityMeta
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 def __new__(cls, world, *args, **kw):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 """Create a new entity and add it to the world"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 entity = object.__new__(cls)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 entity.world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78 entity.entity_id = world.new_entity_id()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 world.entities.add(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 return entity
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 def __getattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83 """Return an :class:`EntityComponentAccessor` for this entity
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84 for the component named.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86 Example::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 my_entity.movement
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 component = getattr(self.world.components, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 return EntityComponentAccessor(component, self)
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 def __setattr__(self, name, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 """Set the entity data in the named component for this entity.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 This sets the values of the component fields to the values of
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 the matching attributes of the value provided. This value must
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 have attributes for each of the component fields.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 This allows you to easily copy component data from one entity
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 to another.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 Example::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 my_entity.position = other_entity.position
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 if name in self.__class__.__slots__:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 super(Entity, self).__setattr__(name, value)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 component = getattr(self.world.components, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110 component.set(self, value)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 def __delattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 """Remove this entity and its data from the component.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 Example::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 del my_entity.renderable
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 component = getattr(self.world.components, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 del component[self]
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 __hash__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 return self.entity_id
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 def __eq__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 return self.world is other.world and self.entity_id == other.entity_id
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 def __repr__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 return "<%s id: %s of %s %x>" % (
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 self.__class__.__name__, self.entity_id,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131 self.world.__class__.__name__, id(self.world))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 def delete(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134 """Delete the entity from its world. This removes all of its
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 component data. If then entity has already been deleted,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 this call does nothing.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138 self.world.entities.discard(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 @property
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141 def exists(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142 """True if the entity still exists in the world"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143 return self in self.world.entities
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
144
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 class EntityComponentAccessor(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
147 """A facade for accessing specific component data for a single entity.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
148 The implementation is lazy and does not actually access the component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
149 data until needed. If an attribute is set for a component that the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
150 entity is not yet a member of, it is automatically added to the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
151 component first.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
152
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
153 :param component: The :class:`grease.Component` being accessed
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
154 :param entity: The :class:`Entity` being accessed
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
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
157 # beware, name mangling ahead. We want to avoid clashing with any
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
158 # user-configured component field names
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
159 __data = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
160
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
161 def __init__(self, component, entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
162 clsname = self.__class__.__name__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
163 self.__dict__['_%s__component' % clsname] = component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
164 self.__dict__['_%s__entity' % clsname] = entity
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 __nonzero__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
167 """The accessor is True if the entity is in the component,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
168 False if not, for convenient membership tests
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 return self.__entity in self.__component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
171
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
172 def __getattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
173 """Return the data for the specified field of the entity's component"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
174 if self.__data is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
175 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
176 data = self.__component[self.__entity]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
177 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
178 raise AttributeError(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
179 clsname = self.__class__.__name__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
180 self.__dict__['_%s__data' % clsname] = data
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
181 return getattr(self.__data, name)
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 def __setattr__(self, name, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
184 """Set the data for the specified field of the entity's component"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
185 if self.__data is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
186 clsname = self.__class__.__name__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
187 if self.__entity in self.__component:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
188 self.__dict__['_%s__data' % clsname] = self.__component[self.__entity]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
189 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
190 self.__dict__['_%s__data' % clsname] = self.__component.set(self.__entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
191 setattr(self.__data, name, value)
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 class ComponentEntitySet(set):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
195 """Set of entities in a component, can be queried by component fields"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
196
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
197 _component = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
198
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
199 def __init__(self, component, entities=()):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
200 self.__dict__['_component'] = component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
201 super(ComponentEntitySet, self).__init__(entities)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
202
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
203 def __getattr__(self, name):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
204 if self._component is not None and name in self._component.fields:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
205 return self._component.fields[name].accessor(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
206 raise AttributeError(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, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
209 if self._component is not None and name in self._component.fields:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
210 self._component.fields[name].accessor(self).__set__(value)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
211 raise AttributeError(name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
212