comparison entities/__init__.py @ 61:2727d6b78978

Implemented creation of dynamic entities.
author KarstenBock@gmx.net
date Sun, 18 Sep 2011 16:26:12 +0200
parents 98f26f7636d8
children a9cc5559ec2a
comparison
equal deleted inserted replaced
60:e2004c9549d7 61:2727d6b78978
12 12
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import sys 16 import sys
17 import character
18 17
19 ENTITIES = [character.Character] 18 from parpg.bGrease import Entity
20 19
21 def getAllEntities (): 20 def createEntity(info, world, extra = None):
22 """Returns a dictionary with the names of the entity classes
23 mapped to the classes themselves"""
24 result = {}
25 for entity in ENTITIES:
26 result[entity.__name__] = entity
27 return result
28
29 def createEntity(info, extra = None):
30 """Called when we need to get an actual object. 21 """Called when we need to get an actual object.
31 @type info: dict 22 @type info: dict
32 @param info: stores information about the object we want to create 23 @param info: stores information about the object we want to create
33 @type extra: dict 24 @type extra: dict
34 @param extra: stores additionally required attributes 25 @param extra: stores additionally required attributes
35 @return: the object""" 26 @return: the object"""
36 # First, we try to get the type and world, which every game_obj needs. 27 # First, we try to get the world, which every game_obj needs.
37 extra = extra or {} 28 extra = extra or {}
38 try:
39 ent_type = info.pop('type')
40 world = info.pop('world')
41 except KeyError:
42 sys.stderr.write("Error: Game object missing type or world.")
43 sys.exit(False)
44 29
45 # add the extra info 30 # add the extra info
46 for key, val in extra.items(): 31 for key, val in extra.items():
47 info[key] = val 32 info[key].update(val)
48 33
49 # this is for testing purposes 34 # this is for testing purposes
50 try: 35 new_ent = Entity(world)
51 return getAllEntities()[ent_type](world, **info) 36 for component, data in info.items():
52 except KeyError: 37 comp_obj = getattr(new_ent, component)
53 return None 38 for key, value in data.items():
39 setattr(comp_obj, key, value)
40 return new_ent