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@5:8bd09c675f28, 2019-05-08 (annotated)
- Committer:
- adat80
- Date:
- Wed May 08 19:41:10 2019 +0000
- Revision:
- 5:8bd09c675f28
- Parent:
- 4:87f3f3e5cdd9
- Child:
- 6:f06ce4cf068a
Full Game prior to unused method clean-up;
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 | 3:97cd7b3d89d0 | 20 | _frame = 0; |
| adat80 | 3:97cd7b3d89d0 | 21 | _levelNumber = 1; |
| adat80 | 2:88019d96e1da | 22 | _killCount = 0; |
| adat80 | 1:3916f272663e | 23 | |
| adat80 | 2:88019d96e1da | 24 | _wall.init(100); |
| adat80 | 1:3916f272663e | 25 | _crossHairs.init(CrossHairsSpeed); |
| adat80 | 1:3916f272663e | 26 | |
| adat80 | 1:3916f272663e | 27 | |
| adat80 | 1:3916f272663e | 28 | |
| adat80 | 1:3916f272663e | 29 | |
| adat80 | 1:3916f272663e | 30 | srand(time(NULL)); |
| adat80 | 1:3916f272663e | 31 | |
| adat80 | 1:3916f272663e | 32 | _enemies = _level.init(_levelNumber); |
| adat80 | 1:3916f272663e | 33 | |
| adat80 | 1:3916f272663e | 34 | |
| 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 | void GameEngine::read_input(Gamepad &pad) |
| adat80 | 1:3916f272663e | 43 | { |
| adat80 | 1:3916f272663e | 44 | _angle = pad.get_angle(); |
| adat80 | 1:3916f272663e | 45 | _mag = pad.get_mag(); |
| adat80 | 1:3916f272663e | 46 | } |
| adat80 | 1:3916f272663e | 47 | |
| adat80 | 1:3916f272663e | 48 | void GameEngine::draw(N5110 &lcd) |
| adat80 | 1:3916f272663e | 49 | { |
| adat80 | 4:87f3f3e5cdd9 | 50 | |
| adat80 | 4:87f3f3e5cdd9 | 51 | |
| adat80 | 4:87f3f3e5cdd9 | 52 | |
| adat80 | 1:3916f272663e | 53 | |
| adat80 | 1:3916f272663e | 54 | |
| adat80 | 3:97cd7b3d89d0 | 55 | char level[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
| adat80 | 2:88019d96e1da | 56 | // so can display a string of a maximum 14 characters in length |
| adat80 | 2:88019d96e1da | 57 | // or create formatted strings - ensure they aren't more than 14 characters long |
| adat80 | 4:87f3f3e5cdd9 | 58 | int length = sprintf(level,"lvl:%i",_levelNumber); // print formatted data to buffer |
| adat80 | 2:88019d96e1da | 59 | // it is important the format specifier ensures the length will fit in the buffer |
| adat80 | 2:88019d96e1da | 60 | if (length <= 14) { // if string will fit on display (assuming printing at x=0) |
| adat80 | 3:97cd7b3d89d0 | 61 | lcd.printString(level,23,0); |
| adat80 | 2:88019d96e1da | 62 | } |
| adat80 | 2:88019d96e1da | 63 | |
| adat80 | 2:88019d96e1da | 64 | |
| adat80 | 2:88019d96e1da | 65 | //draw wall |
| adat80 | 2:88019d96e1da | 66 | _wall.draw(lcd); |
| adat80 | 2:88019d96e1da | 67 | |
| adat80 | 1:3916f272663e | 68 | |
| adat80 | 1:3916f272663e | 69 | |
| adat80 | 3:97cd7b3d89d0 | 70 | //draw enemies |
| adat80 | 3:97cd7b3d89d0 | 71 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 1:3916f272663e | 72 | _enemies[i].draw(lcd); |
| adat80 | 1:3916f272663e | 73 | } |
| adat80 | 1:3916f272663e | 74 | |
| adat80 | 2:88019d96e1da | 75 | _crossHairs.draw(lcd); |
| adat80 | 1:3916f272663e | 76 | } |
| adat80 | 1:3916f272663e | 77 | |
| adat80 | 1:3916f272663e | 78 | void GameEngine::update(Gamepad &pad) |
| adat80 | 1:3916f272663e | 79 | { |
| adat80 | 3:97cd7b3d89d0 | 80 | _frame = _frame + 1; |
| adat80 | 3:97cd7b3d89d0 | 81 | if (_frame%(_fps/2) == 0) { |
| adat80 | 3:97cd7b3d89d0 | 82 | |
| adat80 | 3:97cd7b3d89d0 | 83 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 3:97cd7b3d89d0 | 84 | if(_enemies[i].get_current_action() == attacking) { |
| adat80 | 3:97cd7b3d89d0 | 85 | _wall.take_damage(1); |
| adat80 | 5:8bd09c675f28 | 86 | if (_wall.get_wall_health() <= 0) { |
| adat80 | 5:8bd09c675f28 | 87 | game_over(); |
| adat80 | 3:97cd7b3d89d0 | 88 | } |
| adat80 | 3:97cd7b3d89d0 | 89 | } |
| adat80 | 3:97cd7b3d89d0 | 90 | } |
| adat80 | 3:97cd7b3d89d0 | 91 | } |
| adat80 | 3:97cd7b3d89d0 | 92 | |
| adat80 | 3:97cd7b3d89d0 | 93 | |
| adat80 | 1:3916f272663e | 94 | _level.update(_fps); |
| adat80 | 5:8bd09c675f28 | 95 | _crossHairs.update(_angle,_mag); |
| adat80 | 1:3916f272663e | 96 | /* |
| adat80 | 1:3916f272663e | 97 | _enemy.update(); |
| adat80 | 1:3916f272663e | 98 | _enemy2.update(); |
| adat80 | 1:3916f272663e | 99 | */ |
| adat80 | 1:3916f272663e | 100 | |
| adat80 | 1:3916f272663e | 101 | |
| adat80 | 3:97cd7b3d89d0 | 102 | for (int i=0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 2:88019d96e1da | 103 | _enemies[i].update(_fps); |
| adat80 | 1:3916f272663e | 104 | } |
| adat80 | 1:3916f272663e | 105 | |
| adat80 | 1:3916f272663e | 106 | if ( pad.check_event(Gamepad::B_PRESSED) == true) { |
| adat80 | 1:3916f272663e | 107 | fireShot(pad); |
| adat80 | 2:88019d96e1da | 108 | } else if ( pad.check_event(Gamepad::A_PRESSED) == true) { |
| adat80 | 2:88019d96e1da | 109 | fireShot(pad); |
| adat80 | 1:3916f272663e | 110 | } |
| adat80 | 1:3916f272663e | 111 | |
| adat80 | 1:3916f272663e | 112 | |
| adat80 | 1:3916f272663e | 113 | |
| adat80 | 1:3916f272663e | 114 | } |
| adat80 | 1:3916f272663e | 115 | |
| adat80 | 1:3916f272663e | 116 | void GameEngine::fireShot(Gamepad &gpad) |
| adat80 | 1:3916f272663e | 117 | { |
| adat80 | 2:88019d96e1da | 118 | |
| adat80 | 4:87f3f3e5cdd9 | 119 | gpad.tone(400, 0.1); |
| adat80 | 2:88019d96e1da | 120 | |
| adat80 | 2:88019d96e1da | 121 | |
| adat80 | 1:3916f272663e | 122 | Vector2D shotLoc = _crossHairs.get_pos(); |
| adat80 | 3:97cd7b3d89d0 | 123 | for(int i = 0; i < _level.get_number_of_enemies(); i++) { |
| adat80 | 2:88019d96e1da | 124 | if (_enemies[i].get_alive()) { |
| adat80 | 4:87f3f3e5cdd9 | 125 | was_enemy_shot(i, shotLoc); |
| adat80 | 1:3916f272663e | 126 | } |
| adat80 | 1:3916f272663e | 127 | } |
| adat80 | 1:3916f272663e | 128 | } |
| adat80 | 1:3916f272663e | 129 | |
| adat80 | 3:97cd7b3d89d0 | 130 | |
| adat80 | 4:87f3f3e5cdd9 | 131 | void GameEngine::was_enemy_shot(int i, Vector2D shotLoc) |
| adat80 | 4:87f3f3e5cdd9 | 132 | { |
| adat80 | 4:87f3f3e5cdd9 | 133 | |
| adat80 | 4:87f3f3e5cdd9 | 134 | Vector2D enemyLoc = _enemies[i].get_pos(); |
| adat80 | 4:87f3f3e5cdd9 | 135 | if (abs(enemyLoc.x - shotLoc.x) < 4 && abs(enemyLoc.y - shotLoc.y) < 4) { |
| adat80 | 4:87f3f3e5cdd9 | 136 | _killCount = _killCount + 1; |
| adat80 | 4:87f3f3e5cdd9 | 137 | _enemies[i].set_alive(false); |
| adat80 | 4:87f3f3e5cdd9 | 138 | Action myAc; |
| adat80 | 4:87f3f3e5cdd9 | 139 | myAc = dying; |
| adat80 | 4:87f3f3e5cdd9 | 140 | _enemies[i].set_current_action(myAc); |
| adat80 | 4:87f3f3e5cdd9 | 141 | |
| adat80 | 4:87f3f3e5cdd9 | 142 | |
| adat80 | 4:87f3f3e5cdd9 | 143 | if (_killCount == _level.get_number_of_enemies()) { |
| adat80 | 4:87f3f3e5cdd9 | 144 | _killCount = 0; |
| adat80 | 4:87f3f3e5cdd9 | 145 | _levelNumber = _levelNumber + 1; |
| adat80 | 4:87f3f3e5cdd9 | 146 | _enemies = _level.init(_levelNumber); |
| adat80 | 4:87f3f3e5cdd9 | 147 | } |
| adat80 | 4:87f3f3e5cdd9 | 148 | } |
| adat80 | 4:87f3f3e5cdd9 | 149 | |
| adat80 | 4:87f3f3e5cdd9 | 150 | |
| adat80 | 4:87f3f3e5cdd9 | 151 | } |
| adat80 | 4:87f3f3e5cdd9 | 152 | |
| adat80 | 4:87f3f3e5cdd9 | 153 | |
| adat80 | 4:87f3f3e5cdd9 | 154 | |
| adat80 | 4:87f3f3e5cdd9 | 155 | |
| adat80 | 3:97cd7b3d89d0 | 156 | |
| adat80 | 3:97cd7b3d89d0 | 157 | bool GameEngine::get_game_over() { |
| adat80 | 3:97cd7b3d89d0 | 158 | return _game_over; |
| adat80 | 3:97cd7b3d89d0 | 159 | |
| adat80 | 3:97cd7b3d89d0 | 160 | } |
| adat80 | 3:97cd7b3d89d0 | 161 | void GameEngine::set_game_over(bool is_game_over) { |
| adat80 | 3:97cd7b3d89d0 | 162 | _game_over = is_game_over; |
| adat80 | 3:97cd7b3d89d0 | 163 | } |
| adat80 | 5:8bd09c675f28 | 164 | |
| adat80 | 5:8bd09c675f28 | 165 | void GameEngine::game_over() { |
| adat80 | 5:8bd09c675f28 | 166 | _game_over = true; |
| adat80 | 5:8bd09c675f28 | 167 | |
| adat80 | 5:8bd09c675f28 | 168 | } |