Collision detection

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-05 11:03:52 +01:00
parent c1e6210c83
commit 37d4573778

View File

@ -20,6 +20,7 @@ PLAYER_SPEED = 10
PLAYER_ROTATION_SPEED = 0.17 PLAYER_ROTATION_SPEED = 0.17
RAY_LENGTH = 100 RAY_LENGTH = 100
MAP_SCALE = 40 MAP_SCALE = 40
COLLISION = True
MAP = [ MAP = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@ -28,9 +29,9 @@ MAP = [
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
@ -88,11 +89,9 @@ class Main:
# Move player based on its direction # Move player based on its direction
if event.key.keysym.sym == sdl2.SDLK_UP: if event.key.keysym.sym == sdl2.SDLK_UP:
self.player_position["y"] = int(self.player_position["y"] + player_delta_y) self.movePlayerRelative(player_delta_x, player_delta_y)
self.player_position["x"] = int(self.player_position["x"] + player_delta_x)
elif event.key.keysym.sym == sdl2.SDLK_DOWN: elif event.key.keysym.sym == sdl2.SDLK_DOWN:
self.player_position["y"] = int(self.player_position["y"] - player_delta_y) self.movePlayerRelative(-player_delta_x, -player_delta_y)
self.player_position["x"] = int(self.player_position["x"] - player_delta_x)
# Limit position into dungeon bounds # Limit position into dungeon bounds
if self.player_position["x"] < 0: if self.player_position["x"] < 0:
@ -122,6 +121,25 @@ class Main:
return 0 return 0
def movePlayerRelative(self, player_delta_x, player_delta_y):
# Prevent player from going into walls (X axis)
newPlayerX = int(self.player_position["x"] + player_delta_x)
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:
# Move player (X)
self.player_position["x"] = newPlayerX
# Prevent player from going into walls (Y axis)
newPlayerY = int(self.player_position["y"] + player_delta_y)
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:
# Move player (Y)
self.player_position["y"] = newPlayerY
def draw(self): def draw(self):
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
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,0,128,255), (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling)