Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

main.cpp

Committer:
AppleJuice
Date:
2015-05-05
Revision:
17:7c926de79e02
Parent:
14:f5760f76fe83
Child:
18:0e8b1cc24706

File content as of revision 17:7c926de79e02:

/** 
@file main.cpp
@brief Source file for main game containing function defintions and main loop.
@autor Thomas Davies
@date May 2015
*/

#include "main.h"


int main()
{
    lcd.Initialize();       

    lcd.displayStartScreen();
    while(!isRight()) {}       //wait until right button pressed

    wait(0.5);          //just to ignore layover from first button press.
    lcd.displayInstructionScreen();
    while(!isRight()) {}    //wait until right button pressed.

    lcd.displayCountdown();
    lcd.clear();

    bool refresh = false;

    lcd.drawAllPlatforms();
    lcd.drawBall();
    lcd.refresh();

    gameClock.attach(&clockCounter,0.003);  //attach the master clock! 

    //MAIN GAME LOOP
    //multiple runs on a single loop. stop with a bool

    char buffer[10];
    while(true) {
        if (isFirstCheck) { //if first time checking this clock cycle.
            if(clockCount %(gameSpeed) == 0) {  
                //advance platforms
                advancePlatforms(1);
                refresh = true;
            }

            if(clockCount%10 == 0) {
                //ball movement
                if (isLeft() && isBallDirClear(1)) {    //if josystick left and direction is clear
                    lcd.eraseBall();
                    lcd.setBallPos(lcd.getBallX()+2,lcd.getBallY());
                    refresh = true;

                } else if (isRight() && isBallDirClear(0)) {
                    lcd.eraseBall();
                    lcd.setBallPos(lcd.getBallX()-2,lcd.getBallY());
                    refresh = true;
                }
                
                //if the ball is in free space, animate it falling!
                if(isBallFalling(lcd.getBallY())) { 
                    lcd.eraseBall();
                    lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + 1);
                    refresh = true;
                    isFirstHit = true;
                }
            }
            
            if (clockCount %500 == 0)
            {
                gameLevel++;
                if (gameLevel%4 == 0)
                    if (gameLevel > 60)
                        gameSpeed -=1;
                    else    
                        gameSpeed -= 2;
            }
            
            if (refresh) {
                lcd.drawBall();
                lcd.drawAllPlatforms();
                
                sprintf(buffer,"lvl: %d",gameLevel);
                lcd.printString(buffer,lcd.getMaxX() - 2,-1);
                lcd.refresh();
                refresh = false;
            }

            //check if ball hit roof
            if (lcd.getBallY() == 7) {
                break;
            }
            isFirstCheck = false;
        }
    }

    gameClock.detach();
    
    lcd.clear();

    endScreenController();
    return 1;
}


//#############FUNCTION DEFINITIONS#################//
void advancePlatforms(int n)
{
    //ADVANCE
    lcd.eraseAllPlatforms();
    lcd.shiftAllPlatforms(n);
    lcd.drawAllPlatforms();

    if(!isBallFalling(lcd.getBallY()-1)) {
        //move ball up with platform
        if (isFirstHit) {
            //beep();
            isFirstHit = false;
        }

        lcd.eraseBall();
        lcd.setBallPos(lcd.getBallX(),lcd.getBallY() -1);
    }


}

bool isBallFalling(int bY)
{
    int bX = lcd.getBallX();    //ball X pos
    int bR = lcd.getBallR();    //ball radius

    //find next platform lower than ball
    //check Y pos to see if ball is on platform, return false if on plat
    //check X pos to see if ball is over gap, return true if over gap
    Platform closestPlatform = lcd.nextClosestPlatform(bY - bR);


    //if bottom of ball on level of platform
    if ((bY+bR) == (closestPlatform.y)) {
        //serial.printf ("On level of plat \n");
        if (bX > closestPlatform.x && (bX+bR-1) < (closestPlatform.x + lcd.getPlatGapSize())) { //OVER GAP
            return true;
        } else {
            return false;
        }      //On platform!
    } else if ((bY+bR) >= (lcd.getMaxY()-1)) {
        return false;
    } else  //FREE SPACE
        return true;
}


bool isBallDirClear(int dir)
{
    if (dir == 1) {
        if ((lcd.getBallX()+lcd.getBallR()) < (lcd.getMaxX())) {
            return true;
        }
    } else if (dir == 0) {
        if (lcd.getBallX() > 0) {
            return true;
        }
    }
    return false;
}

bool isRight()
{
    double count = 0.0;
    double total = 0.0;
    while (count < 10) {
        count ++;
        total += joystickX.read();
    }
    if ((total/count)> 0.8)
        return true;

    return false;
}

bool isLeft()
{
    double count = 0.0;
    double total = 0.0;

    while (count < 10) {
        count ++;
        total += joystickX.read();
    }
    if ((total/count) <  0.2)
        return true;

    return false;
}

bool isUp()
{
    double count = 0.0;
    double total = 0.0;

    while (count < 10) {
        count ++;
        total += joystickY.read();
    }

    if ((total/count) <  0.2)
        return true;

    return false;
}
bool isDown()
{
    double count = 0.0;
    double total = 0.0;

    while (count < 10) {
        count ++;
        total += joystickY.read();
    }
    if ((total/count) >  0.8)
        return true;

    return false;
}

