-
Notifications
You must be signed in to change notification settings - Fork 0
/
survival.py
416 lines (331 loc) · 14.4 KB
/
survival.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
# Survival
from OpenGL.GL import *
import glfw
import OpenGL.GL.shaders
import numpy as np
import grafica.basic_shapes as bs
import grafica.easy_shaders as es
import grafica.transformations as tr
import grafica.performance_monitor as pm
import grafica.scene_graph as sg
from shapes import *
from model import *
from numpy import random
import sys, os.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
#Directory file
thisFilePath = os.path.abspath(__file__)
thisFolderPath = os.path.dirname(thisFilePath)
spritesDirectory = os.path.join(thisFolderPath,"sprites")
textPath = os.path.join(spritesDirectory, "derrota.png")
#sys.argv
#Z=sys.argv[1]
# We will use 32 bits data, so an integer has 4 bytes
# 1 byte = 8 bits
SIZE_IN_BYTES = 4
#Variables entregadas al enunciar el programa
z, h, t, p = sys.argv[1:5]
Z = int(z)
H = int(h)
T = float(t)
P = float(p)
# Clase controlador con variables para manejar el estado de ciertos botones
class Controller:
def __init__(self):
self.fillPolygon = True
self.is_w_pressed = False
self.is_s_pressed = False
self.is_a_pressed = False
self.is_d_pressed = False
self.what_to_draw = 1
# we will use the global controller as communication with the callback function
controller = Controller()
# This function will be executed whenever a key is pressed or released
def on_key(window, key, scancode, action, mods):
global controller
# Caso de detectar la tecla [W], actualiza estado de variable
if key == glfw.KEY_W:
if action ==glfw.PRESS:
controller.is_w_pressed = True
elif action == glfw.RELEASE:
controller.is_w_pressed = False
# Caso de detectar la tecla [S], actualiza estado de variable
if key == glfw.KEY_S:
if action ==glfw.PRESS:
controller.is_s_pressed = True
elif action == glfw.RELEASE:
controller.is_s_pressed = False
# Caso de detectar la tecla [A], actualiza estado de variable
if key == glfw.KEY_A:
if action ==glfw.PRESS:
controller.is_a_pressed = True
elif action == glfw.RELEASE:
controller.is_a_pressed = False
# Caso de detectar la tecla [D], actualiza estado de variable
if key == glfw.KEY_D:
if action ==glfw.PRESS:
controller.is_d_pressed = True
elif action == glfw.RELEASE:
controller.is_d_pressed = False
# Caso de detectar la tecla [0], actualiza que se dibuja
if key == glfw.KEY_0 and action == glfw.PRESS:
controller.what_to_draw = 0
# Caso de detectar la tecla [1], actualiza que se dibuja
if key == glfw.KEY_1 and action == glfw.PRESS:
controller.what_to_draw = 1
# Caso de detectar la tecla [2], actualiza que se dibuja
if key == glfw.KEY_2 and action == glfw.PRESS:
controller.what_to_draw = 2
# Caso de detectar la tecla [3], actualiza que se dibuja
if key == glfw.KEY_3 and action == glfw.PRESS:
controller.what_to_draw = 3
# Caso de detecar la barra espaciadora, se cambia el metodo de dibujo
if key == glfw.KEY_SPACE and action ==glfw.PRESS:
controller.fillPolygon = not controller.fillPolygon
# Caso en que se cierra la ventana
elif key == glfw.KEY_ESCAPE and action ==glfw.PRESS:
glfw.set_window_should_close(window, True)
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
# Creating a glfw window
width = 600
height = 600
title = "Survival"
window = glfw.create_window(width, height, title, None, None)
if not window:
glfw.terminate()
glfw.set_window_should_close(window, True)
glfw.make_context_current(window)
# Connecting the callback function 'on_key' to handle keyboard events
glfw.set_key_callback(window, on_key)
# Pipeline para dibujar shapes con colores interpolados
pipeline = es.SimpleTransformShaderProgram()
# Pipeline para dibujar shapes con texturas
tex_pipeline = es.SimpleTextureTransformShaderProgram()
# Pipeline para dibujar shapes con texturas animadas (3 fotogramas)
animated3Tex_pipeline = es.SimpleTextureTransform3FrameAnimationShaderProgram()
# Pipeline encargada de dibujar shapes con texturas controladas por el jugador
PlayableTex_pipeline = es.SimpleTextureTransform3FrameControllerShaderProgram()
# Setting up the clear screen color
glClearColor(0.15, 0.15, 0.15, 1.0)
# Enabling transparencies
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
######### SHAPES ######
NPC = sg.SceneGraphNodeMultiPipeline("npc", [PlayableTex_pipeline])
background = createBackground(pipeline)
store = createStore(tex_pipeline)
storeSign = createAnimatedSign(animated3Tex_pipeline)
details = createDetails(pipeline)
hinata = createHinata(PlayableTex_pipeline)
gpuHinata = hinata.childs[0]
gpuYouWin = YouWingpu(pipeline)
cuadroWin = bs.Shape(VerticesYouwin(0), indicesYouWin())
gpuCuadroWin = createGPUShapeStream(cuadroWin, pipeline)
zombies = []
humanos = []
######## SHAPES WITH MORE PIPELINES ARE CREATE HERE ########
# derrota
gpuCuadradoNegro = createGPUShape(bs.createRainbowQuad(),pipeline)
cuadroNegro = sg.SceneGraphNodeMultiPipeline("cuadroNegro", [pipeline])
cuadroNegro.childs += [gpuCuadradoNegro]
gpuText = createTextureGPUShape(bs.createSimpleQuad(), animated3Tex_pipeline, textPath)
Text = sg.SceneGraphNodeMultiPipeline("derrotaText", [animated3Tex_pipeline])
Text.childs += [gpuText]
derrota = sg.SceneGraphNodeMultiPipeline("derrota", [pipeline, animated3Tex_pipeline])
derrota.childs += [cuadroNegro, Text]
perfMonitor = pm.PerformanceMonitor(glfw.get_time(), 0.5)
# glfw will swap buffers as soon as possible
glfw.swap_interval(0)
# Ubicación de nodos:
hojas= sg.findNode(background, "hojas")
arboles1 = sg.findNode(background, "arboles1")
arboles2 = sg.findNode(background, "arboles2")
lineas1 = sg.findNode(background, "Lineatransito1")
lineas2 = sg.findNode(background, "Lineatransito2")
decoracion1 = sg.findNode(details, "decoration1")
decoracion2 = sg.findNode(details, "decoration2")
Store = sg.findNode(store, "Store")
Sign = sg.findNode(storeSign, "StoreSign")
#Distinción de frames en las texturas:
singInit = np.array([0.0, 0.34, 0.64])
signFin = np.array([0.33, 0.63, 1])
actual_sprite=0
FrameInit = np.array([0, 1/3, 2/3])
FrameFin = np.array([1/3, 2/3, 1])
player = Player(P)
player.setModel(hinata)
player.setController(controller)
lNpc=[]
# Variables útiles para el procedimiento
t0 = glfw.get_time()
dx = 0
t3=T
t5=0
dy = 0
dy2 = 0
derrotaBool = False
victoriaBool = False
# Application loop
while not glfw.window_should_close(window):
# Variables del tiempo
t1 = glfw.get_time()
delta = t1 -t0
t0 = t1
t3 += delta
t4 = t1%1.5
t5 += delta
# Measuring performance
perfMonitor.update(glfw.get_time())
glfw.set_window_title(window, title + str(perfMonitor))
# Using GLFW to check for input events
glfw.poll_events()
# Filling or not the shapes depending on the controller state,
# ahora activa las gafas detectoras
if (controller.fillPolygon):
#glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
gafas = 0
else:
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
gafas = 1
# Clearing the screen
glClear(GL_COLOR_BUFFER_BIT)
#PLAYER
if not derrotaBool and not victoriaBool:
dy -= player.update(delta)
texture = player.getTexture_index()
if texture == 0:
gpuHinata.texture =es.textureSimpleSetup(hinataFrontPath, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
GL_NEAREST, GL_NEAREST)
elif texture == 1:
gpuHinata.texture =es.textureSimpleSetup(hinataBackPath, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
GL_NEAREST, GL_NEAREST)
else:
gpuHinata.texture =es.textureSimpleSetup(hinataSidePath, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
GL_NEAREST, GL_NEAREST)
#NPC
if t3 > T:
for ZomHum in lNpc:
Convertido = ZomHum.Convertirse()
if Convertido:
zombies.append(ZomHum.model)
humanos.remove(ZomHum.model)
nZ = len(zombies)
nH = len(humanos)
for i in range(Z):
rand = random.rand()
zombies.append(createZombie(PlayableTex_pipeline))
lNpc.append(npc(rand-0.5, rand+ 1.1, 2, P))
lNpc[nH + nZ + i].set_model(zombies[nZ + i])
NPC.childs += zombies[nZ:]
nZ = len(zombies)
for i in range(H):
p=0
rand = random.rand()
if random.rand()<P:
p=1
lNpc.append(npc(rand-0.5,rand + 1.1, p, P))
humanos.append(createHumano(PlayableTex_pipeline, lNpc[nH + nZ +i].getStatus()))
lNpc[nH + nZ +i].set_model(humanos[nH + i])
NPC.childs += humanos[nH:]
t3=0
player.Convertirse()
Compare = []
for ZomHum in lNpc:
ZomHum.update(delta)
Convertido = ZomHum.collision(Compare)
Compare.append(ZomHum)
player.collision(ZomHum)
if type(Convertido)==list:
for z in Convertido:
zombies.append(z.model)
humanos.remove(z.model)
elif Convertido:
zombies.append(ZomHum.model)
humanos.remove(ZomHum.model)
# Se agregan los movimientos durante escena
dx += delta*2
if dy2>-0.6:
dy2 = 2 + dy
hojas.transform = tr.matmul([tr.translate(0, 0.3, 0),tr.shearing(0.2*np.sin(dx), 0, 0, 0, 0, 0)])
arboles1.transform = tr.translate(0, dy, 0)
arboles2.transform = tr.translate(0, dy2, 0)
lineas1.transform = tr.translate(0, dy, 0)
lineas2.transform = tr.translate(0, dy2, 0)
decoracion1.transform = tr.translate(0, dy, 0)
decoracion2.transform = tr.translate(0, dy2, 0)
Store.transform = tr.translate(0, dy2, 0)
Sign.transform = tr.translate(0, dy2, 0)
#Animación:
if t4<0.5:
if texture !=3:
time=0
else:
time=2
elif t4<1:
time=1
else:
if texture !=3:
time=2
else:
time=0
# Se dibuja el grafo de escena.
glUseProgram(pipeline.shaderProgram)
sg.drawSceneGraphNode(background, pipeline, "transform")
sg.drawSceneGraphNode(details, pipeline, "transform", GL_LINE_STRIP)
glUseProgram(tex_pipeline.shaderProgram)
sg.drawSceneGraphNode(store, tex_pipeline, "transform")
glUseProgram(animated3Tex_pipeline.shaderProgram)
glUniform3fv(glGetUniformLocation(animated3Tex_pipeline.shaderProgram, "spritesInit"), 1, FrameInit)
glUniform3fv(glGetUniformLocation(animated3Tex_pipeline.shaderProgram, "spritesFin"), 1, FrameFin)
glUniform1i(glGetUniformLocation(animated3Tex_pipeline.shaderProgram, "index"), time)
sg.drawSceneGraphNode(storeSign, animated3Tex_pipeline, "transform")
glUseProgram(PlayableTex_pipeline.shaderProgram)
glUniform3fv(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "spritesInit"), 1, FrameInit)
glUniform3fv(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "spritesFin"), 1, FrameFin)
glUniform1i(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "index"), time)
glUniform1i(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "move"), 0)
glUniform1i(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "gafas"), gafas)
sg.drawSceneGraphNodeMultiPipeline(NPC, [PlayableTex_pipeline], "transform")
if not derrotaBool and player.getStatus() !=2:
glUniform1i(glGetUniformLocation(PlayableTex_pipeline.shaderProgram, "move"), texture)
sg.drawSceneGraphNodeMultiPipeline(hinata, [PlayableTex_pipeline], "transform")
else:
if not derrotaBool: del player
derrotaBool = True
sg.drawSceneGraphNodeMultiPipeline(derrota, [pipeline, animated3Tex_pipeline], "transform")
if victoriaBool:
glUseProgram(pipeline.shaderProgram)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.identity())
vertexData = np.array(VerticesYouwin(t5), dtype = np.float32)
glBindBuffer(GL_ARRAY_BUFFER, gpuCuadroWin.vbo)
glBufferData(GL_ARRAY_BUFFER, len(vertexData) * SIZE_IN_BYTES, vertexData, GL_STREAM_DRAW)
pipeline.drawCall(gpuCuadroWin, GL_TRIANGLES)
pipeline.drawCall(gpuYouWin, GL_TRIANGLES)
if not derrotaBool and dy2<=-0.6 and player.pos[0] <0-0.3 and player.pos[1] >-0.3:
victoriaBool = True
if not derrotaBool:
player.setTexture_index_default()
for ZomHum in lNpc:
if ZomHum.getPos()<-1.3:
if ZomHum.getStatus() == 2:
zombies.remove(ZomHum.model)
ZomHum.model.clear()
NPC.childs.remove(ZomHum.model)
else:
humanos.remove(ZomHum.model)
ZomHum.model.clear()
NPC.childs.remove(ZomHum.model)
lNpc.remove(ZomHum)
# Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
glfw.swap_buffers(window)
# freeing GPU memory
background.clear()
store.clear()
storeSign.clear()
details.clear()
hinata.clear()
NPC.clear()
glfw.terminate()