aboutsummaryrefslogtreecommitdiff
path: root/player.c
blob: 52788f107cf43eee2cac609e4ce81eb3f2a0a7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "all.h"
#include "raymath.h"

#include <float.h>

static void PlayerRotate(void) {
	if (!game.cursor_captured) return;

	Vector2 mouseDelta = GetMouseDelta();
	game.player.yaw += -mouseDelta.x * PLAYER_MOUSE_SENSITIVITY;
	game.player.pitch += mouseDelta.y * PLAYER_MOUSE_SENSITIVITY;

	if (game.player.pitch > 89.0f * DEG2RAD) game.player.pitch = 89.0f * DEG2RAD;
	if (game.player.pitch < -89.0f * DEG2RAD) game.player.pitch = -89.0f * DEG2RAD;
}

static void MoveNormal(float dt) {
	float eyeHeight = PLAYER_EYE_HEIGHT - (game.player.crouch_amount * PLAYER_CROUCH_OFFSET);

	Vector3 forward = { sinf(game.player.yaw), 0, cosf(game.player.yaw) };
	Vector3 right = { sinf(game.player.yaw - PI/2.0f), 0, cosf(game.player.yaw - PI/2.0f) };

	Vector3 moveDir = { 0 };
	if (IsKeyDown(KEY_W)) moveDir = Vector3Add(moveDir, forward);
	if (IsKeyDown(KEY_S)) moveDir = Vector3Subtract(moveDir, forward);
	if (IsKeyDown(KEY_D)) moveDir = Vector3Add(moveDir, right);
	if (IsKeyDown(KEY_A)) moveDir = Vector3Subtract(moveDir, right);

	float speed = PLAYER_MOVE_SPEED;
	if (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT)) speed *= PLAYER_SPRINT_MULTIPLIER;
	speed *= Lerp(1.0f, PLAYER_CROUCH_MOVE_MULTIPLIER, game.player.crouch_amount);

	if (Vector3Length(moveDir) > 0) {
		moveDir = Vector3Scale(Vector3Normalize(moveDir), speed * dt);
	}

	game.player.velocity.y -= PLAYER_GRAVITY * dt;

	if (game.player.is_grounded && IsKeyPressed(KEY_SPACE)) {
		game.player.velocity.y = PLAYER_JUMP_FORCE;
		game.player.is_grounded = false;
	}

	Vector3 remainingMove = moveDir;
	for (int iter = 0; iter < 4 && Vector3Length(remainingMove) > 0.001f; iter++) {
		RayCollision closestHit = { 0 };
		closestHit.distance = FLT_MAX;
		bool hitFound = false;

		float heights[] = { -eyeHeight + 10.0f, -eyeHeight / 2.0f, 0.0f };
		Vector3 currentPos = game.player.pos;

		for (int i = 0; i < 3; i++) {
			Vector3 start = Vector3Add(currentPos, (Vector3){0, heights[i], 0});
			Vector3 dir = Vector3Normalize(remainingMove);
			float dist = Vector3Length(remainingMove) + PLAYER_RADIUS;
			Vector3 end = Vector3Add(start, Vector3Scale(dir, dist));

			RayCollision col;
			if (CheckMapCollision(start, end, &col)) {
				float adjustedDist = col.distance - PLAYER_RADIUS;
				if (adjustedDist < closestHit.distance) {
					closestHit = col;
					closestHit.distance = adjustedDist;
					hitFound = true;
				}
			}
		}

		if (hitFound) {
			float moveDist = fmaxf(0, closestHit.distance - 0.1f);
			Vector3 moveStep = Vector3Scale(Vector3Normalize(remainingMove), moveDist);
			game.player.pos.x += moveStep.x;
			game.player.pos.z += moveStep.z;

			Vector3 slideNormal = { closestHit.normal.x, 0, closestHit.normal.z };
			if (Vector3Length(slideNormal) > 0.001f) {
				slideNormal = Vector3Normalize(slideNormal);
				remainingMove = Vector3Subtract(remainingMove, moveStep);
				float dot = Vector3DotProduct(remainingMove, slideNormal);
				remainingMove = Vector3Subtract(remainingMove, Vector3Scale(slideNormal, dot));
			} else {
				remainingMove = (Vector3){0, 0, 0};
			}
		} else {
			game.player.pos.x += remainingMove.x;
			game.player.pos.z += remainingMove.z;
			break;
		}
	}

	float verticalMove = game.player.velocity.y * dt;
	Vector3 vStart = game.player.pos;

	if (verticalMove < 0) {
		Vector3 start = vStart;
		Vector3 end = Vector3Add(vStart, (Vector3){0, verticalMove - eyeHeight, 0});
		RayCollision vCol;
		if (CheckMapCollision(start, end, &vCol) && vCol.normal.y > 0.5f) {
			game.player.pos.y = vCol.point.y + eyeHeight;
			game.player.velocity.y = 0;
			game.player.is_grounded = true;
		} else {
			game.player.pos.y += verticalMove;
			game.player.is_grounded = false;
		}
	} else if (verticalMove > 0) {
		Vector3 start = vStart;
		float headHeight = PLAYER_HEIGHT - PLAYER_EYE_HEIGHT;
		Vector3 end = Vector3Add(vStart, (Vector3){0, verticalMove + headHeight, 0});
		RayCollision vCol;
		if (CheckMapCollision(start, end, &vCol)) {
			game.player.pos.y = vCol.point.y - headHeight - 1.0f;
			game.player.velocity.y = 0;
		} else {
			game.player.pos.y += verticalMove;
		}
		game.player.is_grounded = false;
	} else {
		Vector3 start = vStart;
		Vector3 end = Vector3Add(vStart, (Vector3){0, -eyeHeight - 2.0f, 0});
		RayCollision vCol;
		if (CheckMapCollision(start, end, &vCol) && vCol.normal.y > 0.5f) {
			game.player.is_grounded = true;
			if (vCol.distance < eyeHeight + 1.0f) {
				game.player.pos.y = vCol.point.y + eyeHeight;
			}
		} else {
			game.player.is_grounded = false;
		}
	}
}

