-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdo_moves.c
237 lines (237 loc) · 7.94 KB
/
do_moves.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "so_long.h"
int get_pixel_color(void *img_ptr, int x, int y)
{
int bpp;
int size_line;
int endian;
char *img_data;
img_data = mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian);
int color = *(int *)(img_data + y * size_line + x * (bpp / 8)); // color del pixel en (x,y)
return color;
}
void draw_image(in *fw, void *img_ptr, int start_x, int start_y)
{
int x;
int y;
int color;
int width;
int height;
int exclude_color;
width = BPP;
height = BPP;
exclude_color = get_pixel_color(img_ptr, 0, 0);
//ft_printf("\nValor del pixel = %i\n", exclude_color); DEBUG
y = 0;
while (y < height)
{
x = 0;
while (x < width)
{
color = get_pixel_color(img_ptr, x, y);
if (color != exclude_color) // si el color no es el color excluido
{
mlx_pixel_put(fw->map->mlx, fw->map->mlx_win, start_x + x, start_y + y, color);
}
x++;
}
y++;
}
}
char* put_values(char *cadena, entity *entity) {
char *copia = ft_strdup(cadena); // Crear una copia de la cadena original
//printf("\nDEBUG do_moves:45 CARACTER DE CADENA: %c CADENA: %s\n", copia[8], copia);
copia[8] = get_low(entity->value);
//printf("\nDEBUG do_moves:47 VALOR DE ENTITY %c VALOR EN MINUSCULA %c\n", entity->value, get_low(entity->value));
//printf("\nHOLA\n");
//printf("\nDEBUG do_moves:49 CADENA 2: %s\n", copia); // DEBUG
return copia;
}
char get_low(char letter)
{
if (letter >= 'A' && letter <= 'Z') {
return letter + 32;
} else {
// Si la letra no es mayúscula, se devuelve tal cual
return letter;
}
}
char *getdirectionimage1(entity *entity, int coordx, int coordy){
char *imgstep1;
imgstep1 = put_values("sprites/p_down_step_t.xpm", entity);//default
//ft_printf("\nDEBUG do_moves:63 CADENA DE TEXTO: %s\n", put_values("sprites/p_down_step_t.xpm", entity));// DEBUG
if (coordx == -1) //a
imgstep1 = put_values("sprites/p_left_step_t.xpm", entity);
else if (coordy == 1) //s
imgstep1 = put_values("sprites/p_down_step_t.xpm", entity);
else if (coordx == 1) //d
imgstep1 = put_values("sprites/p_right_step_t.xpm", entity);
else if (coordy == -1) //w
imgstep1 = put_values("sprites/p_up_step_t.xpm", entity);
return imgstep1;
}
char *getdirectionimage2(entity *entity, int coordx, int coordy){
char *imgstep2;
imgstep2 = put_values("sprites/p_down_step_2_t.xpm", entity);//default
if (coordx == -1) //a
imgstep2 = put_values("sprites/p_left_step_2_t.xpm", entity);
else if (coordy == 1) //s
imgstep2 = put_values("sprites/p_down_step_2_t.xpm", entity);
else if (coordx == 1) //d
imgstep2 = put_values("sprites/p_right_step_2_t.xpm", entity);
else if (coordy == -1) //w
imgstep2 = put_values("sprites/p_up_step_2_t.xpm", entity);
return imgstep2;
}
char *getdirectionstatic(entity *entity, int coordx, int coordy){
char *imgstep2;
imgstep2 = put_values("sprites/p_down_t.xpm", entity);//default
if (coordx == -1) //a
imgstep2 = put_values("sprites/p_left_t.xpm", entity);
else if (coordy == 1) //s
imgstep2 = put_values("sprites/p_down_t.xpm", entity);
else if (coordx == 1) //d
imgstep2 = put_values("sprites/p_right_t.xpm", entity);
else if (coordy == -1) //w
imgstep2 = put_values("sprites/p_up_t.xpm", entity);
return imgstep2;
}
float getsumax(int coordx, float lx){
float sumax = 0;
if (coordx == -1) //a
sumax -= lx;
else if (coordx == 1) //d
sumax = lx;
return sumax;
}
float getsumay(int coordy, float lx){
float sumay = 0;
if (coordy == 1) //s
sumay = lx;
else if (coordy == -1) //w
sumay -= lx;
return sumay;
}
int drawcharacter(in *fw, entity *entity, int coordx, int coordy) {
entity->stepanimation++;
if (entity->stepanimation == 2)
{
entity->ptr = mlx_xpm_file_to_image(fw->map->mlx, getdirectionimage2(entity, coordx, coordy), &fw->map->width, &fw->map->height);
//mlx_do_sync(fw->map->mlx);
}
else if (entity->stepanimation == 4)
{
entity->ptr = mlx_xpm_file_to_image(fw->map->mlx, getdirectionimage1(entity, coordx, coordy), &fw->map->width, &fw->map->height);
//mlx_do_sync(fw->map->mlx);
entity->stepanimation = 0;
}
return (entity->stepanimation);
}
void initplayer(in *fw, entity *entity, int coordx, int coordy) {
//ft_printf("\nDEBUG 1: %c\n", fw->map->mapstruct[entity->y + coordy][entity->x + coordx]);
fw->map->mapstruct[entity->y][entity->x] = '0';
if (fw->map->mapstruct[entity->y + coordy][entity->x + coordx] != 'E')
fw->map->mapstruct[entity->y + coordy][entity->x + coordx] = entity->value;
//ft_printf("\nDEBUG 2: %c\n", entity->value);
}
void *bucle_asincrono(void* arg) {
tempcajon* temporal = (tempcajon*)arg;
in *fw = temporal->tempfw;
entity *entity = temporal->tempentity;
int coordx = temporal->coordx;
int coordy = temporal->coordy;
free(temporal);
float lx = 0;
while (lx <= 1.1) {
entity->xT = (entity->x + getsumax(coordx, lx));
entity->yT = (entity->y + getsumay(coordy, lx));
lx += 0.1;
put_imgs(fw);
entity->stepanimation = drawcharacter(fw, entity, coordx, coordy);
if (entity->value == 'P')
usleep(25000);
else if (entity->value == 'D')
usleep(24000);
else if (entity->value == 'S')
usleep(40000);
}
entity->x += coordx;
entity->y += coordy;
mlx_put_image_to_window(fw->map->mlx, fw->map->mlx_win, fw->map->floor_ptr, entity->x * BPP, entity->y * BPP);//Pone un suelo en la nueva coordenada
entity->ptr = mlx_xpm_file_to_image(fw->map->mlx, getdirectionstatic(entity, coordx, coordy), &fw->map->width, &fw->map->height);//CARGA LA DIRECCION ESTATICA DEL JUGADOR.
draw_image(fw, entity->ptr, entity->x * BPP, entity->y * BPP);//Dibuja el paso estatico
mlx_do_sync(fw->map->mlx);// HE PROBADO A QUITAR ESTO Y EN EL PORTATIL TIENE MENOS BUG VISUAL DE CHIRIBITAS.
entity->iswalking = false;
check_e(fw);
pthread_exit(NULL);
return NULL;
}
void handlemove(in *fw, entity *entity, int coordx, int coordy)
{
if (entity->iswalking || (coordx == 0 && coordy == 0 && entity->iswalking)){
return;
}
entity->iswalking = true;
initplayer(fw, entity, coordx, coordy);
entity->stepanimation = 0;
tempcajon* temporal = (tempcajon*)malloc(sizeof(tempcajon));
temporal->tempfw = fw;
temporal->tempentity = entity;
temporal->coordx = coordx;
temporal->coordy = coordy;
pthread_t hilo;
pthread_create(&hilo, NULL, bucle_asincrono, (void*)temporal);
pthread_detach(hilo);
//printf("Hola desde el hilo principal\n");
//moveEnemyTowardsPlayer(fw, fw->snorlax, fw->player);
}
int timer(clock_t inicio, double tiempo_deseado)
{
clock_t actual = clock();
double tiempo_transcurrido = (double)(actual - inicio) / CLOCKS_PER_SEC;
double tiempo_restante = tiempo_deseado - tiempo_transcurrido;
// Esperar el tiempo restante para alcanzar el tiempo deseado
double tiempo_espera = tiempo_restante * CLOCKS_PER_SEC;
clock_t espera_final = actual + (clock_t)tiempo_espera;
while (clock() < espera_final)// Esperar...
inicio = clock();
return(inicio);
}
int hasEnoughTimeElapsed(void)
{
clock_t currentTime = clock();
double elapsed = (double)(currentTime - lastKeyPressTime) / CLOCKS_PER_SEC;
if (elapsed >= 0.0001)
{
lastKeyPressTime = currentTime;
return 1; // Ha pasado suficiente tiempo
}
return 0; // No ha pasado suficiente tiempo
}
void draw_counter(in *fw, void *img_ptr, int start_x, int start_y)
{
int x;
int y;
int color;
int width;
int height;
int exclude_color;
width = fw->count->widthmove;
height = fw->count->heightmove;
exclude_color = get_pixel_color(img_ptr, 0, 0);
//ft_printf("\nValor del pixel = %i\n", exclude_color); DEBUG
y = 0;
while (y < height)
{
x = 0;
while (x < width)
{
color = get_pixel_color(img_ptr, x, y);
if (color != exclude_color) // si el color no es el color excluido
{
mlx_pixel_put(fw->map->mlx, fw->map->mlx_win, start_x + x, start_y + y, color);
}
x++;
}
y++;
}
}