supercasiobros/src/levelchanger.c

324 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 (get_best_time(world,choice))
{
sprintf(str, "%d", get_best_time(world,choice));
int i=0;
while (str[i])
i++;
dtext(99-6*i, 53, str, C_BLACK, C_WHITE);
}
if (get_highscore(world,choice))
{
sprintf(str, "%d", get_highscore(world,choice));
int i=0;
while (str[i])
i++;
dtext(99-6*i, 45, str, C_BLACK, C_WHITE);
}
if (get_highcoins(world,choice))
{
sprintf(str, "%d", get_highcoins(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)
{
new_game();
for (int i=0; i<7; i+=0)
{
extern image_t img_new_level;
dimage(0,0,&img_new_level);
char lvl[4];
get_lvl_id(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;
}
}
set_highscore(world, choice, get_score());
}
else
{
extern image_t img_new_level;
dimage(0,0,&img_new_level);
char lvl[4];
get_lvl_id(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:
new_game();
switch (PlayLevel(world, choice))
{
case 1: // if level completed
set_best_time(world, choice, get_time_spent());
set_highscore(world, choice, get_score());
set_highcoins(world, choice, get_coins());
break; // go back to menu
case 0: // save score anyways
set_highscore(world, choice, get_score());
set_highcoins(world, choice, get_coins());
goto playagain;
break;
case -1:
set_highscore(world, choice, get_score());
set_highcoins(world, choice, get_coins());
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 (GetProgressStatus())
{
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<=GetProgressStatus())
LevelChanger(4*choice_y+choice_x);
}
break;
case KEY_EXIT:
// fall through
case KEY_MENU:
return;
}
}
}
int frame_id;
int PlayLevel(int w, int l)
{
init_refresh();
//int finish_status=0; // FAil
finish_level=0; mario_dead=0;
set_level(w, l);
if (map_current==0)
{
timer_stop(0);
return -1;
}
camera_adjust();
bonus_set(0, 0, 0);
while(global_quit==0)
{
wait_next_frame();
frame_id++;
//mario_move();
world_move();
dclear(C_WHITE);
world_draw(mario.p.x,mario.p.y);
score_display();
dupdate();
if (mario_dead)
{
mario_immunity=0;
int i=6;
while(mario.p.y>=0)
{
wait_next_frame();
mario.p.y+=i;
dclear(C_WHITE);
world_draw(mario.p.x,mario.p.y);
mario_draw();
score_display();
dupdate();
i--;
}
sleep_ms(3,1000);
quit_refresh();
return 0;
}
if (finish_level)
{
quit_refresh();
// TODO ajouter temps au score etc
if (finish_level==1) sleep_ms(3,3000); // win
return finish_level;
}
}
return 0;
}