comparison demos/rpg/scripts/scene.py @ 530:ea26e7b6f56c

Added the loadActor and loadItem functions so you can load single items at any time. Cleaned up the base object a bit so you can modify it's position easily.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 28 May 2010 20:10:33 +0000
parents d0bce896a526
children 801746c5bb9a
comparison
equal deleted inserted replaced
529:d0bce896a526 530:ea26e7b6f56c
40 class Scene(object): 40 class Scene(object):
41 def __init__(self, gamecontroller): 41 def __init__(self, gamecontroller):
42 self._gamecontroller = gamecontroller 42 self._gamecontroller = gamecontroller
43 43
44 self._map = None 44 self._map = None
45 self._mapname = None
45 self._maincameraname = self._gamecontroller.settings.get("RPG", "DefaultCameraName", "camera1") 46 self._maincameraname = self._gamecontroller.settings.get("RPG", "DefaultCameraName", "camera1")
46 self._cameras = {} 47 self._cameras = {}
47 48
48 self._actorlayer = None 49 self._actorlayer = None
49 50
50 self._player = None 51 self._player = None
51 self._objectlist = {} 52 self._objectlist = {}
53
54 self._objectsettings = None
55 self._modelsettings = None
56
57 def loadItem(self, itemid):
58 itemdict = self._objectsettings.get("items", itemid, {})
59 modeldict = self._modelsettings.get("models", itemdict["typename"], {})
60
61 loadImportFile(modeldict["file"], self._gamecontroller.engine)
62
63 if itemdict["typename"] == "GoldStack":
64 newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid)
65 newitem.value = itemdict["value"]
66 else:
67 newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid)
68
69 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))
70
71 return newitem
72
73 def loadActor(self, actorid):
74 objdict = self._objectsettings.get("npcs", actorid, {})
75 modeldict = self._modelsettings.get("models", objdict["typename"], {})
76
77 loadImportFile(modeldict["file"], self._gamecontroller.engine)
78
79 if modeldict["type"] == "QUESTGIVER":
80 actor = QuestGiver(self._gamecontroller, modeldict["model"], actorid, True)
81 questcount = self._modelsettings.get(actorid, "questcount", 0)
82 for x in range(1,questcount+1):
83 quest = "quest" + str(x)
84 questdict = self._modelsettings.get(actorid, quest, {})
85 quest = Quest(actor, questdict['name'], questdict['desc'])
86
87 for ritem in questdict['items'].split(" , "):
88 if ritem == "GoldStack":
89 quest.addRequiredGold(int(questdict['value']))
90 else:
91 quest.addRequiredItem(ritem)
92
93 actor.addQuest(quest)
94
95 elif modeldict["type"] == "NPC":
96 actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
97
98 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
99
100 return actor
52 101
53 def loadItems(self, mapfilename): 102 def loadItems(self, mapfilename):
54 """ 103 for item in self._objectsettings.get("items", "itemlist", []):
55 @todo: create a new function "loadItem(self, itemname)" which takes care of 104 newitem = self.loadItem(item)
56 loading an individual item in case at some point in the game you want to
57 load one at some point.
58 """
59 mapname = os.path.splitext(os.path.basename(mapfilename))
60 objectfile = "maps/" + mapname[0] + "_objects.xml"
61 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml")
62
63 objectsettings = Setting(app_name="",settings_file=objectfile)
64 modelsettings = Setting(app_name="", settings_file=modelfile)
65
66 for item in objectsettings.get("items", "itemlist", []):
67 itemdict = objectsettings.get("items", item, {})
68 modeldict = modelsettings.get("models", itemdict["typename"], {})
69
70 loadImportFile(modeldict["file"], self._gamecontroller.engine)
71
72 if itemdict["typename"] == "GoldStack":
73 newitem = GoldStack(self._gamecontroller, modeldict["model"], item)
74 newitem.value = itemdict["value"]
75 else:
76 newitem = BaseItem(self._gamecontroller, modeldict["model"], item)
77
78 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))
79 self._objectlist[newitem.instance.getId()] = newitem 105 self._objectlist[newitem.instance.getId()] = newitem
80 106
81 def loadActors(self, mapfilename): 107 def loadActors(self, mapfilename):
82 """ 108 for npc in self._objectsettings.get("npcs", "npclist", []):
83 @todo: create a new function "loadActor(self, actorname)" which takes care of 109 actor = self.loadActor(npc)
84 loading an individual actor in case at some point in the game you want to
85 load one at some point.
86 """
87 mapname = os.path.splitext(os.path.basename(mapfilename))
88 objectfile = "maps/" + mapname[0] + "_objects.xml"
89 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml")
90
91 objectsettings = Setting(app_name="",settings_file=objectfile)
92 modelsettings = Setting(app_name="", settings_file=modelfile)
93
94 for npc in objectsettings.get("npcs", "npclist", []):
95 objdict = objectsettings.get("npcs", npc, {})
96 modeldict = modelsettings.get("models", objdict["typename"], {})
97
98 loadImportFile(modeldict["file"], self._gamecontroller.engine)
99
100 if modeldict["type"] == "QUESTGIVER":
101 actor = QuestGiver(self._gamecontroller, modeldict["model"], npc, True)
102 questcount = modelsettings.get(npc, "questcount", 0)
103 for x in range(1,questcount+1):
104 quest = "quest" + str(x)
105 questdict = modelsettings.get(npc, quest, {})
106 quest = Quest(actor, questdict['name'], questdict['desc'])
107
108 for ritem in questdict['items'].split(" , "):
109 if ritem == "GoldStack":
110 quest.addRequiredGold(int(questdict['value']))
111 else:
112 quest.addRequiredItem(ritem)
113
114 actor.addQuest(quest)
115
116 elif modeldict["type"] == "NPC":
117 actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
118
119 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
120 self._objectlist[actor.instance.getId()] = actor 110 self._objectlist[actor.instance.getId()] = actor
121 111
122 def loadPlayer(self): 112 def loadPlayer(self):
123 """ 113 """
124 @todo: once we have all art assets this should be able to load one of 3 player models 114 @todo: once we have all art assets this should be able to load one of 3 player models
133 def createScene(self, mapfilename): 123 def createScene(self, mapfilename):
134 if not self._map: 124 if not self._map:
135 self._map = loadMapFile(mapfilename, self._gamecontroller.engine) 125 self._map = loadMapFile(mapfilename, self._gamecontroller.engine)
136 else: 126 else:
137 self.destroyScene() 127 self.destroyScene()
128
129 self._mapname = os.path.splitext(os.path.basename(mapfilename))[0]
130 objectfile = "maps/" + self._mapname + "_objects.xml"
131 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml")
132
133 self._objectsettings = Setting(app_name="",settings_file=objectfile)
134 self._modelsettings = Setting(app_name="", settings_file=modelfile)
138 135
139 for cam in self._map.getCameras(): 136 for cam in self._map.getCameras():
140 self._cameras[cam.getId()] = cam 137 self._cameras[cam.getId()] = cam
141 138
142 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 2.0)) 139 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 2.0))
159 for obj in self._objectlist.values(): 156 for obj in self._objectlist.values():
160 obj.destroy() 157 obj.destroy()
161 158
162 if self._map: 159 if self._map:
163 self._gamecontroller.engine.getModel().deleteMap(self._map) 160 self._gamecontroller.engine.getModel().deleteMap(self._map)
164 161
165 self._map = None 162 self._map = None
163 self._mapname = None
164
166 self._actorlayer = None 165 self._actorlayer = None
166 self._itemlayer = None
167 167
168 self._player = None 168 self._player = None
169 self._objectlist.clear() 169 self._objectlist.clear()
170 170
171 self._objectsettings = None
172 self._modelsettings = None
173
171 def getInstancesAt(self, clickpoint, layer): 174 def getInstancesAt(self, clickpoint, layer):
172 """ 175 """
173 Query the main camera for instances on the specified layer. 176 Query the main camera for instances on the specified layer.
174 """ 177 """
175 return self.cameras[self._maincameraname].getMatchingInstances(clickpoint, layer) 178 return self.cameras[self._maincameraname].getMatchingInstances(clickpoint, layer)