ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

game/game.cpp

Committer:
Noximilien
Date:
2019-03-12
Revision:
11:cf2ba52e8b7e
Parent:
10:f02413ae09fe
Child:
12:bfe3a3deaac3

File content as of revision 11:cf2ba52e8b7e:



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

#include "models.h"
#include "main.h"
#include "game.h"
#include "enemy.h"
#include "stars.h"
#include "planet.h"
#include "blast.h"

#define MAX_BLASTS (5)
#define MAX_SMALL_STARS (5)
#define MAX_MEDIUM_STARS (20)

int x_ship_pos = 0;
int y_ship_pos = 24;
int small_star_delay = 0;

const int ship_speed = 2;
const int small_star_delay_max = 3;

Enemy enemy;

Blast blasts[MAX_BLASTS];
Stars small_stars[MAX_SMALL_STARS];
Stars medium_stars[MAX_MEDIUM_STARS];


bool Game::updateAndDraw() {

    shipMovment();
    enemy.enemyMovement();
    
    if (gamepad.check_event(gamepad.B_PRESSED)){
        fireNewBlast();
    }
    if  (small_star_delay == small_star_delay_max){ 
        //This is dealy between small stars generation.
        newSmallStarFlies();
        newMediumStarFlies();
        small_star_delay = 0;
    }
    else {
        small_star_delay += 1;
    }
    
    updateAndDrawBlasts();
    updateAndDrawSmallStars();
    updateAndDrawMediumStars();
    
    lcd.drawSpriteOnTop(x_ship_pos, y_ship_pos, spaceship1_width, spaceship1_height, (int *)spaceShip1);
    /*char buffer[4];
    sprintf(buffer,"%i\n",(int)(x_dir.read()*84));
    printf(buffer);*/
    
    bool want_to_pause = false;
    if (gamepad.check_event(gamepad.START_PRESSED)){
        
        want_to_pause = true;
    }
    return want_to_pause;
}


void Game::shipMovment(){           // The position of the ship
        
    if(x_ship_pos <= 48 && x_ship_pos >= 0){
        if(x_dir.read() > 0.6f){
           x_ship_pos -= ship_speed;
        }
        else if(x_dir.read() < 0.4f){
           x_ship_pos += ship_speed;
        }
    } 
    
    else if (x_ship_pos <= 48){ x_ship_pos = 0;}     //Limits for x direction border IMPROVE IF POSSIBLE.
    else { x_ship_pos = 48;}
    
    
    if (y_ship_pos <= (47 - spaceship1_height) && y_ship_pos >= 0){
        if(y_dir.read() > 0.6f){
           y_ship_pos -= ship_speed; 
        }
        else if(y_dir.read() < 0.4f){
           y_ship_pos += ship_speed; 
        }
    }
    else if (y_ship_pos >= (47 - spaceship1_height)){ y_ship_pos = 47 - spaceship1_height;}     //Limits for y direction border IMPROVE IF POSSIBLE.
    else if (y_ship_pos < 0){ y_ship_pos = 0;}
       
}

void Game::fireNewBlast() {
    // Search the array of blasts if inactive we can use it.
    int found_inactive_blast = -1;
    for (int i = 0; i < MAX_BLASTS; ++i) {
        if (!blasts[i].isActive()) {
            found_inactive_blast = i;
            break;
        }
    }
    
    if (found_inactive_blast != -1) {
        blasts[found_inactive_blast].activate(x_ship_pos + spaceship1_width, y_ship_pos + (spaceship1_height/2));
    }
}

void Game::updateAndDrawBlasts(){
    for (int i = 0; i < MAX_BLASTS; ++i) {
        if (blasts[i].isActive()) {
            blasts[i].updateAndDraw();
        }
    }
}

void Game::newSmallStarFlies() {
    // Search the array of stars if inactive we can use it. - the same as with blasts
    int found_inactive_small_star = -1;
    for (int i = 0; i < MAX_SMALL_STARS; ++i) {
        if (!small_stars[i].isSmallStarActive()) {
            found_inactive_small_star = i;
            break;
        }
    }
    
    if (found_inactive_small_star != -1) {
        small_stars[found_inactive_small_star].smallStarActivat(screen_width, rand() % screen_height);
    }
}

void Game::updateAndDrawSmallStars(){
    for (int i = 0; i < MAX_SMALL_STARS; ++i) {
        if (small_stars[i].isSmallStarActive()) {
            small_stars[i].updateAndDrawSmallStar();
        }
    }
}


void Game::newMediumStarFlies() {
    // Search the array of stars if inactive we can use it. - the same as with blasts
    int found_inactive_medium_star = -1;
    for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
        if (!medium_stars[i].isMediumStarActive()) {
            found_inactive_medium_star = i;
            break;
        }
    }
    
    if (found_inactive_medium_star != -1) {
        medium_stars[found_inactive_medium_star].mediumStarActivat(screen_width, rand() % screen_height); /////////////////////////////////////////// CHANGE
    }
}

void Game::updateAndDrawMediumStars(){
    for (int i = 0; i < MAX_MEDIUM_STARS; ++i) {
        if (medium_stars[i].isMediumStarActive()) {
            medium_stars[i].updateAndDrawMediumStar();
        }
    }
}