Skip to content

Commit

Permalink
Fix diagonal movement speed
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Feb 9, 2024
1 parent d045bc2 commit 7ce1f0f
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions CSMaze/MazeGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,37 +572,32 @@ void FireGun()
float moveSpeedMod = Math.Min(1, frameTime * cfg.MoveSpeed * moveMultiplier);
// A set of events that occurred due to player movement
HashSet<MoveEvent> events = new();
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_W) || IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_UP))
if (!levels[currentLevel].Won && !levels[currentLevel].Killed)
{
if (!levels[currentLevel].Won && !levels[currentLevel].Killed)
Vector2 movementVector = new();
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_W) || IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_UP))
{
events.UnionWith(levels[currentLevel].MovePlayer(facingDirections[currentLevel] * moveSpeedMod, hasGun[currentLevel], true, cfg.EnableCollision));
hasStartedLevel[currentLevel] = true;
movementVector += Vector2.UnitX;
}
}
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_S) || IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_DOWN))
{
if (!levels[currentLevel].Won && !levels[currentLevel].Killed)
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_S) || IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_DOWN))
{
events.UnionWith(levels[currentLevel].MovePlayer(-facingDirections[currentLevel] * moveSpeedMod, hasGun[currentLevel], true, cfg.EnableCollision));
hasStartedLevel[currentLevel] = true;
movementVector -= Vector2.UnitX;
}
}
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_A))
{
if (!levels[currentLevel].Won && !levels[currentLevel].Killed)
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_A))
{
events.UnionWith(levels[currentLevel].MovePlayer(new Vector2(facingDirections[currentLevel].Y * moveSpeedMod, -facingDirections[currentLevel].X * moveSpeedMod),
hasGun[currentLevel], true, cfg.EnableCollision));
hasStartedLevel[currentLevel] = true;
movementVector -= Vector2.UnitY;
}
}
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_D))
{
if (!levels[currentLevel].Won && !levels[currentLevel].Killed)
if (IsKeyPressed(SDL.SDL_Scancode.SDL_SCANCODE_D))
{
movementVector += Vector2.UnitY;
}
movementVector = Vector2.Normalize(movementVector);
float facingAngle = (float)Math.Atan2(facingDirections[currentLevel].Y, facingDirections[currentLevel].X);
movementVector = Vector2.Transform(movementVector, Matrix3x2.CreateRotation(facingAngle));
movementVector *= moveSpeedMod;
if (movementVector.LengthSquared() > 0)
{
events.UnionWith(levels[currentLevel].MovePlayer(new Vector2(-facingDirections[currentLevel].Y * moveSpeedMod, facingDirections[currentLevel].X * moveSpeedMod),
hasGun[currentLevel], true, cfg.EnableCollision));
events.UnionWith(levels[currentLevel].MovePlayer(movementVector, hasGun[currentLevel], true, cfg.EnableCollision));
hasStartedLevel[currentLevel] = true;
}
}
Expand Down

0 comments on commit 7ce1f0f

Please sign in to comment.