supercasiobros/src/levelchanger.c

336 lines
5.8 KiB
C
Raw Normal View History

2020-01-07 21:31:34 +01:00
#include <levelchanger.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
2020-01-08 15:43:58 +01:00
#include <gint/clock.h>
#include <gint/std/stdio.h>
2020-01-07 21:31:34 +01:00
#include <keyboard.h>
#include <mario.h>
#include <world.h>
2020-01-08 15:43:58 +01:00
#include <score.h>
#include <level.h>
#include <save.h>
#include <config.h>
2020-01-25 21:06:34 +01:00
#include <framerate.h>
2020-01-29 18:24:33 +01:00
#include <camera.h>
#include <bonus.h>
2020-01-13 20:26:04 +01:00
static void LevelChanger(int world)
2020-01-13 20:26:04 +01:00
{
int play_level(int, int); // déclaration de la fonction codée plus bas
2020-01-13 20:26:04 +01:00
extern image_t img_levelchanger;
unsigned int choice=0;
2020-01-13 20:26:04 +01:00
// x y, first button coordinates and size
const int xt = 19, yt = 7, wt = 6, ht = 6;
while (1)
{
2020-01-29 14:34:47 +01:00
mkb_clear();
2020-01-13 20:26:04 +01:00
dimage(0,0,&img_levelchanger);
drect(xt+(2+wt)*choice, yt, xt+(wt+2)*choice+wt,yt+ht, C_INVERT);
char str[8];
if (choice != 8)
{
sprintf(str, "%d", choice+1);
dtext(45,19, str, C_BLACK, C_WHITE);
}
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])
2020-01-19 20:17:14 +01:00
i++;
dtext(99-6*i, 45, str, C_BLACK, C_WHITE);
}
2020-01-19 20:17:14 +01:00
if (get_highcoins(world,choice))
{
sprintf(str, "%d", get_highcoins(world,choice));
int i=0;
while (str[i])
2020-01-25 11:54:39 +01:00
i++;
dtext(90-6*i, 37, str, C_BLACK, C_WHITE);
}
2020-01-25 11:54:39 +01:00
2020-01-13 20:26:04 +01:00
dupdate();
//int key=getkey_opt(GETKEY_REP_ARROWS,0).key;
switch (mkb_getkey())
2020-01-13 20:26:04 +01:00
{
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:
2020-01-13 20:26:04 +01:00
if (choice==8)
{
new_game();
mario_smaller();
mario_immunity=0;
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=play_level(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());
2020-01-13 20:26:04 +01:00
}
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 (play_level(world, choice))
2020-01-19 19:59:13 +01:00
{
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
2020-01-19 19:59:13 +01:00
}
2020-01-13 20:26:04 +01:00
}
break;
case KEY_EXIT:
// fall through
case KEY_MENU:
return;
}
2020-01-13 20:26:04 +01:00
}
2020-01-13 20:26:04 +01:00
}
2020-01-07 21:31:34 +01:00
void LaunchUI() // Main Menu
2020-01-07 21:31:34 +01:00
{
extern image_t img_mainmenu;
2020-01-13 20:26:04 +01:00
2020-01-08 15:43:58 +01:00
const int xt=17,yt=9;
unsigned int choice_x=0, choice_y=0;
2020-01-07 21:31:34 +01:00
while (1)
{
2020-01-29 14:34:47 +01:00
mkb_clear();
2020-01-07 21:31:34 +01:00
dimage(0,0,&img_mainmenu);
// Draw world images, but only unlocked ones
2020-01-08 15:43:58 +01:00
//Show unlocked worlds
2020-01-08 15:43:58 +01:00
extern image_t img_w1, img_w2, img_w3, img_w4, img_w5;
switch (get_progress_status())
{
case 4:
dimage(xt, yt+24, &img_w5);
2020-01-29 14:34:47 +01:00
// fall through
2020-01-08 15:43:58 +01:00
case 3:
dimage(xt+72, yt, &img_w4);
2020-01-29 14:34:47 +01:00
// fall through
2020-01-08 15:43:58 +01:00
case 2:
dimage(xt+48, yt, &img_w3);
2020-01-29 14:34:47 +01:00
// fall through
2020-01-08 15:43:58 +01:00
case 1:
dimage(xt+24, yt, &img_w2);
2020-01-29 14:34:47 +01:00
// fall through
2020-01-08 15:43:58 +01:00
case 0:
dimage(xt, yt, &img_w1);
}
// invert color of selected world
2020-01-08 15:43:58 +01:00
drect(xt+24*choice_x,yt+24*choice_y, xt+24*choice_x+20,yt+24*choice_y+20, C_INVERT);
2020-01-07 21:31:34 +01:00
dupdate();
2020-01-08 16:16:05 +01:00
//int key=getkey_opt(GETKEY_REP_ARROWS,0).key;
switch (mkb_getkey())
2020-01-08 16:16:05 +01:00
{
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;
2020-01-13 20:26:04 +01:00
default:
if (4*choice_y+choice_x<=get_progress_status())
LevelChanger(4*choice_y+choice_x);
}
2020-01-07 21:31:34 +01:00
break;
case KEY_EXIT:
// fall through
case KEY_MENU:
return;
}
2020-01-07 21:31:34 +01:00
}
}
2020-01-13 20:26:04 +01:00
2020-01-07 21:31:34 +01:00
int frame_id;
int play_level(int w, int l)
2020-01-07 21:31:34 +01:00
{
2020-01-25 21:06:34 +01:00
init_refresh();
2020-01-08 15:43:58 +01:00
//int finish_status=0; // FAil
finish_level=0; mario_dead=0;
set_level(w, l);
if (map_current==0)
{
timer_stop(0);
return -1;
}
2020-01-29 14:34:47 +01:00
camera_adjust();
bonus_set(0, 0, 0);
2020-01-07 21:31:34 +01:00
while(global_quit==0)
{
2020-01-25 21:06:34 +01:00
wait_next_frame();
2020-01-25 21:06:34 +01:00
frame_id++;
//mario_move();
2020-01-07 21:31:34 +01:00
2020-01-25 21:06:34 +01:00
world_move();
dclear(C_WHITE);
world_draw(mario.p.x,mario.p.y);
score_display();
2020-01-07 21:31:34 +01:00
2020-01-25 21:06:34 +01:00
dupdate();
if (mario_dead)
{
mario_immunity=0;
int i=6;
while(mario.p.y>=0)
2020-01-07 21:31:34 +01:00
{
2020-01-25 21:06:34 +01:00
wait_next_frame();
mario.p.y+=i;
dclear(C_WHITE);
world_draw(mario.p.x,mario.p.y);
mario_draw();
score_display();
2020-01-25 21:06:34 +01:00
dupdate();
i--;
}
sleep_ms(3,1000);
2020-01-25 21:06:34 +01:00
quit_refresh();
2020-01-25 21:06:34 +01:00
return 0;
}
if (finish_level)
{
quit_refresh();
// TODO ajouter temps au score etc
2020-01-25 21:06:34 +01:00
if (finish_level==1)
sleep_ms(3,3000);
2020-01-25 21:06:34 +01:00
return finish_level;
2020-01-07 21:31:34 +01:00
}
2020-01-25 21:06:34 +01:00
2020-01-07 21:31:34 +01:00
}
return 0;
}