This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemies.c
201 lines (172 loc) · 7.25 KB
/
Enemies.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
193
194
195
196
197
198
199
200
201
#include <gb/gb.h>
#include "Player.c"
enum SpaceshipType
{
follower,
spiraler
};
//how many points should a given spaceship earn
uint8_t SpaceshipPoints[] =
{
0,
1
};
typedef struct Enemy Enemy;
typedef struct Enemy
{
GameObject gameObject;
uint8_t accelerationShifts;
uint8_t dragShifts;
uint8_t deathTimer; //if it is set to something other than 0, the death animation is playing
enum SpaceshipType type;
uint8_t points;
uint8_t enemyDisplayTile;
void (*move) (Enemy*, Player*); //this is a pointer to a function that makes the enemy move
void (*updateSprites) (Enemy*);
};
Enemy enemies[MAX_ENEMY_NUMBER]; //the enemies are stored in this
BOOLEAN activeEnemies[MAX_ENEMY_NUMBER]; //if the enemy in enemies at index is active or an open space for an enemy that needs to be loaded
//for loading enemies. it allocates the space for an enemy at the returned index in enemies or returns -1 if the allocation was unsuccessful
inline int8_t getAvailableEnemySpot()
{
for (uint8_t i = 0; i < MAX_ENEMY_NUMBER; i++)
{
if (!activeEnemies[i])
{
activeEnemies[i] = TRUE;
return i;
}
}
return -1;
}
void moveFollower(Enemy* follower, Player* player)
{
fixed16 x = sub(player->gameObject.posx, follower->gameObject.posx);
fixed16 y = sub(player->gameObject.posy, follower->gameObject.posy);
x = x >> follower->accelerationShifts;
y = y >> follower->accelerationShifts;
accelerateGameObject(&follower->gameObject, x, y);
applyDragToGameObject(&follower->gameObject, follower->dragShifts);
}
void updateSprites(Enemy* enemy)
{
setRotatedSprite(enemy->gameObject.firstSprite, enemy->enemyDisplayTile, enemy->gameObject.velx, enemy->gameObject.vely);
}
void initFollower(uint8_t index, Player* player)
{
Enemy* initEnemy = &enemies[index];
initEnemy->accelerationShifts = 11;
initEnemy->dragShifts = 4;
initEnemy->gameObject.firstSprite = 2 + index;
initEnemy->gameObject.spriteSizex = 1;
initEnemy->gameObject.spriteSizey = 1;
initEnemy->gameObject.posx = player->gameObject.posx + (index & 1 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.posy = player->gameObject.posy + (index & 2 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.velx = FIXED(0);
initEnemy->gameObject.vely = FIXED(0);
initEnemy->gameObject.collider.posx = enemies[index].gameObject.posx;
initEnemy->gameObject.collider.posy = enemies[index].gameObject.posy;
initEnemy->gameObject.collider.sizex = FIXED(8);
initEnemy->gameObject.collider.sizey = FIXED(8);
initEnemy->type = follower;
initEnemy->deathTimer = 0;
initEnemy->move = moveFollower;
initEnemy->updateSprites = updateSprites;
initEnemy->enemyDisplayTile = 4;
}
void moveSpiralerPositive(Enemy* spiraler, Player* player)
{
fixed16 x = sub(player->gameObject.posx, spiraler->gameObject.posx);
fixed16 y = sub(player->gameObject.posy, spiraler->gameObject.posy);
x = x >> spiraler->accelerationShifts;
y = y >> spiraler->accelerationShifts;
x += -(y >> 2);
y += (x >> 1);
accelerateGameObject(&spiraler->gameObject, x, y);
applyDragToGameObject(&spiraler->gameObject, spiraler->dragShifts);
}
void moveSpiralerNegative(Enemy* spiraler, Player* player)
{
fixed16 x = sub(player->gameObject.posx, spiraler->gameObject.posx);
fixed16 y = sub(player->gameObject.posy, spiraler->gameObject.posy);
x = x >> spiraler->accelerationShifts;
y = y >> spiraler->accelerationShifts;
x += (y >> 2);
y += -(x >> 1);
accelerateGameObject(&spiraler->gameObject, x, y);
applyDragToGameObject(&spiraler->gameObject, spiraler->dragShifts);
}
void initSpiralerPositive(uint8_t index, Player* player)
{
Enemy* initEnemy = &enemies[index];
initEnemy->accelerationShifts = 8;
initEnemy->dragShifts = 4;
initEnemy->gameObject.firstSprite = 2 + index;
initEnemy->gameObject.spriteSizex = 1;
initEnemy->gameObject.spriteSizey = 1;
initEnemy->gameObject.posx = player->gameObject.posx + (index & 1 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.posy = player->gameObject.posy + (index & 2 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.velx = FIXED(0);
initEnemy->gameObject.vely = FIXED(0);
initEnemy->gameObject.collider.posx = enemies[index].gameObject.posx;
initEnemy->gameObject.collider.posy = enemies[index].gameObject.posy;
initEnemy->gameObject.collider.sizex = FIXED(8);
initEnemy->gameObject.collider.sizey = FIXED(8);
initEnemy->type = spiraler;
initEnemy->deathTimer = 0;
initEnemy->move = moveSpiralerPositive;
initEnemy->updateSprites = updateSprites;
initEnemy->enemyDisplayTile = 13;
}
void initSpiralerNegative(uint8_t index, Player* player)
{
Enemy* initEnemy = &enemies[index];
initEnemy->accelerationShifts = 8;
initEnemy->dragShifts = 4;
initEnemy->gameObject.firstSprite = 2 + index;
initEnemy->gameObject.spriteSizex = 1;
initEnemy->gameObject.spriteSizey = 1;
initEnemy->gameObject.posx = player->gameObject.posx + (index & 1 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.posy = player->gameObject.posy + (index & 2 ? FIXED(80) : FIXED(-80));
initEnemy->gameObject.velx = FIXED(0);
initEnemy->gameObject.vely = FIXED(0);
initEnemy->gameObject.collider.posx = enemies[index].gameObject.posx;
initEnemy->gameObject.collider.posy = enemies[index].gameObject.posy;
initEnemy->gameObject.collider.sizex = FIXED(8);
initEnemy->gameObject.collider.sizey = FIXED(8);
initEnemy->type = spiraler;
initEnemy->deathTimer = 0;
initEnemy->move = moveSpiralerNegative;
initEnemy->updateSprites = updateSprites;
initEnemy->enemyDisplayTile = 13;
}
enum spaceShipFunctionsEnum
{
followerInit,
spiralerInitPositive,
spiralerInitNegative
};
const void (*spaceShipFunctions[]) (uint8_t, Player*) =
{
&initFollower,
&initSpiralerPositive,
&initSpiralerNegative
};
typedef struct Wave Wave;
typedef struct Wave
{
uint8_t initFunctions[8];
uint8_t enemyLoadDelay;
uint8_t waveCountdown[2];
};
const Wave waves[] =
{
{{followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit}, 180, {4, 0}},
{{followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit}, 120, {3, 0}},
{{followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, spiralerInitPositive, spiralerInitNegative}, 180, {4, 0}},
{{followerInit, followerInit, spiralerInitPositive, spiralerInitNegative, followerInit, followerInit, followerInit, followerInit}, 120, {3, 0}},
{{followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit, followerInit}, 60, {2, 0}},
{{spiralerInitPositive, spiralerInitNegative, followerInit, followerInit, spiralerInitPositive, spiralerInitNegative, followerInit, followerInit}, 180, {4, 0}},
{{spiralerInitPositive, spiralerInitNegative, spiralerInitPositive, spiralerInitNegative, spiralerInitPositive, spiralerInitNegative, spiralerInitPositive, spiralerInitNegative}, 240, {6, 0}},
{{followerInit, followerInit, spiralerInitPositive, spiralerInitNegative, followerInit, followerInit, followerInit, followerInit}, 90, {3, 0}},
};