Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Wed Apr 12 17:02:22 2017 +0000
Revision:
20:32f115462bbc
Parent:
19:7ccbb19703f9
Child:
21:e5585569a938
Learning to use STRUCT. Problem with bomb (spawn reviving case).

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 20:32f115462bbc 20 //_d1.drone(Array2, charArray2, lcd);
jasper0712 20:32f115462bbc 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 20:32f115462bbc 25 //whatever(pad, lcd);
jasper0712 17:53aedd20155a 26 }
jasper0712 4:4145abd09075 27 void GameEngine::drawSpawn(N5110 &lcd){
jasper0712 19:7ccbb19703f9 28 //spawning every 2 second, moving spawn at every 1 seconds when fps = 15 & drawit=30.
jasper0712 9:4c4d787c7a8b 29 //It changes depending on the game FPS.
jasper0712 19:7ccbb19703f9 30 if (drawit == 0) {
jasper0712 19:7ccbb19703f9 31 drawit = 30;
jasper0712 20:32f115462bbc 32 spa.randomizeSpawn(Array, charArray);
jasper0712 20:32f115462bbc 33 spa.moveSpawnABC(Array, Array2, charArray, charArray2);
jasper0712 19:7ccbb19703f9 34
jasper0712 4:4145abd09075 35 } else {
jasper0712 19:7ccbb19703f9 36 drawit -= 0.5;
jasper0712 20:32f115462bbc 37 spa.moveSpawnB(Array, Array2, charArray, charArray2);
jasper0712 19:7ccbb19703f9 38 //printf("moving spawn b \n");
jasper0712 4:4145abd09075 39 }
jasper0712 20:32f115462bbc 40 spa.updateSpawn(Array, Array2, charArray, charArray2, lcd);
jasper0712 14:064b8d7f348d 41 //checkGameRule(lcd);
jasper0712 4:4145abd09075 42 }
jasper0712 5:c74bbdda06f4 43 void GameEngine::drawLaserPlayer(Gamepad &pad, N5110 &lcd) {
jasper0712 19:7ccbb19703f9 44 weap.drawPlayer(pad, lcd);
jasper0712 9:4c4d787c7a8b 45 //draw a line for the laser when button is pressed
jasper0712 10:926b1f89c4f1 46 if (pad.buttonhold_A == 1) {
jasper0712 7:3be4369131c6 47 //printf("hi, button is working \n");
jasper0712 5:c74bbdda06f4 48 weap.weaponMath(pad);
jasper0712 20:32f115462bbc 49 weap.drawLaser(Array2, charArray2, lcd);
jasper0712 5:c74bbdda06f4 50 }
jasper0712 5:c74bbdda06f4 51 }
jasper0712 10:926b1f89c4f1 52 void GameEngine::checkGameRule(N5110 &lcd) {
jasper0712 9:4c4d787c7a8b 53 //the only way to lose is when the spawn reaches y = 46.
jasper0712 10:926b1f89c4f1 54 for ( int x = 0; x < 84; x ++) {
jasper0712 20:32f115462bbc 55 if (Array[x][46] > 0) {
jasper0712 10:926b1f89c4f1 56 while (1) {
jasper0712 10:926b1f89c4f1 57 lcd.clear();
jasper0712 10:926b1f89c4f1 58 lcd.printString("Game Over",10,3);
jasper0712 10:926b1f89c4f1 59 lcd.refresh();
jasper0712 10:926b1f89c4f1 60 wait(1.0);
jasper0712 10:926b1f89c4f1 61 }
jasper0712 10:926b1f89c4f1 62 }
jasper0712 10:926b1f89c4f1 63 }
jasper0712 5:c74bbdda06f4 64 }
jasper0712 17:53aedd20155a 65 void GameEngine::bombAndShield(Gamepad &pad, N5110 &lcd) {
jasper0712 20:32f115462bbc 66 weap.bombCooldown(Array, pad, lcd);
jasper0712 14:064b8d7f348d 67 if (pad.check_event(Gamepad::X_PRESSED)) {
jasper0712 15:c68a5c22a2a2 68 //to prevent from detonating the bomb at instant when button X is accidentally pressed
jasper0712 14:064b8d7f348d 69 }
jasper0712 19:7ccbb19703f9 70 if (pad.buttonhold_B == 1) { //if B pressed, generate shield. Shield doesnt have to be fully charged to be activated
jasper0712 15:c68a5c22a2a2 71 if (weap._shield > 0) { //shield will only activate if the shield has not depleted.
jasper0712 20:32f115462bbc 72 weap.energyShield(Array, Array2, charArray, charArray2, lcd);
jasper0712 15:c68a5c22a2a2 73 } else { //or else, deactivate the shield and recharge the shield.
jasper0712 15:c68a5c22a2a2 74 pad.buttonhold_B = 0;
jasper0712 15:c68a5c22a2a2 75 }
jasper0712 15:c68a5c22a2a2 76 }
jasper0712 15:c68a5c22a2a2 77 if (pad.buttonhold_B == 0) { // recharge shield
jasper0712 15:c68a5c22a2a2 78 if (weap._shield < weap._shieldCapacity ) {
jasper0712 15:c68a5c22a2a2 79 weap._shield = weap._shield + weap._shieldRegenRate;
jasper0712 15:c68a5c22a2a2 80 //printf("current shield = %d \n",weap._shield);
jasper0712 16:ddc4d5669a6e 81 //printf("shield regen rate = %d \n",weap._shieldRegenRate);
jasper0712 19:7ccbb19703f9 82 } else { //when the shield is fully charged. It will automatically activate the shield.
jasper0712 19:7ccbb19703f9 83 pad.buttonhold_B = 1;
jasper0712 19:7ccbb19703f9 84 }
jasper0712 14:064b8d7f348d 85 }
jasper0712 15:c68a5c22a2a2 86 weap.shieldMeter(lcd);
jasper0712 20:32f115462bbc 87 }
jasper0712 20:32f115462bbc 88 void GameEngine::whatever(Gamepad &pad, N5110 &lcd) {
jasper0712 20:32f115462bbc 89 int r = 10;
jasper0712 20:32f115462bbc 90 int c2 = r * r;
jasper0712 20:32f115462bbc 91 Vector2D coord = pad.get_coord();
jasper0712 20:32f115462bbc 92 int _y = 24 - coord.y * 24;
jasper0712 20:32f115462bbc 93 int _x = 42 + coord.x * 42;
jasper0712 20:32f115462bbc 94 int x2 = _x + r +1;
jasper0712 20:32f115462bbc 95 int y2 = _y + r +1;
jasper0712 20:32f115462bbc 96 //find all the coordinate in a square depending on the radius of circle.
jasper0712 20:32f115462bbc 97 for (int x1 = _x - r; x1 < x2; x1 ++){
jasper0712 20:32f115462bbc 98 for (int y1 = _y - r; y1 < y2; y1 ++) {
jasper0712 20:32f115462bbc 99 //this code because when y1 get negative. The game crash.
jasper0712 20:32f115462bbc 100 int x3 = x1; int y3 = y1;
jasper0712 20:32f115462bbc 101 if (y3 < 0) {
jasper0712 20:32f115462bbc 102 y3 = 40;
jasper0712 20:32f115462bbc 103 } if (x3 < 0) {
jasper0712 20:32f115462bbc 104 x3 = 40;
jasper0712 20:32f115462bbc 105 }
jasper0712 20:32f115462bbc 106 int a2 = (x3 - _x) * (x3 - _x);
jasper0712 20:32f115462bbc 107 int b2 = (y3 - _y) * (y3 - _y);
jasper0712 20:32f115462bbc 108 if (a2 + b2 <= c2 +1) { //if the coordinate is within the circle
jasper0712 20:32f115462bbc 109 //draw circle and deal damage to it .
jasper0712 20:32f115462bbc 110 lcd.setPixel(x3,y3);
jasper0712 20:32f115462bbc 111 }
jasper0712 20:32f115462bbc 112 }
jasper0712 20:32f115462bbc 113 }
jasper0712 10:926b1f89c4f1 114 }