-
Notifications
You must be signed in to change notification settings - Fork 0
/
blast.c
executable file
·109 lines (99 loc) · 3.26 KB
/
blast.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
#include <math.h>
#include <allegro_primitives.h>
#include <allegro_audio.h>
#include "blast.h"
#include "misc.h"
int asteroid_points[3] = {20, 50, 100};
void blast_init(Blast b[], int size) {
int i;
for (i = 0; i < size; i++) {
b[i].speed = 5;
b[i].live = 0; /* all blasts are dead until they are fired */
b[i].bbox.top = 2;
b[i].bbox.left = 2;
b[i].bbox.right = 2;
b[i].bbox.bottom = 2;
b[i].bbox.color = al_map_rgb(255,0,0);
}
}
void blast_fire(Blast b[], int size, Spaceship *s, ALLEGRO_SAMPLE *fire) {
if (s->lives < 1)
return;
int i;
for (i = 0; i < size; i++) {
if (!b[i].live) {
b[i].sx = s->sx + 5 * sin(s->heading);
b[i].sy = s->sy - 5 * cos(s->heading);
b[i].heading = s->heading; /* the blast fired heads to the same direction as the spaceship */
b[i].live = 1;
b[i].bbox.center.x = b[i].sx;
b[i].bbox.center.y = b[i].sy;
al_play_sample(fire, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
break;
}
}
}
void blast_move(Blast b[], int size) {
int i;
for (i = 0; i < size; i++) {
if (b[i].live) {
b[i].sx += b[i].speed * sin(b[i].heading);
b[i].sy -= b[i].speed * cos(b[i].heading);
b[i].bbox.center.x = b[i].sx;
b[i].bbox.center.y = b[i].sy;
if (b[i].sx > SCREEN_WIDTH || b[i].sx < 0 || b[i].sy > SCREEN_HEIGHT || b[i].sy < 0)
b[i].live = 0; // out of screen
}
}
}
void blast_draw(Blast b[], int size) {
int i;
for (i = 0; i < size; i++) {
if (b[i].live) {
ALLEGRO_TRANSFORM transform;
al_identity_transform(&transform);
al_rotate_transform(&transform, b[i].heading);
al_translate_transform(&transform, b[i].sx, b[i].sy);
al_use_transform(&transform);
al_draw_filled_circle(0, 0, 2, al_map_rgb(255, 255, 255));
}
}
}
void blast_collide(Blast b[], int b_size, Asteroid *a, ALLEGRO_SAMPLE *bang, int *score, int *asteroid_num) {
Asteroid *prev, *p;
/*
* go through all the bullets
*/
int i;
for (i = 0; i < b_size; i++) {
if (b[i].live) /* only check the alive blast */
{
/*
* go through the asteroid list
*/
for (prev = a, p = a->next;
p != NULL;
prev = p, p = p->next)
{
if (bbox_overlap(p->bbox, b[i].bbox))
{
*score += asteroid_points[p->type];
/* split it into two parts */
if (asteroid_split(p))
*asteroid_num += 2; /* if succeed? */
/* kill it anyway */
if (prev) {
prev->next = p->next;
free(p);
*asteroid_num -= 1;
}
/* kill the blast */
b[i].live = 0;
/* bang */
al_play_sample(bang, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
/* add score */
}
}
}
}
}