-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelChanger.py
217 lines (156 loc) · 6.96 KB
/
levelChanger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#======================================================================#
#
# Team:
# Hunter Quant
# Edward Pryor
# Nick marasco
# Shayne Peterson
# Brandon Williams
# Jeremy Rose
#
# Last modification: 11/8/14 Jeremy
#
# Description: Changing levels
# - Currently when the enemy list is empty
#======================================================================#
#Python imports
import sys
#Our class imports
from player import Player
from spawner import Spawner
#Panda3d imports
from panda3d.core import *
from direct.showbase.Transitions import Transitions
from direct.showbase.DirectObject import DirectObject
from direct.interval.IntervalGlobal import Sequence
class LevelChanger(DirectObject):
#Use this to handle changing of levels
#check the emeny list and if it is empty then change the level
#Flag to tell if the level is complete
levelComplete = False
def __init__(self):
#Level map
self.level01 = "resources/theSouthBridge"
self.level02 = "resources/theSocket"
self.level03 = "resources/theDualChannel"
self.level04 = "resources/theRoot"
self.levelMap = {1:self.level01, 2:self.level02, 3:self.level03, 4:self.level04}
self.currentLevel = 1
#Level music map
self.level01M = base.loader.loadMusic("resources/sounds/level1.wav")
self.level02M = base.loader.loadMusic("resources/sounds/level2.wav")
self.level03M = base.loader.loadMusic("resources/sounds/level3.wav")
self.level04M = base.loader.loadMusic("resources/sounds/bossMusic.wav")
self.musicMap = {1:self.level01M, 2:self.level02M, 3:self.level03M, 4:self.level04M}
self.currentMusic = self.musicMap[self.currentLevel]
self.currentMusic.setLoop(True)
self.currentMusic.play()
#Open file to get player spawns
self.pSpawnsFile = open("playerSpawns.txt")
self.pSpawnsList = self.pSpawnsFile.readlines()
self.pSpawnsFile.close()
#Set players current spawn
self.spawnIndex = 0
base.xPos = float(self.pSpawnsList[self.spawnIndex + 1].split("=")[1].translate(None,"\n"))
base.yPos = float(self.pSpawnsList[self.spawnIndex + 2].split("=")[1].translate(None,"\n"))
base.zPos = float(self.pSpawnsList[self.spawnIndex + 3].split("=")[1].translate(None,"\n"))
base.player.playerNode.setPos(0, 0, 30) #resets height
base.player.playerModel.setPos(base.xPos, base.yPos, base.zPos) #resets position
#checks the enemy list
#if the list is empty, level is complete
#set flag to true and change the level.
def checkLevel (self, task):
enemy = base.spawner.spawnId
if(len(base.enemyList) == 0):
if enemy > 0:
self.levelComplete = True
if self.currentLevel == len(self.levelMap):
base.player.hide()
base.player.canUseWeapons = False
base.fsm.request('WinMenu', 1)
return task.done
else:
self.changeLevel(task)
return task.cont
def changeLevel(self, task):
if(self.levelComplete == True):
self.transition = Transitions(loader)
self.transition.setFadeColor(0, 0, 0)
self.fadeOut = self.transition.fadeOut(2)
#unload the current level and models
self.unload(self.levelMap[self.currentLevel])
#load the next level and models
self.load(self.levelMap[self.currentLevel + 1])
#self.fadeIn = self.transition.fadeIn(5)
base.taskMgr.remove(task)
return task.cont
#Unloads current level objects
def unload(self, level):
for i in base.pickuplist:
i.deletePickup = True
for i in base.enemyList:
i.delFlag = True
i.deadFlag = True
#stop the music
self.currentMusic.stop()
#detach playerNode
base.player.playerNode.detachNode()
#Remove enemies
#base.taskMgr.remove("Spawn Enemies")
base.taskMgr.remove("enemyCleanup")
#unload the env and detach remove the node
base.loader.unloadModel(level)
base.environ.removeNode()
#Load the next level
def load(self, level):
#Reset player health for the next level
base.player.adjustHealth(base.player.maxEnergy)
#load Environment - new level
base.environ = base.loader.loadModel(level)
base.environ.reparentTo(base.render)
base.environ.setScale(7, 7, 3)
#update the currentLevel.
self.currentLevel += 1
#reattach player to render
base.player.playerNode.reparentTo(render)
#Set the next levels spawn coordinates
self.spawnIndex += 4
base.xPos = float(self.pSpawnsList[self.spawnIndex + 1].split("=")[1].translate(None,"\n"))
base.yPos = float(self.pSpawnsList[self.spawnIndex + 2].split("=")[1].translate(None,"\n"))
base.zPos = float(self.pSpawnsList[self.spawnIndex + 3].split("=")[1].translate(None,"\n"))
base.player.playerNode.setPos(0,0,30) #resets height
base.player.playerModel.setPos(base.xPos, base.yPos, base.zPos) #resets position
#create new spawner on the env
base.spawner = Spawner(base.environ, level.split("/")[1].translate(None,"\n"))
#Reinit enemies
base.spawner.spawn()
base.taskMgr.add(self.checkLevel, "checkLevel")
base.taskMgr.add(base.enemyCleanUp, "enemyCleanup", taskChain='GameTasks')
self.fadeIn = self.transition.fadeIn(2)
self.currentMusic = self.musicMap[self.currentLevel]
self.currentMusic.setLoop(True)
self.currentMusic.play()
#Returns the current level
def getCurrentLevel(self):
return self.currentLevel
#Goes direcctly to the boss level
def goToBoss(self):
self.transition = Transitions(loader)
self.transition.setFadeColor(0, 0, 0)
self.fadeOut = self.transition.fadeOut(2)
self.unload(self.levelMap[1])
self.load(self.levelMap[4])
self.currentLevel = 4
#Resets enemies upon death
def resetEnemy(self):
base.player.playerModel.setPos(base.xPos, base.yPos, base.zPos)
base.player.playerNode.setPos(0,0,30) #resets height
for i in base.enemyList:
i.delFlag = True
i.deadFlag = True
for i in base.pickuplist:
i.deletePickup = True
#create new spawner on the current level
base.spawner = Spawner(base.environ, self.levelMap[self.currentLevel].split("/")[1].translate(None,"\n"))
#Reinit enemies
base.spawner.spawn()