James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

main.cpp

Committer:
JamesCummins
Date:
2019-04-27
Revision:
29:42651f87522b
Parent:
26:0dc10374546f
Child:
31:c95f1b1d6423

File content as of revision 29:42651f87522b:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: James Nathan Cummins
Username: el17jnc
Student ID Number: 201096364
Date: 22/03/19
*/

#include "Gamepad.h"
#include "mbed.h"
#include "N5110.h"
#include "BrickBreakerEngine.h"
#include "ClassicEngine.h"
#include "OptionsEngine.h"
#include "SDFileSystem.h"
#define RADIUS 3


//Objects
Gamepad gamepad;
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ClassicEngine classic;
BrickBreakerEngine brick;
OptionsEngine opt;
Map map;
AnalogIn randnoise(PTB0);
FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
Ball ball;
Pause pause;
SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd");

//Enumeric class and struct for game mode selection menu
enum StartOption{
    CLASSIC,
    BRICKBREAKER,
    OPTIONS
    };

struct StartSelection{
    int output;
    StartOption next_state[3];
    };

//Methods
void startscreen();
StartOption menu();
void init();
void print_start_menu(int output);
void classic_mode();
void brickbreaker_mode();
void options_menu();


////////////////Main Function/////////////////

int fps = 16;   //declared globally so it doesn't have to be passed to
                //the different game mode functions
int main(){
    init();
    startscreen();
    while(1){
        StartOption choice_selected = menu();
        if(choice_selected == CLASSIC){ classic_mode();}
        if(choice_selected == BRICKBREAKER){ brickbreaker_mode();}
        if(choice_selected == OPTIONS){ options_menu();}
    }
}
  
  
  
//////////////Start up functions///////////////////

void init(){
    gamepad.init();
    lcd.init();
    lcd.setContrast(0.55);
    brick.init(RADIUS, ball);
    opt.init();
    map.init();
    pause.init();
    accelerometer.init();
    sd.disk_initialize();
    wait(1);
}
    
void startscreen() {
    lcd.clear();
    char gamename[] = {'L', 'A', 'B', 'Y', 'R', 'I', 'N', 'T', 'H', ' ', ' ', '\0'};
    int i = 0;
    for(int a = 0; a < 35; a++){
        lcd.clear();
        lcd.drawCircle(24+2*a, 21, 3, FILL_BLACK);
        for (i = 0; i < a/3; i++) {
            lcd.printChar(gamename[i], 15+i*6, 2);
            lcd.refresh();
            }
        wait_ms(50);
    }
    lcd.printString("Press start", 9, 4);
    lcd.printString("to play >", 15, 5);
    lcd.refresh();
    bool advance = false;
    while (!advance){
        if (gamepad.check_event(gamepad.START_PRESSED)){
            lcd.clear();
            lcd.refresh();
            advance = true;
            }
        else { advance = false; }
    }
}

StartOption menu(){    
    StartSelection fsm[3] = {
        {0,{OPTIONS,BRICKBREAKER,CLASSIC}},
        {2,{CLASSIC,OPTIONS,BRICKBREAKER}},
        {4,{BRICKBREAKER,CLASSIC,OPTIONS}}
    };
    StartOption state = CLASSIC;  //start with the arrow on the top option
    int next = 2;  //next_state = 2 so that by default it doesn't change arrow position
    while(!(gamepad.check_event(gamepad.A_PRESSED))){     //select choice by pushing joystick to the right
        state = fsm[state].next_state[next];
        lcd.clear();
        if(gamepad.get_direction() == N){ next = 0;}
        else if(gamepad.get_direction() == S){ next = 1;}   
        else {next = 2;}
        print_start_menu(fsm[state].output);
        lcd.refresh();
        wait(0.25);
    }
    lcd.clear();
    lcd.refresh();
    return state;
}

void print_start_menu(int output){
        lcd.printString(">", 0, output);
        lcd.printString("Classic", 36, 0);
        lcd.printString("BrickBreak", 18, 2);
        lcd.printString("Options", 36, 4);
        lcd.printString("(A = Select)", 12, 5);
}

//////////////////Game Mode Functions////////////////////////////////

void classic_mode(){
    classic.init(ball, map);
    while(!(map.check_wall_collision(gamepad, ball))){
        classic.classic_update(ball, accelerometer, map);
        lcd.clear();
        classic.classic_draw(lcd, map, ball);
        lcd.refresh();
        wait(1/fps);
        if(gamepad.check_event(gamepad.BACK_PRESSED)){ 
            PauseOption choice = pause.pause_menu(gamepad, lcd, fps);
            if(choice == RESUME){}
            if(choice == RESTART){ classic.init(ball, map); }
            if(choice == QUIT){ break; }
            if(choice == HELP){ pause.classic_help(gamepad, lcd); }
        }
    }
}

void brickbreaker_mode(){
    for(int i = 0; i < 45*fps; i++){
        if(i == 1){ brick.set_score(0); }       //reset score when game restarts 
        ball.read_input(accelerometer);
        ball.update();
        /*Vector2D position = _ball.get_position();
        printf("ball_x = %f | ball_y = %f\n", position.x, position.y);  //note: running with tests causes the game to run slow and take ~2min30s*/
        brick.check_square_collision(randnoise, ball);
        lcd.clear();
        brick.brickbreaker_draw(lcd, ball);
        lcd.refresh();
        wait_ms(1000/fps);
        if(gamepad.check_event(gamepad.BACK_PRESSED)){
            PauseOption choice = pause.pause_menu(gamepad, lcd, fps);
            i = pause.brickbreaker_action(choice, gamepad, lcd, i, fps);  //returns which frame to jump to
        }
    }
    brick.write_high_scores();
}

void options_menu(){
    Option choice = BRIGHTNESS;
    while(!(gamepad.check_event(gamepad.A_PRESSED))){
        lcd.clear();
        opt.display_options(lcd);
        choice = opt.option_selection(gamepad, lcd);
        lcd.refresh();
        wait(0.2);
    }
    if(choice == BRIGHTNESS){ opt.change_brightness(gamepad, lcd); }
    if(choice == BALL_SPEED){ opt.change_ball_speed(gamepad, lcd, ball); }
    if(choice == HIGH_SCORES){ opt.view_high_scores(gamepad, lcd); }
}