comparison demos/shooter/scripts/scene.py @ 459:302a69c5141d

Player death is now handled a bit nicer. Added invulnerability. Added 3 more enemy types. Fixed a bug in the collision detection routine that caused some objects not to be updated when a collision is detected.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 09 Apr 2010 17:35:52 +0000
parents e77ebf128a74
children 5e1d6e40d19d
comparison
equal deleted inserted replaced
458:e77ebf128a74 459:302a69c5141d
67 self._timemod = 0 67 self._timemod = 0
68 68
69 self._gameover = False 69 self._gameover = False
70 70
71 def initScene(self, mapobj): 71 def initScene(self, mapobj):
72 #initialize our scene array to some arbitrary size
73 for i in range(0,self._maxnodes):
74 self._nodes.append(SceneNode())
75
72 self._player = Player(self, 'player') 76 self._player = Player(self, 'player')
73 self._player.width = 0.075 77 self._player.width = 0.075
74 self._player.height = 0.075 78 self._player.height = 0.075
75 self._player.init() 79 self._player.init()
76 self._player.start() 80 self._player.start()
77 81
78 enemies = self._layer.getInstances('enemy') 82 enemies = list()
79 83
80 #initialize our scene array to some arbitrary size 84 temp = self._layer.getInstances('dodge1')
81 for i in range(0,self._maxnodes): 85 enemies.extend(temp)
82 self._nodes.append(SceneNode()) 86
83 87 temp = self._layer.getInstances('dodge2')
88 enemies.extend(temp)
89
90 temp = self._layer.getInstances("diag_top_right")
91 enemies.extend(temp)
92
93 temp = self._layer.getInstances("diag_bottom_right")
94 enemies.extend(temp)
95
96 temp = self._layer.getInstances("streaker")
97 enemies.extend(temp)
98
84 for instance in enemies: 99 for instance in enemies:
85 objectName = instance.getObject().getId() 100 objectName = instance.getId()
86 print objectName 101 print objectName
87 102
88 if objectName == "saucer1": 103 if objectName == "dodge1":
89 enemy = Saucer1(self, 'enemy', False) 104 enemy = Saucer1(self, 'enemy', False)
90 elif objectName == "saucer2": 105 elif objectName == "dodge2":
91 enemy = Saucer2(self, 'enemy', False) 106 enemy = Saucer2(self, 'enemy', False)
107 elif objectName == "diag_top_right":
108 enemy = DiagSaucer(self, 'enemy', 0, False)
109 elif objectName == "diag_bottom_right":
110 enemy = DiagSaucer(self, 'enemy', 1, False)
111 elif objectName == "streaker":
112 enemy = Streaker(self, 'enemy', False)
92 else: 113 else:
93 enemy = Ship(self, 'enemy', False) 114 enemy = Ship(self, 'enemy', False)
94 115
95 enemy.instance = instance 116 enemy.instance = instance
96 enemy.start() 117 enemy.start()
109 130
110 def unpause(self, time): 131 def unpause(self, time):
111 self._timemod += time - self._pausedtime 132 self._timemod += time - self._pausedtime
112 self._paused = False 133 self._paused = False
113 134
114 def playerDied(self): 135 def playerHit(self):
115 self._player.destroy() 136 self._player.destroy()
116 if self._player.lives <= -1: 137 if self._player.lives <= -1:
117 self._gameover = True 138 self._gameover = True
118 self._world.gameOver() 139 self._world.gameOver()
119 self.removeAllProjectiles() 140 self.removeAllProjectiles()
120 return 141 return
121 142
122 #TODO: Have to find a better way to do this. If the player 143 self._player.setInvulnerable(2)
123 #dies too many times right at the start of the level he will 144
124 #get pushed past the edge of the map to the left.
125 #IDEA: count down to ready player to start again
126 oldpos = self._player.location
127 pos = oldpos.getExactLayerCoordinates()
128 pos.x -= 5
129 oldpos.setExactLayerCoordinates(pos)
130 self._player.location = oldpos
131 self._camera.setLocation(self._player.location)
132
133 self.removeAllProjectiles()
134 145
135 def removeAllProjectiles(self): 146 def removeAllProjectiles(self):
136 projtodelete = list() 147 projtodelete = list()
137 for p in self._projectiles: 148 for p in self._projectiles:
138 p.destroy() 149 p.destroy()
139 projtodelete.append(p) 150 projtodelete.append(p)
140 151
141 for p in projtodelete: 152 for p in projtodelete:
142 self._projectiles.remove(p) 153 self._projectiles.remove(p)
143
144 def gameOver(self):
145 self._world.gameOver()
146 154
147 def getObjectsInNode(self, nodeindex): 155 def getObjectsInNode(self, nodeindex):
148 return self._nodes[nodeindex].instances 156 return self._nodes[nodeindex].instances
149 157
150 def getObjectsInRange(self, rangeL, rangeR): 158 def getObjectsInRange(self, rangeL, rangeR):
228 obj.fire(fife.DoublePoint(-1,0)) 236 obj.fire(fife.DoublePoint(-1,0))
229 237
230 if obj.boundingbox.intersects(self._player.boundingbox): 238 if obj.boundingbox.intersects(self._player.boundingbox):
231 #player touched an enemy. Destroy player and 239 #player touched an enemy. Destroy player and
232 #re-initialize scene 240 #re-initialize scene
233 self.playerDied() 241 if not self._player.invulnerable:
234 return 242 self.playerHit()
243
235 244
236 245
237 #update the list of projectiles 246 #update the list of projectiles
238 projtodelete = list() 247 projtodelete = list()
239 for p in self._projectiles: 248 for p in self._projectiles:
250 #TODO: the destroy functions should spawn an explosion 259 #TODO: the destroy functions should spawn an explosion
251 #and also destroy the instance and remove itself from the scene 260 #and also destroy the instance and remove itself from the scene
252 self.removeObjectFromScene(o) 261 self.removeObjectFromScene(o)
253 else: 262 else:
254 #player got hit by a projectile 263 #player got hit by a projectile
255 p.destroy() 264 if not self._player.invulnerable:
256 self.playerDied() 265 p.destroy()
257 return 266 self.playerHit()
267
258 268
259 #build a list of projectiles to remove (ttl expired) 269 #build a list of projectiles to remove (ttl expired)
260 if not p.running: 270 if not p.running:
261 projtodelete.append(p) 271 projtodelete.append(p)
262 272