Dependencies:   MMA8452 N5110 PowerControl beep mbed

GameFSM.h

Committer:
stevenle93
Date:
2015-05-05
Revision:
5:c0a58a6916f2
Parent:
4:c141e252d786
Child:
6:3e8ce3108702

File content as of revision 5:c0a58a6916f2:

/**
@file GameFSM.h

@brief Header file of Game's Finite State Machine
*/
#ifndef GAMEFSM_H
#define GAMEFSM_H

#include "Game.h"
#include "Clock.h"
#include "N5110.h"
#include "Menu.h"
#include "PowerControl/PowerControl.h"

Game game;
Clock countdown;
Menu menu;

class FSM
{
public:
    /**
    The function contains FSM of the whole programme
    */
    void proact();
    void resetButton();
    void sleepMode();
};

void FSM::sleepMode()
{
    if ((BuAFlag==0)&&(BuBFlag==0)&&(BuUFlag==0)&&(BuDFlag==0)) {
        Sleep();
        lcd.setBrightness(0);
    }
}

void FSM::resetButton()
{
    BuDFlag = 0;
    BuUFlag = 0;
    BuAFlag = 0;
    BuBFlag = 0;
}

void FSM::proact()
{
    switch(state) {
        case 0: //Main state 0: MAIN MENU - Cursor at Play line
            lcd.clear();
            menu.main();
            lcd.drawCircle(23,27,2,1);
            if (BuDFlag) {
                resetButton();
                state = 1;
            }
            if (BuAFlag) {
                resetButton();
                state = 3;
            }
            //sleepMode();
            break;

        case 1: //Main state 1: MAIN MENU - Cursor at Score line
            lcd.clear();
            menu.main();
            lcd.drawCircle(20,35,2,1);
            if (BuUFlag) {
                resetButton();
                state = 0;
            }
            if (BuAFlag) {
                resetButton();
                state = 2;
            }
            //sleepMode();
            break;

        case 2: //(Main state 2)Sub state 0: HIGH SCORE sector - Cursor is at Easy line
            lcd.clear();
            menu.highscore();
            lcd.drawCircle(23,19,2,1);
            if (BuDFlag) {
                resetButton();
                state = 12; //Go to HIGH SCORE sector/Normal line
            }
            if (BuAFlag) {
                resetButton();
                state = 14; //Go to HIGH SCORE sector/Hard high score
            }
            if (BuBFlag) {
                resetButton();
                state = 1; //Back to MAIN MENU sector/Score line
            }
            //sleepMode();
            break;

        case 12: //(Main state 2)Sub state 1: HIGH SCORE sector - Cursor is at Normal line
            lcd.clear();
            menu.highscore();
            lcd.drawCircle(17,27,2,1);
            if (BuUFlag) {
                resetButton();
                state = 2; //Go to HIGH SCORE sector/Easy line
            }
            if (BuDFlag) {
                resetButton();
                state = 13; //Go to HIGH SCORE sector/Hard line
            }
            if (BuAFlag) {
                resetButton();
                state = 15; //Go to HIGH SCORE sector/Normal high score
            }
            if (BuBFlag) {
                resetButton();
                state = 1; //Back to MAIN MENU sector/Score line
            }
            //sleepMode();
            break;

        case 13: //(Main state 2)Sub state 2: HIGH SCORE sector - Cursor is at Hard line
            lcd.clear();
            menu.highscore();
            lcd.drawCircle(23,35,2,1);
            if (BuUFlag) {
                resetButton();
                state = 12; //Go to HIGH SCORE sector/Normal line
            }
            if (BuAFlag) {
                resetButton();
                state = 16; //Go to HIGH SCORE sector/Hard high score
            }
            if (BuBFlag) {
                resetButton();
                state = 1; //Back to MAIN MENU sector/Score line
            }
            //sleepMode();
            break;

        case 14: //(Main state 2)Sub state 3: HIGH SCORE sector - Easy mode high score
            lcd.clear();
            lcd.printString("EASY MODE",16,0);
            lcd.printString("High score",14,2);
            lcd.printString("Your score",14,4);
            //Easy mode high score
            char HScore0[5];
            sprintf(HScore0,"%d",game.scoArr[0]);
            lcd.printString(HScore0,38,3);
            //Easy mode current score
            char HScore1[5];
            sprintf(HScore1,"%d",game.scoArr[1]);
            lcd.printString(HScore1,38,5);
            if (BuBFlag) {
                resetButton();
                state = 2; //Back to HIGH SCORE sector/Easy line
            }
            //sleepMode();
            break;

        case 15: //(Main state 2)Sub state 4: HIGH SCORE sector - Normal mode high score
            lcd.clear();
            lcd.printString("NORMAL MODE",8,0);
            lcd.printString("High score",14,2);
            lcd.printString("Your score",14,4);
            //Normal mode high score
            char HScore2[5];
            sprintf(HScore2,"%d",game.scoArr[2]);
            lcd.printString(HScore2,38,3);
            //Normal mode current score
            char HScore3[5];
            sprintf(HScore3,"%d",game.scoArr[3]);
            lcd.printString(HScore3,38,5);
            if (BuBFlag) {
                resetButton();
                state = 12; //Back to HIGH SCORE sector/Normal line
            }
            //sleepMode();
            break;

        case 16: //(Main state 2)Sub state 5: HIGH SCORE sector - Hard mode high score
            lcd.clear();
            lcd.printString("HARD MODE",16,0);
            lcd.printString("High score",14,2);
            lcd.printString("Your score",14,4);
            //Hard mode high score
            char HScore4[5];
            sprintf(HScore4,"%d",game.scoArr[4]);
            lcd.printString(HScore4,38,3);
            //Hard mode current score
            char HScore5[5];
            sprintf(HScore5,"%d",game.scoArr[5]);
            lcd.printString(HScore5,38,5);
            if (BuBFlag) {
                resetButton();
                state = 13; //Back to HIGH SCORE sector/Hard line
            }
            //sleepMode();
            break;

            //This case is a game condition setting case
            //Clock countdown was set to 60 seconds
            //Score was set to 0
        case 3: //Main state 3: RESETING STATE - Before entering GAME MODE menu
            game.reset();
            resetButton();
            state = 4;
            break;
//////////GAME MODE SECTOR/////////
        case 4: //(Main state 4)Sub state 0: GAME MODE sector - Cursor at Easy line
            lcd.clear();
            menu.gameset();
            lcd.drawCircle(23,19,2,1);
            if (BuAFlag) {
                resetButton();
                state = 7;
            }
            if (BuBFlag) {
                resetButton();
                state = 0;
            }
            if (BuDFlag) {
                resetButton();
                state = 5; //Go to state 8 of Game set menu
            }
            //sleepMode();
            break;

        case 5: //(Main state 4)Sub state 1: GAME MODE sector - Cursor at Normal line
            lcd.clear();
            menu.gameset();
            lcd.drawCircle(17,27,2,1);
            if (BuAFlag) {
                resetButton();
                state = 8; //Go to state 8: Normal mode game
            }
            if (BuBFlag) {
                resetButton();
                state = 0; //Go back to state 0: Main menu
            }
            if (BuUFlag) {
                resetButton();
                state = 4; //Go to state 4: Game mode menu
            }
            if (BuDFlag) {
                resetButton();
                state = 6; //Go to state 6: Game mode menu
            }
            //sleepMode();
            break;

        case 6: //(Main state 4)Sub state 2: GAME MODE sector - Cursor at Hard line
            lcd.clear();
            menu.gameset();
            lcd.drawCircle(23,35,2,1);
            if (BuAFlag) {
                resetButton();
                state = 9;
            }
            if (BuBFlag) {
                resetButton();
                state = 0;
            }
            if (BuUFlag) {
                resetButton();
                state = 5;
            }
            //sleepMode();
            break;

        case 7: //(Main state 4)Sub state 3: GAME SCREEN sector - Easy mode
            lcd.clear();
            timer.attach(&timerExpired,0.5);
            gatimer.attach(&gatimerExpired,0.1); //Update time for Easy mode is 0.1s
            while(CClock > 0) {
                countdown.countDown();
                game.easyMode();
            }
            if (CClock == 0) {
                countdown.timeout();
            }
            resetButton();
            state = 10; //Go to state 10: Your score menu

            break;

        case 8: //(Main state 4)Sub state 4: GAME SCREEN sector - Normal mode
            lcd.clear();
            timer.attach(&timerExpired,0.5);
            gatimer.attach(&gatimerExpired,0.2); //Update time for Normal mode is 0.2s
            while(CClock > 0) {
                countdown.countDown();
                game.norMode(); //Normal mode of game is activated
            }
            if (CClock == 0) {
                countdown.timeout();
            }
            resetButton();
            state = 10; //Go to state 10: Your score menu

            break;

        case 9: //(Main state 4)Sub state 5: GAME SCREEN sector - Hard mode
            lcd.clear();
            timer.attach(&timerExpired,0.5);
            gatimer.attach(&gatimerExpired,0.4); //Update time for Hard mode is 0.4s
            while(CClock > 0) {
                countdown.countDown();
                game.hardMode(); //Hard mode of the game is activated
            }
            if (CClock == 0) {
                countdown.timeout();
            }
            resetButton();
            state = 10; //Go to state 10: Your score menu

            break;

        case 10: //Main state 5: YOUR SCORE sector - Cursor at Main menu line
            lcd.clear();
            menu.yourscore();
            lcd.drawCircle(8,27,2,1);
            char YScore[14];
            sprintf(YScore,"%d",game.score);
            lcd.printString(YScore,37,2);
            if (BuDFlag) {
                resetButton();
                state = 11;
            }
            if (BuAFlag) {
                resetButton();
                state = 0;
            }
            //sleepMode();
            break;

        case 11: //Main state 6: YOUR SCORE sector - Cursor at Reset line
            lcd.clear();
            menu.yourscore();
            lcd.drawCircle(8,35,2,1);
            lcd.printString(YScore,37,2);
            if (BuUFlag) {
                resetButton();
                state = 10;
            }
            if (BuAFlag) {
                resetButton();
                state = 3;
            }
            //sleepMode();
            break;
    }
    wait(0.1);
}
#endif