Zeyu Feng 201377605

Dependencies:   mbed

On Minerva

main.cpp

Committer:
el19zf
Date:
2020-05-11
Revision:
11:494cc44777fe
Parent:
10:02ab3324be6c
Child:
12:009895f6b6e4

File content as of revision 11:494cc44777fe:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
2019/20

Name:Zeyu Feng
Username:el19zf
Student ID Number:201377605
Date:11/3/2020
*/

// includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "People.h"
#include "PeopleEngine.h"
#include "shot.h"
#include "Collision.h"
#include "Interface.h"

// objects
Gamepad pad;
N5110 lcd;
PeopleEngine engine;
shot shot;
Collision collision;
Interface interface;

//flag and triggers
Ticker ticker;//gradually increase number of shots
Timeout timeout;
volatile int timer_flag = 0;
volatile int timeout_flag = 0;
volatile int count_flag = 18;
volatile int paused_flag = 0;
volatile int option_flag = 0;

//    functions
void flip(){    timer_flag = 1; }
void time_out(){    timeout_flag = 1;   }
void init();
void control_check();
void update();
void shot_update();
void init_timeout();
void main_game(float,int);
void init_value();

int main()
{
    //initial
    init();
    
    interface.Welcome(lcd,pad);

    //a infinite loop 
    while(1) {
        init();
        init_value();
        int option_flag= interface.menu(lcd,pad,option_flag);
        switch(option_flag){
            case 0:
                main_game(0.7,25);
                break;
            case 1:
                main_game(4,40);
                break;
            case 2:
                interface.exit(lcd,pad);
                break;
            }
    }
}

void main_game(float increment,int max){
    
    ticker.attach(&flip,2);//set a ticker
    timeout.attach(&time_out,3);//set a timeout
    
    while((collision.get_health() > 0)&&(!collision.get_des()))
    {   //   increase shots with a ticker
        shot.gen_shot(timer_flag,increment,max);    
        lcd.clear();
        //   shot update
        shot_update();
        
        //   set a count down and update lcd
        update();
        
        pad.leds_on();
        paused_flag = interface.check_pause(lcd,pad,paused_flag);
        
        lcd.refresh();
        //printf("shot refresh\n");
        //printf("size = %d\n",shot._size);
        wait_ms(1000/6);//fps = 6
    }
    printf("END GAME\n");
}

void init()
{
    lcd.init();
    lcd.setContrast(0.5);
    engine.init();
    pad.init();
    shot.init();
    collision.init();
    lcd.refresh();
}

void control_check()
{
    engine.read_input(pad);
    engine.update();
    collision.set_pos(engine.get_pos());
    // if people is shotted, health -1 and reset the game
    if(collision.check(lcd)) {
        if(!collision.get_health()){
            interface.game_over(lcd,pad);
            lcd.clear();
        } else{
            engine.init();
            lcd.clear();
            shot.init();
            init_timeout();
        }
    }
}

void update()
{
    if(timeout_flag)
        control_check();
    //control people and check collision
    engine.draw(lcd);
    collision.draw(lcd);
    if(collision.check_des(lcd)){
        interface.victory(lcd,pad);
        lcd.clear();
    }
    if(!timeout_flag)
        count_flag = interface.count_down(lcd,count_flag);
}

void shot_update()
{
    shot.update();
    // delete invalid shots
    shot.delete_shot();
    //generate shot to keep constant number of shots
    shot.init_shot();
    shot.draw(lcd);
}


void init_timeout()
{
    timeout.attach(&time_out,3);
    timeout_flag = 0;
    count_flag = 18;
}

void init_value()
{
    timer_flag = 0;
    timeout_flag = 0;
    count_flag = 18;
    paused_flag = 0;
    option_flag = 0;
}