supercasiobros/src/box.c

159 lines
3.5 KiB
C
Raw Normal View History

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"
#include "score.h"
#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 12:51:27 +01:00
for (int i=0; i<ennemis_global_size; i++) if (BoxContact(&ennemis_global[i].b,b)) return 1;
return 0;
}
int BoxContact(box_t const * b1, box_t const * b2)
{
if (b1==b2) return 0;/*
bool x_collide= (mario.p.x<=e->b.x && e->b.x<=mario.p.x+mario.p.w-1) || (mario.p.x<=e->b.x+e->b.w-1 && e->b.x+e->b.w-1<=mario.p.x+mario.p.w-1);
bool y_collide= (mario.p.y<=e->b.y && e->b.y<=mario.p.y+mario.p.h-1) || (mario.p.y<=e->b.y+e->b.h-1 && e->b.y+e->b.h-1<=mario.p.y+mario.p.h-1);*/
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);
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);
2020-01-06 20:56:10 +01:00
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)
2020-02-16 12:51:27 +01:00
coef=2;//ralentir dans l'eau
if (frame_id%coef)
2020-02-16 12:51:27 +01:00
return;
2019-11-21 19:30:54 +01:00
if (sgn_vx)
{
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 12:51:27 +01:00
typetemp=world_get_ctg(b->x+b->w-1+i*sgn_vx,b->y+j);
2020-01-29 14:34:47 +01:00
else
2020-02-16 12:51:27 +01:00
typetemp=world_get_ctg(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
//}
2019-11-21 19:30:54 +01:00
}
}
b->x+=t_vx;
2019-11-21 19:30:54 +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
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)
2020-02-16 12:51:27 +01:00
coef=2;// 0.5
if (frame_id%coef)
2020-02-16 12:51:27 +01:00
return;
2019-11-21 19:30:54 +01:00
if (sgn_vy)
{
for (int i=sgn_vy; i<=sgn_vy*b->vy; i++)
{
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 12:51:27 +01:00
typetemp=world_get_ctg(b->x+j ,b->y+b->h-1+i);
2020-01-29 14:34:47 +01:00
else
2020-02-16 12:51:27 +01:00
typetemp=world_get_ctg(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;
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)
2020-02-16 12:51:27 +01:00
b->x++;
2020-01-29 14:34:47 +01:00
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)
2020-02-16 12:51:27 +01:00
b->x++;
2020-01-29 14:34:47 +01:00
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)
2020-02-16 12:51:27 +01:00
b->x++;
2020-01-29 14:34:47 +01:00
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)
2020-02-16 12:51:27 +01:00
b->x--;
2020-01-29 14:34:47 +01:00
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)
2020-02-16 12:51:27 +01:00
b->x--;
2020-01-29 14:34:47 +01:00
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)
2020-02-16 12:51:27 +01:00
b->x--;
2020-01-29 14:34:47 +01:00
if (old==b->x)
{
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;
}
}
//}
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-01-29 14:34:47 +01:00
void box_move(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-01-29 14:34:47 +01:00
void box_jump(box_t * b, int height)
2019-11-21 19:30:54 +01:00
{
int sgn_vy=-1*sgn(height);
if (sgn_vy)
2019-11-21 19:30:54 +01:00
{
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)
2020-02-16 12:51:27 +01:00
sol++;
if (typetemp==CTG_WATER)
2020-02-16 12:51:27 +01:00
eau++;
}
if (sol)
2020-02-16 12:51:27 +01:00
b->vy=height;
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
}