-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerup.c
190 lines (159 loc) · 4.37 KB
/
powerup.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
#include "powerup.h"
static powerup *first_powerup = NULL;
static powerup *last_powerup = NULL;
static SDL_Surface *powerup_sprite[POWERUP_ICON_MAX];
static void powerup_exit(void)
{
int counter;
for(counter = 0; counter < POWERUP_ICON_MAX; counter++)
SDL_FreeSurface(powerup_sprite[counter]);
free_all_powerups();
}
int init_powerups(void)
{
int counter;
char tmp_string[512] = {0};
for(counter = 0; counter < POWERUP_ICON_MAX; counter++)
{
sprintf(tmp_string, "gfx/powerup/%i.png", counter);
if(!(powerup_sprite[counter] = load_image(tmp_string)))
return 0;
}
atexit(powerup_exit);
return 1;
}
powerup *new_powerup(const powerup_type type, const powerup_icon icon,
const char *text, const double x, const double y,
const double dir_x, const double dir_y,
const double moving_speed)
{
/* allocate memory and set new powerup to last powerup */
if(!first_powerup)
{
first_powerup = (powerup *)malloc(sizeof(powerup));
if(!first_powerup)
return NULL;
first_powerup->next = NULL;
first_powerup->prev = NULL;
last_powerup = first_powerup;
}
else
{
last_powerup->next = (powerup *)malloc(sizeof(powerup));
if(!last_powerup->next)
return NULL;
last_powerup->next->prev = last_powerup;
last_powerup->next->next = NULL;
last_powerup = last_powerup->next;
}
/* set powerup properties */
last_powerup->type = type;
last_powerup->icon = icon;
if(text)
strncpy(last_powerup->text, text, P_BUFFER_LENGTH);
else
last_powerup->text[0] = '\0';
last_powerup->x = x;
last_powerup->y = y;
last_powerup->dir_x = dir_x;
last_powerup->dir_y = dir_y;
last_powerup->moving_speed = moving_speed;
last_powerup->last_move = SDL_GetTicks();
last_powerup->speed = 0;
last_powerup->acceleration = 0;
last_powerup->wpn_type = 0;
last_powerup->hp = 0;
last_powerup->hp_max = 0;
last_powerup->atk = 0;
last_powerup->def = 0;
last_powerup->fire_rate = 0;
last_powerup->invert = 0;
last_powerup->weapon_block = 0;
last_powerup->move_block = 0;
last_powerup->start_time = SDL_GetTicks();
last_powerup->duration = 0;
return last_powerup;
}
void free_powerup(powerup *object)
{
if(!object)
return;
if(object == first_powerup)
{
first_powerup = object->next;
if(object->next)
object->next->prev = NULL;
else
last_powerup = NULL;
}
else if(object == last_powerup)
{
object->prev->next = NULL;
last_powerup = object->prev;
}
else
{
object->next->prev = object->prev;
object->prev->next = object->next;
}
free(object);
}
powerup *get_first_powerup(void)
{
return first_powerup;
}
int get_powerup_w(const powerup_icon icon)
{
return powerup_sprite[icon]->w;
}
int get_powerup_h(const powerup_icon icon)
{
return powerup_sprite[icon]->h;
}
void move_all_powerups(void)
{
powerup *tmp_powerup = first_powerup;
powerup *cache_powerup = NULL;
while(tmp_powerup)
{
tmp_powerup->x += tmp_powerup->dir_x * (SDL_GetTicks() - tmp_powerup->last_move) * tmp_powerup->moving_speed;
tmp_powerup->y += tmp_powerup->dir_y * (SDL_GetTicks() - tmp_powerup->last_move) * tmp_powerup->moving_speed;
tmp_powerup->last_move = SDL_GetTicks();
if((tmp_powerup->x > SDL_GetVideoSurface()->w) ||
(tmp_powerup->x < 0 - get_powerup_w(tmp_powerup->icon)) ||
(tmp_powerup->y > SDL_GetVideoSurface()->h) ||
(tmp_powerup->y < 0 -get_powerup_h(tmp_powerup->icon)))
{
cache_powerup = tmp_powerup;
tmp_powerup = tmp_powerup->next;
free_powerup(cache_powerup);
continue;
}
tmp_powerup = tmp_powerup->next;
}
}
void blit_all_powerups(SDL_Surface *dst)
{
powerup *tmp_powerup = NULL;
SDL_Rect tmp_rect;
for(tmp_powerup = first_powerup; tmp_powerup; tmp_powerup = tmp_powerup->next)
{
tmp_rect.x = tmp_powerup->x;
tmp_rect.y = tmp_powerup->y;
SDL_BlitSurface(powerup_sprite[tmp_powerup->icon], NULL, dst, &tmp_rect);
}
}
void extend_all_powerups_timestamp(const Uint32 time)
{
powerup *tmp_powerup = NULL;
for(tmp_powerup = first_powerup; tmp_powerup; tmp_powerup = tmp_powerup->next)
{
tmp_powerup->start_time += time;
tmp_powerup->last_move += time;
}
}
void free_all_powerups(void)
{
while(first_powerup)
free_powerup(first_powerup);
}