void clockCounter ()
{
    clockCount ++;
    isFirstCheck = true;
    //serial.printf("%d mod 12 = %d \n",clockCount,clockCount%12);
}

void endScreenController()
{
    int cursorLoc = 0;
    int cursorArray[3][3] = {
        {1,0,0},
        {0,1,0},
        {0,0,1}
    };
    
    bool record = isRecord();
    lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc],record);
    bool change = false;

    while (true) {
        if (isUp()) {
            cursorLoc ++;
            change = true;
        } else if (isDown()) {
            cursorLoc --;
            change = true;
        }

        if (cursorLoc == 3)
            cursorLoc = 1-record;
        else if (cursorLoc == (-record))
            cursorLoc = 2;

        if (change) {
            lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc],record);
            wait(0.5);
            change = false;
        }

        if (joystickButton.read()) {
            switch(cursorLoc) {
                case 0:
                    //take to initial entry page! (only allow if score > top 3)
                    wait(0.1);      
                    highScoreEditor(recordEntryController(),record);
                    
                    wait(1);
                    //reset once button pressed!
                    while(!joystickButton.read()) {}
                    mbed_reset();
                    break;
                case 1:
                    //take to high score page!
                    highScoreEditor("",0);
                    wait(1);
                    //reset once button pressed.
                    while(!joystickButton.read()) {}
                    mbed_reset();
                    break;
                case 2:
                    //restart
                    mbed_reset();
                    break;
            }
        }
    }
}

char* recordEntryController()
{
    char *letters = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

    int ptr[3] = {0,0,0};

    int cursorLoc = 0;
    int cursorArray[3][3] = {
        {1,0,0},
        {0,1,0},
        {0,0,1}
    };

    lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]);

    bool change = false;

    while (true) {
        if (isUp()) {
            cursorLoc ++;
            change = true;
        } else if (isDown()) {
            cursorLoc --;
            change = true;
        } else if (isLeft()) {
            if (ptr[cursorLoc] == 0)
                ptr[cursorLoc] = 25;
            else
                ptr[cursorLoc] --;
            change = true;
        } else if (isRight()) {
            if (ptr[cursorLoc] == 25)
                ptr[cursorLoc] = 0;
            else
                ptr[cursorLoc] ++;
            change = true;
        }

        //cyclical mouse management
        if (cursorLoc == 3)
            cursorLoc = 0;
        else if (cursorLoc == -1)
            cursorLoc = 2;

        if (change) {
            lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]);
            wait(0.1);
            change = false;
        }

        if (joystickButton.read()) {
            //go to high score screen
            static char initials[10];
            sprintf(initials,"%c%c%c",letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]);
            return initials;
        }
    }
}

void highScoreEditor(char *playerInitials,bool isRecord)
{    
    //check if lvl is in top 3
    FILE *fp = fopen("/local/scores.csv","r");  //open for reading.
 
    char s_hs0[3],s_hs1[3],s_hs2[3];
    int d_hs[3];
    char buffer[2];            

    fscanf(fp,"%3s,%s",s_hs0,buffer);
    d_hs[0] = atoi(buffer);
    fscanf(fp,"%3s,%s",s_hs1,buffer);
    d_hs[1] = atoi(buffer);
    fscanf(fp,"%3s,%s",s_hs2,buffer);
    d_hs[2] = atoi(buffer);
    
    fclose(fp);
    
    if (isRecord) {
        
        if (gameLevel >= d_hs[2]) {
            d_hs[2] = gameLevel;
            strncpy(s_hs2,playerInitials,3);
        }
        if (gameLevel >= d_hs[1]) {
            d_hs[2] = d_hs[1];
            d_hs[1] = gameLevel;
            strncpy(s_hs2,s_hs1,3);
            strncpy(s_hs1,playerInitials,3);
        }
        if (gameLevel >= d_hs[0]) {
            d_hs[1] = d_hs[0];
            d_hs[0] = gameLevel;
            strncpy(s_hs1,s_hs0,3);
            strncpy(s_hs0,playerInitials,3);
        }
        
        char buffer[40];
        sprintf(buffer,"%s,%d\n%s,%d\n%s,%d",s_hs0,d_hs[0],s_hs1,d_hs[1],s_hs2,d_hs[2]);
        fp = fopen("/local/scores.csv","w");
        fprintf(fp,buffer);
        fclose(fp);
    }
     
    lcd.displayHighScores(s_hs0,s_hs1,s_hs2,d_hs);    
}

bool isRecord ()
{
    //check if lvl is in top 3
    FILE *fp = fopen("/local/scores.csv","r");  //open for reading.
 
    int highScore;
    char buffer[2];
    char temp[3];
            
    for (int i = 0;i < 3;i++)
    {
        fscanf(fp,"%3s,%s",temp,buffer);
        highScore = atoi(buffer);
        if (gameLevel > highScore) {
            fclose (fp);
            return 1;
        }
    }
    fclose(fp);
    return 0;
}