static void MoveFly(float dt) {
	Vector3 forward = { 
		cosf(game.player.pitch) * sinf(game.player.yaw), 
		-sinf(game.player.pitch), 
		cosf(game.player.pitch) * cosf(game.player.yaw) 
	};
	Vector3 right = { sinf(game.player.yaw - PI/2.0f), 0, cosf(game.player.yaw - PI/2.0f) };

	Vector3 move = { 0 };
	if (IsKeyDown(KEY_W)) move = Vector3Add(move, forward);
	if (IsKeyDown(KEY_S)) move = Vector3Subtract(move, forward);
	if (IsKeyDown(KEY_D)) move = Vector3Add(move, right);
	if (IsKeyDown(KEY_A)) move = Vector3Subtract(move, right);

	if (IsKeyDown(KEY_SPACE)) move.y += 1.0f;
	if (IsKeyDown(KEY_LEFT_CONTROL)) move.y -= 1.0f;

	float speed = PLAYER_FLY_SPEED;
	if (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT)) speed *= PLAYER_SPRINT_MULTIPLIER;

	if (Vector3Length(move) > 0) {
		move = Vector3Scale(Vector3Normalize(move), speed * dt);
		game.player.pos = Vector3Add(game.player.pos, move);
	}

	game.player.velocity = (Vector3){0, 0, 0};
	game.player.is_grounded = false;
}

void UpdatePlayer(void) {
	float dt = GetFrameTime();
	Vector3 oldPos = game.player.pos;

	if (IsKeyPressed(KEY_F)) {
		game.player.move_mode = (game.player.move_mode == MOVE_NORMAL) ? MOVE_FLY : MOVE_NORMAL;
	}

	PlayerRotate();

	bool canStand = true;
	if (game.player.crouch_amount > 0.1f) {
		Vector3 headPos = game.player.pos;
		Vector3 headEnd = Vector3Add(headPos, (Vector3){0, PLAYER_HEIGHT - PLAYER_EYE_HEIGHT + PLAYER_CROUCH_OFFSET, 0});
		RayCollision col;
		if (CheckMapCollision(headPos, headEnd, &col)) {
			canStand = false;
		}
	}

	float targetCrouch = 0.0f;
	if (IsKeyDown(KEY_LEFT_CONTROL) || !canStand) targetCrouch = 1.0f;
	game.player.crouch_amount = Lerp(game.player.crouch_amount, targetCrouch, PLAYER_CROUCH_SPEED * dt);

	float targetLean = 0.0f;
	if (IsKeyDown(KEY_Q)) targetLean -= 1.0f;
	if (IsKeyDown(KEY_E)) targetLean += 1.0f;
	game.player.lean_amount = Lerp(game.player.lean_amount, targetLean, PLAYER_LEAN_SPEED * dt);

	if (game.player.move_mode == MOVE_FLY) {
		MoveFly(dt);
	} else {
		MoveNormal(dt);
	}

	float leanAngle = game.player.lean_amount * PLAYER_LEAN_ANGLE * DEG2RAD;
	Vector3 bodyForward = { sinf(game.player.yaw), 0, cosf(game.player.yaw) };

	float currentEyeHeight = PLAYER_EYE_HEIGHT - (game.player.crouch_amount * PLAYER_CROUCH_OFFSET);
	float pivotDist = fminf(PLAYER_LEAN_PIVOT_DISTANCE, currentEyeHeight * 0.8f);

	Vector3 neckToEye = { 0, pivotDist, 0 };
	Vector3 rotatedOffset = Vector3RotateByAxisAngle(neckToEye, bodyForward, leanAngle);
	Vector3 pivot = Vector3Subtract(game.player.pos, (Vector3){ 0, pivotDist, 0 });

	game.camera.position = Vector3Add(pivot, rotatedOffset);

	Vector3 forward = {
		cosf(game.player.pitch) * sinf(game.player.yaw),
		-sinf(game.player.pitch),
		cosf(game.player.pitch) * cosf(game.player.yaw)
	};

	Vector3 up = { 0, 1, 0 };
	up = Vector3RotateByAxisAngle(up, forward, leanAngle);
	game.camera.up = up;
	game.camera.target = Vector3Add(game.camera.position, Vector3Scale(forward, 20.0f));

	if (dt > 0) {
		Vector2 velH = { game.player.pos.x - oldPos.x, game.player.pos.z - oldPos.z };
		game.player.horizontal_speed = Vector2Length(velH) / dt;
	} else {
		game.player.horizontal_speed = 0;
	}
}