WIP fps-independent movement speed

This commit is contained in:
Daniele Verducci 2023-01-11 19:50:37 +01:00
parent 33f7a62c0f
commit 6e7f560bc9

View File

@ -40,8 +40,8 @@ CEILING_COLOR = sdl2.ext.Color(0,128,255,255)
FLOOR_COLOR = sdl2.ext.Color(64,64,64,255) FLOOR_COLOR = sdl2.ext.Color(64,64,64,255)
# Player cfg # Player cfg
PLAYER_SPEED = 8 PLAYER_SPEED = 120
PLAYER_ROTATION_SPEED = 0.1 PLAYER_ROTATION_SPEED = 2.0
PLAYER_SPAWN_POSITION = {"x": 1.5, "y": 1.5, "r": 1.7} # r is rotation in radiants PLAYER_SPAWN_POSITION = {"x": 1.5, "y": 1.5, "r": 1.7} # r is rotation in radiants
# Dungeon data # Dungeon data
@ -113,7 +113,7 @@ class Main:
return return
def run(self): def run(self):
lastFpsCalcTime = 0 lastDraw = 0
frames = 0 frames = 0
running = True running = True
@ -124,16 +124,30 @@ class Main:
running = False running = False
break break
# Calculate speed based on FPS
elapsed = time.time() - lastDraw
player_speed = int(PLAYER_SPEED * elapsed)
player_rot_speed = PLAYER_ROTATION_SPEED * elapsed
lastDraw = time.time()
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.player_position["r"] - player_rot_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.player_position["r"] + player_rot_speed
# Fix rotation
if self.player_position["r"] < 0:
self.player_position["r"] = math.pi * 2 + self.player_position["r"]
if self.player_position["r"] > math.pi * 2:
self.player_position["r"] = self.player_position["r"] - math.pi * 2
# 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) + 1 # "+ 1": Adjust for rounding errors player_delta_x = math.cos(self.player_position["r"]) * player_speed
player_delta_y = math.sin(self.player_position["r"]) * PLAYER_SPEED player_delta_y = math.sin(self.player_position["r"]) * player_speed
print(self.player_position["r"])
# Move player based on its direction # Move player based on its direction
if keystate[sdl2.SDL_SCANCODE_UP]: if keystate[sdl2.SDL_SCANCODE_UP]:
@ -150,10 +164,6 @@ class Main:
self.player_position["y"] = 0 self.player_position["y"] = 0
if self.player_position["y"] > MAP_WIN_HEIGHT: if self.player_position["y"] > MAP_WIN_HEIGHT:
self.player_position["y"] = MAP_WIN_HEIGHT self.player_position["y"] = MAP_WIN_HEIGHT
if self.player_position["r"] > 2*math.pi:
self.player_position["r"] = 0
if self.player_position["r"] < 0:
self.player_position["r"] = 2*math.pi
# Open doors # Open doors
if keystate[sdl2.SDL_SCANCODE_SPACE]: if keystate[sdl2.SDL_SCANCODE_SPACE]:
@ -164,14 +174,6 @@ class Main:
self.mapWindow.refresh() self.mapWindow.refresh()
self.raycastWindow.refresh() self.raycastWindow.refresh()
# Calculate FPS
frames = frames + 1
if time.time() - lastFpsCalcTime > 1:
fps = frames/(time.time() - lastFpsCalcTime)
print(int(fps))
frames = 0
lastFpsCalcTime = time.time()
return 0 return 0
def movePlayerRelative(self, player_delta_x, player_delta_y): def movePlayerRelative(self, player_delta_x, player_delta_y):