Adventure game written for ECE2035 at the Georgia Institute of Technology

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

startmenu.cpp

Committer:
trmontgomery
Date:
2018-05-22
Revision:
4:cdc54191ff07
Parent:
2:0876296d9473
Child:
5:93a4c396c1af

File content as of revision 4:cdc54191ff07:

#include "globals.h"
#include "hardware.h"
#include "map.h"
#include "graphics.h"
#include "speech.h"
#include "startmenu.h"
#include <vector>

//note when you define stuff up here like this the methods of the class cant see it I think
#define NO_ACTION 0
#define BUTTON1 1
#define BUTTON2 2
#define BUTTON3 3
#define MENU_BUTTON 4
#define GO_LEFT 5
#define GO_RIGHT 6
#define GO_UP 7
#define GO_DOWN 8
#define NO_RESULT 9
#define GAME_OVER 10
#define FULL_DRAW 11

/*This initializes the menu. i.e. this is the first drawing on the screen.*/
Menu::Menu(int num_options){
    
    this->num_options = num_options;
    this->start = 0;
    this->quit = 0;
    uLCD.locate(1,2);
    //draw pink rectangle on whole screen
    uLCD.filled_rectangle(0, 127, 127, 0, PINK1);
    // write title
    uLCD.textbackground_color(YELLOW);
    uLCD.color(BLACK);
    uLCD.printf("Issa Quest");
    
    if (current_item%3 == 0){
        start_color = GREEN;
    } else if (current_item%3 == 1){
        quit_color = GREEN;
    } else{
        third_color = GREEN;
    }
    
    uLCD.locate(1,4);
    uLCD.color(start_color);
    uLCD.printf("Start da game");
    
    uLCD.locate(1,6);
    uLCD.color(quit_color);
    uLCD.printf("Quit?");
    
    uLCD.locate(1,8);
    uLCD.color(third_color);
    uLCD.printf("Instructions");
}

//Option Definition//
int Menu::o_instructions(){
    uLCD.filled_rectangle(0, 128, 128, 80, PINK1);
    uLCD.locate(1,4);
    uLCD.textbackground_color(YELLOW);
    uLCD.color(BLACK);
    
    uLCD.printf("Go into the house and talk to the red man to start your quest. Be careful not to step on the spikes.");
    
    uLCD.locate(10,15);
    uLCD.color(GREEN);
    uLCD.printf("Back");
    
    GameInputs inp = read_inputs();
    while(1){
        if(!inp.b2){
            uLCD.filled_rectangle(0, 128, 128, 30, PINK1); //erase option
            wait(1);
            return 0;
        }
        inp = read_inputs();
        wait(1);
    }
}

int Menu::get_action(GameInputs inputs){
    if(!inputs.b1){
        uLCD.locate(1,2);
        char but[5];
        sprintf(but, "%d:  %d\n", !inputs.b1, inputs.b1);
        uLCD.printf(but);
        return 1;
    }
    if(!inputs.b2){
        return 2;
    }
    if (!inputs.b3){
        return BUTTON3;
    }
    return NO_ACTION;
}

int Menu::update(int action){ //this might need to rewritten

    switch(action) 
    {           
        case 1: 
            //if button 1 pressed move to next menu item
            current_item = current_item++;
            break;
        case 2: 
            //if button 2 pressed select menu item
            //b2presses++;
            if (current_item > 0 && current_item%this->num_options == 0) {//Start game
                start = true;
            } else if (current_item%this->num_options == 1){ //Quit (screen cut to black)
                quit = true;
            } else { //screen go white
                o_instructions();
            }
            break;
        case MENU_BUTTON:
        break;
        default:        break;
    }
    return NO_RESULT;
}

void Menu::draw(int init){
    if (current_item%this->num_options == 0){
        start_color = GREEN;
    } else if (current_item%this->num_options == 1){
        quit_color = GREEN;
    } else{
        third_color = GREEN;
    }
    
    uLCD.locate(1,4);
    uLCD.color(start_color);
    uLCD.printf("Start da game");
    
    uLCD.locate(1,6);
    uLCD.color(quit_color);
    uLCD.printf("Quit?");
    
    uLCD.locate(1,8);
    uLCD.color(third_color);
    uLCD.printf("Instructions");
    
    start_color = BLACK;
    quit_color = BLACK;
    third_color = BLACK;
}

int Menu::display(){
    ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
    // Menu loop
    while(1)
    {
        // Timer to measure game update speed
        Timer t; t.start();
        
        // Actually do the game update:
        // 1. Read inputs  
            GameInputs in = read_inputs();
        // 2. Determine action (get_action) 
            int a = this->get_action(in); 
            char act[5];
            sprintf(act, "%d", a);
            uLCD.printf(act);      
        // 3. Update game (update_game)
            int u = this->update(a);
        
        // 4. Draw frame (draw_game)
        this->draw(u);
        
        //check for start
        if (start){
            uLCD.filled_rectangle(0, 127, 127, 0, 0x000000);
            return start;
        }
        
        // 5. Frame delay
        t.stop();
        int dt = t.read_ms();
        if (dt < 500) wait_ms(500 - dt);
    }
}