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
|
#include "player.h"
#include "game.h"
Player createPlayer()
{
return (Player){
.position = Vector3Zero(),
.direction = (Vector3){0.0, 0.0, 0.0},
.velocity = Vector3Zero(),
.camera = (Camera){
.position = (Vector3){0.0, 0.0, 0.0},
.target = Vector3Zero(),
.up = (Vector3){0.0, 1.0, 0.0},
.fovy = 90.0,
.projection = CAMERA_PERSPECTIVE
},
.cameraAngle = Vector2Zero()
};
}
// Fake physics, distance from ground.
void updatePlayerHeight(Player* player, Game* game)
{
float height = getWorldHeightAtLocation(
&game->world, player->position.x, player->position.z) + PLAYER_HEIGHT;
player->position.y = height;
}
void updatePlayerLookingAround(Player* player, Game* game)
{
Settings* settings = &game->settings;
Vector2* cameraAngle = &player->cameraAngle;
// Get mouse speed.
Vector2 mouseSpeed = Vector2Scale(GetMouseDelta(), settings->mouseSpeed);
mouseSpeed = Vector2Scale(mouseSpeed, 1.0 / (PI * 2.0));
// Update camera angle.
*cameraAngle = Vector2Add(*cameraAngle, mouseSpeed);
cameraAngle->x = Wrap(cameraAngle->x, -PI, PI);
cameraAngle->y = Clamp(cameraAngle->y, -PI / 2.5, PI / 2.5);
// Set player direction.
Matrix matrix = MatrixRotateXYZ(
(Vector3){-cameraAngle->y, cameraAngle->x, 0.0});
player->direction = (Vector3){matrix.m2, matrix.m6, matrix.m10};
}
void updatePlayerMovement(Player* player, Game* game)
{
Camera* camera = &player->camera;
Vector2* cameraAngle = &player->cameraAngle;
Settings* settings = &game->settings;
if (!game->isCursorEnabled)
{
updatePlayerLookingAround(player, game);
}
// Walking around.
player->velocity = Vector3Zero();
if (IsKeyDown(settings->forwardKey))
{
player->velocity.z += cosf(cameraAngle->x);
player->velocity.x += -sinf(cameraAngle->x);
}
if (IsKeyDown(settings->leftKey))
{
player->velocity.z += cosf(cameraAngle->x - (PI / 2.0));
player->velocity.x += -sinf(cameraAngle->x - (PI / 2.0));
}
if (IsKeyDown(settings->backwardKey))
{
player->velocity.z += -cosf(cameraAngle->x);
player->velocity.x += sinf(cameraAngle->x);
}
if (IsKeyDown(settings->rightKey))
{
player->velocity.z += cosf(cameraAngle->x + (PI / 2.0));
player->velocity.x += -sinf(cameraAngle->x + (PI / 2.0));
}
player->velocity = Vector3Scale(player->velocity, PLAYER_SPEED);
// Apply velocity.
player->position = Vector3Add(
player->position, Vector3Scale(player->velocity, GetFrameTime()));
updatePlayerHeight(player, game);
// Apply camera.
camera->position = player->position;
camera->target = Vector3Add(player->position, player->direction);
}
bool playerCanEntityBeSelected(Player* player, Entity entity)
{
return getEntityDistance(entity, player->position)
<= PLAYER_MAX_SELECT_DISTANCE;
}
void playerInteractWithEntity(Player* player, Entity* entity, Game* game)
{
printf("%d\n", interactWithEntity(entity, game, SELECTION_INTERACT));
}
void playerUpdateSelectedEntity(Player* player, Entity* entity, Game* game)
{
if (!playerCanEntityBeSelected(player, *entity))
{
return;
}
DrawBoundingBox(entity->box, RED);
if (IsKeyPressed(game->settings.interactKey))
{
playerInteractWithEntity(player, entity, game);
}
}
// TODO: move magic numbers to settings
void updatePlayer(Player* player, Game* game)
{
updatePlayerMovement(player, game);
Ray ray = (Ray){
.position = player->position,
.direction = player->direction
};
if (game->isCrossHairEnabled)
{
DrawRay(ray, YELLOW);
}
WorldUID uid = castRayAtWorld(&game->world, ray, false, NULL);
if (uid != -1)
{
playerUpdateSelectedEntity(player, &game->world.entities[uid], game);
}
}
|