-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.c
195 lines (167 loc) · 4.18 KB
/
player.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
#include "player.h"
#include <stdlib.h>
#include <math.h>
static float player_number_dir(int player, int player_count)
{
if (player_count == 1)
return PLAYER_DIRECTION_DEFAULT;
else if (player_count > 2)
return 315 - 90 * (player - 1);
else
return 315 - 180 * (player - 1);
}
player_t *player_new(int num, int player_count)
{
player_t *new = malloc(sizeof(player_t));
new->trail = malloc(PLAYER_TRAIL_SEGMENTS * sizeof(cvertex_t));
new->trail_toggle = 1;
for (int i = 0; i < PLAYER_TRAIL_SEGMENTS; i++)
new->trail[i] = (cvertex_t){{0, 0}, {255, 255, 255, 255} };
new->entity = entity_new();
new->entity->size = (vector2_t){PLAYER_SIZE_DEFAULT,
PLAYER_SIZE_DEFAULT};
new->lives = PLAYER_LIVES_DEFAULT;
new->dir = player_number_dir(num, player_count);
new->speed = PLAYER_SPEED_DEFAULT;
new->debounce_time = PLAYER_DEBOUNCE_TIME;
new->entity->pos = (vector2_t){0, 0};
new->angular_vel = 0;
/* State Flags */
new->normal = 1;
new->shoot = 0;
/* Powerup Flags */
new->powerup_tiny = 0;
new->powerup_bullets = 0;
new->powerup_ghost = 0;
new->powerup_shield = 0;
/* Timers */
new->timer_flash = 0.f;
new->timer_shoot = 0.f;
new->timer_powerup_bullets = 0.f;
new->timer_powerup_bullets_delay = 0.f;
new->timer_powerup_ghost = 0.f;
new->timer_powerup_tiny = 0.f;
/* Color */
new->color = player_number_color(num);
/* Input Pin Assignment */
new->right_pin = player_number_right_pin(num);
new->left_pin = player_number_left_pin(num);
return new;
}
void player_rotate(player_t *player, float angle)
{
player->dir = fmodf(player->dir + angle, 360);
}
void player_resize(player_t *player, float width)
{
player->entity->pos = (vector2_t){player->entity->pos.x
+ player->entity->size.x/2 - width/2,
player->entity->pos.y
+ player->entity->size.y/2 - width/2};
player->entity->size = (vector2_t){width, width};
}
void player_injure(player_t *player)
{
if (player->powerup_shield)
player->powerup_shield = 0;
else
--player->lives;
player->debounce_time = 0;
}
void player_shoot(player_t *player, bullet_t *bullet)
{
vector2_t dir = player_direction_vector(player);
bullet->entity->pos = (vector2_t){player->entity->pos.x
+ player->entity->size.x/2 - 2 + 15 * dir.x,
player->entity->pos.y
+ player->entity->size.y/2 - 2 + 15 * dir.y};
float mag = sqrtf(fabsf(player->entity->vel.x * player->entity->vel.x
+ player->entity->vel.y * player->entity->vel.y));
bullet->entity->vel = (vector2_t){(10 + PLAYER_SPEED_MAX)
* player->entity->vel.x/mag,
(10 + PLAYER_SPEED_MAX)
* player->entity->vel.y/mag};
bullet->dead = 0;
player->timer_shoot = 0;
player->shoot = 0;
}
void player_powerup(player_t *player, powerup_t *powerup)
{
powerup->free = 1;
switch (powerup->type) {
case POWERUP_TYPE_BULLETS:
player->powerup_bullets = 1;
player->timer_powerup_bullets = 0.f;
break;
case POWERUP_TYPE_LIFE:
player->lives = player->lives == PLAYER_LIVES_MAX
? PLAYER_LIVES_MAX : player->lives + 1;
break;
case POWERUP_TYPE_SHIELD:
player->powerup_shield = 1;
break;
case POWERUP_TYPE_TINY:
player->powerup_tiny = 1;
player->timer_powerup_tiny = 0.f;
break;
case POWERUP_TYPE_GHOST:
player->powerup_ghost = 1;
player->timer_powerup_ghost = 0.f;
break;
}
}
vector2_t player_direction_vector(const player_t *player)
{
return (vector2_t){cosf((float)M_PI * player->dir/180.f),
sinf((float)M_PI * player->dir/180.f)};
}
rpi_gpio_pin_t player_number_left_pin(int player)
{
switch (player) {
case 1:
return PLAYER_1_LEFT;
case 2:
return PLAYER_2_LEFT;
case 3:
return PLAYER_3_LEFT;
case 4:
return PLAYER_4_LEFT;
default:
return PLAYER_1_LEFT;
}
}
rpi_gpio_pin_t player_number_right_pin(int player)
{
switch (player) {
case 1:
return PLAYER_1_RIGHT;
case 2:
return PLAYER_2_RIGHT;
case 3:
return PLAYER_3_RIGHT;
case 4:
return PLAYER_4_RIGHT;
default:
return PLAYER_1_RIGHT;
}
}
color_t player_number_color(int player)
{
switch (player) {
case 1:
return PLAYER_1_COLOR;
case 2:
return PLAYER_2_COLOR;
case 3:
return PLAYER_3_COLOR;
case 4:
return PLAYER_4_COLOR;
default:
return PLAYER_1_COLOR;
}
}
void player_free(player_t *player)
{
entity_free(player->entity);
free(player);
}