Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Thu Apr 13 22:21:02 2017 +0000
Revision:
25:edd6a95607b1
Parent:
24:d6187d39f09b
Child:
26:140515d80457
help me pls im tired. new bug found. spawnDE one shot only

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasper0712 4:4145abd09075 1 #include "GameEngine.h"
jasper0712 4:4145abd09075 2
jasper0712 4:4145abd09075 3 GameEngine::GameEngine()
jasper0712 4:4145abd09075 4 {
jasper0712 4:4145abd09075 5
jasper0712 4:4145abd09075 6 }
jasper0712 4:4145abd09075 7
jasper0712 4:4145abd09075 8 GameEngine::~GameEngine()
jasper0712 4:4145abd09075 9 {
jasper0712 4:4145abd09075 10
jasper0712 4:4145abd09075 11 }
jasper0712 17:53aedd20155a 12 void GameEngine::init() {
jasper0712 17:53aedd20155a 13 _d1.droneInit(20, 46, 0);
jasper0712 17:53aedd20155a 14 _d2.droneInit(62, 46, 83);
jasper0712 18:d82059ce929b 15 weap.init();
jasper0712 18:d82059ce929b 16 spa.init();
jasper0712 19:7ccbb19703f9 17 //printf("init completed \n");
jasper0712 17:53aedd20155a 18 }
jasper0712 17:53aedd20155a 19 void GameEngine::update(Gamepad &pad, N5110 &lcd) {
jasper0712 24:d6187d39f09b 20 //_d1.drone(Array2, charArray2, lcd);
jasper0712 24:d6187d39f09b 21 //_d2.drone(Array2, charArray2, lcd);
jasper0712 17:53aedd20155a 22 drawSpawn(lcd);
jasper0712 17:53aedd20155a 23 drawLaserPlayer(pad, lcd);
jasper0712 17:53aedd20155a 24 bombAndShield(pad, lcd);
jasper0712 17:53aedd20155a 25 }
jasper0712 4:4145abd09075 26 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 19:7ccbb19703f9 27 //spawning every 2 second, moving spawn at every 1 seconds when fps = 15 & drawit=30.
jasper0712 25:edd6a95607b1 28 //It changes depending on the game FPS.
jasper0712 25:edd6a95607b1 29 for (int x = 0; x < Cols; x++) {
jasper0712 25:edd6a95607b1 30 for (int y = 0; y < Rows; y++) {
jasper0712 25:edd6a95607b1 31 spa.deleteChar(x, y, Array2, charArray); //clean up on the array if the spawn dies after taking damage.
jasper0712 25:edd6a95607b1 32 }
jasper0712 25:edd6a95607b1 33 }
jasper0712 22:2e75b50b26f0 34 if (drawit == 0) { //DONT FORGET TO CHANGE THIS TO TICKER. MORE EFFICIENT.
jasper0712 19:7ccbb19703f9 35 drawit = 30;
jasper0712 20:32f115462bbc 36 spa.randomizeSpawn(Array, charArray);
jasper0712 20:32f115462bbc 37 spa.moveSpawnABC(Array, Array2, charArray, charArray2);
jasper0712 24:d6187d39f09b 38 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 4:4145abd09075 39 } else {
jasper0712 19:7ccbb19703f9 40 drawit -= 0.5;
jasper0712 24:d6187d39f09b 41 spa.moveSpawnDE(Array, Array2, charArray, charArray2);
jasper0712 20:32f115462bbc 42 spa.moveSpawnB(Array, Array2, charArray, charArray2);
jasper0712 19:7ccbb19703f9 43 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 44 }
jasper0712 20:32f115462bbc 45 spa.updateSpawn(Array, Array2, charArray, charArray2, lcd);
jasper0712 14:064b8d7f348d 46 //checkGameRule(lcd);
jasper0712 4:4145abd09075 47 }
jasper0712 5:c74bbdda06f4 48 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 49 weap.drawPlayer(pad, lcd);
jasper0712 9:4c4d787c7a8b 50 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 51 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 52 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 53 weap.weaponMath(pad);
jasper0712 20:32f115462bbc 54 weap.drawLaser(Array2, charArray2, lcd);
jasper0712 5:c74bbdda06f4 55 }
jasper0712 5:c74bbdda06f4 56 }
jasper0712 10:926b1f89c4f1 57 void GameEngine::checkGameRule(N5110 &lcd) {
jasper0712 9:4c4d787c7a8b 58 //the only way to lose is when the spawn reaches y = 46.
jasper0712 10:926b1f89c4f1 59 for ( int x = 0; x < 84; x ++) {
jasper0712 20:32f115462bbc 60 if (Array[x][46] > 0) {
jasper0712 10:926b1f89c4f1 61 while (1) {
jasper0712 10:926b1f89c4f1 62 lcd.clear();
jasper0712 10:926b1f89c4f1 63 lcd.printString("Game Over",10,3);
jasper0712 10:926b1f89c4f1 64 lcd.refresh();
jasper0712 10:926b1f89c4f1 65 wait(1.0);
jasper0712 10:926b1f89c4f1 66 }
jasper0712 10:926b1f89c4f1 67 }
jasper0712 10:926b1f89c4f1 68 }
jasper0712 5:c74bbdda06f4 69 }
jasper0712 17:53aedd20155a 70 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 21:e5585569a938 71 weap.bombCooldown(Array2, pad, lcd);
jasper0712 14:064b8d7f348d 72 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 15:c68a5c22a2a2 73 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 14:064b8d7f348d 74 }
jasper0712 19:7ccbb19703f9 75 if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
jasper0712 15:c68a5c22a2a2 76 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 20:32f115462bbc 77 weap.energyShield(Array, Array2, charArray, charArray2, lcd);
jasper0712 15:c68a5c22a2a2 78 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 79 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 80 }
jasper0712 15:c68a5c22a2a2 81 }
jasper0712 15:c68a5c22a2a2 82 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 83 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 84 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 85 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 86 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 19:7ccbb19703f9 87 } else { //when the shield is fully charged. It will automatically activate the shield.
jasper0712 19:7ccbb19703f9 88 pad.buttonhold_B = 1;
jasper0712 19:7ccbb19703f9 89 }
jasper0712 14:064b8d7f348d 90 }
jasper0712 15:c68a5c22a2a2 91 weap.shieldMeter(lcd);
jasper0712 10:926b1f89c4f1 92 }