-
Notifications
You must be signed in to change notification settings - Fork 0
/
render2d.py
271 lines (233 loc) · 9.12 KB
/
render2d.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
from os import read
import readWad
import pygame
import random
import math
things = readWad.readMapThings()
vertexes = readWad.readMapVertex()
linedefs = readWad.readMapLinedefs()
sidedefs = readWad.readMapSidedefs()
sectors = readWad.readMapSectors()
nodes =readWad.readMapNodes()
segs = readWad.readMapSegs()
subsectors = readWad.readMapSubsectors()
if readWad.useGLnodes():
glvertexes= readWad.readMapGLVertex()
glsegs = readWad.readMapGLSegs()
glsubsectors = readWad.readMapGLSubsectors()
glnodes = readWad.readMapGLNodes()
mapX=100
mapY=600
zoom=20
startPos = [0,0]
pygame.init()
display = (1000, 600)
scree = pygame.display.set_mode(display)
def isOnleftSide(x, y, node):
dx = x - node[0]
dy = y - node[1]
return (((dx * node[3]) - (dy * node[2])) <= 0)
def calculateOffsetX(x,angle,offset):
x=x+offset*math.cos(angle)
return x
def calculateOffsetY(y,angle,offset):
y=y+offset*math.sin(angle)
return y
def drawWalls():
for linedef in linedefs:
l=vertexes[linedef[0]]
r=vertexes[linedef[1]]
pygame.draw.line(scree, (200,200,200), (l[0]/zoom+mapX,l[1]/zoom+mapY),(r[0]/zoom+mapX,r[1]/zoom+mapY),2)
def drawPlayerDot():
pygame.draw.line(scree, (250,0,0), (startPos[0]/zoom+mapX,startPos[1]/zoom+mapY),(startPos[0]/zoom+mapX,startPos[1]/zoom+mapY),5)
def drawSectors():
for sector in sectors:
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
s=[]
for linedef in linedefs:
if sectors[sidedefs[linedef[5]][5]]==sector:
s+=[[vertexes[linedef[0]][0]/zoom+mapX,vertexes[linedef[0]][1]/zoom+mapY]]
s+=[[vertexes[linedef[1]][0]/zoom+mapX,vertexes[linedef[1]][1]/zoom+mapY]]
try: pygame.draw.polygon(scree, (c[0],c[1],c[2]), s)
except: pass
s=[]
def drawNode(node):
#if node[12]>237 or node[13]>237: return
r=pygame.Rect((node[6]/zoom+mapX,node[4]/zoom+mapY),((node[7]/zoom+mapX)-(node[6]/zoom+mapX)+1,(node[5]/zoom+mapY)-(node[4]/zoom+mapY)+1))
pygame.draw.rect(scree, (0,250,0), r,1)
rr=pygame.Rect((node[10]/zoom+mapX,node[8]/zoom+mapY),((node[11]/zoom+mapX)-(node[10]/zoom+mapX)+1,(node[9]/zoom+mapY)-(node[8]/zoom+mapY)+1))
pygame.draw.rect(scree, (250,0,0), rr,1)
pygame.draw.line(scree, (0,0,250), (node[0]/zoom+mapX,node[1]/zoom+mapY),((node[0]/zoom+mapX)+node[2]/zoom,(node[1]/zoom+mapY)+node[3]/zoom),1)
def searchForPlayer(node):
drawNode(node)
isOnLeft = isOnleftSide(startPos[0],startPos[1], node)
print(isOnLeft)
if isOnLeft:
return node[13]
else:
return node[12]
def drawSubsectors():
for subsector in subsectors:
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
v=[]
for size in range(subsector[0]):
vx=vertexes[segs[subsector[1]+size][0]]
vy=vertexes[segs[subsector[1]+size][1]]
l=linedefs[segs[subsector[1]+size][3]]
v.append([vx[0]/zoom+mapX,vx[1]/zoom+mapY])
v.append([vy[0]/zoom+mapX,vy[1]/zoom+mapY])
try:
pygame.draw.polygon(scree, (c[0],c[1],c[2]), v)
except: pass
def drawGLSubsectors():
for glsubsector in glsubsectors:
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
v=[]
for size in range(glsubsector[0]):
vx=0
vy=0
if glsegs[glsubsector[1]+size][1]==0:
vx=vertexes[glsegs[glsubsector[1]+size][0]]
v+=[[vx[0]/zoom+mapX,vx[1]/zoom+mapY]]
else:
vx=glvertexes[glsegs[glsubsector[1]+size][0]]
v+=[[vx[0]/zoom+mapX,vx[1]/zoom+mapY]]
if glsegs[glsubsector[1]+size][3]==0:
vy=vertexes[glsegs[glsubsector[1]+size][2]]
v+=[[vy[0]/zoom+mapX,vy[1]/zoom+mapY]]
else:
vy=glvertexes[glsegs[glsubsector[1]+size][2]]
v+=[[vy[0]/zoom+mapX,vy[1]/zoom+mapY]]
pygame.draw.polygon(scree, (c[0],c[1],c[2]), v)
def drawGLSubsector(glsubsector):
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
v=[]
for size in range(glsubsector[0]):
vx=0
vy=0
if glsegs[glsubsector[1]+size][1]==0:
vx=vertexes[glsegs[glsubsector[1]+size][0]]
v+=[[vx[0]/zoom+mapX,vx[1]/zoom+mapY]]
else:
vx=glvertexes[glsegs[glsubsector[1]+size][0]]
v+=[[vx[0]/zoom+mapX,vx[1]/zoom+mapY]]
if glsegs[glsubsector[1]+size][3]==0:
vy=vertexes[glsegs[glsubsector[1]+size][2]]
v+=[[vy[0]/zoom+mapX,vy[1]/zoom+mapY]]
else:
vy=glvertexes[glsegs[glsubsector[1]+size][2]]
v+=[[vy[0]/zoom+mapX,vy[1]/zoom+mapY]]
pygame.draw.polygon(scree, (c[0],c[1],c[2]), v)
def drawSegs():
for subsector in subsectors:
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
for size in range(subsector[0]):
vx=vertexes[segs[subsector[1]+size][0]]
vy=vertexes[segs[subsector[1]+size][1]]
pygame.draw.line(scree, (c[0],c[1],c[2]), (vx[0]/zoom+mapX,vx[1]/zoom+mapY),(vy[0]/zoom+mapX,vy[1]/zoom+mapY))
def drawGLSegs():
for glsubsector in glsubsectors:
c=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
glvv=[]
for glv in glvertexes:
glvx=glv[0]/zoom+mapX
glvy=glv[1]/zoom+mapY
glvv.append([glvx,glvy])
vs=vertexes
for size in range(glsubsector[0]):
try:
vx=vs[glsegs[glsubsector[1]+size][0]]
vy=vs[glsegs[glsubsector[1]+size][1]]
pygame.draw.line(scree, (c[0],c[1],c[2]), (vx[0]/zoom+mapX,vx[1]/zoom+mapY),(vy[0]/zoom+mapX,vy[1]/zoom+mapY))
except: pass
def chooseType():
a = input("[1] - Render map\n[2] - Render segs\n[3] - Render subsectors\n[4] - Search for player\n")
if a.replace(' ', '') == "1":
return 1
elif a.replace(' ', '') == "2":
return 2
elif a.replace(' ', '') == "3":
return 3
elif a.replace(' ', '') == "4":
return 4
else:
print("Invalid input")
chooseType()
def getStartPosition():
for thing in things:
if thing[3] == 1:
return [thing[0], thing[1]]
def main():
global startPos, zoom, mapX, mapY
startPos = getStartPosition()
renderType = chooseType()
# init mouse movement and center mouse on screen
displayCenter = [500,300]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)
paused = False
run = True
clock = pygame.time.Clock()
curNode = len(glnodes)-1
print(curNode)
getTicksLastFrame=0
tt=0
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if event.key == pygame.K_UP or event.key == pygame.K_w:
mapY += 100
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
mapY -= 100
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
mapX += 100
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
mapX -= 100
if event.key == pygame.K_r:
mapX = 100
mapY = 500
if event.key == pygame.K_PLUS:
zoom -= 2
if event.key == pygame.K_MINUS:
zoom += 2
if event.key == pygame.K_RETURN:
renderType = chooseType()
if not paused:
# get keys
scree.fill((0,0,0))
keypress = pygame.key.get_pressed()
#mouseMove = pygame.mouse.get_rel()
if renderType==1 or renderType==3 or renderType==4:
drawWalls()
drawPlayerDot()
t = pygame.time.get_ticks()
deltaTime = (t - getTicksLastFrame) / 1000.0
getTicksLastFrame = t
tt+=deltaTime
if renderType==4:
if(tt>1):
if int(curNode & 0x8000):
tt=0
curNode = len(glnodes)-1
else:
curNode = searchForPlayer(glnodes[curNode])
tt=0
else:
if not int(curNode & 0x8000):
drawNode(glnodes[curNode])
#drawSegs()
#drawSubsectors()
#drawGLSubsectors()
#debug1(drawGLSubsector[76])
#drawSectors()
pygame.display.flip()
clock.tick()
pygame.display.set_caption("fps: " + str(clock.get_fps()))
pygame.quit()