supercasiobros/src/box.c

174 lines
3.9 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++) if (boxContact(&ennemis_global[i].b,b)) return 1;
return 0;
}
bool boxContact(box_t const * b1, box_t const * b2)
{
if (b1==b2) return 0;
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;
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;// 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
if (time_id%coef) return; // move 1 frame of 2 when in water ~to be improved
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=worldGetCellCategory(b->x+b->w-1+i*sgn_vx,b->y+j);
else
typetemp=worldGetCellCategory(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 (worldGetCellCategory(b->x, b->y)==CTG_WATER || worldGetCellCategory(b->x+b->w-1, b->y)==CTG_WATER)
coef=2;// 0.5
if (time_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=worldGetCellCategory(b->x+j ,b->y+b->h-1+i);
else
typetemp=worldGetCellCategory(b->x+j ,b->y-i);
if (typetemp==CTG_SOIL)
{
if (b->vy>0)
{
int old=b->x;
{ // 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;
}
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 boxMove(box_t * b)
{
//velx
move_x(b);
move_y(b);
}
void boxJump(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=worldGetCellCategory(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;
}
}