supercasiobros/src/box.c

155 lines
3.1 KiB
C
Executable File

#include "box.h"
#include "world.h"
#include "ennemi.h"
#include "score.h"
#include <constants.h>
#include <base.h>
int check_collision(box_t *b)
{
for (int i=0; i<ennemis_global_size; i++)
{
box_t* t=&ennemis_global[i].b;
if (t!=b)
{
int x_collide= (t->x<=b->x && b->x<t->x+t->w) || (t->x<=b->x+b->w-1 && b->x+b->w<t->x+t->w);
int y_collide= (t->y<=b->y && b->y<t->y+t->h) || (t->y<=b->y+b->h-1 && b->y+b->h<t->y+t->h);
if (x_collide&&y_collide)
return 1;
}
}
return 0;
}
static void move_x(box_t * b)
{
int sgn_vx=sgn(b->vx);
int t_vx=((sgn_vx*b->vx+time_id%2)/2)*sgn_vx; // si n impair, alterne entre n+1 n-1
sgn_vx=sgn(t_vx);
int coef=1;//1;
if (world_get_ctg(b->x, b->y)==CTG_WATER || world_get_ctg(b->x+b->w-1, b->y)==CTG_WATER)
coef=2;//ralentir dans l'eau
if (frame_id%coef)
return;
if (sgn_vx)
{
for (int i=1; i<=sgn_vx*t_vx; i++)
{
for (int j=0; j<b->h; j++)
{
int typetemp;
if (sgn_vx>0)
typetemp=world_get_ctg(b->x+b->w-1+i*sgn_vx,b->y+j);
else
typetemp=world_get_ctg(b->x+i*sgn_vx,b->y+j);
if (typetemp==CTG_SOIL)
{
b->x+=(i-1)*sgn_vx;
b->vx=0;
return;
}
//}
}
}
b->x+=t_vx;
}
}
static void move_y(box_t * b)
{
b->last_vy=b->vy;
int sgn_vy=sgn(b->vy);
int coef=1;//1;
if (world_get_ctg(b->x, b->y)==CTG_WATER || world_get_ctg(b->x+b->w-1, b->y)==CTG_WATER)
coef=2;// 0.5
if (frame_id%coef)
return;
if (sgn_vy)
{
for (int i=sgn_vy; i<=sgn_vy*b->vy; i++)
{
for (int j=0; j<b->w; j++)
{
int typetemp;
if (sgn_vy>0)
typetemp=world_get_ctg(b->x+j ,b->y+b->h-1+i);
else
typetemp=world_get_ctg(b->x+j ,b->y-i);
if (typetemp==CTG_SOIL)
{
if (b->vy>0)
{
int old=b->x;
if (world_get_ctg(b->x+2, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+3, b->y+b->h-1+i)==CTG_EMPTY)
b->x++;
if (world_get_ctg(b->x+1, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+2, b->y+b->h-1+i)==CTG_EMPTY)
b->x++;
if (world_get_ctg(b->x, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+1, b->y+b->h-1+i)==CTG_EMPTY)
b->x++;
if (world_get_ctg(b->x+b->w-3, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+b->w-4, b->y+b->h-1+i)==CTG_EMPTY)
b->x--;
if (world_get_ctg(b->x+b->w-2, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+b->w-3, b->y+b->h-1+i)==CTG_EMPTY)
b->x--;
if (world_get_ctg(b->x+b->w-1, b->y+b->h-1+i)==CTG_SOIL && world_get_ctg(b->x+b->w-2, b->y+b->h-1+i)==CTG_EMPTY)
b->x--;
if (old==b->x)
{
b->y+=(i-1)*sgn_vy;
b->vy=0;
return;
}
}
else
{
b->y+=(i-1)*sgn_vy;
b->vy=0;
return;
}
}
//}
}
}
b->y+=b->vy;
}
//if (b->vy>=-6)
b->vy-=b->gravity;
}
void box_move(box_t * b)
{
//velx
move_x(b);
move_y(b);
}
void box_jump(box_t * b, int height)
{
int sgn_vy=-1*sgn(height);
if (sgn_vy)
{
int sol=0, eau=0;
for (int j=0; j<b->w; j++)
{
int typetemp=world_get_ctg(b->x+j ,b->y+sgn_vy);
if (typetemp==CTG_SOIL)
sol++;
if (typetemp==CTG_WATER)
eau++;
}
if (sol)
b->vy=height;
else if (eau)
b->vy+=height/2;
}
}