forked from devpack/mayhem
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvaisseau_data.cpp
executable file
·197 lines (167 loc) · 5.8 KB
/
vaisseau_data.cpp
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
#include "vaisseau_data.h"
#include "allegro_compatibility.h"
#include "option.h"
#include "platform_data.h"
int init_vaisseau_data(struct vaisseau_data *v, struct vaisseau_gfx *gfx,
float mass, float thrust_max, int anglestep,
int max_fuel, int speed_fuel_down, int speed_fuel_up,
int max_shield_force, int speed_shield_force_down,
int speed_shield_force_up) {
v->thrust_max = ftofix(thrust_max);
v->mass = ftofix(mass);
v->anglestep = anglestep;
v->max_fuel = max_fuel;
v->speed_fuel_down = speed_fuel_down;
v->speed_fuel_up = speed_fuel_up;
v->max_shield_force = max_shield_force;
v->speed_shield_force_down = speed_shield_force_down;
v->speed_shield_force_up = speed_shield_force_down;
int i;
for (i = 0; i < MAX_TIR; i++) {
v->tir[i].x = 0;
v->tir[i].y = 0;
v->tir[i].xposprecise = itofix(0);
v->tir[i].yposprecise = itofix(0);
v->tir[i].dx = itofix(0);
v->tir[i].dy = itofix(0);
v->tir[i].free = true;
}
for (i = 0; i < MAX_TIR; i++) {
v->backtir[i].x = 0;
v->backtir[i].y = 0;
v->backtir[i].xposprecise = itofix(0);
v->backtir[i].yposprecise = itofix(0);
v->backtir[i].dx = itofix(0);
v->backtir[i].dy = itofix(0);
v->backtir[i].free = true;
}
for (i = 0; i < 8; i++) {
v->debris[i].x = 0;
v->debris[i].y = 0;
v->debris[i].xposprecise = itofix(0);
v->debris[i].yposprecise = itofix(0);
v->debris[i].ax = itofix(0);
v->debris[i].ay = itofix(0);
v->debris[i].vx = itofix(0);
v->debris[i].vy = itofix(0);
v->debris[i].impultion = itofix(0);
v->debris[i].angle = 0;
v->debris[i].active = false;
}
v->gfx = gfx;
v->sprite_ptr = gfx->sprite;
v->sprite_buffer_rota =
al_create_bitmap(32, 32); // create ALLEGRO_BITMAP pour le sprite
if (!v->sprite_buffer_rota)
return -1;
clear_bitmap(v->sprite_buffer_rota); // On nettoye
int num_frames = (360 / anglestep) + 1; // Add one frame for handling rounding errors
v->coll_map.init(32, 32, num_frames);
for (int frame = 0; frame < num_frames; frame++) {
double angle = v->anglestep * frame;
clear_bitmap(v->sprite_buffer_rota);
rotate_sprite(v->sprite_buffer_rota, v->sprite_ptr, 0, 0, angle);
ALLEGRO_LOCKED_REGION *reg = al_lock_bitmap(
v->sprite_buffer_rota, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
assert(reg);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
auto pixel = get_pixel(reg, x, y);
v->coll_map.set_pixel(x, y, frame, is_nonblack_pixel(pixel));
}
}
al_unlock_bitmap(v->sprite_buffer_rota);
}
return 0;
}
void clean_vaisseau_data(struct vaisseau_data *v) {
if (v->sprite_buffer_rota)
al_destroy_bitmap(v->sprite_buffer_rota); // On nettoye
}
void init_ship_pos_from_platforms(struct vaisseau_data *v,
struct platform_data *plats) {
int xpos, ypos;
xpos = (plats->xmin + plats->xmax) / 2 - 16;
ypos = plats->yflat - 29;
v->xpos = xpos;
v->ypos = ypos;
v->xposprecise = itofix(xpos);
v->yposprecise = itofix(ypos);
v->fuel = v->max_fuel;
v->shield_force = v->max_shield_force;
v->explode_count = 0;
v->explode_tick = 0;
v->explode = false;
v->explode_appear_time_passed = false;
v->option_type = OPT_NOOPTION;
v->speed_shield_force_down = VAISSEAU_SPEED_SHIELD_FORCE_DOWN;
v->thrust_max = ftofix(VAISSEAU_THRUST_MAX);
v->impactx = 0;
v->impacty = 0;
v->ax = itofix(0);
v->ay = itofix(0);
v->vx = itofix(0);
v->vy = itofix(0);
v->thrust = itofix(0);
v->shield = false;
v->fire = false;
v->fire_delay = false;
v->landed = true;
v->rebound = false;
v->angle = 0;
v->angle_precise = 0;
v->refueling = false;
}
void fuel_shield_calcul(int nbvaisseau, struct vaisseau_data *v, double dt) {
while (nbvaisseau--) {
v->refueling = false;
if (v->fire_delay)
v->fuel -= 3 * (dt / 0.025);
if (!(v->landed || v->rebound) && (v->thrust != itofix(0)) && (v->fuel > 0))
v->fuel -= v->speed_fuel_down * (dt / 0.025);
else if ((v->landed || v->rebound) && (v->thrust == itofix(0)) &&
(v->fuel < v->max_fuel)) {
v->fuel += v->speed_fuel_up * (dt / 0.025);
v->refueling = true;
}
if (v->fuel < 0)
v->fuel = 0;
if (v->fuel > v->max_fuel)
v->fuel = v->max_fuel;
if (v->shield && v->shield_force > 0)
v->shield_force -= v->speed_shield_force_down * (dt / 0.025);
else if (!v->shield && v->shield_force < v->max_shield_force)
v->shield_force += v->speed_shield_force_up * (dt / 0.025);
if (v->shield_force < 0)
v->shield_force = 0;
if (v->shield_force > v->max_shield_force)
v->shield_force = v->max_shield_force;
v++;
}
}
void collision_map::init(int width_in, int height_in, int num_frames_in) {
width = width_in;
height = height_in;
num_frames = num_frames_in;
int num_pixels = width * height * num_frames;
coll_map = new bool[num_pixels];
for (int i = 0; i < num_pixels; i++) {
coll_map[i] = false;
}
}
collision_map::~collision_map() { delete[] coll_map; }
bool collision_map::is_collide_pixel(int x, int y, int frame) {
return coll_map[get_index(x, y, frame)];
}
void collision_map::set_pixel(int x, int y, int frame, bool value) {
coll_map[get_index(x, y, frame)] = value;
}
int collision_map::get_index(int x, int y, int frame) {
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
assert(frame >= 0 && frame < num_frames);
int frame_offset = width * height * frame;
int line_offset = width * y;
int pos = frame_offset + line_offset + x;
return pos;
}