Dependencies:   mbed

On Minerva

main.cpp

Committer:
el19zf
Date:
2020-05-10
Revision:
10:02ab3324be6c
Parent:
9:62d6559f0d50
Child:
11:494cc44777fe

File content as of revision 10:02ab3324be6c:

/*
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"


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

//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 = 30;

//    functions
void flip()
{
    timer_flag = 1;
}
void time_out()
{
    timeout_flag = 1;
}
void init();
void control_check();
void draw();
void shot_update();
void count_down();
void reset_leds();
void welcome();
void init_timeout();
void game_over();

int main()
{
    //initial
    init();
    //set a ticker
    welcome();
    ticker.attach(&flip,2);
    timeout.attach(&time_out,5);

    //a infinite loop to control position of the people, update the game state
    while(1) {
        //   increase shots with a ticker
        shot.gen_shot(timer_flag);    
        lcd.clear();
        //   shot update
        shot_update();
        
        //   lose control before timeout
        draw();
        reset_leds();
        
        lcd.refresh();
        //printf("shot refresh\n");
        //printf("size = %d\n",shot._size);
        wait_ms(1000/6);//fps = 6
    }
}


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 init the game
    if(collision.check(lcd)) {
        if(!collision.get_health()){
            game_over();
        } else{
            engine.init();
            lcd.clear();
            shot.init();
            init_timeout();
        }
    }
}

void draw()
{
    if(timeout_flag)
        control_check();
    //control people and check collision
    engine.draw(lcd);
    collision.draw(lcd);
    if(!timeout_flag)
        count_down();
}

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 count_down()
{   
    char buffer[6];
    sprintf(buffer,"%d",(int)count_flag/6);
    lcd.printString(buffer,40,2);
    printf("count: %d\n",count_flag);
    count_flag--;
}
void reset_leds()
{
    pad.leds_on();
}
void welcome(){
    lcd.printString("   Welcome!   ",0,1);
    lcd.printString("   Game by    ",0,2);
    lcd.printString("   Z. FENG    ",0,3);
    lcd.refresh();
    while (pad.A_pressed() ==false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}

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

void game_over(){
    while(pad.B_pressed()==false){
        lcd.clear();
        lcd.printString("  Game over   ",0,2);
        lcd.refresh();
        wait(0.1);
    }
}