75 lines
1.4 KiB
C
75 lines
1.4 KiB
C
#include <camera.h>
|
|
|
|
#include <mario.h>
|
|
#include <world.h>
|
|
#include <base.h>
|
|
#include <keyboard.h>
|
|
|
|
static int y=0;
|
|
static int last_vy=0;
|
|
static int immobile=0;
|
|
static int distance=0;
|
|
static int delta_y=0; // the player can choose the y positionning of the camera with arrows
|
|
|
|
int cameraX() {return min(max(mario.p.x-40,0),map_current->w*8-128);}
|
|
int cameraY() {return max(max(y-32,0)-delta_y,0);}
|
|
|
|
void cameraMove() // only vertical movement
|
|
{
|
|
if (MKB_getKeyState(MK_UP) && abs(delta_y)<24) delta_y-=4;
|
|
if (MKB_getKeyState(MK_DOWN) && abs(delta_y)<24) delta_y+=4;
|
|
if (!MKB_getKeyState(MK_DOWN) && !MKB_getKeyState(MK_UP)) delta_y-=4*sgn(delta_y);
|
|
|
|
static int camera_vy=0;
|
|
if (mario.p.y==last_vy) // mario arrêté
|
|
immobile++;
|
|
else
|
|
{
|
|
last_vy=mario.p.y;
|
|
immobile=0;
|
|
}
|
|
if (mario.p.y-cameraY()-mario.p.h<16 || mario.p.y-cameraY()>54)
|
|
{
|
|
y+=5*sgn(mario.p.y-y);
|
|
}
|
|
|
|
if (immobile>=2)
|
|
{
|
|
//y+=(mario.p.y-y)/3;
|
|
if (camera_vy==0)
|
|
{
|
|
distance=2*(mario.p.y-y)*sgn(mario.p.y-y);
|
|
}
|
|
|
|
//décéleration après avoir parvouru la moitié de la distance, sinon acceleration
|
|
if (camera_vy*(camera_vy+1)>distance)
|
|
camera_vy--;
|
|
else
|
|
camera_vy++;
|
|
//y++;
|
|
|
|
if (camera_vy<0)
|
|
camera_vy=0;
|
|
|
|
if (((y+camera_vy*sgn(mario.p.y-y)/2)-(mario.p.y))*sgn(mario.p.y-y)>0)
|
|
{
|
|
y=mario.p.y;
|
|
camera_vy=0;
|
|
}
|
|
else
|
|
y+=(camera_vy*sgn(mario.p.y-y))/2;
|
|
|
|
}
|
|
last_vy=mario.p.y;
|
|
}
|
|
|
|
void cameraAdjust()
|
|
{
|
|
y=mario.p.y;
|
|
}
|
|
|
|
void cameraReset()
|
|
{
|
|
y=0;
|
|
}
|