Mercurial > fife-parpg
comparison demos/shooter/scripts/scene.py @ 453:cf53848fb187
Scene now gets updated when an object moves from one node to another.
Player is now part of the scene.
Projectiles can be files by both player and enemies.
Some code cleanup.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Tue, 06 Apr 2010 19:12:41 +0000 |
parents | f07d779362da |
children | bd7e8f708adf |
comparison
equal
deleted
inserted
replaced
452:f07d779362da | 453:cf53848fb187 |
---|---|
48 self._engine = engine | 48 self._engine = engine |
49 self._model = engine.getModel() | 49 self._model = engine.getModel() |
50 self._layer = objectLayer | 50 self._layer = objectLayer |
51 self._nodes = list() | 51 self._nodes = list() |
52 | 52 |
53 self._player = Player(self._model, 'player', self._layer) | 53 self._player = Player(self, 'player') |
54 self._player.width = 0.075 | 54 self._player.width = 0.075 |
55 self._player.height = 0.075 | 55 self._player.height = 0.075 |
56 self._player.start() | 56 self._player.start() |
57 | 57 |
58 self._projectiles = list() | 58 self._projectiles = list() |
59 self._lasttime = 0 | 59 self._lasttime = 0 |
60 | 60 |
61 self._maxnodes = 128 | 61 self._maxnodes = 128 |
62 self._xscale = 0 | |
63 | |
64 self._time = 0 | |
65 self._timedelta = 0 | |
62 | 66 |
63 def initScene(self, mapobj): | 67 def initScene(self, mapobj): |
64 layer = mapobj.getLayer('objects') | 68 self._xscale = self._layer.getCellGrid().getXScale() |
65 xscale = layer.getCellGrid().getXScale() | |
66 | 69 |
67 enemies = layer.getInstances('enemy') | 70 enemies = self._layer.getInstances('enemy') |
68 | 71 |
69 #initialize our scene array to some arbitrary size | 72 #initialize our scene array to some arbitrary size |
70 for i in range(0,self._maxnodes): | 73 for i in range(0,self._maxnodes): |
71 self._nodes.append(SceneNode()) | 74 self._nodes.append(SceneNode()) |
72 | 75 |
73 for instance in enemies: | 76 for instance in enemies: |
74 objectName = instance.getObject().getId() | 77 objectName = instance.getObject().getId() |
75 print objectName | 78 print objectName |
76 | 79 |
77 if objectName == "saucer1": | 80 if objectName == "saucer1": |
78 enemy = Saucer1(self._model, 'enemy', self._layer, False) | 81 enemy = Saucer1(self, 'enemy', False) |
79 elif objectName == "saucer2": | 82 elif objectName == "saucer2": |
80 enemy = Saucer2(self._model, 'enemy', self._layer, False) | 83 enemy = Saucer2(self, 'enemy', False) |
81 else: | 84 else: |
82 enemy = Ship(self._model, 'enemy', self._layer, False) | 85 enemy = Ship(self, 'enemy', False) |
83 | 86 |
84 enemy.instance = instance | 87 enemy.instance = instance |
85 enemy.start() | 88 enemy.start() |
86 | 89 |
87 loc = instance.getLocation().getExactLayerCoordinates() | 90 loc = instance.getLocation().getExactLayerCoordinates() |
88 nodeindex = int(loc.x * xscale) | 91 nodeindex = int(loc.x * self._xscale) |
92 enemy.scenenodeid = nodeindex | |
89 self._nodes[nodeindex].spaceobjects.append(enemy) | 93 self._nodes[nodeindex].spaceobjects.append(enemy) |
94 | |
95 #and finally add the player to the scene | |
96 self.addObjectToScene(self._player) | |
90 | 97 |
91 def getObjectsInNode(self, nodeindex): | 98 def getObjectsInNode(self, nodeindex): |
92 return self._nodes[nodeindex].instances | 99 return self._nodes[nodeindex].instances |
93 | 100 |
94 def getObjectsInRange(self, rangeL, rangeR): | 101 def getObjectsInRange(self, rangeL, rangeR): |
96 | 103 |
97 for i in range(rangeL, rangeR): | 104 for i in range(rangeL, rangeR): |
98 objects.extend(self._nodes[i].spaceobjects) | 105 objects.extend(self._nodes[i].spaceobjects) |
99 | 106 |
100 return objects | 107 return objects |
108 | |
109 def addObjectToScene(self, obj): | |
110 #TODO: search to ensure the object isn't already part of the scene | |
111 loc = obj.instance.getLocation().getExactLayerCoordinates() | |
112 nodeindex = int(loc.x * self._xscale) | |
113 | |
114 self._nodes[nodeindex].spaceobjects.append(obj) | |
115 obj.scenenodeid = nodeindex | |
116 | |
117 def addProjectileToScene(self, projectile): | |
118 self._projectiles.append(projectile) | |
119 | |
120 def moveObjectInScene(self, obj): | |
121 loc = obj.instance.getLocation().getExactLayerCoordinates() | |
122 nodeindex = int(loc.x * self._xscale) | |
123 | |
124 if nodeindex != obj.scenenodeid: | |
125 self._nodes[obj.scenenodeid].spaceobjects.remove(obj) | |
126 self._nodes[nodeindex].spaceobjects.append(obj) | |
127 obj.scenenodeid = nodeindex | |
101 | 128 |
102 def removeObjectFromScene(self, obj): | 129 def removeObjectFromScene(self, obj): |
103 for node in self._nodes: | 130 for node in self._nodes: |
104 if obj in node.spaceobjects: | 131 if obj in node.spaceobjects: |
105 node.spaceobjects.remove(obj) | 132 node.spaceobjects.remove(obj) |
107 | 134 |
108 def attachCamera(self, cam): | 135 def attachCamera(self, cam): |
109 self._camera = cam | 136 self._camera = cam |
110 self._camera.setLocation(self._player.location) | 137 self._camera.setLocation(self._player.location) |
111 | 138 |
112 def _getPlayer(self): | |
113 return self._player | |
114 | |
115 def update(self, time, keystate): | 139 def update(self, time, keystate): |
116 timedelta = time - self._lasttime | 140 timedelta = time - self._time |
117 self._lasttime = time | 141 self._timedelta = timedelta |
142 self._time = time | |
143 | |
144 self._keystate = keystate | |
118 | 145 |
119 #update camera location | 146 #update camera location |
120 loc = self._camera.getLocation() | 147 loc = self._camera.getLocation() |
121 exactloc = self._camera.getLocation().getExactLayerCoordinates() | 148 exactloc = self._camera.getLocation().getExactLayerCoordinates() |
149 #slowly move to the right | |
122 exactloc.x += timedelta * 0.001 | 150 exactloc.x += timedelta * 0.001 |
123 loc.setExactLayerCoordinates(exactloc) | 151 loc.setExactLayerCoordinates(exactloc) |
124 self._camera.setLocation(loc) | 152 self._camera.setLocation(loc) |
125 | 153 |
126 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0)) | 154 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0)) |
135 leftnode = 0 | 163 leftnode = 0 |
136 if rightnode > self._maxnodes: | 164 if rightnode > self._maxnodes: |
137 rightnode = self._maxnodes | 165 rightnode = self._maxnodes |
138 screenlist = self.getObjectsInRange(leftnode, rightnode) | 166 screenlist = self.getObjectsInRange(leftnode, rightnode) |
139 | 167 |
168 | |
140 #update objects on the screen | 169 #update objects on the screen |
141 for obj in screenlist: | 170 for obj in screenlist: |
142 obj.update(timedelta) | 171 obj.update(timedelta) |
143 | 172 if obj.changedposition: |
144 #update the player | 173 self.moveObjectInScene(obj) |
145 self._player.update(timedelta, keystate, self._camera) | 174 |
175 #Testing enemy fire | |
176 #prjct = obj.fire(time, fife.DoublePoint(-1,0)) | |
177 #if prjct: | |
178 # self._projectiles.append(prjct) | |
179 | |
180 if obj != self._player: | |
181 if obj.boundingbox.intersects(self._player.boundingbox): | |
182 #player touched an enemy. Destroy player and | |
183 #re-initialize scene | |
184 self._player.destroy() | |
185 | |
146 | 186 |
147 #update the list of projectiles | 187 #update the list of projectiles |
148 projtodelete = list() | 188 projtodelete = list() |
149 for p in self._projectiles: | 189 for p in self._projectiles: |
150 p.update(timedelta) | 190 p.update(timedelta) |
151 #check to see if the projectile hit any object on the screen | 191 #check to see if the projectile hit any object on the screen |
152 for o in screenlist: | 192 for o in screenlist: |
153 if p.boundingbox.intersects(o.boundingbox): | 193 #cant get hit by your own bullet |
154 self._player.applyScore(100) | 194 if p.owner != o: |
155 p.destroy() | 195 if p.boundingbox.intersects(o.boundingbox): |
156 o.destroy() | 196 self._player.applyScore(100) |
157 self.removeObjectFromScene(o) | 197 p.destroy() |
198 o.destroy() | |
199 #temporary... the destroy functions should spawn an explosion | |
200 #and also destroy the instance and remove itself from the scene | |
201 self.removeObjectFromScene(o) | |
158 | 202 |
159 #build a list of projectiles to remove (ttl expired) | 203 #build a list of projectiles to remove (ttl expired) |
160 if not p.running: | 204 if not p.running: |
161 projtodelete.append(p) | 205 projtodelete.append(p) |
162 | 206 |
163 #remove any non running projectiles | 207 #remove any expired projectiles |
164 for p in projtodelete: | 208 for p in projtodelete: |
165 self._projectiles.remove(p) | 209 self._projectiles.remove(p) |
166 | 210 |
167 #fire the currently selected gun | 211 |
168 if keystate['SPACE']: | 212 def _getPlayer(self): |
169 prjct = self._player.fire(time) | 213 return self._player |
170 if prjct: | 214 |
171 self._projectiles.append(prjct) | 215 def _getKeyState(self): |
216 return self._keystate | |
217 | |
218 def _getCamera(self): | |
219 return self._camera | |
220 | |
221 def _getObjectLayer(self): | |
222 return self._layer | |
223 | |
224 def _getModel(self): | |
225 return self._model | |
226 | |
227 def _getTime(self): | |
228 return self._time | |
229 | |
230 def _getTimeDelta(self): | |
231 return self._timedelta | |
172 | 232 |
173 player = property(_getPlayer) | 233 player = property(_getPlayer) |
174 | 234 keystate = property(_getKeyState) |
235 camera = property(_getCamera) | |
236 objectlayer = property(_getObjectLayer) | |
237 model = property(_getModel) | |
238 time = property(_getTime) | |
239 timedelta = property(_getTimeDelta) | |
240 |