ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Game_Project_2

Dependencies:   Gamepad N5110 Pokemon mbed

Fork of 2645_Game_Project_2 by ELEC2645 (2016/17)

main.cpp

Committer:
200923317
Date:
2017-04-24
Revision:
3:b4de529de482
Parent:
2:a2bb794f830c
Child:
4:713ac9379ac6

File content as of revision 3:b4de529de482:

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Pokemon.h"
#include "Images.h"

//rewritten code to implement new/better ideas. part of the code is same as before but lots of changes were needed.

//-------------------------------- objects -------------------------------------
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Pokemon pk;
Images sp;
//--------------------------- Structs/Typedefs ---------------------------------
struct joyInput {
    Direction d; //direction of joystick to navigate menu's
};

typedef enum State {START, PARTNER, MENU, FIGHT, POKEMON, SETTINGS} Gamepage;
//assigning names to different states

void init();
void drawStart();
void select(int x, int y, int L);
void deselect(int _x, int _y, int _L);
void balls();
float drawPartner();
float partnerChoice(int choice);
void choice(int p);
float drawMenu();
void drawFight();
void drawPoke();
void menu();
void settings();

//------------------------------------------------------------------------------

int main()
{
    init();
    drawStart();
    Gamepage state = START;
//-----------------------GAME LOOP START-------------------------------
    while(1) {
        if (state == START) {
            state = PARTNER;

        } else if (state == PARTNER) {
            int partner = drawPartner();
            int correct = partnerChoice(partner);
            if( correct == 1) {
                state = PARTNER;
            } else if(correct == 0) {
                choice(partner);
                lcd.refresh();
                state = MENU;
            }

        } else if (state == FIGHT) {
            state = MENU;

        } else if (state == SETTINGS) {
            state = MENU;

        } else if (state == MENU) {
            wait(1.0);
            int box = drawMenu();
            if (box == 0) {
                state = FIGHT;
                lcd.clear();
                lcd.printString("FIGHT",8,2);
            } else if (box ==1) {
                state = POKEMON;
                lcd.clear();
                lcd.printString("POKEMON",20,4);
            } else if (box ==2) {
                state = SETTINGS;
                lcd.clear();
                lcd.printString("SETTINGS",50,2);
            }
            lcd.refresh();
            wait(2.0);
            lcd.clear();

        } else if (state == POKEMON) {
            state = MENU;
            drawPoke();
        }
    }
}



void init()
{
    pad.init();
    lcd.init();
    lcd.setBrightness(1.0);
}

void  drawStart()
{

    lcd.printString("  Welcome to  ",0,0);
    lcd.printString(" the World of ",0,1);
    lcd.printString("    Pokemon   ",0,2);
    lcd.printString("  Press Start ",0,4);
    lcd.refresh();

    while( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.led(1,1);
        pad.led(6,1);
        wait(0.5);
        pad.led(2,1);
        pad.led(5,1);
        wait(0.5);
        pad.leds_on();
        wait(0.5);
        pad.led(1,0);
        pad.led(6,0);
        wait(0.5);
        pad.led(2,0);
        pad.led(5,0);
        wait(0.5);
        pad.leds_off();
        wait(0.5);
    }
    wait(1.0);
}

void select(int x,int y, int L)
{
    lcd.drawRect(x,y,L,L,FILL_TRANSPARENT);
}

void deselect(int _x,int _y, int _L)
{
    lcd.drawRect(_x,_y,_L,_L, FILL_WHITE);
}

void balls()
{
    sp.ball(11,18);
    sp.ball(35,18);
    sp.ball(59,18);
    lcd.refresh();
}

float drawPartner()
{
    select(9,16,15);
    int offset = 0;
    int _d = pad.get_direction();
    while(pad.check_event(Gamepad::A_PRESSED) == false) {
        if(_d == E && offset <= 1) {
            offset ++;
        } else if(_d == W && offset >=1) {
            offset --;
        }
        if(offset == 0) {
            deselect(57,16,15);
            deselect(33,16,15);
            select(9,16,15);
            balls();
        } else if(offset == 1) {
            deselect(57,16,15);
            deselect(9,16,15);
            select(33,16,15);
            balls();
        } else if(offset == 2) {
            deselect(9,16,15);
            deselect(33,16,15);
            select(57,16,15);
            balls();
        }
        lcd.refresh();
        wait(0.5);
    }
    return offset;
}

float partnerChoice(int choice)
{
    lcd.clear();
    int _d = pad.get_direction();
    if(choice == 0) {
        sp.bulbasaur(16,0);
    } else if(choice == 1) {
        sp.charmander(16,0);
    } else if(choice == 2) {
        sp.squirtle(16,0);
    }
    lcd.printString("Are you",0,2);
    lcd.printString(" sure??",0,3);
    lcd.refresh();
    while(pad.check_event(Gamepad::A_PRESSED)== false) {
        lcd.printString("Yes",65,1);
        lcd.printString("No",65,5);
        select(62,6,8);
        lcd.refresh();
        int offset1 = 0;
        if(offset1 == 0 && _d == S) {
            deselect(62,6,8);
            select(62,6,8);
        } else if(offset1 == 1 && _d ==N) {
            deselect(62,38,8);
            select(62,38,8);
        }
        return offset1;
    }

}

void choice(int p)
{
    lcd.clear();
    if (p == 0) {
        pk.setType(Grass);
        lcd.printString("You Chose",16,2);
        lcd.printString("Bulbasaur",15,3);
    } else if (p == 1) {
        pk.setType(Fire);
        lcd.printString("You Chose",16,2);
        lcd.printString("Charmander",15,3);
    } else if (p == 2) {
        pk.setType(Water);
        lcd.printString("You Chose ",16,2);
        lcd.printString("Squirtle",15,3);
    }
}


float drawMenu()
{
}
void drawFight()
{
}
void drawPoke()
{
}
void menu()
{
}
void settings()
{
}