/
main.py
230 lines (190 loc) · 6.59 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
# Panda3d import
from panda3d.core import *
from panda3d.ai import *
from panda3d.physics import *
from panda3d.core import WindowProperties
from direct.filter.CommonFilters import CommonFilters
from direct.actor.Actor import Actor
from direct.showbase.ShowBase import ShowBase
# Util import
import os
import sys
import getpass
from datetime import datetime as date
import ctypes
# Class imports
from gui.menu import Menu
from gui.hud import Hud
from gui.gameover import Gameover
from gui.classement import Classement
from database import Database
from cuboid import Cuboid
from spawn import Spawn
from terrain import Terrain
from collision import Collision, distance
from entity import Entity
from skybox import Skybox
from monster import Monster
from slime import Slime
class MyApp(ShowBase):
def __init__(self):
self.debug = False
ShowBase.__init__(self)
self.accept("escape", sys.exit)
# the dt should depend on the framerate
self.dt = 0.25
# Make ranking
self.database = Database()
self.ranking = self.database.getRankingFromDatabase()
self.classement = Classement(self.ranking)
self.classement.hideMenu()
self.musicManager.setConcurrentSoundLimit(2)
# initiate game state
self.state = 'Menu'
self.terrain = Terrain(1024)
self.menu = Menu()
self.loadStartMenu()
if (self.debug == False):
self.disableMouse()
def loadStartMenu(self):
self.accept("Menu-Start-Parkour", self.loadGame)
self.accept("Menu-Start-World", self.loadGame)
self.accept("Menu-Start-Ranking", self.loadRankingMenu)
self.menu.showStartMenu()
self.show_cursor()
def exitStartMenu(self):
self.ignore("Menu-Start-Ranking")
self.ignore("Menu-Start-World")
self.ignore("Menu-Start-Parkour")
self.menu.hideStartMenu()
def loadGameOverMenu(self, score):
self.gameover = Gameover(score)
taskMgr.doMethodLater(5, self.restartGame, 'timer')
self.show_cursor()
def loadRankingMenu(self):
self.accept("Menu-Ranking-Return", self.exitRankingMenu)
self.exitStartMenu()
self.classement.showMenu()
self.show_cursor()
def exitRankingMenu(self):
print("hidding ranking menu")
self.ignore("Menu-Ranking-Return")
self.classement.hideMenu()
self.loadStartMenu()
def loadGame(self):
self.exitStartMenu()
print("Loading game")
self.state = 'Loading'
if (self.debug == False):
self.hide_cursor
self.setLights()
self.terrain.load()
self.hud = Hud()
self.hud.show()
self.loadEntities()
# positionate the camera
if (self.debug == False):
self.camera.lookAt(self.slime.model)
# Load Skybox
Skybox(self.render)
# register events
self.ydelta = 300
self.zdelta = 60
self.accept("wheel_up", self.camzoom, [True])
self.accept("wheel_down", self.camzoom, [False])
# register tasks
self.task_mgr.add(self.mainLoop, "MainTask")
if (self.debug == False):
self.task_mgr.add(self.updateCamera, "CameraTask")
self.startGame()
def startGame(self):
print("Starting game")
# Load music
self.music = base.loader.loadMusic(
"assets/sounds/hytale-ost-kweebec-village.mp3")
self.music.play()
self.music.setVolume(0.1)
self.setFrameRateMeter(True)
self.state = 'Game'
# init console
"""
self.userConsole = pc.Console()
commands = {"restart":self.__init__,
"teleport": self.slime.teleport,
"color": self.slime.setColor,
"stop": self.endGame
}
self.userConsole.create(commands,app=self)
"""
def endGame(self):
self.state = 'Finished'
print("SCORE : "+str(Monster.score))
self.music.stop()
self.loadGameOverMenu(Monster.score)
today = date.today()
name = getpass.getuser()
try:
self.database.insertValues(name.capitalize(),Monster.score,today.strftime("%d/%m/%Y"))
except:
pass
def restartGame(self, task):
os.execl(sys.executable, sys.executable, *sys.argv)
def loadEntities(self):
startingPoint = (100, 0, 10)
self.AIworld = AIWorld(render)
self.collision = Collision(Monster.monster)
# terrain, initialPos, slimeModelPath, floorPos, scale, lifePoint, volumicMass, movingSpeed, dt
self.slime = Slime(self.terrain, startingPoint, "assets/models/slime.egg",
10, 10, 100, 0.01, 5, self.dt, "slime", self.collision)
self.spawn = Spawn([self.slime]+Monster.monster,
self.terrain, self.AIworld, self.collision)
self.spawn.spawn()
def setLights(self):
sun = DirectionalLight("sun")
sun.setColor((1, 1, 0.9, 1))
sun.setScene(render)
self.sunNp = render.attachNewNode(sun)
self.sunNp.setPos(-10, -10, 30)
self.sunNp.lookAt(0, 0, 0)
render.setLight(self.sunNp)
alight = AmbientLight('alight')
alight.setColor((0.35, 0.35, 0.35, 1))
alnp = render.attachNewNode(alight)
render.setLight(alnp)
def mainLoop(self, task):
if (self.state == "Finished"):
return
if (self.slime.lifePoint <= 0 or self.slime.scale >= 1000):
self.endGame()
self.AIworld.update()
self.spawn.spawn()
for e in [self.slime]+Monster.monster:
e.update()
self.hud.setLifeBarValue(self.slime.lifePoint)
self.hud.setScore(Monster.score)
return task.cont
def camzoom(self, decrease):
if (decrease):
self.ydelta -= 5
self.zdelta -= 1
else:
self.ydelta += 5
self.zdelta += 1
def updateCamera(self, task):
if (self.state != 'Game'):
return
self.cam.setPos(self.slime.pos.getX(), self.slime.pos.getY(
)-self.ydelta, self.slime.pos.getZ()+self.zdelta)
self.cam.lookAt(self.slime.model)
return task.cont
def hide_cursor(self):
props = WindowProperties()
props.setCursorHidden(True)
self.win.requestProperties(props)
def show_cursor(self):
"""set the Cursor visible again"""
props = WindowProperties()
props.setCursorHidden(False)
self.win.requestProperties(props)
App = MyApp()
App.run()