/
ship.c
285 lines (231 loc) · 6.04 KB
/
ship.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include "list.h"
#include "util.h"
#include "missile.h"
#include "animation.h"
#include "asteroid.h"
#include "asteroids.h"
#include "ship.h"
static ALLEGRO_BITMAP *sprite;
static ALLEGRO_BITMAP *thrust_sprite;
static ALLEGRO_BITMAP *explosion_sprites[60];
void
ship_accelerate(SHIP *ship)
{
ship->velocity->x += (float) sin(deg2rad(ship->angle)) * ACCEL_SCALE;
ship->velocity->y += (float) -(cos(deg2rad(ship->angle))) * ACCEL_SCALE;
}
bool
ship_asteroid_collision(SHIP *s, ASTEROID *a)
{
float ship_x = s->position->x - (s->width / 2);
float ship_y = s->position->y - (s->height / 2);
float rock_x = a->position->x - (a->width / 2);
float rock_y = a->position->y - (a->height / 2);
return collision(ship_x, ship_y, s->width, s->height,
rock_x, rock_y, a->width, a->height);
}
SHIP *
ship_create(void)
{
SHIP *ship = malloc(sizeof(SHIP));
ship->position = malloc(sizeof(VECTOR));
ship->velocity = malloc(sizeof(VECTOR));
ship->missiles = NULL;
ship->sprite = sprite;
ship->thrust_sprite = thrust_sprite;
ship->width = al_get_bitmap_width(ship->sprite);
ship->height = al_get_bitmap_height(ship->sprite);
ship->angle = 0.0;
ship->velocity->x = 0.0;
ship->velocity->y = 0.0;
ship->position->x = SCREEN_W / 2;
ship->position->y = SCREEN_H / 2;
ship->explosion = NULL;
ship->missiles = NULL;
ship->thrust_visible = false;
ship->fire_debounce = false;
for(int i = 0; i < MAX_MISSILES; i++)
ship->missiles = list_append(ship->missiles, missile_create());
return ship;
}
void
ship_drag(SHIP *ship)
{
ship->velocity->x *= DRAG;
ship->velocity->y *= DRAG;
}
bool
ship_explode(SHIP *ship)
{
if(ship->explosion)
return false;
ANIMATION *explosion = animation_new(explosion_sprites, 60);
/* explosion->slowdown = 10; */
explosion->position->x = ship->position->x - (explosion->width / 2);
explosion->position->y = ship->position->y - (explosion->height / 2);
ship->explosion = explosion;
return true;
}
void
ship_fire(SHIP *ship, ALLEGRO_TIMER *timer)
{
LIST *head = NULL;
MISSILE *missile = NULL;
VECTOR position;
/* full button press required for each missile */
if(ship->fire_debounce)
return;
/* find an inactive missile to launch */
head = list_first(ship->missiles);
while(head && !missile) {
MISSILE *m = (MISSILE *) head->data;
if(!m->active)
missile = m;
head = head->next;
}
/* all missiles in use? */
if(missile == NULL)
return;
position.x = ship->position->x;
position.y = ship->position->y;
missile_fire(missile, &position, ship->angle, timer);
ship->fire_debounce = true;
}
void
ship_free(SHIP *ship)
{
LIST *head;
if(ship->explosion)
animation_free(ship->explosion);
head = list_first(ship->missiles);
while(head) {
missile_free((MISSILE *) head->data);
head = head->next;
}
list_free(ship->missiles);
free(ship->position);
free(ship->velocity);
free(ship);
ship = NULL;
}
void
ship_hyperspace(SHIP *ship)
{
if(ship->hyper_debounce)
return;
ship->hyper_debounce = true;
ship->velocity->x = 0.0;
ship->velocity->y = 0.0;
ship->position->x = rand_f(0, SCREEN_W);
ship->position->y = rand_f(0, SCREEN_H);
}
bool
ship_init(void)
{
if((sprite = al_load_bitmap("data/sprites/ship/ship.png")) == NULL) {
fprintf(stderr, "failed to load ship sprite\n");
return false;
}
if((thrust_sprite = al_load_bitmap("data/sprites/ship/ship-thrust.png")) == NULL) {
fprintf(stderr, "failed to load ship thrust sprite\n");
return false;
}
/* ship explosion animation frames */
for(int i = 0; i < 60; i++) {
char name[255];
sprintf(name, "data/sprites/ship/explosion/%d.png", i + 1);
if((explosion_sprites[i] = al_load_bitmap(name)) == NULL) {
fprintf(stderr, "failed to load ship explosion sprite %d\n", i);
return false;
}
}
return true;
}
void
ship_rotate(SHIP *ship, float deg)
{
ship->angle += deg;
if(ship->angle > 360.0)
ship->angle -= 360.0;
if(ship->angle < 0)
ship->angle += 360.0;
}
void
ship_draw(SHIP *ship, bool thrusting)
{
if(ship->explosion != NULL) {
animation_draw(ship->explosion);
return;
}
ALLEGRO_BITMAP *current_sprite;
/* this creates a flashing thrust visualization
* not _exactly_ like the original (too fast), but close. (FIXME) */
current_sprite = ship->sprite;
if(thrusting && !ship->thrust_visible)
current_sprite = ship->thrust_sprite;
al_draw_rotated_bitmap(
current_sprite,
ship->width / 2,
ship->height / 2,
ship->position->x,
ship->position->y,
deg2rad(ship->angle),
DRAWING_FLAGS);
ship->thrust_visible = (ship->thrust_visible) ? false : true;
}
void
ship_shutdown(void)
{
al_destroy_bitmap(sprite);
al_destroy_bitmap(thrust_sprite);
}
SHIP *
ship_update(SHIP *ship, bool keys[], ALLEGRO_TIMER *timer)
{
LIST *head = NULL;
/* move forward */
if(keys[KEY_UP])
ship_accelerate(ship);
/* rotate */
if(keys[KEY_LEFT])
ship_rotate(ship, -3);
if(keys[KEY_RIGHT])
ship_rotate(ship, 3);
/* hyperspace */
if(keys[KEY_LCONTROL])
ship_hyperspace(ship);
/* shoot */
if(keys[KEY_SPACE])
ship_fire(ship, timer);
if(ship->explosion) {
animation_update(ship->explosion);
/* if the animation is complete, create a new ship */
if(ship->explosion->current_frame >= ship->explosion->n_frames - 1) {
/* FIXME: need preemptive collision detection, wait() */
SHIP *old = ship;
ship = ship_create();
ship_free(old);
}
return ship;
}
/* ship missile positions */
head = list_first(ship->missiles);
while(head) {
missile_update((MISSILE *) head->data, timer);
head = head->next;
}
ship->position->x += ship->velocity->x;
ship->position->y += ship->velocity->y;
wrap_position(ship->position);
/* slow down over time */
ship_drag(ship);
return ship;
}