-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
534 lines (426 loc) · 16.3 KB
/
game.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
from state import *
from character import *
from stage import *
from item import *
from player import *
from music import *
from sprite import *
import random
import sys
from direct.actor.Actor import Actor
from panda3d.core import Point2, Point3
from panda3d.core import LightRampAttrib, ClockObject
from direct.filter.CommonFilters import CommonFilters
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.Transitions import Transitions
class Game(State):
mapOffset = {"up": (0,1), "down": (0,-1), "left": (-1,0), "right": (1,0)}
def __init__(self, stage, characters, player, player2):
State.__init__(self)
# how to know the players that will be in game? a ChoosePlayer screen before the constructor?
self.characters = characters
self.player = player
self.player2 = player2
self.stage = stage
self.room = self.stage.start
self.isOver = False
self.players = []
#print(self.players)
self.startMap()
self.status = {}
posi = 0
for c in self.characters:
# initialize character status string
self.status[c] = (OnscreenText(mayChange= True ,
style=2, fg=(1,1,1,1),
pos=(1.4*posi - 0.8,-0.83), scale = .07)
)
posi += 1
self.transitions = Transitions(GameLoader.loader)
self.clock = ClockObject()
self.lastTime = 0
GameLoader.music.addSfx('barrel_breaks')
GameLoader.music.addSfx('stone','aif')
def spawnObject(self, ob):
ob.setMap(self.currentMap())
ob.getNode().reparentTo(NodePath(self.currentMap().getNode()))
x,y = self.currentMap().posToGrid(ob.getPos())
if ob.symbol == 'i':
GameLoader.music.addSfx(ob.sfx)
try:
self.currentMap().tiles[Map.COLLISION][y][x] = ob.symbol
except IndexError as e:
print('Index error: ' , ob.id, e )
def currentMap(self):
return self.stage.maps[self.room]
def exitMap(self):
for i in range(self.currentMap().width):
for j in range(self.currentMap().height):
atype = self.currentMap().tileType(1,(i,j))
if atype == 'block' or atype == 'liftable':
self.currentMap().tiles[1][j][i] = ' '
for b in self.currentMap().blocks:
b.setPos(b.originalPos)
x,y = self.currentMap().posToGrid(b.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = 'b'
b.getNode().reparentTo(NodePath(self.currentMap().getNode()))
for b in self.currentMap().liftables:
b.setPos(b.originalPos)
x,y = self.currentMap().posToGrid(b.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = 'l'
b.getNode().reparentTo(NodePath(self.currentMap().getNode()))
for d in self.currentMap().doors:
if d.permanent:
x,y = self.currentMap().posToGrid(d.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = 'd'
d.getNode().reparentTo(NodePath(self.currentMap().getNode()))
for char in [self.characters[self.player], self.characters[self.player2]]:
char.lifting = None
NodePath(self.currentMap().getNode()).detachNode()
def startMap(self):
if not self.currentMap().started:
for obstacle in self.currentMap().obstacles:
self.spawnObject(obstacle)
for item in self.currentMap().items:
self.spawnObject(item)
for block in self.currentMap().blocks:
self.spawnObject(block)
for liftable in self.currentMap().liftables:
self.spawnObject(liftable)
for switch in self.currentMap().switches:
switch.getNode().reparentTo(NodePath(self.currentMap().getNode()))
for door in self.currentMap().doors:
self.spawnObject(door)
self.currentMap().started = True
for e in self.currentMap().enemies:
self.spawnObject(e)
self.characters[self.player].setMap(self.currentMap())
self.characters[self.player2].setMap(self.currentMap())
def changeMap(self,direction,char):
self.transitions.fadeOut(1)
self.transitions.fadeIn(2)
#TODO modularize for more characters
self.exitMap()
self.room = self.stage.doors[self.room][direction]
NodePath(self.currentMap().getNode()).reparentTo(self.node)
map = self.stage.maps[self.room]
x, y = self.currentMap().posToGrid(char.getPos())
if direction == "right": x = 1
elif direction == "left": x = map.width-2
elif direction == "down": y = 1
elif direction == "up": y = map.height-2
pos = self.currentMap().gridToPos((x,y))
self.characters[self.player].setPos(pos)
self.characters[self.player2].setPos(pos)
self.characters[self.player2].setDirection(self.characters[self.player].direction)
self.startMap()
def register(self, render, camera, keys, render2d):
State.register(self, render, camera, keys, render2d)
self.node.attachNewNode(self.stage.maps[self.room].getNode())
for c in self.characters:
self.status[c].reparentTo(self.node2d)
for char in self.characters.values():
char.getNode().reparentTo(self.node)
char.stunRender = self.node
for l in self.stage.getLights():
render.setLight(l)
# COWABUNGA test!!!
self.hearts = {}
numChar=0
self.heartsNode = NodePath(PandaNode('hearts'))
self.heartsNode.reparentTo(self.node2d)
for char in self.characters:
self.hearts[char] = []
for i in range(Character.maxHearts):
self.hearts[char].append(Sprite("heart.png", 0.05, 0.05))
self.hearts[char][i].setPos(-0.8 + 1.4*numChar + (i%3)*0.055 , -0.9 - int(i/3)*0.055)
self.hearts[char][i].getNode().reparentTo(self.heartsNode)
numChar += 1
#COWABUNGA comment this to stop the madness
render.setAttrib(LightRampAttrib.makeSingleThreshold(0.1, 1))
# THE TRUE CARTOON SHADER :P
# self.separation = 1 # Pixels
self.filters = CommonFilters(base.win, self.camera.camera)
# cell shading
# filterok = self.filters.setCartoonInk(separation=self.separation)
# glow
filterok = self.filters.setBloom(blend=(0.5,0.5,0.5,1), desat=-0.5, intensity=0.5, size="small")
self.camera.setPos(0, -2.5, -2.5)
self.camera.lookAt(0, 0, 0)
def iterate(self):
State.iterate(self)
self.camera.look()
self.moveObjects()
self.doCharActions()
self.moveChars()
self.buryDeadPeople()
self.activateSwitches()
self.updateHUD()
for c in self.characters:
self.characters[c].tryToRecover()
if self.isOver:
return "GameOver"
elif self.keys['start']:
return "Paused"
def updateHUD(self):
for c in self.characters:
self.status[c].setText(self.characters[c].getStatus())
if self.characters[c].healthChanged:
for heart in self.hearts[c]:
heart.getNode().detachNode()
for i in range(self.characters[c].hearts):
self.hearts[c][i].getNode().reparentTo(self.heartsNode)
self.characters[c].healthChanged = False
def moveObjects(self):
# BLOCK MOVEMENT ACTION
for block in self.currentMap().blocks:
if block.isMoving:
block.move(block.direction)
p1, p2 = block.getCollisionPos(block.direction)
x1, y1 = self.currentMap().posToGrid(p1)
x2, y2 = self.currentMap().posToGrid(p2)
try:
for x,y in [(x1,y1), (x2,y2)]:
if self.stage.maps[self.room].tileType(1, (x,y)) == 'enemy':
for enemy in self.currentMap().enemies:
lPos = self.currentMap().posToGrid(enemy.getPos())
if tuple(lPos) == (x,y):
self.collision(block, enemy)
else:
for char in self.characters:
if (x,y) == self.currentMap().posToGrid(self.characters[char].getPos()):
bx,by = self.currentMap().posToGrid(block.getPos())
self.currentMap().tiles[Map.COLLISION][by][bx] = ' '
self.characters[char].stun()
block.stop()
block.getNode().detachNode()
except IndexError:
block.stop()
# LIFTABLE MOVEMENT ACTION
for liftable in self.currentMap().liftables:
if liftable.isMoving:
liftable.move(liftable.direction)
p1, p2 = liftable.getCollisionPos(liftable.direction)
x1, y1 = self.currentMap().posToGrid(p1)
x2, y2 = self.currentMap().posToGrid(p2)
try:
for x,y in [(x1,y1), (x2,y2)]:
if self.stage.maps[self.room].tileType(1, (x,y)) == 'enemy':
for enemy in self.currentMap().enemies:
lPos = self.currentMap().posToGrid(enemy.getPos())
if tuple(lPos) == (x,y):
self.collision(liftable, enemy)
elif self.stage.maps[self.room].tileType(1, (x,y)) != 'free':
liftable.stop()
liftable.getNode().detachNode()
else:
for char in self.characters:
if (x,y) == self.currentMap().posToGrid(self.characters[char].getPos()):
print "Stun nele!"
self.characters[char].stun()
liftable.stop()
liftable.getNode().detachNode()
except IndexError:
# to do, create a liftable.destroy(), which animates the liftable and do these two actions:
liftable.stop()
liftable.getNode().detachNode()
def doCharActions(self):
for char in [self.characters[self.player], self.characters[self.player2]]:
add = "1"
if char == self.characters[self.player]:
add = ""
if self.keys['attack'+add]:
self.keys['attack'+add] = False
print('Using %s' % (char.currentItem()) )
p1, p2 = char.getCollisionPos(char.direction)
x1, y1 = self.currentMap().posToGrid(p1)
x2, y2 = self.currentMap().posToGrid(p2)
for x,y in [(x1,y1), (x2,y2)]:
if self.stage.maps[self.room].tileType(Map.COLLISION, (x,y)) == 'door':
for d in self.currentMap().doors:
if tuple(self.currentMap().posToGrid(d.getPos())) == (x,y):
opened = d.open(char.currentItem())
if opened:
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
char.destroyCurrentItem()
if self.keys['cancel'+add]:
self.keys['cancel'+add] = False
print('Changing slot')
char.changeSlot()
if self.keys['action'+add]:
self.keys['action'+add] = False
if char.lifting:
char.lifting.setHeight(0)
char.lifting.move(char.direction)
char.lifting = None
else:
p1, p2 = char.getCollisionPos(char.direction)
x1, y1 = self.currentMap().posToGrid(p1)
x2, y2 = self.currentMap().posToGrid(p2)
for x,y in [(x1,y1), (x2,y2)]:
if self.stage.maps[self.room].tileType(Map.COLLISION, (x,y)) == 'block':
for block in self.currentMap().blocks:
if tuple(self.currentMap().posToGrid(block.getPos())) == (x,y):
GameLoader.music.playSfx('stone')
block.move(char.direction)
elif self.stage.maps[self.room].tileType(Map.COLLISION, (x,y)) == 'item':
for item in self.currentMap().items:
if tuple(self.currentMap().posToGrid(item.getPos())) == (x,y):
self.collision(char, item)
elif self.currentMap().tileType(Map.COLLISION, (x,y)) == 'liftable':
for liftable in self.currentMap().liftables:
lPos = self.currentMap().posToGrid(liftable.getPos())
if tuple(lPos) == (x,y):
char.pick(liftable)
self.currentMap().tiles[1][y][x] = ' '
def moveChars(self):
for char in [self.characters[self.player], self.characters[self.player2]]:
add = "1"
if char == self.characters[self.player]:
add = ""
directions = [key for key in ["up","down","left","right"] if self.keys[key+add]]
if len(directions) == 0:
char.stop()
for dir in directions:
#TODO to be re-refactored
p1, p2 = char.getCollisionPos(dir)
if self.currentMap().futPosAreFree(p1, p2):
char.move(dir)
if char.lifting:
char.lifting.setPos(char.getPos())
ex = self.stage.maps[self.room].getExit(self.currentMap().posToGrid(p1))
if ex and (ex in self.stage.doors[self.room].keys()):
self.changeMap(ex,char)
else:
char.setDirection(dir)
for enemy in self.currentMap().enemies:
#TODO actually enemies are still present in the map
x,y = self.currentMap().posToGrid(enemy.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
# COWABUNGA
# se botarmos aqui uma funcao que define como o inimigo anda
# vai dar tudo certo
# por exemplo ele pode as vezes andar em direcao a um heroi e
# as vezes ser random
# por enquanto e so random
# - OK, agreed
dir = ['up','down','left','right'][random.randint(0,3)]
p1, p2 = enemy.getCollisionPos(dir)
if self.currentMap().futPosAreFree(p1, p2):
enemy.enemy_move(dir)
else:
enemy.setDirection(dir)
x,y = self.currentMap().posToGrid(enemy.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = 'e'
p1, p2 = char.getCollisionPos(char.direction)
x1, y1 = self.currentMap().posToGrid(p1)
x2, y2 = self.currentMap().posToGrid(p2)
for x,y in [(x1,y1), (x2,y2)]:
collisionTiles = ["enemy"]
collisionElements = {"enemy": self.currentMap().enemies}
for t in collisionTiles:
if self.stage.maps[self.room].tileType(Map.COLLISION, (x,y)) == t:
for e in collisionElements[t]:
lPos = self.currentMap().posToGrid(e.getPos())
if tuple(lPos) == (x,y):
self.collision(char, e)
def activateSwitches(self):
mp = self.currentMap()
for s in mp.switches:
x,y = mp.posToGrid(s.getPos())
charPos = [self.currentMap().posToGrid(self.characters[char].getPos()) for char in self.characters]
if mp.tiles[Map.COLLISION][y][x] != ' ' or ((x,y) in charPos):
#print 'activated!'
mp.tiles[Map.GROUND][y][x] = 'S'
s.activate()
else:
#print 'deactivated!'
mp.tiles[Map.GROUND][y][x] = 's'
s.deactivate()
for d in mp.doors:
openDoor = True
if d.openWith == "switches":
for ds in d.switches:
for ms in mp.switches:
if (ds == ms.name) and (not ms.active):
openDoor = False
break
if openDoor:
d.open("switches")
x,y = mp.posToGrid(d.getPos())
mp.tiles[Map.COLLISION][y][x] = ' '
def collision(self, a, b):
print "Collision: TYPE A:", a.getType(), "TYPE B:", b.getType()
if b.getType() == 'item':
for i in range(len(self.currentMap().items)):
if i < len(self.currentMap().items): #we need this because the size of the list may change if we remove an item
if tuple(self.currentMap().items[i].getPos()) == tuple(b.getPos()):
# removes the item
self.currentMap().items.pop(i)
x, y = self.currentMap().posToGrid((NodePath(b.getNode()).getX(), NodePath(b.getNode()).getZ()))
# it's not drawed anymore
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
NodePath(b.getNode()).detachNode()
#it's picked
oldItem = a.pickItem(b)
if oldItem != None:
print oldItem.name
oldItem.setPos(self.currentMap().gridToPos((x,y)))
oldItem.originalPos = oldItem.getPos()
self.spawnObject(oldItem)
self.currentMap().items.append(oldItem)
if a.getType() == 'liftable' or b.getType() == 'liftable':
GameLoader.music.playSfx('barrel_breaks')
if a.getType() == 'liftable' and b.getType() == 'enemy':
a.stop()
a.getNode().detachNode()
x,y = self.currentMap().posToGrid(a.getPos()) #nao precisaria
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
b.takeDamage(10000)
if not b.isAlive():
x, y = self.currentMap().posToGrid(b.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
NodePath(b.getNode()).removeNode()
self.currentMap().enemies.remove(b)
if a.getType() == 'block' and b.getType() == 'enemy':
a.stop()
a.getNode().detachNode()
x,y = self.currentMap().posToGrid(a.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
b.takeDamage(10000)
if not b.isAlive():
x, y = self.currentMap().posToGrid(b.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
NodePath(b.getNode()).removeNode()
self.currentMap().enemies.remove(b)
if a.getType() == 'Character':
print("Character collided with", b.getType())
if b.getType() == 'enemy':
a.takeDamage(1)
def buryDeadPeople(self):
# commented while fixing the bugs
for enemy in self.currentMap().enemies:
if not enemy.isAlive():
x, y = self.currentMap().posToGrid(enemy.getPos())
self.currentMap().tiles[Map.COLLISION][y][x] = ' '
NodePath(enemy.getNode()).removeNode()
self.currentMap().enemies.remove(enemy)
#if not self.player.isAlive() : #tratar isso corretamente!
for char in self.characters:
if not self.characters[char].isAlive():
self.isOver = True
def enter(self):
State.enter(self)
self.stage.playMusic()
def exit(self):
# self.transitions.fadeOut(2)
# self.transitions.fadeIn(3)
# self.transitions.noFade()
self.stage.stopMusic()
# self.heartsNode.removeNode()
# for c in self.characters:
# NodePath(self.characters[c].getNode()).removeNode()
# self.status[c].removeNode()
# NodePath(self.currentMap().getNode()).removeNode()
State.exit(self)
# to do (or not): create GameServer and GameClient classes to inherit from Game