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, 1, 1, 0,
1, 0, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
],
] ]
TEXTURE_SIZE = 8 TEXTURE_SIZE = 8
@ -178,7 +190,7 @@ class Main:
posX = i % MAP_SIZE * MAP_SCALE posX = i % MAP_SIZE * MAP_SCALE
posY = math.floor(i / MAP_SIZE) * MAP_SCALE posY = math.floor(i / MAP_SIZE) * MAP_SCALE
color = 0 color = 0
if MAP[i] == 1: if MAP[i] > 0:
color = 255 color = 255
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1)) sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1))
@ -194,6 +206,10 @@ class Main:
if rayAngle > math.pi * 2: if rayAngle > math.pi * 2:
rayAngle = rayAngle - math.pi * 2 rayAngle = rayAngle - math.pi * 2
# Which map wall tiles have been hit by rayX and rayY
mapBlockHitX = 0
mapBlockHitY = 0
# Check horizontal lines # Check horizontal lines
dof = 0 # Depth of field dof = 0 # Depth of field
if rayAngle == 0 or rayAngle == math.pi: if rayAngle == 0 or rayAngle == math.pi:
@ -223,6 +239,7 @@ class Main:
mapArrayPosition = mapY * MAP_SIZE + mapX mapArrayPosition = mapY * MAP_SIZE + mapX
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0: if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitY = MAP[mapArrayPosition] # Save which map wall tile we reached
else: else:
# Didn't hit the wall: check successive horizontal line # Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset rayX = rayX + xOffset
@ -264,6 +281,7 @@ class Main:
mapArrayPosition = mapY * MAP_SIZE + mapX mapArrayPosition = mapY * MAP_SIZE + mapX
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0: if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitX = MAP[mapArrayPosition] # Save which map wall tile we reached
else: else:
# Didn't hit the wall: check successive horizontal line # Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset rayX = rayX + xOffset
@ -297,13 +315,15 @@ class Main:
textureSegmentEnd = textureSegmentStart + textureSegmentLength textureSegmentEnd = textureSegmentStart + textureSegmentLength
# Obtain texture value in the pixel representing the current segment and calculate shading # Obtain texture value in the pixel representing the current segment and calculate shading
if vertDist > horizDist: if vertDist > horizDist:
texIndex = mapBlockHitY - 1 # The texture covering the selected map tile (0 is no texture, 1 is texture at TEXTURES[0] etc)
texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE) texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
shading = 127 shading = 127
else: else:
texIndex = mapBlockHitX - 1 # The texture covering the selected map tile
texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE) texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
shading = 255 shading = 255
texel = TEXTURE[texColumn + textureColumnPixel * TEXTURE_SIZE] texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
# Calculate color resulting from texture pixel value + shading # Calculate color resulting from texture pixel value + shading
color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255) color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255)
# Clipping # Clipping