supercasiobros/src/score.c

142 lines
2.1 KiB
C

#include "score.h"
#include "mario.h"
#include <gint/display.h>
#include <gint/std/stdio.h>
#include <gint/clock.h>
#include <gint/timer.h>
// displaying time for volatile counters: here=1.5sec
#define DISPLAY_TIME 30
static int score=0;
static int lifes=3;
int combo=0;
int combo_id=-10;
static int pieces=0;
static unsigned int time_last_piece=0;
unsigned int time_id=0;
static int time_left=0;
int time_spent=0;
int finish_level=0;
void gameNew()
{
lifes=3;
pieces=0;
score=0;
combo=0;
combo_id=-10;
marioSmaller();
mario_immunity=0;
levelNew();
time_spent=0;
}
void levelNew()
{
finish_level=0;
time_left=400;
time_id=0;
}
int scoreGet()
{
return score;
}
int coinsGet()
{
return pieces;
}
void scoreReset()
{
score=0;
combo=0;
combo_id=-10;
}
void coinAdd()
{
pieces++;
time_last_piece=time_id;
scoreAdd(200);
}
void scoreAdd(int i)
{
if (i==0) return;
if (time_id-combo_id<=12 && combo)
{
score+=i;
combo_id=time_id;
combo+=i;
}
else
{
combo_id=time_id;
combo=i;
score+=i;
}
}
void scoreDisplay()
{
char str[10];
if (time_id<=(unsigned int)(combo_id+DISPLAY_TIME) && time_id>=(unsigned int)(combo_id)) //displays score
{
sprintf(str, "%d", score);
dtext(0,0,str, C_BLACK, C_WHITE);
}
if (time_id-combo_id<=8 && combo) // & combo only when there is a combo
{
sprintf(str, "+ %d", combo);
dtext(0,6,str, C_BLACK, C_WHITE);
}
else combo=0; // combo too old
if (time_last_piece<=time_id && time_id<=time_last_piece+DISPLAY_TIME) // display pieces only when 1 is taken during 2 seconds
{
sprintf(str, "c*%d", pieces);
dtext(50,0,str, C_BLACK, C_WHITE);
}
if ((mario_dead|finish_level)==0)
{
if (0==(time_id++)%8)
{
time_left--;
time_spent++;
if (time_left==0)
{
extern image_t img_time_over;
dimage(0,0, &img_time_over); dupdate();
sleep_ms(3,3000);
finish_level=2;
return;
}
}
}
sprintf(str, "%d", time_left);
int i=0; while (str[i]) i++;
dtext(128-6*i, 1, str, C_BLACK, C_WHITE);
}
int getTimeSpent(void) { return time_spent; }
int lifesGet(void) { return lifes; }
void lifesSet(int l) { lifes = l; }
void lifesAdd(int l) { lifes += l; }