2019-11-21 19:30:54 +01:00
|
|
|
#include "box.h"
|
|
|
|
#include "world.h"
|
2019-12-07 14:32:38 +01:00
|
|
|
#include "ennemi.h"
|
2019-12-11 16:31:18 +01:00
|
|
|
#include "score.h"
|
2020-01-15 20:42:51 +01:00
|
|
|
#include <constants.h>
|
2020-01-29 14:34:47 +01:00
|
|
|
#include <base.h>
|
2019-12-07 19:26:55 +01:00
|
|
|
|
|
|
|
int check_collision(box_t *b)
|
|
|
|
{
|
2020-02-16 20:43:35 +01:00
|
|
|
for (int i=0; i<ennemis_global_size; i++) if (boxContact(&ennemis_global[i].b,b)) return 1;
|
2020-02-16 12:51:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-16 20:43:35 +01:00
|
|
|
bool boxContact(box_t const * b1, box_t const * b2)
|
2020-02-16 12:51:27 +01:00
|
|
|
{
|
2020-02-17 10:45:11 +01:00
|
|
|
if (b1==b2) return 0;
|
2020-02-16 12:51:27 +01:00
|
|
|
int x_collide= (b2->x >= b1->x && b2->x < b1->x+b1->w) || (b1->x >= b2->x && b1->x < b2->x + b2->w);
|
|
|
|
int y_collide= (b2->y >= b1->y && b2->y < b1->y+b1->h) || (b1->y >= b2->y && b1->y < b2->y + b2->h);
|
|
|
|
if (x_collide&&y_collide) return 1;
|
2019-12-07 19:26:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-21 19:30:54 +01:00
|
|
|
static void move_x(box_t * b)
|
|
|
|
{
|
|
|
|
int sgn_vx=sgn(b->vx);
|
2019-12-11 16:31:18 +01:00
|
|
|
|
2020-02-04 20:12:17 +01:00
|
|
|
int t_vx=((sgn_vx*b->vx+time_id%2)/2)*sgn_vx; // si n impair, alterne entre n+1 n-1
|
2019-12-11 16:31:18 +01:00
|
|
|
sgn_vx=sgn(t_vx);
|
2020-01-06 20:56:10 +01:00
|
|
|
|
2020-02-17 10:45:11 +01:00
|
|
|
int coef=1;// move each frame
|
|
|
|
if (worldGetCellCategory(b->x, b->y)==CTG_WATER || worldGetCellCategory(b->x+b->w-1, b->y)==CTG_WATER) coef=2; //slow down in water
|
2020-02-17 17:53:37 +01:00
|
|
|
if (time_id%coef) return; // move 1 frame of 2 when in water ~to be improved
|
2020-01-15 20:42:51 +01:00
|
|
|
|
2019-11-21 19:30:54 +01:00
|
|
|
if (sgn_vx)
|
|
|
|
{
|
2020-02-04 20:12:17 +01:00
|
|
|
for (int i=1; i<=sgn_vx*t_vx; i++)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
|
|
|
for (int j=0; j<b->h; j++)
|
|
|
|
{
|
2020-01-29 14:34:47 +01:00
|
|
|
int typetemp;
|
|
|
|
if (sgn_vx>0)
|
2020-02-16 20:43:35 +01:00
|
|
|
typetemp=worldGetCellCategory(b->x+b->w-1+i*sgn_vx,b->y+j);
|
2020-01-29 14:34:47 +01:00
|
|
|
else
|
2020-02-16 20:43:35 +01:00
|
|
|
typetemp=worldGetCellCategory(b->x+i*sgn_vx,b->y+j);
|
2020-01-29 14:34:47 +01:00
|
|
|
if (typetemp==CTG_SOIL)
|
|
|
|
{
|
|
|
|
b->x+=(i-1)*sgn_vx;
|
|
|
|
b->vx=0;
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 19:30:54 +01:00
|
|
|
|
2020-01-27 16:46:37 +01:00
|
|
|
//}
|
2019-11-21 19:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 16:31:18 +01:00
|
|
|
b->x+=t_vx;
|
2019-11-21 19:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:24:16 +01:00
|
|
|
|
2019-11-21 19:30:54 +01:00
|
|
|
static void move_y(box_t * b)
|
|
|
|
{
|
2019-12-08 16:34:32 +01:00
|
|
|
b->last_vy=b->vy;
|
2019-11-21 19:30:54 +01:00
|
|
|
int sgn_vy=sgn(b->vy);
|
2020-02-16 12:51:27 +01:00
|
|
|
|
2020-01-15 20:42:51 +01:00
|
|
|
int coef=1;//1;
|
2020-02-16 20:43:35 +01:00
|
|
|
if (worldGetCellCategory(b->x, b->y)==CTG_WATER || worldGetCellCategory(b->x+b->w-1, b->y)==CTG_WATER)
|
2020-02-16 12:51:27 +01:00
|
|
|
coef=2;// 0.5
|
2020-02-17 17:53:37 +01:00
|
|
|
if (time_id%coef)
|
2020-02-16 12:51:27 +01:00
|
|
|
return;
|
2020-01-15 20:42:51 +01:00
|
|
|
|
2019-11-21 19:30:54 +01:00
|
|
|
if (sgn_vy)
|
|
|
|
{
|
|
|
|
for (int i=sgn_vy; i<=sgn_vy*b->vy; i++)
|
|
|
|
{
|
2019-11-24 11:14:16 +01:00
|
|
|
for (int j=0; j<b->w; j++)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
2020-01-29 14:34:47 +01:00
|
|
|
int typetemp;
|
|
|
|
if (sgn_vy>0)
|
2020-02-16 20:43:35 +01:00
|
|
|
typetemp=worldGetCellCategory(b->x+j ,b->y+b->h-1+i);
|
2020-01-29 14:34:47 +01:00
|
|
|
else
|
2020-02-16 20:43:35 +01:00
|
|
|
typetemp=worldGetCellCategory(b->x+j ,b->y-i);
|
2020-01-29 14:34:47 +01:00
|
|
|
if (typetemp==CTG_SOIL)
|
|
|
|
{
|
|
|
|
if (b->vy>0)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
2020-01-29 14:34:47 +01:00
|
|
|
int old=b->x;
|
2020-02-17 10:45:11 +01:00
|
|
|
|
|
|
|
{ // contournement du coin si seul le coin touche
|
|
|
|
box_t temp_hitbox=*b;
|
|
|
|
|
|
|
|
int sgn_vx=0;
|
|
|
|
|
|
|
|
if (worldGetCellCategory(b->x+2, b->y+b->h-1+i)==CTG_SOIL && worldGetCellCategory(b->x+3, b->y+b->h-1+i)==CTG_EMPTY)
|
|
|
|
sgn_vx++;
|
|
|
|
if (worldGetCellCategory(b->x+1, b->y+b->h-1+i)==CTG_SOIL && worldGetCellCategory(b->x+2, b->y+b->h-1+i)==CTG_EMPTY)
|
|
|
|
sgn_vx++;
|
|
|
|
if (worldGetCellCategory(b->x, b->y+b->h-1+i)==CTG_SOIL && worldGetCellCategory(b->x+1, b->y+b->h-1+i)==CTG_EMPTY)
|
|
|
|
sgn_vx++;
|
|
|
|
|
|
|
|
if (worldGetCellCategory(b->x+b->w-2, b->y+b->h-1+i)==CTG_SOIL && worldGetCellCategory(b->x+b->w-3, b->y+b->h-1+i)==CTG_EMPTY)
|
|
|
|
sgn_vx--;
|
|
|
|
if (worldGetCellCategory(b->x+b->w-1, b->y+b->h-1+i)==CTG_SOIL && worldGetCellCategory(b->x+b->w-2, b->y+b->h-1+i)==CTG_EMPTY)
|
|
|
|
sgn_vx--;
|
|
|
|
|
|
|
|
int fail=0;
|
|
|
|
for (int i=0; i<abs(sgn_vx); i++)
|
|
|
|
{
|
|
|
|
for (int j=0; j<b->h; j++) // test de validité
|
|
|
|
{
|
|
|
|
int typetemp;
|
|
|
|
if (sgn_vx>0) typetemp=worldGetCellCategory(temp_hitbox.x+temp_hitbox.w-1+i*sgn_vx,temp_hitbox.y+j);
|
|
|
|
else typetemp=worldGetCellCategory(temp_hitbox.x+i*sgn_vx,temp_hitbox.y+j);
|
|
|
|
|
|
|
|
if (typetemp==CTG_SOIL) fail=1;
|
|
|
|
}
|
|
|
|
temp_hitbox.x+=sgn(sgn_vx);
|
|
|
|
}
|
|
|
|
if (!fail) *b=temp_hitbox;
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:34:47 +01:00
|
|
|
if (old==b->x)
|
2019-12-05 19:24:16 +01:00
|
|
|
{
|
|
|
|
b->y+=(i-1)*sgn_vy;
|
|
|
|
b->vy=0;
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 19:30:54 +01:00
|
|
|
}
|
2020-01-29 14:34:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
b->y+=(i-1)*sgn_vy;
|
|
|
|
b->vy=0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 16:46:37 +01:00
|
|
|
//}
|
2019-11-21 19:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
b->y+=b->vy;
|
|
|
|
}
|
2020-02-16 12:51:27 +01:00
|
|
|
//if (b->vy>=-6)
|
2020-01-06 20:56:10 +01:00
|
|
|
b->vy-=b->gravity;
|
2019-11-21 19:30:54 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-16 20:43:35 +01:00
|
|
|
void boxMove(box_t * b)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
2020-02-16 12:51:27 +01:00
|
|
|
//velx
|
2019-11-21 19:30:54 +01:00
|
|
|
move_x(b);
|
|
|
|
move_y(b);
|
|
|
|
}
|
|
|
|
|
2020-02-16 20:43:35 +01:00
|
|
|
void boxJump(box_t * b, int height)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
2019-11-21 19:56:37 +01:00
|
|
|
int sgn_vy=-1*sgn(height);
|
|
|
|
if (sgn_vy)
|
2019-11-21 19:30:54 +01:00
|
|
|
{
|
2020-01-15 20:42:51 +01:00
|
|
|
int sol=0, eau=0;
|
2020-01-11 16:01:11 +01:00
|
|
|
for (int j=0; j<b->w; j++)
|
2019-11-21 19:56:37 +01:00
|
|
|
{
|
2020-02-16 20:43:35 +01:00
|
|
|
int typetemp=worldGetCellCategory(b->x+j ,b->y+sgn_vy);
|
2020-01-15 20:42:51 +01:00
|
|
|
if (typetemp==CTG_SOIL)
|
2020-02-16 12:51:27 +01:00
|
|
|
sol++;
|
2020-01-15 20:42:51 +01:00
|
|
|
if (typetemp==CTG_WATER)
|
2020-02-16 12:51:27 +01:00
|
|
|
eau++;
|
2019-11-21 19:56:37 +01:00
|
|
|
}
|
|
|
|
if (sol)
|
2020-02-16 12:51:27 +01:00
|
|
|
b->vy=height;
|
2020-01-15 20:42:51 +01:00
|
|
|
else if (eau)
|
2020-02-16 12:51:27 +01:00
|
|
|
b->vy+=height/2;
|
2019-11-21 19:30:54 +01:00
|
|
|
}
|
2020-02-16 12:51:27 +01:00
|
|
|
}
|