-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.c
192 lines (138 loc) · 4.86 KB
/
game.c
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
//
// heligun: 3D flight sim shooter
// Copyright (c) 2016 Joseph Kuziel
//
// This software is MIT licensed.
//
#include "game.h"
#include "mathext.h"
#include <stdio.h>
// Globals
static Helicopter g_player;
float g_sunAngle;
// Constants
static const float HELI_X_LINEAR_WRAP = 4.0f;
static const float HELI_Y_LINEAR_WRAP = 4.0f;
static const float HELI_Z_LINEAR_MAX = 1.2f;
static const float HELI_LINEAR_DAMPING = 0.05f;
static const float HELI_X_LINEAR_ACCEL = 4.0f;
static const float HELI_Y_LINEAR_ACCEL = 3.0f;
static const float HELI_Z_LINEAR_ACCEL = 2.0f;
static const float HELI_X_ROTATION_DAMPING = 0.5f;
static const float HELI_Y_ROTATION_DAMPING = 0.5f;
static const float HELI_X_ROTATION_MAX = 0.7f;
static const float HELI_Y_ROTATION_MAX = 0.7f;
static const float HELI_ROTATIONAL_DAMPING = 0.05f;
static const float HELI_X_ROTATIONAL_ACCEL = 5.0f;
static const float HELI_Y_ROTATIONAL_ACCEL = 4.0f;
static const float HELI_Z_ROTATIONAL_ACCEL = 6.0f;
static const float SUN_ROTATIONAL_SPEED = 0.02f;
// Public Functions
void startGame() {
g_player.px1 = 0.0f;
g_player.py1 = 0.0f;
g_player.pz1 = 1.2f;
g_player.px2 = 0.0f;
g_player.py2 = 0.0f;
g_player.pz2 = 0.0f;
g_player.px3 = 0.0f;
g_player.py3 = 0.0f;
g_player.pz3 = 0.0f;
g_player.rx1 = 0.0f;
g_player.ry1 = 0.0f;
g_player.rz1 = 0.0f;
g_player.rx2 = 0.0f;
g_player.ry2 = 0.0f;
g_player.rz2 = 0.0f;
g_player.rx3 = 0.0f;
g_player.ry3 = 0.0f;
g_player.rz3 = 0.0f;
g_sunAngle = 0.0f;
}
void updateGame(float time_delta) {
// Rotational
g_player.rx2 += g_player.rx3 * time_delta;
g_player.ry2 += g_player.ry3 * time_delta;
g_player.rz2 += g_player.rz3 * time_delta;
g_player.rx2 -= g_player.rx2 * HELI_ROTATIONAL_DAMPING;
g_player.ry2 -= g_player.ry2 * HELI_ROTATIONAL_DAMPING;
g_player.rz2 -= g_player.rz2 * HELI_ROTATIONAL_DAMPING;
g_player.rx1 += g_player.rx2 * time_delta;
g_player.ry1 += g_player.ry2 * time_delta;
g_player.rz1 += g_player.rz2 * time_delta;
if(fabs(g_player.rx1) > HELI_X_ROTATION_DAMPING
&& (g_player.rx1 * g_player.rx2) > 0.0f) {
g_player.rx1 -=
SCALE(
fabs(g_player.rx1)
, HELI_X_ROTATION_DAMPING
, HELI_X_ROTATION_MAX
, 0.0f
, g_player.rx2
) * time_delta;
}
if(fabs(g_player.ry1) > HELI_Y_ROTATION_DAMPING
&& (g_player.ry1 * g_player.ry2) > 0.0f) {
g_player.ry1 -=
SCALE(
fabs(g_player.ry1)
, HELI_Y_ROTATION_DAMPING
, HELI_Y_ROTATION_MAX
, 0.0f
, g_player.ry2
) * time_delta;
}
g_player.rx1 = CLAMP(g_player.rx1,-HELI_X_ROTATION_MAX,HELI_X_ROTATION_MAX);
g_player.ry1 = CLAMP(g_player.ry1,-HELI_Y_ROTATION_MAX,HELI_Y_ROTATION_MAX);
g_player.rz1 = WRAPF(g_player.rz1, 0.0f, M_PI_2X);
// Linear
g_player.px3 =
sinf(g_player.rx1) * sinf(g_player.rz1) * HELI_Y_LINEAR_ACCEL
+ sinf(g_player.ry1) * cosf(g_player.rz1) * HELI_X_LINEAR_ACCEL;
g_player.py3 =
- sinf(g_player.rx1) * cosf(g_player.rz1) * HELI_Y_LINEAR_ACCEL
+ sinf(g_player.ry1) * sinf(g_player.rz1) * HELI_X_LINEAR_ACCEL;
g_player.px2 += g_player.px3 * time_delta;
g_player.py2 += g_player.py3 * time_delta;
g_player.pz2 += g_player.pz3 * time_delta;
g_player.px2 -= g_player.px2 * HELI_LINEAR_DAMPING;
g_player.py2 -= g_player.py2 * HELI_LINEAR_DAMPING;
g_player.pz2 -= g_player.pz2 * HELI_LINEAR_DAMPING;
g_player.px1 += g_player.px2 * time_delta;
g_player.py1 += g_player.py2 * time_delta;
g_player.pz1 += g_player.pz2 * time_delta;
g_player.px1 = WRAPF(g_player.px1, 0.0f, HELI_X_LINEAR_WRAP);
g_player.py1 = WRAPF(g_player.py1, 0.0f, HELI_Y_LINEAR_WRAP);
g_player.pz1 = CLAMP(g_player.pz1, 0.0f, HELI_Z_LINEAR_MAX);
g_sunAngle =WRAPF(g_sunAngle+SUN_ROTATIONAL_SPEED*time_delta, 0.0f,M_PI_2X);
}
void changePlayerThrottle(int v) {
if(v != 0) {
g_player.pz3 += (v > 0 ? 1.f : -1.f) * HELI_Z_LINEAR_ACCEL;
}
}
void rotatePlayer(int x, int y, int z) {
if(x != 0) {
g_player.rx3 += (x > 0 ? 1.f:-1.f) * HELI_X_ROTATIONAL_ACCEL;
}
if(y != 0) {
g_player.ry3 += (y > 0 ? 1.f:-1.f) * HELI_Y_ROTATIONAL_ACCEL;
}
if(z != 0) {
g_player.rz3 += (z > 0 ? 1.f:-1.f) * HELI_Z_ROTATIONAL_ACCEL;
}
}
void firePlayerBullets(int v) {
if(v > 0) {
printf("fire bullets\n");
}
}
void firePlayerMissle() {
printf("fire missile\n");
}
Helicopter getPlayer() {
return g_player;
}
float getSunAngle() {
return g_sunAngle;
}