-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
172 lines (158 loc) · 3.63 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
/**
* Ultrafancy "metasphere" effect.
*/
#include "tools.h"
#include <stdio.h>
#include <fcntl.h>
#include "Vector.h"
#include "game.h"
//debug messages to shell
//#define GAME_DEBUG
int time = 0;
player_t p[2];
typedef struct updateData
{
Vector oldPos;
Vector movDir;
Vector rotDir;
} updateData_t;
void gameInit(void) {
p[0].pos = MakeVector( 1.0, 0.0, 0.0 );
p[1].pos = MakeVector( -1.0, 0.0, 0.0 );
for(int i = 0; i < 2; i++)
{
p[i].life = 0.3;
p[i].input.forwards = 0;
p[i].input.backwards = 0;
p[i].input.left = 0;
p[i].input.right = 0;
p[i].input.shoot = 0;
p[i].input.dodgeUp = 0;
p[i].input.dodgeDown = 0;
p[i].dodgeTime = 0;
p[i].dodgeAcc = 0;
p[i].shotTime = 0;
p[i].shotInd = 0;
for(int j = 0; j < 200; j++)
{
p[i].shotAct[j] = 0;
}
}
}
// Called every 10 ms
//calculates the new position of each game relevant object, i.e. player and shots
int gameUpdate(void)
{
time++;
updateData_t data[2];
// Save some important values.
for(int i = 0; i < 2; i++)
{
data[i].oldPos = p[i].pos;
p[i].pos.y = 0;
}
Scalar dist = VectorDistance(p[0].pos, p[1].pos);
for(int i = 0; i < 2; i++)
{
data[i].movDir = VectorNorm(VectorSub(p[!i].pos, p[i].pos));
data[i].rotDir = VectorNorm(VectorCross(data[i].movDir, YVector));
}
// Rotational movement.
for(int i = 0; i < 2; i++)
{
if(p[i].input.right)
{
p[i].pos = VectorAdd(p[i].pos, VectorMul(data[i].rotDir, 0.05));
}
if(p[i].input.left)
{
p[i].pos = VectorSub(p[i].pos, VectorMul(data[i].rotDir, 0.05));
}
p[i].pos = VectorAdd(p[i].pos, VectorMul(data[i].movDir, VectorDistance(p[i].pos, p[!i].pos) - dist));
}
for(int i = 0; i < 2; i++)
{
// Axis movement.
if(p[i].input.forwards)
{
p[i].pos = VectorAdd(p[i].pos, VectorMul(data[i].movDir, 0.05));
}
if(p[i].input.backwards)
{
p[i].pos = VectorSub(p[i].pos, VectorMul(data[i].movDir, 0.05));
}
// Dodge movement
p[i].pos.y = data[i].oldPos.y;
if(time - p[i].dodgeTime > 60)
{
if(p[i].input.dodgeUp)
{
p[i].dodgeTime = time;
p[i].dodgeAcc = 0.15;
}
else if(p[i].input.dodgeDown)
{
p[i].dodgeTime = time;
p[i].dodgeAcc = -0.15;
}
else
{
p[i].pos.y = 0;
p[i].dodgeAcc = 0;
}
}
else
{
p[i].dodgeAcc = (p[i].dodgeAcc + 0.01 * sgn(p[i].pos.y)) * 0.93;
p[i].pos.y = p[i].pos.y - p[i].dodgeAcc;
}
}
// Check out-of-bounds.
for(int i = 0; i < 2; i++)
{
if(VectorDistance(p[i].pos, ZeroVector ) > 10.0)
{
Scalar sd = VectorDistance(p[i].pos, ZeroVector) - 10.0;
p[i].pos = VectorSub(p[i].pos, VectorMul(VectorNorm(VectorSub(p[i].pos, ZeroVector)), sd));
}
}
if(VectorDistance(p[0].pos, p[1].pos) < 1)
{
float abd = (VectorDistance(p[0].pos, p[1].pos) - 1) / 2;
for(int i = 0; i < 2; i++) {p[i].pos = VectorAdd(p[i].pos, VectorMul(data[i].movDir, abd));}
}
for(int i = 0; i < 2; i++)
{
// Shoot
data[i].movDir = VectorNorm(VectorSub(p[!i].pos, p[i].pos));
if(p[i].input.shoot && time - p[i].shotTime > 2 )
{
p[i].shot[p[i].shotInd] = p[i].pos;
p[i].shotDir[p[i].shotInd] = VectorMul(data[i].movDir, 0.1);
p[i].shotAct[p[i].shotInd] = 1;
p[i].shotInd = (p[i].shotInd + 1) % SHOTS_NUM;
p[i].shotTime = time;
}
// Move bullets
for(int j = 0; j < SHOTS_NUM; j++ ) {
if(p[i].shotAct[j])
{
p[i].shot[j] = VectorAdd(p[i].shot[j], p[i].shotDir[j]);
if(VectorDistance(p[i].shot[j], p[!i].pos) < 0.2 )
{
p[i].shotAct[j] = 0;
p[!i].life -= 0.01;
}
else if(VectorDistance(ZeroVector, p[i].shot[j]) > 10.3)
{
p[i].shotAct[j] = 0;
}
}
}
}
return 0;
}
// Clean up what needs cleaning up.
void gameCleanup(void) {
// Nothing
}