annotate bGrease/component/general.py @ 146:abfeac863dd6

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