Meteor defense project

Dependencies:   N5110 mbed

GameEngine/GameEngine.cpp

Committer:
jasper0712
Date:
2017-04-23
Revision:
36:2608622e5018
Parent:
34:6ac9541d4c31
Child:
37:45da88e36d1d

File content as of revision 36:2608622e5018:

#include "GameEngine.h"

GameEngine::GameEngine()
{

}

GameEngine::~GameEngine()
{

}
void GameEngine::startingMenu(Gamepad &pad, N5110 &lcd) {
    _gui.startingMenu(lcd);
    ledNumber ++;
    pad.led(ledNumber, 1.0);
    pad.led(ledNumber - 1, 0.0);
    if (ledNumber == 6) {
        ledNumber = 0;    
    } else {
        pad.led(6, 0.0);    
    } 
    _gui.preventDoubleClick(pad);
    
}
void GameEngine::init(int w) {
    NumberOfLife = 3; //3 life on every wave
    _d1.droneInit(20, 46, 0, 42); //drone1 is taking left side pixel 0 to 42.
    _d2.droneInit(62, 46, 42, 84); //drone2 is taking right side pixel 42 to 83;
    weap.init();
    spa.init(w);
    //printf("init completed \n");
}
void GameEngine::update(Gamepad &pad, N5110 &lcd) {
    _d1.droneScan(Array2, charArray2, lcd);
    _d2.droneScan(Array2, charArray2, lcd);
    drawSpawn(lcd); 
    drawLaserPlayer(pad, lcd);
    bombAndShield(pad, lcd);
    checkGameRule(pad, lcd);
}
void GameEngine::weapUpgrade(Gamepad &pad, N5110 &lcd) {
    if (pad.check_event(Gamepad::START_PRESSED)) { 
        _gui.upgrading = 1;
        _gui.preventDoubleClick(pad);
        while (_gui.upgrading == 1) {
            Variables copyVar = weap.getVar(); //this shit is not working.
            //printf("copyvar %d \n",copyVar.laserDamage);
            _gui.upgrades(copyVar, pad, lcd); //go to the main upgrade page
            if (_gui.saveTheData == 1) {
                Data _upgrades = _gui.savedata(); //saving data
                //_gui.testingtest(_upgrades);  //code used to test the struct
                weap.setVar(_upgrades.up, _upgrades.up1);
                weap.setVar(_upgrades.u1p, _upgrades.u1p1);
                //weap.testtest();
                //printf("TESTING up = %d, up1 = %d, u1p = %d, u1p1 = %d \n",_upgrades.up,_upgrades.up1,_upgrades.u1p,_upgrades.u1p1); 
                _gui.saveTheData = 0;
                _gui.upgrading = 0; //get out of while loop after done upgrading
            }
            lcd.refresh();
            wait(0.2);
            lcd.clear();
        } 
        doneUpgrade = 1;
        //remember to re-initialise everything after a wave.
    }
}
void GameEngine::drawSpawn(N5110 &lcd){
    updateArray(); //clean up & update the array after the spawn took damage.
    if (drawit == 0) { //
        drawit = 30;
        spa.randomizeSpawn(Array, charArray);
        spa.moveSpawnABC(Array, Array2, charArray, charArray2);
        spa.moveSpawnDE(Array, Array2, charArray, charArray2);
        //printf("why u do this to cannon_Firing?? cannon_Firing = %d \n",weap.cannon_Firing);
        if ( fireInTheHole_flag ==1) { //this code because whenever spawn ABCDE moves, weap.cannon_Firing goes to 0 automatically (NO IDEA WHY)
            weap.cannon_Firing = 1;    
        }
    } else {
        drawit -= 0.5;
        spa.moveSpawnDE(Array, Array2, charArray, charArray2);
        spa.moveSpawnB(Array, Array2, charArray, charArray2);
        //printf("moving spawn b \n");
    }
    spa.updateSpawn(Array, Array2, charArray, charArray2, 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);
        
        //cannon can only be fired when the laser is switched on.
        if ( pad.check_event(Gamepad::Y_PRESSED)) { //press button to set flag (fire the cannon)
            fireInTheHole_flag = 1; 
        }
    }
    if ( fireInTheHole_flag == 1) {
        weap.cannon(lcd);    
    }
    if ( weap.cannon_Firing == 0) { //clear flag when cannon is done 
        fireInTheHole_flag = 0;
    }
}
void GameEngine::checkGameRule(Gamepad &pad, N5110 &lcd) {
    //the only way to lose a life is when a spawn reaches y = 46.
    numberOfLife_leds(pad);
    for ( int x = 0; x < 84; x ++) {
        if (Array[x][46] > 0) { //if anything reaches line y=46
            NumberOfLife --;
            printf("opps you lost a life. NumberOfLife = %d \n",NumberOfLife);
            if (NumberOfLife == 0) {
                while (1) {
                    lcd.clear();
                    lcd.printString("Game Over",10,3);
                    lcd.refresh();   
                    pad.leds_on();
                    wait(0.1);
                    pad.leds_off();
                    wait(0.1);
                }
            }
        }
    }       
}
void GameEngine::numberOfLife_leds(Gamepad &pad) { 
    //light up LEDs depends on number of lifes left.
    if (NumberOfLife == 3) {
        pad.leds_on();
    }
    if (NumberOfLife == 2) {
        pad.led(1, 1.0);
        pad.led(2, 1.0);
        pad.led(3, 0.0);
        pad.led(4, 0.0);
        pad.led(5, 1.0);
        pad.led(6, 1.0);
    }
    if (NumberOfLife == 1) {
        pad.led(1, 1.0);
        pad.led(2, 0.0);
        pad.led(3, 0.0);
        pad.led(4, 0.0);
        pad.led(5, 0.0);
        pad.led(6, 1.0);   
    }
}
void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) { 
    weap.init();
    weap.bombCooldown(Array2, pad, lcd);
    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); 
}
void GameEngine::updateArray() { //clean up tool
    for (int x = 0; x < Cols; x++) {
        for (int y = 0; y < Rows; y++) {
            Array[x][y] = Array2[x][y];
            //printf(" Array[%d][%d] = %d \n",x,y,Array2[x][y]);
            spa.deleteChar(x, y, Array, charArray2);
        }
    }    
}