comparison src/parpg/entities/__init__.py @ 60:d153be6d36cc

Added functions to create entities.
author KarstenBock@gmx.net
date Mon, 05 Sep 2011 14:43:48 +0200
parents 5ac50245e42c
children ad75fa042b99
comparison
equal deleted inserted replaced
59:2915dbb60940 60:d153be6d36cc
1 # This file is part of PARPG.
2
3 # PARPG is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7
8 # PARPG is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
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/>.
15
16 import sys
17 import character
18
19 ENTITIES = [character.Character]
20
21 def getAllEntities ():
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.
31 @type info: dict
32 @param info: stores information about the object we want to create
33 @type extra: dict
34 @param extra: stores additionally required attributes
35 @return: the object"""
36 # First, we try to get the type and world, which every game_obj needs.
37 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
45 # add the extra info
46 for key, val in extra.items():
47 info[key] = val
48
49 # this is for testing purposes
50 return getAllEntities()[ent_type](world, **info)