-
Notifications
You must be signed in to change notification settings - Fork 0
/
coach.py
468 lines (385 loc) · 16.1 KB
/
coach.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
from random import randint
from glfont.glfont import GLFont
from pyglfw.glfw import glfwGetWindowSize
from OpenGL.GL import *
from OpenGL.GLU import gluProject
from sound import sounds
import vectormath
import gameglobals
class ScoreBoard(dict):
def __init__(self, players):
self.players = players
self.joinedCombo = 0
def getTotalPoints(self):
return self.joinedCombo
def addTotalPoints(self, playerName):
pts = self.getTotalPoints()
self[playerName][0] += pts #update player score account
self.resetJoinedCombo()
return pts
def addSelfPoints(self, playerName):
playerAccount = self[playerName]
pts = playerAccount[1]
playerAccount[0] += pts #update player score account by earned pts
self.resetJoinedCombo()
return pts
def incrementCombo(self, player, hitMsgList):
playerName = player.name
self[playerName][1] += 1 #update own combo
self.joinedCombo += 1 #update joined combo
#renew floating coords
hitMsgList.append([self[playerName][1],
1.0,
player.getPosition(),
self.players[playerName].color])
def addPoint(self, playerName):
self[playerName][0] += 1
return 1
def resetJoinedCombo(self):
for val in self.values(): #reset player combos
val[1] = 0 #null them
self.joinedCombo = 0 #reset joined as well
def reset(self):
for playerName in self.players.keys():
# (player`s total score (minuses), current combo points
self[playerName] = [0, 0]
self.joinedCombo = 0
def resetOwnCombo(self, playerName):
self[playerName][1] = 0
class Coach:
def __init__(self):
self.state = 1
self.afterCollideTriggerBox = False
self.beforeCollideTriggerBox = False
self.initEvents()
self.matchPoints = 21
self.ball = None
self.gameOver = False
#faults list will contain msg text, points, and delta up px for text
self.faultMsgList = []
#hits list will contain combo, delta up px and coords of the collision (X HITS!)
self.hitMsgList = []
self.taunts = []
self.players = {}
self.scoreBoard = ScoreBoard(self.players)
self.objectList = []
self.font_aa = GLFont(r"DATA\\FNT\\bold_aa.glf")
def addTaunt(self, player, msg):
pos = [player.body.getPosition()[0], player.body.getPosition()[1], player.body.getPosition()[2]]
self.taunts.append([msg,
1.0,
pos, player.color])
def restart(self):
self.gameOver = False
self.scoreBoard.reset()
self.resetBall()
self.resetPlayers()
def setGameOver(self):
sounds.playSound("GameOver", (0,0,0))
self.gameOver = True
def getLoserNameColor(self):
for name, object in self.players.items():
if self.scoreBoard[name][0] >= self.matchPoints:
return name, object.color
def initEvents(self):
self.events = {'lastFieldOwner':None,
'lastTouchedObject':None,
'lastTouchedPlayer':None}
def resetBall(self, resetCoords = (0.0, 10.0, 0.0)):
if resetCoords == (0.0, 10.0, 0.0): #a little bit misadjust centered coords
pairs = ((-0.01, 0.0), (-0.01, -0.01), (0.0, -0.01), (0.01, -0.01), (0.01, 0.0), (0.01, 0.01), (0.0, 0.01))
newXZ = pairs[randint(0, 6)]
resetCoords = (newXZ[0], 10.0, newXZ[1])
self.ball.geom.reset = True
self.ball.geom.resetCoords = resetCoords
def resetPlayers(self):
for player in self.players.values():
player.setInitPosition()
player.setAngle()
def getBallCoords(self, playerName):
player = self.players[playerName]
littleBit = ((player.min[0] + player.max[0]) / 5, (player.min[1] + player.max[1]) / 5)
ballCoords = ((player.min[0] + player.max[0]) / 2 - littleBit[0],
3.3, (player.min[1] + player.max[1]) / 2 - littleBit[1])
return ballCoords
def checkIsBallInSquare(self, ball, min, max):
min0 = min[0]
min1 = min[1]
max0 = max[0]
max1 = max[1]
#0.5 is for the middle line
if min0 == 0: min0 = max0 / 10 * 0.5
if min1 == 0: min1 = max1 / 10 * 0.5
if max0 == 0: max0 = min0 / 10 * 0.5
if max1 == 0: max1 = min1 / 10 * 0.5
return vectormath.isPointInSquare(ball, (min0, min1), (max0, max1))
def registerGroundEvent(self):
#our objects of interest:
ball, ground = self.ball, None
#asign objects of interest
for object in self.objectList:
if object.name == 'Ground':
ground = object
#handle events
ballCoords = ball.geom.getPosition()
#ball + ground
if ball.body.getLinearVel()[1] < -0.3:
sounds.playSound("BallGround", ballCoords)
if not vectormath.isPointInSquare(ballCoords, (-10, -10), (10, 10)): #critical event
#BALL HAS HIT THE FIELD OUTSIDE
#and we handle score here
out = False
if self.events['lastTouchedObject'] != None: #if last touched was not middle line
lastTouchedObject = self.events['lastTouchedObject'].name
if 'Player' in lastTouchedObject:
#player has kicked the ball out
pts = self.scoreBoard.addSelfPoints(lastTouchedObject)
resetCoords = self.getBallCoords(lastTouchedObject)
self.faultMsgList.append(['Out from ' + lastTouchedObject + '!', str(pts), 0])
out = lastTouchedObject
getsPoints = lastTouchedObject
elif self.events['lastFieldOwner']: #if ground was touched in one of the players field last add points to owner
owner = self.events['lastFieldOwner'].name
if self.events['lastTouchedPlayer']:
#case when noone touched the ball after throwing minus
lastPlayer = self.events['lastTouchedPlayer'].name
if owner == lastPlayer:
pts = self.scoreBoard.addSelfPoints(owner)
else:
pts = self.scoreBoard.addTotalPoints(owner)
else:
pts = self.scoreBoard.addPoint(owner)
out = owner
self.faultMsgList.append(['Out from ' + owner + '`s field!', str(pts), 0])
resetCoords = self.getBallCoords(owner)
getsPoints = owner
if self.scoreBoard[getsPoints][0] >= self.matchPoints:
self.setGameOver()
return
else:
self.faultMsgList.append(['Ball out from middle line!', '', 0])
resetCoords = (0, 10, 0)
if out=="Player":
pl = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
#pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
#while pl2==pl:
# pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
sounds.playTauntMsg(pl, "GreatShot", pl.body.getPosition())
self.addTaunt(pl, "Great shot!")
# sounds.playTauntMsg(pl2, "Yes", pl.body.getPosition())
# self.addTaunt(pl2, "Yes!")
## print self.scoreBoard
#finishing handling critical branch
self.scoreBoard.resetJoinedCombo()
self.initEvents()
self.resetBall(resetCoords)
self.resetPlayers()
if out!=False and out!="Player":
pl = self.players[out]
sounds.playTauntMsg(pl, "MoveIn", pl.body.getPosition())
self.addTaunt(pl, "Move in!")
else:
#BALL HAS HIT THE FIELD INSIDE
#save the touched field
for p in self.players.values():
if self.checkIsBallInSquare(ballCoords, p.min, p.max):
self.events['lastFieldOwner'] = p.geom
self.events['lastTouchedObject'] = ground
break
self.events['lastTouchedObject'] = None #if has hit the middle line
def registerPlayerEvent(self):
ball = self.ball
for object in self.objectList:
if 'Player' in object.name:
player = object
#ball + player
sounds.playSound("BallPlayer", ball.geom.getPosition())
lastTouchedObject = self.events['lastTouchedObject']
if lastTouchedObject == None: #picked from middle line
self.scoreBoard.resetJoinedCombo() #clear combo
self.scoreBoard.incrementCombo(player, self.hitMsgList) #(+1)
else:#picked from ground or player
if lastTouchedObject.name == 'Ground': #picked from ground inside
self.scoreBoard.resetJoinedCombo()
playerName = player.name
self.scoreBoard.incrementCombo(player, self.hitMsgList)
if (self.events['lastFieldOwner'] == player)\
and (self.events['lastTouchedPlayer'] == player)\
and vectormath.isPointInSquare(ball.geom.getPosition()
, self.players[playerName].min
, self.players[playerName].max): #critical event. double-touched -> fault
if playerName=="Player":
pl = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
#pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
#while pl2==pl:
# pl2 = self.players[["Player_Red", "Player_Green", "Player_Yellow"][randint(0,2)]]
sounds.playTauntMsg(pl, "AreYouCrazy", pl.body.getPosition())
self.addTaunt(pl, "Are you crazy?")
#sounds.playTauntMsg(pl2, "No", pl.body.getPosition())
#self.addTaunt(pl2, "No!")
resetCoords = self.getBallCoords(playerName)
self.scoreBoard.addSelfPoints(playerName)
self.faultMsgList.append([playerName + ' touched twice!', '1', 0])
if self.scoreBoard[playerName][0] >= self.matchPoints:
self.setGameOver()
return
self.initEvents()
self.resetBall(resetCoords)
self.resetPlayers()
return
else: #picked from player
if self.events['lastTouchedPlayer'] != player: #reset own combo, when picked from other
self.scoreBoard.resetOwnCombo(player.name)
self.scoreBoard.incrementCombo(player, self.hitMsgList)
self.events['lastTouchedObject'] = player
self.events['lastTouchedPlayer'] = player
def register(self, player):
name = player.geom.name
self.players[name] = player
#see scoreBoard.reset
self.scoreBoard[name] = [0, 0]
def msgFlowFunction(self, x, px):
#this method is used to make the fault messages and "HITS" msg flow up and dissapear
return 1.0 - (x/px) * (x/px)* (x/px)* (x/px)
def draw(self, performance):
W, H = glfwGetWindowSize()
scale = gameglobals.fontScale
nameScoreList = []
#draw players scores
player = self.players['Player']
text = 'Player: ' + str(self.scoreBoard['Player'][0])
x = 10.0
y = self.font_aa.getSize(text)[1] * scale
nameScoreList.append([text, x, y, player.color])
playerName = 'Player_Red'
player = self.players[playerName]
text = playerName + ': ' + str(self.scoreBoard[playerName][0])
x = 10.0
y = H
nameScoreList.append([text, x, y, player.color])
playerName = 'Player_Green'
player = self.players[playerName]
text = playerName + ': ' + str(self.scoreBoard[playerName][0])
x = W - self.font_aa.getSize(text)[0] * scale - 10
y = H
nameScoreList.append([text, x, y, player.color])
playerName = 'Player_Yellow'
player = self.players[playerName]
text = playerName + ': ' + str(self.scoreBoard[playerName][0])
x = W - self.font_aa.getSize(text)[0] * scale - 10
y = self.font_aa.getSize(text)[1] * scale
nameScoreList.append([text, x, y, player.color])
self.font_aa.begin()
for nameScore in nameScoreList:
glColor3f(nameScore[3][0], nameScore[3][1], nameScore[3][2])
self.font_aa.renderShadowed(nameScore[0], nameScore[1], nameScore[2], (0,0,0), scale)
self.font_aa.end()
#draw "hits" messages
px = 50
for hit in self.hitMsgList:
if hit[1] < px:
addend = self.msgFlowFunction(hit[1], px)
hitText = ' HITS!'
if hit[0] < 2:
hitText = ' HIT!'
text = '+' + str(hit[0]) + hitText
pCoords = hit[2]
x, y, _ = gluProject(pCoords[0], pCoords[1], pCoords[2])
y = y + hit[1]
glColor4f(hit[3][0], hit[3][1], hit[3][2], addend)
self.font_aa.begin()
self.font_aa.renderOutlined(text, x, y, (0,0,0), scale)
self.font_aa.end()
hit[1] += 200 * (1.2 - addend) * performance
else:
self.hitMsgList = self.hitMsgList[1:]
#draw the faults messages
px = 80
for fault in self.faultMsgList:
if fault[2] < px:
addend = self.msgFlowFunction(fault[2], px)
text = fault[0]
pts = fault[1]
x, y = W / 2 - self.font_aa.getSize(text)[0] * scale / 2, H - 50 * scale
y = y + fault[2]
glColor4f(1.0, 0.8, 0.0, addend)
self.font_aa.begin()
self.font_aa.renderShadowed(text, x, y, (0,0,0), scale)
if pts:
glColor4f(0.5, 0.8, 0.1, addend)
pts = '+' + pts
x = W / 2 - self.font_aa.getSize(pts)[0] * scale / 2
y -= self.font_aa.getSize(pts)[1] * scale
self.font_aa.renderShadowed(pts, x, y, (0,0,0), scale)
self.font_aa.end()
fault[2] += 200 * (1.2 - addend) * performance
else:
self.faultMsgList = self.faultMsgList[1:]
#draw the taunts
px = 100
for taunt in self.taunts:
if taunt[1] < px:
addend = self.msgFlowFunction(taunt[1], px)
text = taunt[0]
pCoords = taunt[2]
x, y, _ = gluProject(pCoords[0], pCoords[1], pCoords[2])
y = y + taunt[1]
x = x - self.font_aa.getSize(text)[0] * scale / 2
c = taunt[3] #players.colorNamesColors[taunt[2]]
glColor4f(c[0], c[1], c[2], addend)
self.font_aa.begin()
self.font_aa.renderShadowed(text, x, y, (0,0,0), scale)
self.font_aa.end()
taunt[1] += 200 * (1.2 - addend) * performance
else:
self.taunts= self.taunts[1:]
#draw current total combo
total = self.scoreBoard.getTotalPoints()
if total > 1:
comboScale = scale * 1.25
totalText = 'Combo ' + str(total) + '!'
x = W / 2 - self.font_aa.getSize(totalText)[0] * comboScale / 2
y = 2 * self.font_aa.getSize(totalText)[1] * comboScale
glColor4f(0.0, 1.0, 1.0, 1)
self.font_aa.begin()
self.font_aa.renderShadowed(totalText, x, y, (0,0,0), comboScale)
self.font_aa.end()
#draw game over message
if self.gameOver == True:
loserMsg, loserColor = self.getLoserNameColor()
loserMsg += ' lost!'
msg = 'Game Over'
loserScale = scale * 1.25
gOverScale = scale * 2.5
x0 = W / 2 - self.font_aa.getSize(loserMsg)[0] * loserScale / 2
y0 = H / 2 + H / 4 - self.font_aa.getSize(loserMsg)[1] * loserScale / 2
x1 = W / 2 - self.font_aa.getSize(msg)[0] * gOverScale / 2
y1 = H / 2 - self.font_aa.getSize(msg)[1] * gOverScale / 2
self.font_aa.begin()
glColor4f(loserColor[0], loserColor[1], loserColor[2], 0.9)
self.font_aa.render(loserMsg, x0, y0, loserScale)
glColor4f(1.0, 0.0, 0.0, 0.9)
self.font_aa.render(msg, x1, y1, gOverScale)
self.font_aa.end()
def manageGame(self):
#This is the main logic function of the coach
#Coach has 3 states to keep in mind when registering event
#States are needed to avoid registering ball+player event when
#the ball is standing on player not bouncing
#This is the place where TriggerBox is used
if not self.gameOver:
objectNames = [object.name for object in self.objectList]
beforeCollideTriggerBox = self.afterCollideTriggerBox #initvalue=False
self.afterCollideTriggerBox = ('Trigger' in objectNames)# True if ball and TriggerBox collide else False
afterCollidePlayer = ('Player' in ''.join(objectNames)) # True if ball and Player collide else False
if 'Ground' in objectNames:
self.registerGroundEvent()
if self.state==1 and beforeCollideTriggerBox==False and self.afterCollideTriggerBox==True:
self.state=2
if self.state==2 and afterCollidePlayer==True:
self.registerPlayerEvent()
self.state=3
elif self.state==2 and beforeCollideTriggerBox==True and self.afterCollideTriggerBox==False:
self.state=1
elif self.state==3 and self.afterCollideTriggerBox==False:
self.state=1