153 lines
2.7 KiB
C
153 lines
2.7 KiB
C
#include <plateforme.h>
|
|
|
|
#include <world.h>
|
|
#include <mario.h>
|
|
#include <tile.h>
|
|
#include <score.h>
|
|
#include <camera.h>
|
|
#include <level.h>
|
|
#include <gint/std/string.h>
|
|
#include <gint/std/stdlib.h>
|
|
|
|
int plateforme_table_size=0;
|
|
plateforme_t* plateformes=0;
|
|
|
|
|
|
static int sgn(int x)
|
|
{
|
|
if (x==0)
|
|
return 0;
|
|
else if (x>0)
|
|
return 1;
|
|
return -1;
|
|
}
|
|
|
|
|
|
|
|
void reset_plateforme(plateforme_t* p)
|
|
{
|
|
p->x=p->xinit;
|
|
p->y=p->yinit;
|
|
p->v=p->vinit;
|
|
}
|
|
|
|
void move_plateforme(plateforme_t* p)
|
|
{/*
|
|
if (p->x+p->width-world_get_real_x0()<-50)
|
|
return;
|
|
if (p->x-world_get_real_x0()-128>50)
|
|
return;
|
|
|
|
|
|
if (p->y-world_get_real_y0()-64>50)
|
|
return;
|
|
if (p->y+3-world_get_real_y0()<-50)
|
|
return;
|
|
*/
|
|
|
|
int xc = (mario.p.x<p->x+p->width && mario.p.x>=p->x) || (mario.p.x+mario.p.w<p->x+p->width && mario.p.x+mario.p.w>=p->x);
|
|
int yc = (mario.p.y-3==p->y);
|
|
|
|
if (p->type==P_FALLING)
|
|
{
|
|
if (p->v)
|
|
p->v++;
|
|
p->y+=p->v/2;
|
|
}
|
|
|
|
if (p->type==P_MOVING_H)
|
|
{
|
|
int s=sgn(p->v);
|
|
int t_v=((s*p->v+time_id%2)/2)*s;
|
|
if (p->v==0)
|
|
p->v=p->vinit;
|
|
p->x+=t_v;
|
|
if (xc&&yc)
|
|
mario.p.x+=t_v;
|
|
if (p->x+p->width > p->xmax && p->v>0)
|
|
p->v *= -1;
|
|
if (p->x < p->xmin && p->v<0)
|
|
p->v *= -1;
|
|
}
|
|
|
|
if (p->type==P_MOVING_V)
|
|
{
|
|
int s=sgn(p->v);
|
|
int t_v=((s*p->v+time_id%2)/2)*s;
|
|
if (p->v==0)
|
|
p->v=p->vinit;
|
|
p->y+=t_v;
|
|
if (p->y > p->ymax || p->y+3 < p->ymin)
|
|
p->v *= -1;
|
|
}
|
|
|
|
if (xc&&yc)
|
|
{
|
|
int s=sgn(p->v);
|
|
int t_v=((s*p->v+time_id%2)/2)*s;
|
|
if (p->type==P_FALLING)
|
|
mario.p.y+=p->v/2;
|
|
if (p->type==P_MOVING_V)
|
|
mario.p.y+=t_v;
|
|
if (p->type==P_MOVING_H)
|
|
mario.p.x+=t_v/2;
|
|
}
|
|
}
|
|
|
|
void display_plateformes()
|
|
{
|
|
if (plateformes==0 || plateforme_table_size==0)
|
|
return;
|
|
plateforme_t * p=0;
|
|
for (int i=0; i<plateforme_table_size; i++)
|
|
{
|
|
p=&plateformes[i];
|
|
for (int j=p->x; j < p->x+p->width; j+=3)
|
|
draw_tile(j-camera_x(),p->y-camera_y(), &tplateforme, 0,0);
|
|
//drect(p->x-world_get_real_x0(),64-(p->y-world_get_real_y0()),p->x-world_get_real_x0()+p->width,64-(p->y-world_get_real_y0()-3), C_BLACK);
|
|
}
|
|
}
|
|
|
|
void move_plateformes()
|
|
{
|
|
if (plateformes==0 || plateforme_table_size==0)
|
|
return;
|
|
plateforme_t * p=0;
|
|
for (int i=0; i<plateforme_table_size; i++)
|
|
{
|
|
p=&plateformes[i];
|
|
move_plateforme(p);
|
|
}
|
|
}
|
|
|
|
int plateforme_check_collide(int x, int y)
|
|
{
|
|
if (plateformes==0 || plateforme_table_size==0)
|
|
return 0;
|
|
for (int i=0; i<plateforme_table_size; i++)
|
|
{
|
|
plateforme_t * p=&plateformes[i];
|
|
if (p->x<=x && p->x+p->width>x && p->y<=y && p->y+3>y)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
void init_plateformes(plateforme_t * table, int s)
|
|
{
|
|
plateforme_table_size=s;
|
|
if (plateformes)
|
|
{
|
|
free(plateformes);
|
|
plateformes=0;
|
|
}
|
|
if (0==table)
|
|
return;
|
|
int size=sizeof(plateforme_t)*plateforme_table_size;
|
|
plateformes=malloc(size);
|
|
if (plateformes==0)
|
|
malloc_error();
|
|
|
|
memcpy(plateformes, table, size);
|
|
} |