annotate src/parpg/bGrease/component/general.py @ 80:84d03dc70904

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