annotate src/parpg/bGrease/entity.py @ 127:90b49fda0340

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