Meteor defense project

Dependencies:   N5110 mbed

GameEngine/GameEngine.cpp

Committer:
jasper0712
Date:
2017-04-13
Revision:
23:6d197a452d7c
Parent:
22:2e75b50b26f0
Child:
24:d6187d39f09b

File content as of revision 23:6d197a452d7c:

#include "GameEngine.h"

GameEngine::GameEngine()
{

}

GameEngine::~GameEngine()
{

}
void GameEngine::init() {
    _d1.droneInit(20, 46, 0);
    _d2.droneInit(62, 46, 83);   
    weap.init();
    spa.init();
    //printf("init completed \n");
}
void GameEngine::update(Gamepad &pad, N5110 &lcd) {
    _d1.drone(Array2, charArray2, lcd);
    _d2.drone(Array2, charArray2, lcd);
    drawSpawn(lcd); 
    drawLaserPlayer(pad, lcd);
    bombAndShield(pad, lcd);
}
void GameEngine::drawSpawn(N5110 &lcd){
    //spawning every 2 second, moving spawn at every 1 seconds when fps = 15 & drawit=30. 
    //It changes depending on the game FPS.
    if (drawit == 0) { //DONT FORGET TO CHANGE THIS TO TICKER. MORE EFFICIENT.
        drawit = 30;
        spa.randomizeSpawn(Array, charArray);
        spa.moveSpawnABC(Array, Array2, charArray, charArray2);
        spa.moveSpawnD(Array, Array2, charArray, charArray2);
    } else {
        drawit -= 0.5;
        spa.moveSpawnD(Array, Array2, charArray, charArray2);
        spa.moveSpawnB(Array, Array2, charArray, charArray2);
        //printf("moving spawn b \n");
    }
    spa.updateSpawn(Array, Array2, charArray, charArray2, lcd); 
    //checkGameRule(lcd);
}
void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
    weap.drawPlayer(pad, lcd);
    //draw a line for the laser when button is pressed
    if (pad.buttonhold_A == 1) {
        //printf("hi, button is working \n");
        weap.weaponMath(pad);
        weap.drawLaser(Array2, charArray2, lcd);
    }
}
void GameEngine::checkGameRule(N5110 &lcd) {
    //the only way to lose is when the spawn reaches y = 46.
    for ( int x = 0; x < 84; x ++) {
        if (Array[x][46] > 0) {
            while (1) {
                lcd.clear();
                lcd.printString("Game Over",10,3);
                lcd.refresh();
                wait(1.0);
            }
        }
    }       
}
void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) { 
    weap.bombCooldown(Array2, pad, lcd);
    //spa.deleteChar(x, y, Arr2, cArr2);
    if (pad.check_event(Gamepad::X_PRESSED)) {
        //to prevent from detonating the bomb at instant when button X is accidentally pressed 
    }
    if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
        if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
            weap.energyShield(Array, Array2, charArray, charArray2, lcd);
        } else { //or else, deactivate the shield and recharge the shield.
            pad.buttonhold_B = 0;
        }    
    } 
    if (pad.buttonhold_B == 0) { // recharge shield 
        if (weap._shield < weap._shieldCapacity ) {
            weap._shield = weap._shield + weap._shieldRegenRate;
            //printf("current shield = %d \n",weap._shield); 
            //printf("shield regen rate = %d \n",weap._shieldRegenRate);
        } else { //when the shield is fully charged. It will automatically activate the shield.
            pad.buttonhold_B = 1;
        }
    }
    weap.shieldMeter(lcd);
}