annotate bGrease/entity.py @ 109:8d87e1203402

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