Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
GameEngine/GameEngine.cpp@8:475b80f88ceb, 2019-05-08 (annotated)
- Committer:
- adat80
- Date:
- Wed May 08 23:40:34 2019 +0000
- Revision:
- 8:475b80f88ceb
- Parent:
- 6:f06ce4cf068a
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| adat80 | 1:3916f272663e | 1 | #include "GameEngine.h" |
| adat80 | 1:3916f272663e | 2 | |
| adat80 | 3:97cd7b3d89d0 | 3 | |
| adat80 | 2:88019d96e1da | 4 | |
| adat80 | 1:3916f272663e | 5 | GameEngine::GameEngine() |
| adat80 | 1:3916f272663e | 6 | { |
| adat80 | 1:3916f272663e | 7 | |
| adat80 | 1:3916f272663e | 8 | } |
| adat80 | 1:3916f272663e | 9 | |
| adat80 | 1:3916f272663e | 10 | GameEngine::~GameEngine() |
| adat80 | 1:3916f272663e | 11 | { |
| adat80 | 1:3916f272663e | 12 | |
| adat80 | 1:3916f272663e | 13 | } |
| adat80 | 1:3916f272663e | 14 | |
| adat80 | 1:3916f272663e | 15 | void GameEngine::init(int CrossHairsSpeed, int fps) |
| adat80 | 1:3916f272663e | 16 | { |
| adat80 | 1:3916f272663e | 17 | // initialise the game parameters |
| adat80 | 3:97cd7b3d89d0 | 18 | _game_over = false; |
| adat80 | 1:3916f272663e | 19 | _fps = fps; |
| adat80 | 6:f06ce4cf068a | 20 | _frame = 0; //keep count of number of frames |
| adat80 | 6:f06ce4cf068a | 21 | _levelNumber = 1; //keep count of number of frames |
| adat80 | 6:f06ce4cf068a | 22 | _killCount = 0; //keep count of number of enemies killed |
| adat80 | 1:3916f272663e | 23 | |
| adat80 | 6:f06ce4cf068a | 24 | //initialise wall object with max life |
| adat80 | 2:88019d96e1da | 25 | _wall.init(100); |
| adat80 | 6:f06ce4cf068a | 26 | |
| adat80 | 6:f06ce4cf068a | 27 | //initialise cross hairs with cross hairs speed |
| adat80 | 1:3916f272663e | 28 | _crossHairs.init(CrossHairsSpeed); |
| adat80 | 1:3916f272663e | 29 | |
| adat80 | 6:f06ce4cf068a | 30 | //seed random with time |
| adat80 | 1:3916f272663e | 31 | srand(time(NULL)); |
| adat80 | 1:3916f272663e | 32 | |
| adat80 | 6:f06ce4cf068a | 33 | //set enemies pointer to point to array of enemies |
| adat80 | 1:3916f272663e | 34 | _enemies = _level.init(_levelNumber); |
| adat80 | 1:3916f272663e | 35 | |
| adat80 | 1:3916f272663e | 36 | |
| adat80 | 1:3916f272663e | 37 | |
| adat80 | 1:3916f272663e | 38 | |
| adat80 | 1:3916f272663e | 39 | |
| adat80 | 1:3916f272663e | 40 | |
| adat80 | 1:3916f272663e | 41 | |
| adat80 | 1:3916f272663e | 42 | } |
| adat80 | 1:3916f272663e | 43 | |
| adat80 | 1:3916f272663e | 44 | void GameEngine::read_input(Gamepad &pad) |
| adat80 | 1:3916f272663e | 45 | { |
| adat80 | 1:3916f272663e | 46 | _angle = pad.get_angle(); |
| adat80 | 1:3916f272663e | 47 | _mag = pad.get_mag(); |
| adat80 | 1:3916f272663e | 48 | } |
| adat80 | 1:3916f272663e | 49 | |
| adat80 | 1:3916f272663e | 50 | void GameEngine::draw(N5110 &lcd) |
| adat80 | 1:3916f272663e | 51 | { |
| adat80 | 6:f06ce4cf068a | 52 | //print level number at top of display |
| adat80 | 6:f06ce4cf068a | 53 | char level[14]; |
| adat80 | 6:f06ce4cf068a | 54 | sprintf(level,"lvl:%i",_levelNumber); |
| adat80 | 6:f06ce4cf068a | 55 | lcd.printString(level,23,0); |
| adat80 | 2:88019d96e1da | 56 | |
| adat80 | 2:88019d96e1da | 57 | //draw wall |
| adat80 | 2:88019d96e1da | 58 | _wall.draw(lcd); |
| adat80 | 2:88019d96e1da | 59 | |
| adat80 | 6:f06ce4cf068a | 60 | //draw all enemies |
| adat80 | 3:97cd7b3d89d0 | 61 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 1:3916f272663e | 62 | _enemies[i].draw(lcd); |
| adat80 | 1:3916f272663e | 63 | } |
| adat80 | 1:3916f272663e | 64 | |
| adat80 | 6:f06ce4cf068a | 65 | //draw cross hairs |
| adat80 | 2:88019d96e1da | 66 | _crossHairs.draw(lcd); |
| adat80 | 1:3916f272663e | 67 | } |
| adat80 | 1:3916f272663e | 68 | |
| adat80 | 1:3916f272663e | 69 | void GameEngine::update(Gamepad &pad) |
| adat80 | 1:3916f272663e | 70 | { |
| adat80 | 6:f06ce4cf068a | 71 | //increment frame count |
| adat80 | 3:97cd7b3d89d0 | 72 | _frame = _frame + 1; |
| adat80 | 6:f06ce4cf068a | 73 | |
| adat80 | 6:f06ce4cf068a | 74 | /*if statement finds modulus of frame by fps/2 and checks if equal to 0 |
| adat80 | 6:f06ce4cf068a | 75 | /this will be true every half second |
| adat80 | 6:f06ce4cf068a | 76 | /when this is true if an enemies state is that it is attacking the wall |
| adat80 | 6:f06ce4cf068a | 77 | /the wall will be damaged |
| adat80 | 6:f06ce4cf068a | 78 | */ |
| adat80 | 3:97cd7b3d89d0 | 79 | if (_frame%(_fps/2) == 0) { |
| adat80 | 3:97cd7b3d89d0 | 80 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 3:97cd7b3d89d0 | 81 | if(_enemies[i].get_current_action() == attacking) { |
| adat80 | 3:97cd7b3d89d0 | 82 | _wall.take_damage(1); |
| adat80 | 5:8bd09c675f28 | 83 | if (_wall.get_wall_health() <= 0) { |
| adat80 | 6:f06ce4cf068a | 84 | game_over(); //if wall health gets to zero game is over |
| adat80 | 3:97cd7b3d89d0 | 85 | } |
| adat80 | 3:97cd7b3d89d0 | 86 | } |
| adat80 | 3:97cd7b3d89d0 | 87 | } |
| adat80 | 3:97cd7b3d89d0 | 88 | } |
| adat80 | 3:97cd7b3d89d0 | 89 | |
| adat80 | 6:f06ce4cf068a | 90 | //update the level object |
| adat80 | 1:3916f272663e | 91 | _level.update(_fps); |
| adat80 | 6:f06ce4cf068a | 92 | //update cross hairs object |
| adat80 | 5:8bd09c675f28 | 93 | _crossHairs.update(_angle,_mag); |
| adat80 | 1:3916f272663e | 94 | |
| adat80 | 6:f06ce4cf068a | 95 | //update all enemies |
| adat80 | 3:97cd7b3d89d0 | 96 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 2:88019d96e1da | 97 | _enemies[i].update(_fps); |
| adat80 | 1:3916f272663e | 98 | } |
| adat80 | 1:3916f272663e | 99 | |
| adat80 | 6:f06ce4cf068a | 100 | //check if either A or B are pressed, if true fire shot |
| adat80 | 1:3916f272663e | 101 | if ( pad.check_event(Gamepad::B_PRESSED) == true) { |
| adat80 | 1:3916f272663e | 102 | fireShot(pad); |
| adat80 | 2:88019d96e1da | 103 | } else if ( pad.check_event(Gamepad::A_PRESSED) == true) { |
| adat80 | 2:88019d96e1da | 104 | fireShot(pad); |
| adat80 | 1:3916f272663e | 105 | } |
| adat80 | 1:3916f272663e | 106 | |
| adat80 | 1:3916f272663e | 107 | |
| adat80 | 1:3916f272663e | 108 | |
| adat80 | 1:3916f272663e | 109 | } |
| adat80 | 1:3916f272663e | 110 | |
| adat80 | 1:3916f272663e | 111 | void GameEngine::fireShot(Gamepad &gpad) |
| adat80 | 1:3916f272663e | 112 | { |
| adat80 | 6:f06ce4cf068a | 113 | gpad.tone(400, 0.1); //gunshot noise |
| adat80 | 6:f06ce4cf068a | 114 | Vector2D shotLoc = _crossHairs.get_pos(); //get cross hairs location to get shot location |
| adat80 | 6:f06ce4cf068a | 115 | //check if enemies are alive |
| adat80 | 3:97cd7b3d89d0 | 116 | for(int i = 0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 2:88019d96e1da | 117 | if (_enemies[i].get_alive()) { |
| adat80 | 6:f06ce4cf068a | 118 | //function finds if enemy i has been shot |
| adat80 | 4:87f3f3e5cdd9 | 119 | was_enemy_shot(i, shotLoc); |
| adat80 | 1:3916f272663e | 120 | } |
| adat80 | 1:3916f272663e | 121 | } |
| adat80 | 1:3916f272663e | 122 | } |
| adat80 | 1:3916f272663e | 123 | |
| adat80 | 4:87f3f3e5cdd9 | 124 | void GameEngine::was_enemy_shot(int i, Vector2D shotLoc) |
| adat80 | 4:87f3f3e5cdd9 | 125 | { |
| adat80 | 6:f06ce4cf068a | 126 | Vector2D enemyLoc = _enemies[i].get_pos(); //get enemy position |
| adat80 | 6:f06ce4cf068a | 127 | //compare x and y distance of enemy and shot and if it is less than 4 enemy is shot |
| adat80 | 4:87f3f3e5cdd9 | 128 | if (abs(enemyLoc.x - shotLoc.x) < 4 && abs(enemyLoc.y - shotLoc.y) < 4) { |
| adat80 | 6:f06ce4cf068a | 129 | _killCount = _killCount + 1; //incriment kill count |
| adat80 | 6:f06ce4cf068a | 130 | _enemies[i].set_alive(false); //set alive flag to false |
| adat80 | 6:f06ce4cf068a | 131 | //set enemy action to dying |
| adat80 | 6:f06ce4cf068a | 132 | Action myAc; |
| adat80 | 4:87f3f3e5cdd9 | 133 | myAc = dying; |
| adat80 | 4:87f3f3e5cdd9 | 134 | _enemies[i].set_current_action(myAc); |
| adat80 | 4:87f3f3e5cdd9 | 135 | |
| adat80 | 6:f06ce4cf068a | 136 | //checks if the kill count has reached the enemy number of level |
| adat80 | 4:87f3f3e5cdd9 | 137 | if (_killCount == _level.get_number_of_enemies()) { |
| adat80 | 6:f06ce4cf068a | 138 | _killCount = 0; //resets kill count |
| adat80 | 6:f06ce4cf068a | 139 | _levelNumber = _levelNumber + 1; //incriments level number |
| adat80 | 6:f06ce4cf068a | 140 | _enemies = _level.init(_levelNumber); //initialise new level and set pointer to new enemy array |
| adat80 | 4:87f3f3e5cdd9 | 141 | } |
| adat80 | 4:87f3f3e5cdd9 | 142 | } |
| adat80 | 4:87f3f3e5cdd9 | 143 | |
| adat80 | 4:87f3f3e5cdd9 | 144 | |
| adat80 | 4:87f3f3e5cdd9 | 145 | } |
| adat80 | 4:87f3f3e5cdd9 | 146 | |
| adat80 | 3:97cd7b3d89d0 | 147 | bool GameEngine::get_game_over() { |
| adat80 | 6:f06ce4cf068a | 148 | return _game_over; //true if over, false if not |
| adat80 | 3:97cd7b3d89d0 | 149 | |
| adat80 | 3:97cd7b3d89d0 | 150 | } |
| adat80 | 3:97cd7b3d89d0 | 151 | void GameEngine::set_game_over(bool is_game_over) { |
| adat80 | 6:f06ce4cf068a | 152 | _game_over = is_game_over; //true if over, false if not |
| adat80 | 3:97cd7b3d89d0 | 153 | } |
| adat80 | 5:8bd09c675f28 | 154 | |
| adat80 | 5:8bd09c675f28 | 155 | void GameEngine::game_over() { |
| adat80 | 6:f06ce4cf068a | 156 | _game_over = true; //sets the game as over |
| adat80 | 5:8bd09c675f28 | 157 | |
| adat80 | 5:8bd09c675f28 | 158 | } |