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:
5:add0351183be
Parent:
4:713ac9379ac6
Child:
6:6e9a415436e0

File content as of revision 5:add0351183be:

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Pokemon.h"
#include "Images.h"
#include "Bitmap.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();  //initialising screen and gamepad
    drawStart();  //screen shows startup screen
    Gamepage state = START;  //go to start state
//-----------------------GAME LOOP START-------------------------------
    while(1) {
        if (state == START) {
            state = PARTNER;

        } else if (state == PARTNER) { //choosing your 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) { //fight with wild pokemon
            state = MENU;

        } else if (state == SETTINGS) {  //settings, screen brightness
            state = MENU;

        } else if (state == MENU) { // main 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) { //check partner pokemon stats
            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) { // until start is pressed, this loop cycles
        pad.led(1,1); //cycle through LED's on gamepad
        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, int H) //select box's
{
    lcd.drawRect(x,y,L,H,FILL_TRANSPARENT);
}

void deselect(int _x,int _y, int _L, int _H) //removing select box's
{
    lcd.drawRect(_x,_y,_L,_H, FILL_WHITE);
}

void balls() //show the pokeballs on the screen for player to choose from
{
    sp.ball(11,18);
    sp.ball(35,18);
    sp.ball(59,18);
    lcd.refresh();
}

float drawPartner() //choice of pokeball
{
    select(9,16,15,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,15);
            deselect(33,16,15,15);
            select(9,16,15,15);
            balls();
        } else if(offset == 1) {
            deselect(57,16,15,15);
            deselect(9,16,15,15);
            select(33,16,15,15);
            balls();
        } else if(offset == 2) {
            deselect(9,16,15,15);
            deselect(33,16,15,15);
            select(57,16,15,15);
            balls();
        }
        lcd.refresh();
        wait(0.5);
    }
    return offset;
}

float partnerChoice(int choice) //shows pokemon inside ball, gives choice to choose that mon
{
    lcd.clear();
    int _d = pad.get_direction();
    int end = 0;
    if(choice == 0) {
        sp.bulbasaur(16,0); //grass starter
    } else if(choice == 1) {
        sp.charmander(16,0);//fire starter
    } else if(choice == 2) {
        sp.squirtle(16,0);//water starter
    }
    lcd.printString("Are you",0,2);
    lcd.printString(" sure??",0,3);
    lcd.printString("Yes",65,1);
    lcd.printString("No",65,5);
    select(62,6,10,10);
    lcd.refresh();
    while(pad.check_event(Gamepad::A_PRESSED)== false) {
        lcd.printString("Yes",65,1);
        lcd.printString("No",65,5);
        lcd.refresh();
        wait(0.25);
        int offset1 = 0;
        if(offset1 == 0 && _d == S) { //choose no
            deselect(62,6,10,10);
            select(62,38,10,10);
            end = 0;
        } else if(offset1 == 1 && _d ==N) {//choose yes
            deselect(62,38,10,10);
            select(62,6,10,10);
            end = 1;
        }
        return offset1;

    }
    return end;
}

void choice(int p) //sets up class for your chosen starter
{
    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);
    }
}

void menu()
{
    lcd.clear();
    lcd.printString("  FIGHT",8,1);
    lcd.printString(" POKEMON",8,3);
    lcd.printString(" SETTINGS",8,5);
}

float drawMenu()
{
    menu();
    int Xo = 19;
    int Yo = 11;
    select(Xo, Yo,42,12);
    lcd.refresh();
    wait(1.0);
    int box = 0;
    while( pad.check_event(Gamepad::A_PRESSED) == false) {
        int d = pad.get_direction();
        if (d == S && box >= 1 ) {
            deselect(Xo, Yo,48,12);
            Yo = Yo + 16;
            select(Xo, Yo,48,12);
            lcd.refresh();
            box = box + 1;
            wait(1.0);

        } else if (d == N && box <= 1) {
            deselect(Xo, Yo,48,12);
            Yo = Yo - 16;
            select(Xo, Yo,48,12);
            lcd.refresh();
            box = box - 1;
            wait(1.0);

        }
    }
    return box;
}

void drawFight()
{
}
void drawPoke()
{
    lcd.clear();
    while(pad.check_event(Gamepad::B_PRESSED) == false) {
        std::string pkn = pk.Name();
        std::string pkl = pk.Level();
        std::string pkh = pk.HP();
        std::string pkt = pk.Type();
        char a[50];
        char b[50];
        char c[50];
        char d[50];
        strncpy(a, pkn.c_str(), sizeof(a));
        strncpy(b, pkl.c_str(), sizeof(b));
        strncpy(c, pkt.c_str(), sizeof(c));
        strncpy(d, pkh.c_str(), sizeof(d));
        lcd.printString("Pokemon:",2,0);
        lcd.printString(a,2,1);
        lcd.printString(b,2,2);
        lcd.printString(c,2,3);
        lcd.printString(d,2,4);
        lcd.printString(" B = Back ",24,5);
        lcd.refresh();
    }
}

void settings()
{
    lcd.clear();
    int bright = 1;
    while(pad.check_event(Gamepad::B_PRESSED) == false) {
        lcd.printString("SET BRIGHTNESS", 2, 20);
        lcd.printString("B = BACK", 4, 20);
        bright = pad.read_pot();
        float brightness = 0.5*bright;
        lcd.setBrightness(brightness);
    }
}