annotate src/parpg/bGrease/component/general.py @ 157:db6403c1a7a1

Fixed that dropped items cannot be picked up.
author KarstenBock@gmx.net
date Fri, 07 Oct 2011 14:36:36 +0200
parents 0f659c7675f6
children
rev   line source
92
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
1 #############################################################################
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
2 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
4 # All Rights Reserved.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
5 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
6 # This software is subject to the provisions of the MIT License
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
7 # A copy of the license should accompany this distribution.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
11 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
12 #############################################################################
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
13
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
14 __version__ = '$Id$'
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
15
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
16 from parpg.bGrease.component import base
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
17 from parpg.bGrease.component import field
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
18 from parpg.bGrease.entity import ComponentEntitySet
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
19
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
20
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
21 class Component(dict):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
22 """General component with a configurable schema
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
23
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
24 The field schema is defined via keyword args where the
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
25 arg name is the field name and the value is the type object.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
26
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
27 The following types are supported for fields:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
28
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
29 - :class:`int`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
30 - :class:`float`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
31 - :class:`bool`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
32 - :class:`str`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
33 - :class:`object`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
34 - |Vec2d|
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
35 - |Vec2dArray|
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
36 - |RGBA|
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
37 - |Rect|
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
38 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
39
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
40 deleted_entities = ()
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
41 """List of entities deleted from the component since the last time step"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
42
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
43 new_entities = ()
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
44 """List of entities added to the component since the last time step"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
45
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
46 def __init__(self, **fields):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
47 self.fields = {}
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
48 for fname, ftype in fields.items():
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
49 assert ftype in field.types, fname + " has an illegal field type"
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
50 self.fields[fname] = field.Field(self, fname, ftype)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
51 self.entities = ComponentEntitySet(self)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
52 self._added = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
53 self._deleted = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
54
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
55 def set_world(self, world):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
56 self.world = world
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
57
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
58 def step(self, dt):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
59 """Update the component for the next timestep"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
60 delitem = super(Component, self).__delitem__
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
61 for entity in self._deleted:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
62 delitem(entity)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
63 self.new_entities = self._added
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
64 self.deleted_entities = self._deleted
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
65 self._added = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
66 self._deleted = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
67
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
68 def set(self, entity, data=None, **data_kw):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
69 """Set the component data for an entity, adding it to the
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
70 component if it is not already a member.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
71
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
72 If data is specified, its data for the new entity's fields are
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
73 copied from its attributes, making it easy to copy another
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
74 entity's data. Keyword arguments are also matched to fields.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
75 If both a data attribute and keyword argument are supplied for
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
76 a single field, the keyword arg is used.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
77 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
78 if data is not None:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
79 for fname, field in self.fields.items():
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
80 if fname not in data_kw and hasattr(data, fname):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
81 data_kw[fname] = getattr(data, fname)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
82 data = self[entity] = Data(self.fields, entity, **data_kw)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
83 return data
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
84
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
85 def __setitem__(self, entity, data):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
86 assert entity.world is self.world, "Entity not in component's world"
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
87 if entity not in self.entities:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
88 self._added.append(entity)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
89 self.entities.add(entity)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
90 super(Component, self).__setitem__(entity, data)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
91
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
92 def remove(self, entity):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
93 if entity in self.entities:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
94 self._deleted.append(entity)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
95 self.entities.remove(entity)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
96 return True
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
97 return False
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
98
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
99 __delitem__ = remove
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
100
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
101 def __repr__(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
102 return '<%s %x of %r>' % (
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
103 self.__class__.__name__, id(self), getattr(self, 'world', None))
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
104
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
105
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
106 class Singleton(Component):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
107 """Component that may contain only a single entity"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
108
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
109 def add(self, entity_id, data=None, **data_kw):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
110 if entity_id not in self._data:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
111 self.entity_id_set.clear()
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
112 self._data.clear()
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
113 Component.add(self, entity_id, data, **data_kw)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
114
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
115 @property
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
116 def entity(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
117 """Return the entity in the component, or None if empty"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
118 if self._data:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
119 return self.manager[self._data.keys()[0]]
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
120
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
121
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
122 class Data(object):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
123
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
124 def __init__(self, fields, entity, **data):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
125 self.__dict__['_Data__fields'] = fields
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
126 self.__dict__['entity'] = entity
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
127 for field in fields.values():
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
128 if field.name in data:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
129 setattr(self, field.name, data[field.name])
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
130 else:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
131 setattr(self, field.name, field.default())
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
132
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
133 def __setattr__(self, name, value):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
134 if name in self.__fields:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
135 self.__dict__[name] = self.__fields[name].cast(value)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
136 else:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
137 raise AttributeError("Invalid data field: " + name)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
138
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
139 def __repr__(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
140 return '<%s(%r)>' % (self.__class__.__name__, self.__dict__)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
141
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
142