235 lines
3.7 KiB
C++
235 lines
3.7 KiB
C++
#include "raylib.h"
|
|
#include <cmath>
|
|
|
|
extern bool IsSolidAt(Vector3 position, float radius);
|
|
|
|
Vector3 playerPosition = {-76.0f, 1.0f, -76.0f};
|
|
|
|
float verticalVelocity = 0.0f;
|
|
float playerYaw = 0.0f;
|
|
float playerPitch = 0.0f;
|
|
|
|
float weaponBobTime = 0.0f;
|
|
float weaponShootTime = 0.0f;
|
|
|
|
const float moveSpeed = 11.0f;
|
|
const float jumpForce = 9.0f;
|
|
const float gravity = 20.0f;
|
|
|
|
static const float PLAYER_RADIUS = 0.45f;
|
|
static const float PLAYER_GROUND_Y = 1.0f;
|
|
static const float PLAYER_HEAD_Y = 2.0f;
|
|
static const float CEILING_Y = 3.0f;
|
|
|
|
Camera3D GetPlayerCamera()
|
|
{
|
|
Camera3D camera = {};
|
|
|
|
camera.position = playerPosition;
|
|
|
|
Vector3 forward =
|
|
{
|
|
cosf(playerPitch) * cosf(playerYaw),
|
|
sinf(playerPitch),
|
|
cosf(playerPitch) * sinf(playerYaw)
|
|
};
|
|
|
|
camera.target =
|
|
{
|
|
camera.position.x + forward.x,
|
|
camera.position.y + forward.y,
|
|
camera.position.z + forward.z
|
|
};
|
|
|
|
camera.up = {0.0f, 1.0f, 0.0f};
|
|
camera.fovy = 75.0f;
|
|
camera.projection = CAMERA_PERSPECTIVE;
|
|
|
|
return camera;
|
|
}
|
|
|
|
static void MovePlayer(Vector3 movement)
|
|
{
|
|
Vector3 nextX = playerPosition;
|
|
nextX.x += movement.x;
|
|
|
|
if (!IsSolidAt(nextX, PLAYER_RADIUS))
|
|
playerPosition.x = nextX.x;
|
|
|
|
Vector3 nextZ = playerPosition;
|
|
nextZ.z += movement.z;
|
|
|
|
if (!IsSolidAt(nextZ, PLAYER_RADIUS))
|
|
playerPosition.z = nextZ.z;
|
|
}
|
|
|
|
void UpdatePlayer(float deltaTime)
|
|
{
|
|
Vector2 mouse = GetMouseDelta();
|
|
|
|
playerYaw += mouse.x * 0.003f;
|
|
playerPitch -= mouse.y * 0.003f;
|
|
|
|
if (playerPitch > 1.45f)
|
|
playerPitch = 1.45f;
|
|
|
|
if (playerPitch < -1.45f)
|
|
playerPitch = -1.45f;
|
|
|
|
Vector3 movement = {0.0f, 0.0f, 0.0f};
|
|
|
|
Vector3 forward =
|
|
{
|
|
cosf(playerYaw),
|
|
0.0f,
|
|
sinf(playerYaw)
|
|
};
|
|
|
|
Vector3 right =
|
|
{
|
|
-sinf(playerYaw),
|
|
0.0f,
|
|
cosf(playerYaw)
|
|
};
|
|
|
|
if (IsKeyDown(KEY_W))
|
|
{
|
|
movement.x += forward.x;
|
|
movement.z += forward.z;
|
|
}
|
|
|
|
if (IsKeyDown(KEY_S))
|
|
{
|
|
movement.x -= forward.x;
|
|
movement.z -= forward.z;
|
|
}
|
|
|
|
if (IsKeyDown(KEY_D))
|
|
{
|
|
movement.x += right.x;
|
|
movement.z += right.z;
|
|
}
|
|
|
|
if (IsKeyDown(KEY_A))
|
|
{
|
|
movement.x -= right.x;
|
|
movement.z -= right.z;
|
|
}
|
|
|
|
float movementLength =
|
|
sqrtf(
|
|
movement.x * movement.x +
|
|
movement.z * movement.z
|
|
);
|
|
|
|
if (movementLength > 0.0f)
|
|
{
|
|
movement.x /= movementLength;
|
|
movement.z /= movementLength;
|
|
|
|
MovePlayer({
|
|
movement.x * moveSpeed * deltaTime,
|
|
0.0f,
|
|
movement.z * moveSpeed * deltaTime
|
|
});
|
|
|
|
weaponBobTime += deltaTime * 10.0f;
|
|
}
|
|
|
|
bool grounded =
|
|
playerPosition.y <= PLAYER_GROUND_Y + 0.001f;
|
|
|
|
if (grounded)
|
|
{
|
|
playerPosition.y = PLAYER_GROUND_Y;
|
|
verticalVelocity = 0.0f;
|
|
|
|
if (IsKeyPressed(KEY_SPACE))
|
|
verticalVelocity = jumpForce;
|
|
}
|
|
|
|
verticalVelocity -= gravity * deltaTime;
|
|
|
|
playerPosition.y +=
|
|
verticalVelocity * deltaTime;
|
|
|
|
if (playerPosition.y < PLAYER_GROUND_Y)
|
|
{
|
|
playerPosition.y = PLAYER_GROUND_Y;
|
|
verticalVelocity = 0.0f;
|
|
}
|
|
|
|
if (playerPosition.y + PLAYER_HEAD_Y > CEILING_Y)
|
|
{
|
|
playerPosition.y =
|
|
CEILING_Y - PLAYER_HEAD_Y;
|
|
|
|
if (verticalVelocity > 0.0f)
|
|
verticalVelocity = 0.0f;
|
|
}
|
|
|
|
if (weaponShootTime > 0.0f)
|
|
weaponShootTime -= deltaTime;
|
|
}
|
|
|
|
void DrawPlayerWeapon()
|
|
{
|
|
float bobX = 0.0f;
|
|
float bobY = 0.0f;
|
|
|
|
bobX = sinf(weaponBobTime) * 5.0f;
|
|
bobY = fabsf(cosf(weaponBobTime)) * 4.0f;
|
|
|
|
if (weaponShootTime > 0.0f)
|
|
{
|
|
bobX += GetRandomValue(-4, 4);
|
|
bobY += GetRandomValue(-5, 5);
|
|
}
|
|
|
|
int w = GetScreenWidth();
|
|
int h = GetScreenHeight();
|
|
|
|
float x = w * 0.76f + bobX;
|
|
float y = h * 0.68f + bobY;
|
|
|
|
DrawRectangle(
|
|
(int)x,
|
|
(int)y,
|
|
190,
|
|
95,
|
|
DARKGRAY
|
|
);
|
|
|
|
DrawRectangle(
|
|
(int)x + 55,
|
|
(int)y - 35,
|
|
80,
|
|
55,
|
|
GRAY
|
|
);
|
|
|
|
DrawRectangle(
|
|
(int)x + 72,
|
|
(int)y - 62,
|
|
45,
|
|
40,
|
|
BLACK
|
|
);
|
|
|
|
DrawRectangle(
|
|
(int)x + 20,
|
|
(int)y + 30,
|
|
45,
|
|
65,
|
|
BLACK
|
|
);
|
|
|
|
DrawRectangle(
|
|
(int)x + 120,
|
|
(int)y + 15,
|
|
65,
|
|
35,
|
|
BLACK
|
|
);
|
|
}
|