WIP trying to implement floors
This commit is contained in:
parent
5b8b45e793
commit
edad79cb9e
@ -27,6 +27,7 @@ RAYCAST_RESOLUTION_SCALING = 4
|
|||||||
RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RESOLUTION_SCALING)
|
RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RESOLUTION_SCALING)
|
||||||
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RESOLUTION_SCALING)
|
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RESOLUTION_SCALING)
|
||||||
DOF = 2*MAP_SIZE # Depth Of Field
|
DOF = 2*MAP_SIZE # Depth Of Field
|
||||||
|
COLOR_CEILING = sdl2.ext.Color(0,0,128,255)
|
||||||
|
|
||||||
# Player cfg
|
# Player cfg
|
||||||
PLAYER_SPEED = 8
|
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
|
PLAYER_SPAWN_POSITION = {"x": int(MAP_SCALE * 2), "y": int(MAP_SCALE * 5), "r": 0} # r is rotation in radiants
|
||||||
|
|
||||||
# Dungeon data
|
# Dungeon data
|
||||||
MAP = [
|
MAP_WALLS = [
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
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,
|
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,
|
3, 3,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 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 = [
|
TEXTURES = [
|
||||||
[
|
[
|
||||||
1, 1, 1, 1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
@ -90,8 +110,10 @@ class Main:
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Check valid map
|
# Check valid map
|
||||||
if len(MAP) != MAP_SIZE * MAP_SIZE:
|
if len(MAP_WALLS) != MAP_SIZE * MAP_SIZE:
|
||||||
raise ValueError("Map size is {}, but should be a power of {}".format(len(MAP), 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
|
# Graphics
|
||||||
sdl2.ext.init()
|
sdl2.ext.init()
|
||||||
@ -124,9 +146,9 @@ class Main:
|
|||||||
keystate = sdl2.SDL_GetKeyboardState(None)
|
keystate = sdl2.SDL_GetKeyboardState(None)
|
||||||
# Rotate player
|
# Rotate player
|
||||||
if keystate[sdl2.SDL_SCANCODE_LEFT]:
|
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]:
|
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
|
# Compute deltax and deltay based on player direction
|
||||||
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
|
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
|
||||||
@ -173,7 +195,7 @@ class Main:
|
|||||||
mapX = int(newPlayerX / MAP_SCALE)
|
mapX = int(newPlayerX / MAP_SCALE)
|
||||||
mapY = int(self.player_position["y"] / MAP_SCALE)
|
mapY = int(self.player_position["y"] / MAP_SCALE)
|
||||||
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_WALLS[mapArrayPosition] == 0:
|
||||||
# Move player (X)
|
# Move player (X)
|
||||||
self.player_position["x"] = newPlayerX
|
self.player_position["x"] = newPlayerX
|
||||||
|
|
||||||
@ -182,7 +204,7 @@ class Main:
|
|||||||
mapX = int(self.player_position["x"] / MAP_SCALE)
|
mapX = int(self.player_position["x"] / MAP_SCALE)
|
||||||
mapY = int(newPlayerY / MAP_SCALE)
|
mapY = int(newPlayerY / MAP_SCALE)
|
||||||
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_WALLS[mapArrayPosition] == 0:
|
||||||
# Move player (Y)
|
# Move player (Y)
|
||||||
self.player_position["y"] = newPlayerY
|
self.player_position["y"] = newPlayerY
|
||||||
|
|
||||||
@ -206,28 +228,23 @@ class Main:
|
|||||||
def draw2Dmap(self):
|
def draw2Dmap(self):
|
||||||
# 2D map
|
# 2D map
|
||||||
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen
|
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
|
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] > 0:
|
if MAP_WALLS[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))
|
||||||
|
|
||||||
def drawRays(self):
|
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, COLOR_CEILING, (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)
|
|
||||||
|
|
||||||
# Casts rays for raycasting
|
# Casts rays for raycasting
|
||||||
playerAngle = self.player_position["r"]
|
playerAngle = self.player_position["r"]
|
||||||
|
|
||||||
# Cast one ray for every window pixel, from -0,5 rads to +0,5 rads (about 60° viewing angle)
|
# 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):
|
for i in range(RAYCAST_RENDER_WIDTH):
|
||||||
rayAngle = playerAngle + (i/RAYCAST_RENDER_WIDTH) - 0.5
|
rayAngle = self.angleBoudariesFix(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
|
|
||||||
|
|
||||||
# Which map wall tiles have been hit by rayX and rayY
|
# Which map wall tiles have been hit by rayX and rayY
|
||||||
mapBlockHitX = 0
|
mapBlockHitX = 0
|
||||||
@ -260,9 +277,9 @@ class Main:
|
|||||||
mapX = int(rayX / MAP_SCALE)
|
mapX = int(rayX / MAP_SCALE)
|
||||||
mapY = int(rayY / MAP_SCALE)
|
mapY = int(rayY / MAP_SCALE)
|
||||||
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_WALLS[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
|
mapBlockHitY = MAP_WALLS[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
|
||||||
@ -302,9 +319,9 @@ class Main:
|
|||||||
mapX = int(rayX / MAP_SCALE)
|
mapX = int(rayX / MAP_SCALE)
|
||||||
mapY = int(rayY / MAP_SCALE)
|
mapY = int(rayY / MAP_SCALE)
|
||||||
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_WALLS[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
|
mapBlockHitX = MAP_WALLS[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
|
||||||
@ -331,6 +348,7 @@ class Main:
|
|||||||
# Center line vertically in window
|
# Center line vertically in window
|
||||||
lineOffset = RAYCAST_RENDER_HEIGHT / 2 - lineHeight / 2
|
lineOffset = RAYCAST_RENDER_HEIGHT / 2 - lineHeight / 2
|
||||||
|
|
||||||
|
# WALLS
|
||||||
# Draw pixels vertically from top to bottom to obtain a line
|
# Draw pixels vertically from top to bottom to obtain a line
|
||||||
textureSegmentEnd = 0
|
textureSegmentEnd = 0
|
||||||
for textureColumnPixel in range(0, TEXTURE_SIZE):
|
for textureColumnPixel in range(0, TEXTURE_SIZE):
|
||||||
@ -363,14 +381,43 @@ class Main:
|
|||||||
lineStart = textureSegmentStart
|
lineStart = textureSegmentStart
|
||||||
if lineStart < 0:
|
if lineStart < 0:
|
||||||
lineStart = 0
|
lineStart = 0
|
||||||
# Upscaling
|
|
||||||
lineStart = lineStart * RAYCAST_RESOLUTION_SCALING
|
|
||||||
lineEnd = lineEnd * RAYCAST_RESOLUTION_SCALING
|
|
||||||
# Draw segment
|
# 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):
|
for repeat in range(1, RAYCAST_RESOLUTION_SCALING + 1):
|
||||||
x = i * RAYCAST_RESOLUTION_SCALING + repeat
|
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):
|
def dist(self, ax, ay, bx, by):
|
||||||
return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))
|
return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))
|
||||||
|
Loading…
Reference in New Issue
Block a user