comparison demos/rpg/scripts/scene.py @ 528:796d49ab9380

Cleaned up the createScene function. Items can now be picked up.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 28 May 2010 14:52:56 +0000
parents 19db5a8619a4
children d0bce896a526
comparison
equal deleted inserted replaced
527:79d5741fada6 528:796d49ab9380
26 26
27 import sys, os, re, math, random, shutil 27 import sys, os, re, math, random, shutil
28 28
29 from fife import fife 29 from fife import fife
30 from fife.extensions.loaders import loadMapFile 30 from fife.extensions.loaders import loadMapFile
31 from fife.extensions.loaders import loadImportFile
31 from fife.extensions.fife_settings import Setting 32 from fife.extensions.fife_settings import Setting
32 33
33 from scripts.actors.baseactor import Actor 34 from scripts.actors.baseactor import Actor
34 from scripts.actors.baseactor import QuestGiver, Quest 35 from scripts.actors.baseactor import QuestGiver, Quest
35 from scripts.actors.player import Player 36 from scripts.actors.player import Player
47 self._actorlayer = None 48 self._actorlayer = None
48 49
49 self._player = None 50 self._player = None
50 self._objectlist = {} 51 self._objectlist = {}
51 52
53 def loadItems(self, mapfilename):
54 """
55 @todo: create a new function "loadItem(self, itemname)" which takes care of
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
80
81 def loadActors(self, mapfilename):
82 """
83 @todo: create a new function "loadActor(self, actorname)" which takes care of
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 (qname, qtext) = modelsettings.get(npc, quest, [])
106 actor.addQuest(Quest(actor, qname, qtext))
107
108 elif modeldict["type"] == "NPC":
109 actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
110
111 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
112 self._objectlist[actor.instance.getId()] = actor
113
114 def loadPlayer(self):
115 """
116 @todo: once we have all art assets this should be able to load one of 3 player models
117 """
118 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml")
119 modelsettings = Setting(app_name="", settings_file=modelfile)
120 modeldict = modelsettings.get("models", "Player", {})
121
122 loadImportFile(modeldict["file"], self._gamecontroller.engine)
123 self._player = Player(self._gamecontroller, "warrior")
124
52 def createScene(self, mapfilename): 125 def createScene(self, mapfilename):
53 if not self._map: 126 if not self._map:
54 self._map = loadMapFile(mapfilename, self._gamecontroller.engine) 127 self._map = loadMapFile(mapfilename, self._gamecontroller.engine)
55 else: 128 else:
56 self.destroyScene() 129 self.destroyScene()
61 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 2.0)) 134 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 2.0))
62 135
63 self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer")) 136 self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer"))
64 self._itemlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ItemLayer", "item_layer")) 137 self._itemlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ItemLayer", "item_layer"))
65 138
66 self._player = Player(self._gamecontroller, "warrior") 139 self.loadItems(mapfilename)
67 140 self.loadActors(mapfilename)
68 mapname = os.path.splitext(os.path.basename(mapfilename)) 141
69 objectfile = "maps/" + mapname[0] + "_objects.xml" 142 #finally load the player
70 itemfile = "maps/allobjects.xml" 143 self.loadPlayer()
71 objectsettings = Setting(app_name="",settings_file=objectfile) 144
72 itemsettings = Setting(app_name="", settings_file=itemfile)
73
74 for item in objectsettings.get("items", "itemlist", []):
75 itemdict = objectsettings.get("items", item, {})
76 modeldict = itemsettings.get("models", itemdict["typename"])
77
78 if itemdict["typename"] == "GoldStack":
79 newitem = GoldStack(self._gamecontroller, modeldict["model"], item)
80 #newitem.value = itemdict["value"]
81 print itemdict["value"]
82 else:
83 newitem = BaseItem(self._gamecontroller, modeldict["model"], item)
84
85 self._objectlist[newitem.instance.getId()] = newitem
86
87 for npc in objectsettings.get("npcs", "npclist", []):
88 objdict = objectsettings.get("npcs", npc, {})
89 modeldict = itemsettings.get("models", objdict["typename"])
90
91 if modeldict["type"] == "QUESTGIVER":
92 actor = QuestGiver(self._gamecontroller, modeldict["model"], npc, True)
93 questcount = itemsettings.get(npc, "questcount", 0)
94 for x in range(1,questcount+1):
95 quest = "quest" + str(x)
96 (qname, qtext) = itemsettings.get(npc, quest, [])
97 actor.addQuest(Quest(actor, qname, qtext))
98
99 elif modeldict["type"] == "NPC":
100 actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
101
102 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
103 self._objectlist[actor.instance.getId()] = actor
104
105 145
106 def destroyScene(self): 146 def destroyScene(self):
107 self._cameras.clear() 147 self._cameras.clear()
108 148
109 self._player.destroy() 149 self._player.destroy()
118 self._actorlayer = None 158 self._actorlayer = None
119 159
120 self._player = None 160 self._player = None
121 self._objectlist.clear() 161 self._objectlist.clear()
122 162
123 def getInstancesAt(self, clickpoint): 163 def getInstancesAt(self, clickpoint, layer):
124 """ 164 """
125 Query the main camera for instances on the actor layer. 165 Query the main camera for instances on the specified layer.
126 """ 166 """
127 return self.cameras[self._maincameraname].getMatchingInstances(clickpoint, self._actorlayer) 167 return self.cameras[self._maincameraname].getMatchingInstances(clickpoint, layer)
128 168
129 def getLocationAt(self, clickpoint): 169 def getLocationAt(self, clickpoint):
130 """ 170 """
131 Query the main camera for the Map location (on the actor layer) 171 Query the main camera for the Map location (on the actor layer)
132 that a screen point refers to. 172 that a screen point refers to.