-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
364 lines (306 loc) · 10.9 KB
/
menu.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import ode
from OpenGL.GL import *
from pyglfw.glfw import *
import objects
from glfont.glfont import GLFont
import vectormath
import sys
import gameglobals
from camera import Camera
from sound import sounds
import gameinfo
CLOCK = glfwGetTime
def near_callback(args, geom1, geom2):
"""Callback function for the collide() method.
This function checks if the given geoms do collide and
creates contact joints if they do.
"""
# Check if the objects do collide
friction = 100.0
bounce = 1.0
contacts = ode.collide(geom1, geom2)
geoms = [geom1, geom2]
world, contactgroup = args
# Create contact joints
for c in contacts:
c.setBounce(bounce)
c.setMu(friction)#ode.Infinity)
j = ode.ContactJoint(world, contactgroup, c)
j.attach(geom1.getBody(), geom2.getBody())
#meant to determine current menu
class EntryIndex:
def __init__(self):
self.value = 0
self.previousValue = 0
def set(self, val):
self.previousValue = self.value
self.value = val
def setPrevious(self):
temp = self.previousValue
self.previousValue = self.value
self.value = temp
def get(self):
return self.value
class Entries(list):
def __init__(self):
self.coords = (0, 0)
self.baseColor = (0,0,1)
self.selectedColor = (1,0,0)
W, H = glfwGetWindowSize()
self.entryFontScale = gameglobals.fontScale * 1.5
self.entryLineSpacing = 3 * self.entryFontScale
self.cursorPos = (W/2, H/2)
self.cursorAngle = 0.0
self.font_aa = GLFont(r"DATA\\FNT\\bold_aa.glf")
self.defaultColor = (0.0, 1.0, 1.0)
self.textureCursor = objects.loadTexture("DATA\\TEX\\cursor.png")
def addEntry(self, string, state):
#[text, active = False, state]
self.append([string, False, state])
def setSelectionIndex(self, selection, entryIndex):
if selection == 'Start Game':
entryIndex.set(1) #set the menu index to ingame menu
if selection == 'Restart Game':
entryIndex.set(1) #set the menu index to ingame menu
if selection == 'Exit to Main Menu':
entryIndex.set(0) #set the menu index to main menu
if selection == 'Rules of the game':
entryIndex.set(2) #set the menu index to Rules of the game menu
def applyInputs(self, entryIndex):
W, H = glfwGetWindowSize()
mx, my = glfwGetMousePos()
my = -my
if (mx, my + H) != self.cursorPos:
if mx <= 0: mx = 0
if mx >= W: mx = W
if my >= 0: my = 0
if my <= -H: my = -H
self.cursorPos = (mx, my + H)
glfwSetMousePos(mx, -my)
#if mousebutton1 is pressed, enter selected menu
W, H = glfwGetWindowSize()
menuY = H / 2 + 50 * self.entryFontScale
for i in range(len(self)):
pos = i + 1
string = self[i][0]
#vertical positioning will work fine only with one height for all entries
x0 = W / 2 - self.font_aa.getSize(string)[0] * self.entryFontScale / 2
y0 = menuY - pos * self.font_aa.getSize(string)[1] * self.entryFontScale #+ self.entryLineSpacing
x1 = x0 + self.font_aa.getSize(string)[0] * self.entryFontScale
y1 = y0 + self.entryLineSpacing - self.font_aa.getSize(string)[1] * self.entryFontScale
if vectormath.isPointInSquare((self.cursorPos[0], 0, self.cursorPos[1]), (x0, y1), (x1, y0)):
self[i][1] = True
if glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_PRESS\
and glfwGetMouseButton(GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE:
sounds.playSound("Menu", (0,0,0))
selection = self[i][0]
gameglobals.gameState = self[i][2]
self.setSelectionIndex(selection, entryIndex)
def drawCursor(self):
x, y = self.cursorPos
self.font_aa.begin()
glBindTexture(GL_TEXTURE_2D, self.textureCursor)
glTranslatef(x, y, 0.0)
glRotatef(self.cursorAngle, 0.0, 0.0, 1.0)
self.cursorAngle += 0.5
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
L = 10 * gameglobals.fontScale
glTexCoord2f(0.0, 0.0); glVertex2i(-L, -L)
glTexCoord2f(1.0, 0.0); glVertex2i( L, -L)
glTexCoord2f(1.0, 1.0); glVertex2i( L, L)
glTexCoord2f(0.0, 1.0); glVertex2i(-L, L)
glEnd()
self.font_aa.end()
def drawDeveloper(self):
W = glfwGetWindowSize()[0]
glColor4f(1.0, 1.0, 1.0, 0.9)
self.font_aa.begin()
dScale = gameglobals.fontScale / 2
x = W - (self.font_aa.getSize(gameinfo.developer)[0] + 5) * dScale
y = (self.font_aa.getSize(gameinfo.developer)[1] + 5) * dScale
self.font_aa.renderOutlined(gameinfo.developer, x, y, (0,0,0), dScale)
self.font_aa.end()
def drawTitle(self):
W, H = glfwGetWindowSize()
title = ["3D game", "Squares"]
glColor4f(0.0, 0.0, 1.0, 0.9)
self.font_aa.begin()
#unscaled font size is 14, i believe
titleScale = gameglobals.fontScale * 2
y = H - H / 8
for row in title:
x = W / 2 - self.font_aa.getSize(row)[0] * titleScale / 2
self.font_aa.renderOutlined(row, x, y, (0,0,0), titleScale)
y -= self.font_aa.getSize(row)[1] * titleScale / 2
titleScale = gameglobals.fontScale * 4
self.font_aa.end()
def draw(self):
W, H = glfwGetWindowSize()
menuY = H / 2 + 50 * self.entryFontScale
for i in range(len(self)):
pos = i + 1
string = self[i][0]
#vertical positioning will work fine only with one height for all entries
x = W / 2 - self.font_aa.getSize(string)[0] * self.entryFontScale / 2
y = menuY - pos * self.font_aa.getSize(string)[1] * self.entryFontScale + self.entryLineSpacing
if self[i][1] == True: #if active
color = [1,0,0]
else:
color = self.defaultColor
glColor3f(color[0], color[1], color[2])
self.font_aa.begin()
self.font_aa.render(string, x, y, self.entryFontScale)
self.font_aa.end()
self[i][1] = False
self.resetColors()
#render cursor
self.drawDeveloper()
def resetColors(self):
for entry in self:
entry[1] = self.defaultColor
def renderLongText(font, text, scale, x, y):
height = (font.getSize("Height")[1] / 2 + 5) * scale
font.begin()
for line in text.split('\n'):
font.renderOutlined(line, x, y, (0, 0, 0), scale)
y -= height
font.end()
class MainEntries(Entries):
#this is main menu
def __init__(self):
Entries.__init__(self)
#format:
#string, according state
#menu = 0
#start game anew = 1
#resume game = 2
#quit game = 3
self.addEntry('Start Game', 2)
self.addEntry('Rules of the game', 0)
self.addEntry('About', 0)
self.addEntry('Exit Game', 3)
def setSelectionIndex(self, selection, entryIndex):
if selection == 'Start Game':
entryIndex.set(1) #set the menu index to ingame menu
if selection == 'Rules of the game':
entryIndex.set(2) #set the menu index to Rules of the game menu
if selection == 'About':
entryIndex.set(3) #set the menu index to About menu
def draw(self):
Entries.draw(self)
#render game title
self.drawTitle()
class InGameEntries(Entries):
#this is ingame menu
def __init__(self):
Entries.__init__(self)
self.addEntry('Resume', 1)
self.addEntry('Restart Game', 2)
self.addEntry('Rules of the game', 0)
self.addEntry('Exit to Main Menu', 0)
def setSelectionIndex(self, selection, entryIndex):
if selection == 'Restart Game':
entryIndex.set(1) #set the menu index to ingame menu
if selection == 'Exit to Main Menu':
entryIndex.set(0) #set the menu index to main menu
if selection == 'Rules of the game':
entryIndex.set(2) #set the menu index to Rules of the game menu
def draw(self):
Entries.draw(self)
#render game title
self.drawTitle()
class RulesEntries(Entries):
#this is ingame menu
def __init__(self):
Entries.__init__(self)
for i in range(0, 4): self.addEntry(' ', 0) #stuff the dummies to push down "Back"
self.addEntry('Back', 0)
def setSelectionIndex(self, selection, entryIndex):
if selection == 'Back':
entryIndex.setPrevious() #set the menu index to previous menu
def draw(self):
Entries.draw(self)
#draw rules text
H = glfwGetWindowSize()[1]
fontScale = gameglobals.fontScale / 1.35
x = 5 * fontScale
y = H - x
glColor3f(1,1,1)
renderLongText(self.font_aa, gameinfo.rules, fontScale, x, y)
class AboutEntries(RulesEntries):
def draw(self):
Entries.draw(self)
#draw about info
H = glfwGetWindowSize()[1]
fontScale = gameglobals.fontScale / 1.35
glColor3f(1,1,1)
sep = "\n \n \n"
bigText = gameinfo.info + sep + gameinfo.controls + sep + gameinfo.about + sep + gameinfo.credits
fontHeight = self.font_aa.getSize("Height")[1] * fontScale
x = 5 * fontScale
y = H - x
renderLongText(self.font_aa, bigText, fontScale, x, y)
#draw version
self.font_aa.begin()
self.font_aa.renderOutlined(gameinfo.version, 5 * fontScale, fontHeight, (0,0,0), fontScale)
self.font_aa.end()
def drawDeveloper(self):
W = glfwGetWindowSize()[0]
glColor4f(1.0, 1.0, 1.0, 0.9)
self.font_aa.begin()
dScale = gameglobals.fontScale / 2
x = W - (self.font_aa.getSize(gameinfo.developer)[0] + 5) * dScale
y = (self.font_aa.getSize(gameinfo.developer)[1] + 5) * dScale
self.font_aa.renderOutlined(gameinfo.developer, x, y, (0,0,0), dScale)
self.font_aa.end()
class GameMenu(ode.World):
def __init__(self):
self.objectList = []
self.performance = 0
ode.World.__init__(self)
self.setGravity((0.0, -9.81/2, 0.0))
self.space = ode.Space()
self.contactgroup = ode.JointGroup()
self.time0 = 0
#here are menus: mainmenu, ingame
self.entryList = [MainEntries(), InGameEntries(), RulesEntries(), AboutEntries()]
self.entryIndex = EntryIndex()
self.font_aa = GLFont(r"DATA\\FNT\\bold_aa.glf")
self.camera = Camera(0.0, 24.0, 0.0, -30.0, 0.0)
def launchClock(self):
self.time0 = CLOCK()
def applyInputs(self):
glfwEnable(GLFW_STICKY_MOUSE_BUTTONS)
#apply menu inputs
self.entryList[self.entryIndex.get()].applyInputs(self.entryIndex)
def draw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
self.camera.draw()
glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 10.0, 0.0, 1.0))
for part in self.objectList:
glPushMatrix()
part.draw()
glPopMatrix()
#draw active menu
self.entryList[self.entryIndex.get()].draw()
self.entryList[self.entryIndex.get()].drawCursor()
def addObject(self, object):
self.objectList.append(object)
def idle(self):
gameMenu = self
newTime = CLOCK()
deltaTime = newTime - gameMenu.time0
if deltaTime >= 1.0 / 60: #maxfps = 60
gameMenu.performance = deltaTime
gameMenu.applyInputs()
gameMenu.space.collide((gameMenu, gameMenu.contactgroup), near_callback)
if deltaTime < 0.015: DT = 0.015
else: DT = deltaTime
gameMenu.step(DT)
gameMenu.contactgroup.empty()
gameMenu.draw()
glfwSwapBuffers()
gameMenu.time0 = newTime