supercasiobros/src/levelchanger.c

319 lines
5.7 KiB
C

#include <levelchanger.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include <gint/std/stdio.h>
#include <keyboard.h>
#include <mario.h>
#include <world.h>
#include <score.h>
#include <level.h>
#include <save.h>
#include <config.h>
#include <framerate.h>
#include <camera.h>
#include <bonus.h>
int PlayLevel(int, int); // déclaration de la fonction codée plus bas
static void LevelChanger(int world)
{
MKB_clear(); // clear keyboard
unsigned int choice=0;
while (1)
{
extern image_t img_levelchanger;
dimage(0,0,&img_levelchanger); // Draw menu
drect(19+8*choice, 7, 25+8*choice, 13, C_INVERT); // Invert color of selected level
char str[8];
if (choice != 8)
{
sprintf(str, "%d", choice+1);
dtext(45,19, str, C_BLACK, C_WHITE); // Maybe later replace it by level names
}
else dtext(13,19, "MODE COURSE :", C_BLACK, C_WHITE);
if (saveGetTime(world,choice))
{
sprintf(str, "%d", saveGetTime(world,choice));
int i=0;
while (str[i])
i++;
dtext(99-6*i, 53, str, C_BLACK, C_WHITE);
}
if (saveGetScore(world,choice))
{
sprintf(str, "%d", saveGetScore(world,choice));
int i=0;
while (str[i])
i++;
dtext(99-6*i, 45, str, C_BLACK, C_WHITE);
}
if (saveGetCoins(world,choice))
{
sprintf(str, "%d", saveGetCoins(world,choice));
int i=0;
while (str[i])
i++;
dtext(90-6*i, 37, str, C_BLACK, C_WHITE);
}
dupdate();
//int key=getkey_opt(GETKEY_REP_ARROWS,0).key;
switch (MKB_getkey())
{
case KEY_LEFT:
if (choice) /* >0 */ choice--;
break;
case KEY_RIGHT:
if (choice!=8) /* <8 */ choice++;
break;
case KEY_EXE:
// fall through
case KEY_SHIFT:
if (choice==8)
{
gameNew();
for (int i=0; i<7; i+=0)
{
extern image_t img_new_level;
dimage(0,0,&img_new_level);
char lvl[4];
getLevelID(world, i, lvl);
dtext(53,28, lvl, C_WHITE, C_BLACK);
sprintf(lvl, "%d", lifesGet());
dtext(65,54, lvl, C_WHITE, C_BLACK);
dupdate();
sleep_ms(3,2000);
int a=PlayLevel(world, i);
if (a==0) lifesAdd(-1);
if (a==1) i++;
if (a==-1) break;
if (lifesGet()==0)
{
extern image_t img_game_over;
dimage(0,0,&img_game_over);
dupdate();
sleep_ms(3,2000);
// TODO: Highscore management
break;
}
}
saveSetScore(world, choice, scoreGet());
}
else
{
extern image_t img_new_level;
dimage(0,0,&img_new_level);
char lvl[4];
getLevelID(world, choice, lvl);
dtext(53,28, lvl, C_WHITE, C_BLACK);
sprintf(lvl, "%d", lifesGet());
dtext(65,54, lvl, C_WHITE, C_BLACK);
dupdate();
sleep_ms(3,2000);
playagain:
gameNew();
switch (PlayLevel(world, choice))
{
case 1: // if level completed
saveSetTime(world, choice, getTimeSpent());
saveSetScore(world, choice, scoreGet());
saveSetCoins(world, choice, coinsGet());
break; // go back to menu
case 0: // save score anyways
saveSetScore(world, choice, scoreGet());
saveSetCoins(world, choice, coinsGet());
goto playagain;
break;
case -1:
saveSetScore(world, choice, scoreGet());
saveSetCoins(world, choice, coinsGet());
break; // do not play again
}
}
break;
case KEY_EXIT:
// fall through
case KEY_MENU:
return;
}
}
}
void launchUI() // Main Menu
{
extern image_t img_mainmenu;
const int xt=17,yt=9;
unsigned int choice_x=0, choice_y=0;
while (1)
{
MKB_clear();
dimage(0,0,&img_mainmenu);
// Draw world images, but only unlocked ones
//Show unlocked worlds
extern image_t img_w1, img_w2, img_w3, img_w4, img_w5;
switch (saveGetProgressStatus())
{
case 4:
dimage(xt, yt+24, &img_w5);
// fall through
case 3:
dimage(xt+72, yt, &img_w4);
// fall through
case 2:
dimage(xt+48, yt, &img_w3);
// fall through
case 1:
dimage(xt+24, yt, &img_w2);
// fall through
case 0:
dimage(xt, yt, &img_w1);
}
// invert color of selected world
drect(xt+24*choice_x,yt+24*choice_y, xt+24*choice_x+20,yt+24*choice_y+20, C_INVERT);
dupdate();
//int key=getkey_opt(GETKEY_REP_ARROWS,0).key;
switch (MKB_getkey())
{
case KEY_UP:
if (choice_y) // >0
choice_y--;
break;
case KEY_LEFT:
if (choice_x) // >0
choice_x--;
break;
case (KEY_DOWN):
if (!choice_y) // ==0
choice_y++;
break;
case (KEY_RIGHT):
if (choice_x!=3) // <3
choice_x++;
break;
case KEY_EXE:
// fall through
case KEY_SHIFT:
switch (4*choice_y+choice_x)
{
case 5: // No idea yet !
break;
case 6:
configmenu();
break;
case 7:
return;
default:
if (4*choice_y+choice_x<=saveGetProgressStatus())
LevelChanger(4*choice_y+choice_x);
}
break;
case KEY_EXIT:
// fall through
case KEY_MENU:
return;
}
}
}
int PlayLevel(int w, int l)
{
time_id=0;
initRefreshTimer();
//int finish_status=0; // FAil
finish_level=0; mario.dead=0;
setLevel(w, l);
if (map_current==0)
{
timer_stop(0);
return -1;
}
cameraAdjust();
bonusSet(0, 0, 0);
mario.last_vx_sgn=1;
while(global_quit==0)
{
waitNextFrame();
//marioMove();
worldMove();
dclear(C_WHITE);
worldDraw(mario.p.x,mario.p.y);
scoreDisplay();
dupdate();
if (mario.dead)
{
mario.immunity=0;
int i=6;
while(mario.p.y>=0)
{
waitNextFrame();
mario.p.y+=i;
dclear(C_WHITE);
worldDraw(mario.p.x,mario.p.y);
marioDraw();
scoreDisplay();
dupdate();
i--;
}
sleep_ms(3,1000);
quitRefreshTimer();
return 0;
}
if (finish_level)
{
quitRefreshTimer();
// TODO ajouter temps au score etc
if (finish_level==1) sleep_ms(3,3000); // win
return finish_level;
}
}
return 0;
}