Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Mon Apr 10 09:56:47 2017 +0000
Revision:
17:53aedd20155a
Parent:
16:ddc4d5669a6e
Child:
18:d82059ce929b
done with drone? maybe.

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 17:53aedd20155a 15 weap.shieldInit();
jasper0712 17:53aedd20155a 16 }
jasper0712 17:53aedd20155a 17 void GameEngine::update(Gamepad &pad, N5110 &lcd) {
jasper0712 17:53aedd20155a 18 _d1.drone(Array1, charArray, lcd);
jasper0712 17:53aedd20155a 19 _d2.drone(Array1, charArray, lcd);
jasper0712 17:53aedd20155a 20 drawSpawn(lcd);
jasper0712 17:53aedd20155a 21 drawLaserPlayer(pad, lcd);
jasper0712 17:53aedd20155a 22 bombAndShield(pad, lcd);
jasper0712 17:53aedd20155a 23 }
jasper0712 4:4145abd09075 24 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 9:4c4d787c7a8b 25 //spawning every 2 second, moving every 1 seconds at 15 fps & drawit=15.
jasper0712 9:4c4d787c7a8b 26 //It changes depending on the game FPS.
jasper0712 4:4145abd09075 27 if (drawit ==0) {
jasper0712 13:38cbce17d7d7 28 drawit =30;
jasper0712 13:38cbce17d7d7 29 spa.randomizeSpawn(Array2, charArray);
jasper0712 13:38cbce17d7d7 30 spa.moveSpawnABC(Array1, Array2, charArray);
jasper0712 4:4145abd09075 31 } else {
jasper0712 4:4145abd09075 32 drawit -=0.5;
jasper0712 13:38cbce17d7d7 33 spa.moveSpawnB(Array1, Array2, charArray);
jasper0712 4:4145abd09075 34 }
jasper0712 13:38cbce17d7d7 35 spa.updateSpawn(Array1, Array2, charArray, lcd);
jasper0712 14:064b8d7f348d 36 //checkGameRule(lcd);
jasper0712 4:4145abd09075 37 }
jasper0712 5:c74bbdda06f4 38 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 9:4c4d787c7a8b 39 weap.laserDamage();
jasper0712 5:c74bbdda06f4 40 weap.drawWeapon(pad, lcd);
jasper0712 9:4c4d787c7a8b 41 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 42 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 43 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 44 weap.weaponMath(pad);
jasper0712 16:ddc4d5669a6e 45 weap.drawLaser(Array1, charArray, lcd);
jasper0712 5:c74bbdda06f4 46 }
jasper0712 5:c74bbdda06f4 47 }
jasper0712 9:4c4d787c7a8b 48
jasper0712 10:926b1f89c4f1 49 void GameEngine::checkGameRule(N5110 &lcd) {
jasper0712 9:4c4d787c7a8b 50 //the only way to lose is when the spawn reaches y = 46.
jasper0712 10:926b1f89c4f1 51 for ( int x = 0; x < 84; x ++) {
jasper0712 10:926b1f89c4f1 52 if (Array1[x][46] > 0) {
jasper0712 10:926b1f89c4f1 53 while (1) {
jasper0712 10:926b1f89c4f1 54 lcd.clear();
jasper0712 10:926b1f89c4f1 55 lcd.printString("Game Over",10,3);
jasper0712 10:926b1f89c4f1 56 lcd.refresh();
jasper0712 10:926b1f89c4f1 57 wait(1.0);
jasper0712 10:926b1f89c4f1 58 }
jasper0712 10:926b1f89c4f1 59 }
jasper0712 10:926b1f89c4f1 60 }
jasper0712 5:c74bbdda06f4 61 }
jasper0712 17:53aedd20155a 62 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 13:38cbce17d7d7 63 weap.bombCooldown(Array1, pad, lcd);
jasper0712 14:064b8d7f348d 64 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 15:c68a5c22a2a2 65 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 14:064b8d7f348d 66 }
jasper0712 15:c68a5c22a2a2 67 if (pad.buttonhold_B == 1) { //if B pressed, generate shield
jasper0712 15:c68a5c22a2a2 68 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 15:c68a5c22a2a2 69 weap.energyShield(Array1, Array2, charArray, lcd);
jasper0712 15:c68a5c22a2a2 70 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 71 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 72 }
jasper0712 15:c68a5c22a2a2 73 }
jasper0712 15:c68a5c22a2a2 74 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 75 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 76 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 77 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 78 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 15:c68a5c22a2a2 79 }
jasper0712 14:064b8d7f348d 80 }
jasper0712 15:c68a5c22a2a2 81 weap.shieldMeter(lcd);
jasper0712 10:926b1f89c4f1 82 }