annotate 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
rev   line source
60
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
1 # This file is part of PARPG.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
2
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
4 # it under the terms of the GNU General Public License as published by
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
6 # (at your option) any later version.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
7
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
11 # GNU General Public License for more details.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
12
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
13 # You should have received a copy of the GNU General Public License
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
15
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
16 import sys
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
17 import character
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
18
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
19 ENTITIES = [character.Character]
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
20
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
21 def getAllEntities ():
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
22 """Returns a dictionary with the names of the entity classes
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
23 mapped to the classes themselves"""
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
24 result = {}
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
25 for entity in ENTITIES:
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
26 result[entity.__name__] = entity
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
27 return result
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
28
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
29 def createEntity(info, extra = None):
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
30 """Called when we need to get an actual object.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
31 @type info: dict
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
32 @param info: stores information about the object we want to create
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
33 @type extra: dict
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
34 @param extra: stores additionally required attributes
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
35 @return: the object"""
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
36 # First, we try to get the type and world, which every game_obj needs.
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
37 extra = extra or {}
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
38 try:
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
39 ent_type = info.pop('type')
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
40 world = info.pop('world')
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
41 except KeyError:
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
42 sys.stderr.write("Error: Game object missing type or world.")
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
43 sys.exit(False)
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
44
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
45 # add the extra info
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
46 for key, val in extra.items():
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
47 info[key] = val
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
48
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
49 # this is for testing purposes
d153be6d36cc Added functions to create entities.
KarstenBock@gmx.net
parents: 34
diff changeset
50 return getAllEntities()[ent_type](world, **info)