A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Thu Apr 25 04:40:30 2019 +0000
Revision:
20:6405835af6e2
Parent:
14:3361879490b2
the true updated headless only functional game

Who changed what in which revision?

UserRevisionLine numberNew 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 7:4aaa37a711a1 19
el17sm 7:4aaa37a711a1 20 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el17sm 7:4aaa37a711a1 21 Gamepad gamepad;
el17sm 7:4aaa37a711a1 22
el17sm 7:4aaa37a711a1 23 int counter = 0;
el17sm 1:1fa7ecca8dfb 24
el17sm 12:a1c1991835ca 25 bool entity_collision(Entity &a, Entity &b){ // returns true if the two entity hitboxes collide
el17sm 11:63e54f6e7939 26 if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) ||
el17sm 11:63e54f6e7939 27 ((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 10:1a3499f6b583 28 {
el17sm 11:63e54f6e7939 29 if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) ||
el17sm 11:63e54f6e7939 30 ((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 31 {
el17sm 10:1a3499f6b583 32 return true;
el17sm 10:1a3499f6b583 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 13:d04a6caba40d 39 bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
el17sm 11:63e54f6e7939 40 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 41 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 42 if(i != current_entity){
el17sm 13:d04a6caba40d 43 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 13:d04a6caba40d 44 ((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 11:63e54f6e7939 45 {
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 13:d04a6caba40d 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 {
el17sm 13:d04a6caba40d 49 return true;
el17sm 13:d04a6caba40d 50 }
el17sm 12:a1c1991835ca 51 }
el17sm 11:63e54f6e7939 52 }
el17sm 11:63e54f6e7939 53 }
el17sm 11:63e54f6e7939 54 }
el17sm 11:63e54f6e7939 55 return false;
el17sm 11:63e54f6e7939 56 }
el17sm 11:63e54f6e7939 57
el17sm 12:a1c1991835ca 58 // 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 13:d04a6caba40d 59 bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
el17sm 11:63e54f6e7939 60 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 61 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 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 13:d04a6caba40d 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 11:63e54f6e7939 65 {
el17sm 13:d04a6caba40d 66 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 13:d04a6caba40d 67 ((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 68 {
el17sm 13:d04a6caba40d 69 return true;
el17sm 13:d04a6caba40d 70 }
el17sm 13:d04a6caba40d 71 }
el17sm 11:63e54f6e7939 72 }
el17sm 11:63e54f6e7939 73 }
el17sm 11:63e54f6e7939 74 }
el17sm 11:63e54f6e7939 75 return false;
el17sm 11:63e54f6e7939 76 }
el17sm 11:63e54f6e7939 77
el17sm 1:1fa7ecca8dfb 78 int main()
el17sm 1:1fa7ecca8dfb 79 {
el17sm 1:1fa7ecca8dfb 80 lcd.init();
el17sm 2:dbfff27a8a94 81 lcd.setContrast(0.45);
el17sm 3:359a49bace1b 82 gamepad.init();
el17sm 2:dbfff27a8a94 83 while(1){
el17sm 10:1a3499f6b583 84 Player player(39, 27);
el17sm 12:a1c1991835ca 85 int no_of_enemies = 3;
el17sm 13:d04a6caba40d 86 bool valid_enemies[no_of_enemies];
el17sm 13:d04a6caba40d 87 for (int i = 0; i < no_of_enemies; i++){valid_enemies[i] = false;}
el17sm 11:63e54f6e7939 88 Entity *enemies[no_of_enemies];
el17sm 13:d04a6caba40d 89 enemies[0] = new Headless(20, 20);
el17sm 13:d04a6caba40d 90 valid_enemies[0] = true;
el17sm 13:d04a6caba40d 91 enemies[1] = new Headless(20, 30);
el17sm 13:d04a6caba40d 92 valid_enemies[1] = true;
el17sm 13:d04a6caba40d 93 enemies[2] = new Headless(60, 30);
el17sm 13:d04a6caba40d 94 valid_enemies[2] = true;
el17sm 11:63e54f6e7939 95
el17sm 10:1a3499f6b583 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 11:63e54f6e7939 100 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 101 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 102 if(entity_collision(player, *enemies[i])){
el17sm 13:d04a6caba40d 103 goto gameover;
el17sm 13:d04a6caba40d 104 }
el17sm 13:d04a6caba40d 105 }
el17sm 20:6405835af6e2 106 }
el17sm 13:d04a6caba40d 107 for (int i = 0; i < bullets_max; i++){
el17sm 13:d04a6caba40d 108 if (player.valid_bullets[i]){
el17sm 20:6405835af6e2 109 if (player.bullets_array[i]->out_of_bounds_check()){
el17sm 20:6405835af6e2 110 player.valid_bullets[i] = false;
el17sm 20:6405835af6e2 111 player.bullets_array[i]->~Bullets;
el17sm 20:6405835af6e2 112 }
el17sm 20:6405835af6e2 113 else {
el17sm 20:6405835af6e2 114 for (int j = 0; j < no_of_enemies; j++){
el17sm 20:6405835af6e2 115 if (valid_enemies[j]){
el17sm 20:6405835af6e2 116 if(entity_collision(*player.bullets_array[i], *enemies[j])){
el17sm 20:6405835af6e2 117 enemies[j]->take_damage(player.get_attack());
el17sm 20:6405835af6e2 118 player.valid_bullets[i] = false;
el17sm 20:6405835af6e2 119 player.bullets_array[i]->~Bullets;
el17sm 20:6405835af6e2 120 break;
el17sm 20:6405835af6e2 121 }
el17sm 13:d04a6caba40d 122 }
el17sm 13:d04a6caba40d 123 }
el17sm 20:6405835af6e2 124 }
el17sm 11:63e54f6e7939 125 }
el17sm 20:6405835af6e2 126 }
el17sm 11:63e54f6e7939 127
el17sm 10:1a3499f6b583 128 // Player Movement
el17sm 10:1a3499f6b583 129 Vector2D mapped_coord = gamepad.get_mapped_coord();
el17sm 10:1a3499f6b583 130 player.move(mapped_coord.x, mapped_coord.y);
el17sm 10:1a3499f6b583 131 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 10:1a3499f6b583 132
el17sm 14:3361879490b2 133 // Enemy Death
el17sm 14:3361879490b2 134 for (int i = 0; i < no_of_enemies; i++){
el17sm 14:3361879490b2 135 if(enemies[i]->death_check()){
el17sm 14:3361879490b2 136 valid_enemies[i] = false;
el17sm 14:3361879490b2 137 enemies[i]->~Entity;
el17sm 14:3361879490b2 138 }
el17sm 14:3361879490b2 139 }
el17sm 14:3361879490b2 140
el17sm 10:1a3499f6b583 141 // Enemy Movement
el17sm 11:63e54f6e7939 142 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 143 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 144 enemies[i]->update_prev_pos();
el17sm 13:d04a6caba40d 145 enemies[i]->move(pos_x, pos_y);
el17sm 13:d04a6caba40d 146 }
el17sm 11:63e54f6e7939 147 };
el17sm 14:3361879490b2 148 for (int i = 0; i < bullets_max; i++){
el17sm 14:3361879490b2 149 if (player.valid_bullets[i]){
el17sm 14:3361879490b2 150 player.bullets_array[i]->move(1, 0);
el17sm 14:3361879490b2 151 }
el17sm 14:3361879490b2 152 };
el17sm 11:63e54f6e7939 153 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 154 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 155 enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i, valid_enemies));
el17sm 13:d04a6caba40d 156 enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i, valid_enemies));
el17sm 13:d04a6caba40d 157 }
el17sm 11:63e54f6e7939 158 };
el17sm 10:1a3499f6b583 159
el17sm 10:1a3499f6b583 160 // Entity Collision Detection
el17sm 10:1a3499f6b583 161
el17sm 10:1a3499f6b583 162 // MiniMap Screen Detection
el17sm 10:1a3499f6b583 163
el17sm 10:1a3499f6b583 164 // Pause Detection
el17sm 10:1a3499f6b583 165 if(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 166 lcd.clear();
el17sm 10:1a3499f6b583 167 lcd.printString("Paused", 0, 0);
el17sm 10:1a3499f6b583 168 lcd.refresh();
el17sm 10:1a3499f6b583 169 wait(0.05);
el17sm 10:1a3499f6b583 170 while(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 171 };
el17sm 10:1a3499f6b583 172 wait(0.05);
el17sm 10:1a3499f6b583 173 while(!gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 174 };
el17sm 10:1a3499f6b583 175 wait(0.05);
el17sm 10:1a3499f6b583 176 while(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 177 };
el17sm 10:1a3499f6b583 178 }
el17sm 10:1a3499f6b583 179
el17sm 10:1a3499f6b583 180
el17sm 10:1a3499f6b583 181 // screen update
el17sm 10:1a3499f6b583 182 lcd.clear();
el17sm 10:1a3499f6b583 183 lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
el17sm 11:63e54f6e7939 184 lcd.drawSpriteTransparent(pos_x-player.get_offset_x(),
el17sm 11:63e54f6e7939 185 pos_y-player.get_offset_y(),
el17sm 10:1a3499f6b583 186 player.get_sprite_height(),
el17sm 10:1a3499f6b583 187 player.get_sprite_width(),
el17sm 12:a1c1991835ca 188 player.get_frame());
el17sm 11:63e54f6e7939 189 for (int i = 0; i < no_of_enemies; i++){
el17sm 13:d04a6caba40d 190 if (valid_enemies[i]){
el17sm 13:d04a6caba40d 191 lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
el17sm 13:d04a6caba40d 192 enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
el17sm 13:d04a6caba40d 193 enemies[i]->get_sprite_height(),
el17sm 13:d04a6caba40d 194 enemies[i]->get_sprite_width(),
el17sm 13:d04a6caba40d 195 enemies[i]->get_frame());
el17sm 13:d04a6caba40d 196 }
el17sm 11:63e54f6e7939 197 };
el17sm 13:d04a6caba40d 198 for (int i = 0; i < bullets_max; i++){
el17sm 13:d04a6caba40d 199 if (player.valid_bullets[i]){
el17sm 13:d04a6caba40d 200 lcd.drawSpriteTransparent(player.bullets_array[i]->get_pos_x()-player.bullets_array[i]->get_offset_x(),
el17sm 13:d04a6caba40d 201 player.bullets_array[i]->get_pos_y()-player.bullets_array[i]->get_offset_y(),
el17sm 13:d04a6caba40d 202 player.bullets_array[i]->get_sprite_height(),
el17sm 13:d04a6caba40d 203 player.bullets_array[i]->get_sprite_width(),
el17sm 13:d04a6caba40d 204 player.bullets_array[i]->get_frame());
el17sm 13:d04a6caba40d 205 }
el17sm 13:d04a6caba40d 206 };
el17sm 10:1a3499f6b583 207 lcd.refresh();
el17sm 12:a1c1991835ca 208 wait_ms(1000/20); // setting FPS
el17sm 10:1a3499f6b583 209 counter++;
el17sm 10:1a3499f6b583 210
el17sm 10:1a3499f6b583 211 }
el17sm 11:63e54f6e7939 212 gameover:
el17sm 11:63e54f6e7939 213 lcd.clear();
el17sm 11:63e54f6e7939 214 lcd.printString("Game Over", 0, 0);
el17sm 11:63e54f6e7939 215 lcd.printString("Retry?", 0, 1);
el17sm 11:63e54f6e7939 216 lcd.refresh();
el17sm 11:63e54f6e7939 217 player.~Player();
el17sm 11:63e54f6e7939 218 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 219 enemies[i]->~Entity();
el17sm 11:63e54f6e7939 220 };
el17sm 11:63e54f6e7939 221 wait(0.05);
el17sm 11:63e54f6e7939 222 while(!gamepad.check_event(Gamepad::A_PRESSED)){
el17sm 11:63e54f6e7939 223 };
el17sm 11:63e54f6e7939 224 wait(0.05);
el17sm 11:63e54f6e7939 225 while(gamepad.check_event(Gamepad::A_PRESSED)){
el17sm 10:1a3499f6b583 226 };
el17sm 10:1a3499f6b583 227 };
el17sm 10:1a3499f6b583 228 }