James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Pause/Pause.cpp

Committer:
JamesCummins
Date:
2019-04-18
Revision:
22:4e305ff8a050
Parent:
21:9d1447765ee1
Child:
29:42651f87522b

File content as of revision 22:4e305ff8a050:

#include "Pause.h"

Pause::Pause(){
}

Pause::~Pause(){
}

void Pause::init(){
    _state = RESUME;
}

int Pause::pause_menu(Gamepad &gamepad, N5110 &lcd, int fps, int frame, Mode mode){
    PauseOption choice = RESUME;
    while(!(gamepad.check_event(gamepad.A_PRESSED))){
        lcd.clear();
        display_pause_options(lcd);
        choice = pause_selection(gamepad, lcd);
        lcd.refresh();
        wait(0.2);
    }
    int jump_to_frame = frame;
    if(choice == RESUME){ jump_to_frame = frame; } //Just keep code iterating
    if(choice == RESTART){ jump_to_frame = 0; } //return to frame 1 if restarted
    if(choice == QUIT){ jump_to_frame = 45*fps; } //jump to final frame
    if(choice == HELP){  //display relevant help screen
        if(mode == CLASSIC_MODE){ classic_help(gamepad, lcd); }
        if(mode == BRICKBREAKER_MODE){ brickbreaker_help(gamepad, lcd); }
    }
    return jump_to_frame;
}

void Pause::display_pause_options(N5110 &lcd){
    lcd.printString("GAME PAUSED:", 6, 0);
    lcd.printString("Resume", 24, 1);
    lcd.printString("Restart", 21, 2);
    lcd.printString("Quit", 30, 3);
    lcd.printString("Help", 30, 4);
    lcd.printString("(Select = A)", 6, 5);
}

PauseOption Pause::pause_selection(Gamepad &gamepad, N5110 &lcd){
    PauseSelection fsm[4] = {
        {1,{HELP,RESTART,RESUME}},
        {2,{RESUME,QUIT,RESTART}},
        {3,{RESTART,HELP,QUIT}},
        {4,{QUIT,RESUME,HELP}}
    };
    if(gamepad.get_direction() == N){ _next_state = 0;}
    else if(gamepad.get_direction() == S){ _next_state = 1;}
    else{ _next_state = 2;}
    _state = fsm[_state].next_state[_next_state];
    lcd.printString(">", 6, fsm[_state].output);
    lcd.printString("<", 72, fsm[_state].output);
    return _state;
}

void Pause::brickbreaker_help(Gamepad &gamepad, N5110 &lcd){
    while(!(gamepad.check_event(gamepad.A_PRESSED))){
        lcd.clear();
        lcd.printString("Help", 30, 0);
        lcd.printString("Tilt gamepad", 0, 1);
        lcd.printString("to move ball", 0, 2);
        lcd.printString("Game length=", 0, 3);
        lcd.printString("45 secs", 0, 4);
        lcd.printString("A = go back", 0, 5);
        lcd.refresh();
        wait(0.5);
    }
}

void Pause::classic_help(Gamepad &gamepad, N5110 &lcd){
    while(!(gamepad.check_event(gamepad.A_PRESSED))){
        lcd.clear();
        lcd.printString("Help", 30, 0);
        lcd.printString("Don't leave", 0, 1);
        lcd.printString("the path! Tilt", 0, 2);
        lcd.printString("pad to roll", 0, 3);
        lcd.printString("the ball", 0, 4);
        lcd.printString("A = go back", 0, 5);
        lcd.refresh();
        wait(0.5);
    }
}