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
|
#include "antifaShip.h"
#include "game.h"
// TODO: Get rid of some magic numbers.
void initAntifaShip(Entity * entity, Game * game) {
entity->model = &game->assets.models[ANTIFA_SHIP_ASSET];
// Acceleration stuff.
entity->useAcceleration = true;
entity->acceleration = (EntityAcceleration){
.speedUp = 30.0,
.speedDown = 15,
.rotation = (Vector3){2.0, 2.0, 2.0}
};
// Set Data pointer.
entity->data = KF_MALLOC(sizeof(AntifaShip));
if (entity->data == NULL) {
ALLOCATION_ERROR;
return;
}
AntifaShip * data = (AntifaShip*)entity->data;
data->lastMouse = Vector2Zero();
data->forwardSpeed = 0.0;
}
void closeAntifaShip(Entity * entity) {
if (entity->data != NULL)
KF_FREE(entity->data);
}
void controlAntifaShipJoystick(Game * game, Entity * entity) {
Vector3 stick = (Vector3){
GetGamepadAxisMovement(0, 1),
-GetGamepadAxisMovement(0, 0),
GetGamepadAxisMovement(0, 2) * 0.25
};
stick = Vector3Scale(stick, 0.5);
entityJoystickControl(entity, stick, fabs(GetGamepadAxisMovement(0, 3) * 300.0));
}
void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
AntifaShip * data = (AntifaShip*)entity->data;
// Get mouse values.
Vector2 mouse = GetMousePosition();
float speed = GetMouseWheelMove();
data->forwardSpeed += (speed * game->settings.scrollBarSpeed);
if (data->forwardSpeed < 0.0)
data->forwardSpeed = 0.0;
Vector2 v = Vector2Subtract(mouse, data->lastMouse);
data->lastMouse = mouse;
// Using mouse as a joystick.
Vector3 mouseStick = (Vector3){
(v.y / GetScreenHeight()) * game->settings.mouseSensitivity,
(-v.x / GetScreenWidth()) * game->settings.mouseSensitivity,
0.0,
};
// Swap axis for more movement with mouse.
if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) {
mouseStick.z = -mouseStick.y;
mouseStick.y = 0.0;
}
entityJoystickControl(entity, mouseStick, data->forwardSpeed);
}
void updateAntifaShip(Game * game, Entity * entity) {
switch (game->settings.controlMode) {
case JOYSTICK_CONTROL:
controlAntifaShipJoystick(game, entity);
break;
case KEYBOARD_AND_MOUSE_CONTROL:
controlAntifaShipKeyboardAndMouse(game, entity);
break;
default:
break;
}
}
void drawAntifaShip(Game * game, Entity * entity) {
entityDraw(entity);
}
|