-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceship.c
executable file
·98 lines (84 loc) · 2.42 KB
/
spaceship.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
#include <math.h>
#include <allegro_primitives.h>
#include <allegro_audio.h>
#include "spaceship.h"
#include "misc.h"
void ship_init(Spaceship *s) {
s->sx = SCREEN_WIDTH/2;
s->sy = SCREEN_HEIGHT/2;
s->heading = 0;
s->speed = 0;
s->lives = 3;
s->accelerating = 0;
s->color = al_map_rgb(0, 255, 255);
s->bbox.center.x = SCREEN_WIDTH/2;
s->bbox.center.y = SCREEN_HEIGHT/2;
s->bbox.left = 10;
s->bbox.right = 10;
s->bbox.top= 12;
s->bbox.bottom =10;
s->bbox.heading = 0;
s->bbox.color = al_map_rgb(255, 255, 255);
}
void ship_draw(Spaceship s) {
if (s.lives <= 1)
return;
ALLEGRO_TRANSFORM transform;
al_identity_transform(&transform);
al_rotate_transform(&transform, s.heading);
al_translate_transform(&transform, s.sx, s.sy);
al_use_transform(&transform);
al_draw_line(-8, 9, 0, -11, s.color, 3.0f);
al_draw_line( 0, -11, 8, 9, s.color, 3.0f);
al_draw_line(-6, 4, -1, 4, s.color, 3.0f);
al_draw_line( 6, 4, 1, 4, s.color, 3.0f);
if (s.accelerating) {
al_draw_line(-3, 5, -3, 15, al_map_rgb(255, 0, 0), 2.0f);
al_draw_line( 3, 5, 3, 15, al_map_rgb(255, 0, 0), 2.0f);
}
}
void ship_damage(Spaceship *s) {
s->lives--;
if (s->lives > 0) {
s->sx = SCREEN_WIDTH / 2;
s->sy = SCREEN_HEIGHT / 2;
s->heading = 0;
s->speed = 0;
}
}
void ship_move(Spaceship *s) {
if (s->lives < 1)
return;
s->sy -= s->speed * cos(s->heading);
s->sx += s->speed * sin(s->heading);
/* loop boundary */
if (s->sy < 0)
s->sy += SCREEN_WIDTH;
if (s->sy > SCREEN_HEIGHT)
s->sy -= SCREEN_HEIGHT;
if (s->sx < 0)
s->sx += SCREEN_WIDTH;
if (s->sx > SCREEN_WIDTH)
s->sx -= SCREEN_WIDTH;
/* move and rotate the bounding box */
s->bbox.center.x = s->sx;
s->bbox.center.y = s->sy;
s->bbox.heading = s->heading;
}
void ship_acc(Spaceship *s, ALLEGRO_SAMPLE *thrust) {
s->speed += SHIP_SPEEDUP;
if (s->speed > MAX_SHIP_SPEED)
s->speed = MAX_SHIP_SPEED;
al_play_sample(thrust, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
}
void ship_slowndown(Spaceship *s) {
s->speed -= SHIP_SPEEDUP;
if (s->speed < MIN_SHIP_SPEED)
s->speed = MIN_SHIP_SPEED;
}
void ship_turn_right(Spaceship *s) {
s->heading += SHIP_ROTATE_ANGLE;
}
void ship_turn_left(Spaceship *s) {
s->heading -= SHIP_ROTATE_ANGLE;
}