supercasiobros/src/box.c

104 lines
1.6 KiB
C
Executable File

#include "box.h"
#include "world.h"
int sgn(int x)
{
if (x==0)
return 0;
else if (x>0)
return 1;
return -1;
}
static void move_x(box_t * b)
{
int sgn_vx=sgn(b->vx);
if (sgn_vx)
{
for (int i=sgn_vx; i<=sgn_vx*b->vx; i++)
{
int previous_tested_y=-545;
int type=CTG_EMPTY;
for (int j=0; j<b->h; j++)
{
int t=(b->y+j)/8;
if (t!=previous_tested_y)
{
previous_tested_y=t;
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_DEATH || typetemp==CTG_SOIL)
{
b->x+=(i-1)*sgn_vx;
b->vx=0;
return;
}
}
}
}
b->x+=b->vx;
}
}
static void move_y(box_t * b)
{
int sgn_vy=sgn(b->vy);
if (sgn_vy)
{
for (int i=sgn_vy; i<=sgn_vy*b->vy; i++)
{
int previous_tested_x=-545;
for (int j=0; j<b->w; j++)
{
int t=(b->x+j)/8;
if (t!=previous_tested_x)
{
previous_tested_x=t;
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_DEATH || typetemp==CTG_SOIL)
{
b->y+=(i-1)*sgn_vy;
b->vy=0;
return;
}
}
}
}
b->y+=b->vy;
}
b->vy-=b->gravity;
}
int box_move(box_t * b)
{
//velx
move_x(b);
move_y(b);
}
int box_jump(box_t * b, int height)
{
int sol=0;
int sgn_vy=-1*sgn(height);
if (sgn_vy)
{
for (int j=0; j<b->h; j++)
{
int typetemp=world_get_ctg(b->x+j ,b->y+sgn_vy);
if (typetemp==CTG_DEATH || typetemp==CTG_SOIL)
sol=1;
}
if (sol)
b->vy=height;
}
}