A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
main.cpp@22:7abf4581bc9b, 2019-04-25 (annotated)
- Committer:
- el17sm
- Date:
- Thu Apr 25 05:53:30 2019 +0000
- Revision:
- 22:7abf4581bc9b
- Parent:
- 17:99e533f7f2fb
- Child:
- 23:5a8f75e93508
Snakes 90% done, need to change entity to group of entity collision into repulsion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17sm | 0:8e92b66a0755 | 1 | /* |
el17sm | 0:8e92b66a0755 | 2 | ELEC2645 Embedded Systems Project |
el17sm | 0:8e92b66a0755 | 3 | School of Electronic & Electrical Engineering |
el17sm | 0:8e92b66a0755 | 4 | University of Leeds |
el17sm | 0:8e92b66a0755 | 5 | Name: Steven Mahasin |
el17sm | 0:8e92b66a0755 | 6 | Username: el17sm |
el17sm | 0:8e92b66a0755 | 7 | Student ID Number: 201192939 |
el17sm | 0:8e92b66a0755 | 8 | Date: 11/04/2019 |
el17sm | 1:1fa7ecca8dfb | 9 | */ |
el17sm | 1:1fa7ecca8dfb | 10 | |
el17sm | 7:4aaa37a711a1 | 11 | #include "mbed.h" |
el17sm | 7:4aaa37a711a1 | 12 | #include "Gamepad.h" |
el17sm | 7:4aaa37a711a1 | 13 | #include "N5110.h" |
el17sm | 7:4aaa37a711a1 | 14 | #include "math.h" |
el17sm | 7:4aaa37a711a1 | 15 | #include "sprites.h" |
el17sm | 7:4aaa37a711a1 | 16 | #include "Entity.h" |
el17sm | 7:4aaa37a711a1 | 17 | #include "Player.h" |
el17sm | 10:1a3499f6b583 | 18 | #include "Headless.h" |
el17sm | 17:99e533f7f2fb | 19 | #include "Snake.h" |
el17sm | 7:4aaa37a711a1 | 20 | |
el17sm | 7:4aaa37a711a1 | 21 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17sm | 7:4aaa37a711a1 | 22 | Gamepad gamepad; |
el17sm | 7:4aaa37a711a1 | 23 | |
el17sm | 7:4aaa37a711a1 | 24 | int counter = 0; |
el17sm | 1:1fa7ecca8dfb | 25 | |
el17sm | 22:7abf4581bc9b | 26 | bool entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide |
el17sm | 22:7abf4581bc9b | 27 | { |
el17sm | 11:63e54f6e7939 | 28 | if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) || |
el17sm | 22:7abf4581bc9b | 29 | ((b.get_pos_x() <= a.get_pos_x() + a.get_hitbox_width() - 1) && (a.get_pos_x() + a.get_hitbox_width() - 1 <= b.get_pos_x() + b.get_hitbox_width() - 1))) { |
el17sm | 11:63e54f6e7939 | 30 | if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) || |
el17sm | 22:7abf4581bc9b | 31 | ((b.get_pos_y() <= a.get_pos_y() + a.get_hitbox_height() - 1) && (a.get_pos_y() + a.get_hitbox_height() - 1 <= b.get_pos_y() + b.get_hitbox_height() - 1))) { |
el17sm | 10:1a3499f6b583 | 32 | return true; |
el17sm | 22:7abf4581bc9b | 33 | } |
el17sm | 10:1a3499f6b583 | 34 | } |
el17sm | 10:1a3499f6b583 | 35 | return false; |
el17sm | 10:1a3499f6b583 | 36 | } |
el17sm | 10:1a3499f6b583 | 37 | |
el17sm | 12:a1c1991835ca | 38 | // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction |
el17sm | 22:7abf4581bc9b | 39 | bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 22:7abf4581bc9b | 40 | { |
el17sm | 22:7abf4581bc9b | 41 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 42 | if (valid_enemies[i]) { |
el17sm | 22:7abf4581bc9b | 43 | if(i != current_entity) { |
el17sm | 13:d04a6caba40d | 44 | if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) || |
el17sm | 22:7abf4581bc9b | 45 | ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1))) { |
el17sm | 13:d04a6caba40d | 46 | if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) || |
el17sm | 22:7abf4581bc9b | 47 | ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1))) { |
el17sm | 13:d04a6caba40d | 48 | return true; |
el17sm | 13:d04a6caba40d | 49 | } |
el17sm | 12:a1c1991835ca | 50 | } |
el17sm | 11:63e54f6e7939 | 51 | } |
el17sm | 11:63e54f6e7939 | 52 | } |
el17sm | 11:63e54f6e7939 | 53 | } |
el17sm | 11:63e54f6e7939 | 54 | return false; |
el17sm | 11:63e54f6e7939 | 55 | } |
el17sm | 11:63e54f6e7939 | 56 | |
el17sm | 12:a1c1991835ca | 57 | // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction |
el17sm | 22:7abf4581bc9b | 58 | bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 22:7abf4581bc9b | 59 | { |
el17sm | 22:7abf4581bc9b | 60 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 61 | if (valid_enemies[i]) { |
el17sm | 22:7abf4581bc9b | 62 | if(i != current_entity) { |
el17sm | 13:d04a6caba40d | 63 | if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) || |
el17sm | 22:7abf4581bc9b | 64 | ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1))) { |
el17sm | 13:d04a6caba40d | 65 | if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) || |
el17sm | 22:7abf4581bc9b | 66 | ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1))) { |
el17sm | 13:d04a6caba40d | 67 | return true; |
el17sm | 22:7abf4581bc9b | 68 | } |
el17sm | 13:d04a6caba40d | 69 | } |
el17sm | 11:63e54f6e7939 | 70 | } |
el17sm | 11:63e54f6e7939 | 71 | } |
el17sm | 11:63e54f6e7939 | 72 | } |
el17sm | 11:63e54f6e7939 | 73 | return false; |
el17sm | 11:63e54f6e7939 | 74 | } |
el17sm | 11:63e54f6e7939 | 75 | |
el17sm | 1:1fa7ecca8dfb | 76 | int main() |
el17sm | 1:1fa7ecca8dfb | 77 | { |
el17sm | 1:1fa7ecca8dfb | 78 | lcd.init(); |
el17sm | 2:dbfff27a8a94 | 79 | lcd.setContrast(0.45); |
el17sm | 3:359a49bace1b | 80 | gamepad.init(); |
el17sm | 22:7abf4581bc9b | 81 | while(1) { |
el17sm | 10:1a3499f6b583 | 82 | Player player(39, 27); |
el17sm | 12:a1c1991835ca | 83 | int no_of_enemies = 3; |
el17sm | 13:d04a6caba40d | 84 | bool valid_enemies[no_of_enemies]; |
el17sm | 22:7abf4581bc9b | 85 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 86 | valid_enemies[i] = false; |
el17sm | 22:7abf4581bc9b | 87 | } |
el17sm | 11:63e54f6e7939 | 88 | Entity *enemies[no_of_enemies]; |
el17sm | 17:99e533f7f2fb | 89 | enemies[0] = new Headless(20, 20); |
el17sm | 17:99e533f7f2fb | 90 | valid_enemies[0] = true; |
el17sm | 17:99e533f7f2fb | 91 | enemies[1] = new Snake(20, 30); |
el17sm | 17:99e533f7f2fb | 92 | valid_enemies[1] = true; |
el17sm | 16:ddb203a74dfc | 93 | // enemies[2] = new Snake(60, 30); |
el17sm | 16:ddb203a74dfc | 94 | // valid_enemies[2] = true; |
el17sm | 22:7abf4581bc9b | 95 | |
el17sm | 22:7abf4581bc9b | 96 | while(1) { |
el17sm | 11:63e54f6e7939 | 97 | int pos_x = player.get_pos_x(); |
el17sm | 11:63e54f6e7939 | 98 | int pos_y = player.get_pos_y(); |
el17sm | 11:63e54f6e7939 | 99 | // Damage action |
el17sm | 22:7abf4581bc9b | 100 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 101 | if (valid_enemies[i]) { |
el17sm | 22:7abf4581bc9b | 102 | if(entity_collision(player, *enemies[i])) { |
el17sm | 13:d04a6caba40d | 103 | goto gameover; |
el17sm | 13:d04a6caba40d | 104 | } |
el17sm | 13:d04a6caba40d | 105 | } |
el17sm | 13:d04a6caba40d | 106 | }; |
el17sm | 22:7abf4581bc9b | 107 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 22:7abf4581bc9b | 108 | if (player.valid_bullets[i]) { |
el17sm | 22:7abf4581bc9b | 109 | if (player.bullets_array[i]->out_of_bounds_check()) { |
el17sm | 22:7abf4581bc9b | 110 | player.valid_bullets[i] = false; |
el17sm | 22:7abf4581bc9b | 111 | delete player.bullets_array[i]; |
el17sm | 22:7abf4581bc9b | 112 | } else { |
el17sm | 22:7abf4581bc9b | 113 | for (int j = 0; j < no_of_enemies; j++) { |
el17sm | 22:7abf4581bc9b | 114 | if (valid_enemies[j]) { |
el17sm | 22:7abf4581bc9b | 115 | if(entity_collision(*player.bullets_array[i], *enemies[j])) { |
el17sm | 22:7abf4581bc9b | 116 | enemies[j]->take_damage(player.get_attack()); |
el17sm | 22:7abf4581bc9b | 117 | player.valid_bullets[i] = false; |
el17sm | 22:7abf4581bc9b | 118 | delete player.bullets_array[i]; |
el17sm | 22:7abf4581bc9b | 119 | break; |
el17sm | 22:7abf4581bc9b | 120 | } |
el17sm | 13:d04a6caba40d | 121 | } |
el17sm | 13:d04a6caba40d | 122 | } |
el17sm | 22:7abf4581bc9b | 123 | } |
el17sm | 11:63e54f6e7939 | 124 | } |
el17sm | 22:7abf4581bc9b | 125 | } |
el17sm | 22:7abf4581bc9b | 126 | |
el17sm | 10:1a3499f6b583 | 127 | // Player Movement |
el17sm | 10:1a3499f6b583 | 128 | Vector2D mapped_coord = gamepad.get_mapped_coord(); |
el17sm | 10:1a3499f6b583 | 129 | player.move(mapped_coord.x, mapped_coord.y); |
el17sm | 10:1a3499f6b583 | 130 | player.buttons(gamepad.check_event(Gamepad::A_PRESSED), gamepad.check_event(Gamepad::B_PRESSED), gamepad.check_event(Gamepad::Y_PRESSED), gamepad.check_event(Gamepad::X_PRESSED)); |
el17sm | 22:7abf4581bc9b | 131 | |
el17sm | 14:3361879490b2 | 132 | // Enemy Death |
el17sm | 22:7abf4581bc9b | 133 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 134 | if (valid_enemies[i]) { |
el17sm | 22:7abf4581bc9b | 135 | if(enemies[i]->death_check()) { |
el17sm | 22:7abf4581bc9b | 136 | valid_enemies[i] = false; |
el17sm | 22:7abf4581bc9b | 137 | delete enemies[i]; |
el17sm | 22:7abf4581bc9b | 138 | } |
el17sm | 14:3361879490b2 | 139 | } |
el17sm | 14:3361879490b2 | 140 | } |
el17sm | 22:7abf4581bc9b | 141 | |
el17sm | 10:1a3499f6b583 | 142 | // Enemy Movement |
el17sm | 22:7abf4581bc9b | 143 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 144 | if (valid_enemies[i]) { |
el17sm | 13:d04a6caba40d | 145 | enemies[i]->update_prev_pos(); |
el17sm | 13:d04a6caba40d | 146 | enemies[i]->move(pos_x, pos_y); |
el17sm | 13:d04a6caba40d | 147 | } |
el17sm | 11:63e54f6e7939 | 148 | }; |
el17sm | 22:7abf4581bc9b | 149 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 22:7abf4581bc9b | 150 | if (player.valid_bullets[i]) { |
el17sm | 22:7abf4581bc9b | 151 | player.bullets_array[i]->move(player.get_bullet_speed(), 0); |
el17sm | 14:3361879490b2 | 152 | } |
el17sm | 14:3361879490b2 | 153 | }; |
el17sm | 22:7abf4581bc9b | 154 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 155 | if (valid_enemies[i]) { |
el17sm | 13:d04a6caba40d | 156 | enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i, valid_enemies)); |
el17sm | 13:d04a6caba40d | 157 | enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i, valid_enemies)); |
el17sm | 13:d04a6caba40d | 158 | } |
el17sm | 11:63e54f6e7939 | 159 | }; |
el17sm | 22:7abf4581bc9b | 160 | |
el17sm | 10:1a3499f6b583 | 161 | // Entity Collision Detection |
el17sm | 22:7abf4581bc9b | 162 | |
el17sm | 10:1a3499f6b583 | 163 | // MiniMap Screen Detection |
el17sm | 22:7abf4581bc9b | 164 | |
el17sm | 10:1a3499f6b583 | 165 | // Pause Detection |
el17sm | 22:7abf4581bc9b | 166 | if(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 10:1a3499f6b583 | 167 | lcd.clear(); |
el17sm | 10:1a3499f6b583 | 168 | lcd.printString("Paused", 0, 0); |
el17sm | 10:1a3499f6b583 | 169 | lcd.refresh(); |
el17sm | 10:1a3499f6b583 | 170 | wait(0.05); |
el17sm | 22:7abf4581bc9b | 171 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 10:1a3499f6b583 | 172 | }; |
el17sm | 10:1a3499f6b583 | 173 | wait(0.05); |
el17sm | 22:7abf4581bc9b | 174 | while(!gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 10:1a3499f6b583 | 175 | }; |
el17sm | 10:1a3499f6b583 | 176 | wait(0.05); |
el17sm | 22:7abf4581bc9b | 177 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 10:1a3499f6b583 | 178 | }; |
el17sm | 10:1a3499f6b583 | 179 | } |
el17sm | 22:7abf4581bc9b | 180 | |
el17sm | 22:7abf4581bc9b | 181 | |
el17sm | 10:1a3499f6b583 | 182 | // screen update |
el17sm | 10:1a3499f6b583 | 183 | lcd.clear(); |
el17sm | 10:1a3499f6b583 | 184 | lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]); |
el17sm | 11:63e54f6e7939 | 185 | lcd.drawSpriteTransparent(pos_x-player.get_offset_x(), |
el17sm | 11:63e54f6e7939 | 186 | pos_y-player.get_offset_y(), |
el17sm | 10:1a3499f6b583 | 187 | player.get_sprite_height(), |
el17sm | 10:1a3499f6b583 | 188 | player.get_sprite_width(), |
el17sm | 12:a1c1991835ca | 189 | player.get_frame()); |
el17sm | 22:7abf4581bc9b | 190 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 191 | if (valid_enemies[i]) { |
el17sm | 13:d04a6caba40d | 192 | lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(), |
el17sm | 13:d04a6caba40d | 193 | enemies[i]->get_pos_y()-enemies[i]->get_offset_y(), |
el17sm | 13:d04a6caba40d | 194 | enemies[i]->get_sprite_height(), |
el17sm | 13:d04a6caba40d | 195 | enemies[i]->get_sprite_width(), |
el17sm | 13:d04a6caba40d | 196 | enemies[i]->get_frame()); |
el17sm | 13:d04a6caba40d | 197 | } |
el17sm | 11:63e54f6e7939 | 198 | }; |
el17sm | 22:7abf4581bc9b | 199 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 22:7abf4581bc9b | 200 | if (player.valid_bullets[i]) { |
el17sm | 13:d04a6caba40d | 201 | lcd.drawSpriteTransparent(player.bullets_array[i]->get_pos_x()-player.bullets_array[i]->get_offset_x(), |
el17sm | 13:d04a6caba40d | 202 | player.bullets_array[i]->get_pos_y()-player.bullets_array[i]->get_offset_y(), |
el17sm | 13:d04a6caba40d | 203 | player.bullets_array[i]->get_sprite_height(), |
el17sm | 13:d04a6caba40d | 204 | player.bullets_array[i]->get_sprite_width(), |
el17sm | 13:d04a6caba40d | 205 | player.bullets_array[i]->get_frame()); |
el17sm | 13:d04a6caba40d | 206 | } |
el17sm | 13:d04a6caba40d | 207 | }; |
el17sm | 10:1a3499f6b583 | 208 | lcd.refresh(); |
el17sm | 12:a1c1991835ca | 209 | wait_ms(1000/20); // setting FPS |
el17sm | 10:1a3499f6b583 | 210 | counter++; |
el17sm | 22:7abf4581bc9b | 211 | |
el17sm | 10:1a3499f6b583 | 212 | } |
el17sm | 22:7abf4581bc9b | 213 | gameover: { |
el17sm | 11:63e54f6e7939 | 214 | lcd.clear(); |
el17sm | 11:63e54f6e7939 | 215 | lcd.printString("Game Over", 0, 0); |
el17sm | 11:63e54f6e7939 | 216 | lcd.printString("Retry?", 0, 1); |
el17sm | 11:63e54f6e7939 | 217 | lcd.refresh(); |
el17sm | 11:63e54f6e7939 | 218 | player.~Player(); |
el17sm | 22:7abf4581bc9b | 219 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 22:7abf4581bc9b | 220 | if (valid_enemies[i]) { |
el17sm | 22:7abf4581bc9b | 221 | delete enemies[i]; |
el17sm | 22:7abf4581bc9b | 222 | } |
el17sm | 11:63e54f6e7939 | 223 | }; |
el17sm | 11:63e54f6e7939 | 224 | wait(0.05); |
el17sm | 22:7abf4581bc9b | 225 | while(!gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 11:63e54f6e7939 | 226 | }; |
el17sm | 11:63e54f6e7939 | 227 | wait(0.05); |
el17sm | 22:7abf4581bc9b | 228 | while(gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 22:7abf4581bc9b | 229 | } |
el17sm | 16:ddb203a74dfc | 230 | } |
el17sm | 22:7abf4581bc9b | 231 | } |
el17sm | 10:1a3499f6b583 | 232 | } |