WIP trying to implement floors

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-09 08:14:21 +01:00
parent 5b8b45e793
commit edad79cb9e

View File

@ -27,6 +27,7 @@ RAYCAST_RESOLUTION_SCALING = 4
RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RESOLUTION_SCALING)
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RESOLUTION_SCALING)
DOF = 2*MAP_SIZE # Depth Of Field
COLOR_CEILING = sdl2.ext.Color(0,0,128,255)
# Player cfg
PLAYER_SPEED = 8
@ -34,7 +35,7 @@ PLAYER_ROTATION_SPEED = 0.1
PLAYER_SPAWN_POSITION = {"x": int(MAP_SCALE * 2), "y": int(MAP_SCALE * 5), "r": 0} # r is rotation in radiants
# Dungeon data
MAP = [
MAP_WALLS = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
@ -53,6 +54,25 @@ MAP = [
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
]
MAP_FLOORS = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
]
TEXTURES = [
[
1, 1, 1, 1, 1, 1, 1, 1,
@ -90,8 +110,10 @@ class Main:
def __init__(self):
# Check valid map
if len(MAP) != MAP_SIZE * MAP_SIZE:
raise ValueError("Map size is {}, but should be a power of {}".format(len(MAP), MAP_SIZE))
if len(MAP_WALLS) != MAP_SIZE * MAP_SIZE:
raise ValueError("Walls map size is {}, but should be a power of {}".format(len(MAP_WALLS), MAP_SIZE))
if len(MAP_FLOORS) != MAP_SIZE * MAP_SIZE:
raise ValueError("Floors map size is {}, but should be a power of {}".format(len(MAP_FLOORS), MAP_SIZE))
# Graphics
sdl2.ext.init()
@ -124,9 +146,9 @@ class Main:
keystate = sdl2.SDL_GetKeyboardState(None)
# Rotate player
if keystate[sdl2.SDL_SCANCODE_LEFT]:
self.player_position["r"] = self.player_position["r"] - PLAYER_ROTATION_SPEED
self.player_position["r"] = self.angleBoudariesFix(self.player_position["r"] - PLAYER_ROTATION_SPEED)
elif keystate[sdl2.SDL_SCANCODE_RIGHT]:
self.player_position["r"] = self.player_position["r"] + PLAYER_ROTATION_SPEED
self.player_position["r"] = self.angleBoudariesFix(self.player_position["r"] + PLAYER_ROTATION_SPEED)
# Compute deltax and deltay based on player direction
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
@ -173,7 +195,7 @@ class Main:
mapX = int(newPlayerX / MAP_SCALE)
mapY = int(self.player_position["y"] / MAP_SCALE)
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_WALLS[mapArrayPosition] == 0:
# Move player (X)
self.player_position["x"] = newPlayerX
@ -182,7 +204,7 @@ class Main:
mapX = int(self.player_position["x"] / MAP_SCALE)
mapY = int(newPlayerY / MAP_SCALE)
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_WALLS[mapArrayPosition] == 0:
# Move player (Y)
self.player_position["y"] = newPlayerY
@ -206,28 +228,23 @@ class Main:
def draw2Dmap(self):
# 2D map
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen
for i in range(len(MAP)):
for i in range(len(MAP_WALLS)):
posX = i % MAP_SIZE * MAP_SCALE
posY = math.floor(i / MAP_SIZE) * MAP_SCALE
color = 0
if MAP[i] > 0:
if MAP_WALLS[i] > 0:
color = 255
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1))
def drawRays(self):
sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,0,128,255), (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling)
sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,128,0,255), (0, RAYCAST_WIN_HEIGHT/2, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws floor)
sdl2.ext.draw.fill(self.raycastSurface, COLOR_CEILING, (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling)
# Casts rays for raycasting
playerAngle = self.player_position["r"]
# Cast one ray for every window pixel, from -0,5 rads to +0,5 rads (about 60° viewing angle)
for i in range(RAYCAST_RENDER_WIDTH):
rayAngle = playerAngle + (i/RAYCAST_RENDER_WIDTH) - 0.5
if rayAngle < 0:
rayAngle = math.pi * 2 + rayAngle
if rayAngle > math.pi * 2:
rayAngle = rayAngle - math.pi * 2
rayAngle = self.angleBoudariesFix(playerAngle + (i/RAYCAST_RENDER_WIDTH) - 0.5)
# Which map wall tiles have been hit by rayX and rayY
mapBlockHitX = 0
@ -260,9 +277,9 @@ class Main:
mapX = int(rayX / MAP_SCALE)
mapY = int(rayY / MAP_SCALE)
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_WALLS[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitY = MAP[mapArrayPosition] # Save which map wall tile we reached
mapBlockHitY = MAP_WALLS[mapArrayPosition] # Save which map wall tile we reached
else:
# Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset
@ -302,9 +319,9 @@ class Main:
mapX = int(rayX / MAP_SCALE)
mapY = int(rayY / MAP_SCALE)
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_WALLS[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitX = MAP[mapArrayPosition] # Save which map wall tile we reached
mapBlockHitX = MAP_WALLS[mapArrayPosition] # Save which map wall tile we reached
else:
# Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset
@ -331,6 +348,7 @@ class Main:
# Center line vertically in window
lineOffset = RAYCAST_RENDER_HEIGHT / 2 - lineHeight / 2
# WALLS
# Draw pixels vertically from top to bottom to obtain a line
textureSegmentEnd = 0
for textureColumnPixel in range(0, TEXTURE_SIZE):
@ -363,14 +381,43 @@ class Main:
lineStart = textureSegmentStart
if lineStart < 0:
lineStart = 0
# Upscaling
lineStart = lineStart * RAYCAST_RESOLUTION_SCALING
lineEnd = lineEnd * RAYCAST_RESOLUTION_SCALING
# Draw segment
self.drawVertLineScaled(color, i, int(lineStart), int(lineEnd))
# FLOOR
if lineEnd >= RAYCAST_WIN_HEIGHT:
continue
wallLineEnd = int(lineEnd)
for y in range(wallLineEnd, RAYCAST_RENDER_HEIGHT):
dy = y - ( RAYCAST_WIN_HEIGHT / 2.0 )
deg = rayAngle
raFix = math.cos(self.angleBoudariesFix(playerAngle - rayAngle))
tx = self.player_position["x"] / 2 + math.cos(deg) * (RAYCAST_RENDER_WIDTH / 2) * TEXTURE_SIZE / dy / raFix
ty = self.player_position["y"] / 2 - math.sin(deg) * (RAYCAST_RENDER_HEIGHT / 2) * TEXTURE_SIZE / dy / raFix
textureIndex = MAP_FLOORS[int(ty / TEXTURE_SIZE) * mapX + int(tx / TEXTURE_SIZE)] #* TEXTURE_SIZE * TEXTURE_SIZE
texturePixelIndex = int(ty * TEXTURE_SIZE + tx)
print("Texture index: {} pixel: {}".format(textureIndex, texturePixelIndex))
color = TEXTURES[textureIndex][texturePixelIndex]
self.drawPointScaled(color, i, y)
def angleBoudariesFix(self, angle):
if angle < 0:
return math.pi * 2 + angle
if angle > math.pi * 2:
return angle - math.pi * 2
def drawVertLineScaled(self, color, i, startY, endY):
# Upscaling
startY = startY * RAYCAST_RESOLUTION_SCALING
endY = endY * RAYCAST_RESOLUTION_SCALING
for repeat in range(1, RAYCAST_RESOLUTION_SCALING + 1):
x = i * RAYCAST_RESOLUTION_SCALING + repeat
sdl2.ext.draw.line(self.raycastSurface, color, (x, int(lineStart), x, int(lineEnd)))
sdl2.ext.draw.line(self.raycastSurface, color, (x, startY, x, endY))
def drawPointScaled(self, color, x, y):
scaledX = int(x * RAYCAST_RESOLUTION_SCALING)
scaledY = int(y * RAYCAST_RESOLUTION_SCALING)
sdl2.ext.draw.fill(self.raycastSurface, color, (scaledX, scaledY, scaledX + RAYCAST_RESOLUTION_SCALING, scaledY + RAYCAST_RESOLUTION_SCALING))
def dist(self, ax, ay, bx, by):
return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))