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:
Tue Apr 23 03:10:09 2019 +0000
Revision:
11:63e54f6e7939
Parent:
10:1a3499f6b583
Child:
12:a1c1991835ca
Added move checks (for entity to group of entity collision checks);; Added a plausible way to update each entity;

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 10:1a3499f6b583 25 bool entity_collision(Entity &a, Entity &b){
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 11:63e54f6e7939 38 bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
el17sm 11:63e54f6e7939 39 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 40 if(i != current_entity){
el17sm 11:63e54f6e7939 41 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 11:63e54f6e7939 42 ((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 43 {
el17sm 11:63e54f6e7939 44 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 11:63e54f6e7939 45 ((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 11:63e54f6e7939 46 {
el17sm 11:63e54f6e7939 47 return true;
el17sm 11:63e54f6e7939 48 }
el17sm 11:63e54f6e7939 49 }
el17sm 11:63e54f6e7939 50 }
el17sm 11:63e54f6e7939 51 }
el17sm 11:63e54f6e7939 52 return false;
el17sm 11:63e54f6e7939 53 }
el17sm 11:63e54f6e7939 54
el17sm 11:63e54f6e7939 55 bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
el17sm 11:63e54f6e7939 56 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 57 if(i != current_entity){
el17sm 11:63e54f6e7939 58 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 11:63e54f6e7939 59 ((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 60 {
el17sm 11:63e54f6e7939 61 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 11:63e54f6e7939 62 ((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 11:63e54f6e7939 63 {
el17sm 11:63e54f6e7939 64 return true;
el17sm 11:63e54f6e7939 65 }
el17sm 11:63e54f6e7939 66 }
el17sm 11:63e54f6e7939 67 }
el17sm 11:63e54f6e7939 68 }
el17sm 11:63e54f6e7939 69 return false;
el17sm 11:63e54f6e7939 70 }
el17sm 11:63e54f6e7939 71
el17sm 1:1fa7ecca8dfb 72 int main()
el17sm 1:1fa7ecca8dfb 73 {
el17sm 1:1fa7ecca8dfb 74 lcd.init();
el17sm 2:dbfff27a8a94 75 lcd.setContrast(0.45);
el17sm 3:359a49bace1b 76 gamepad.init();
el17sm 2:dbfff27a8a94 77 while(1){
el17sm 10:1a3499f6b583 78 Player player(39, 27);
el17sm 11:63e54f6e7939 79 const int no_of_enemies = 3;
el17sm 11:63e54f6e7939 80 Entity *enemies[no_of_enemies];
el17sm 11:63e54f6e7939 81 Headless enemy1(20, 20);
el17sm 11:63e54f6e7939 82 Headless enemy2(20, 30);
el17sm 11:63e54f6e7939 83 Headless enemy3(60, 30);
el17sm 11:63e54f6e7939 84 enemies[0] = &enemy1;
el17sm 11:63e54f6e7939 85 enemies[1] = &enemy2;
el17sm 11:63e54f6e7939 86 enemies[2] = &enemy3;
el17sm 11:63e54f6e7939 87
el17sm 10:1a3499f6b583 88 while(1){
el17sm 11:63e54f6e7939 89 int pos_x = player.get_pos_x();
el17sm 11:63e54f6e7939 90 int pos_y = player.get_pos_y();
el17sm 11:63e54f6e7939 91 // Damage action
el17sm 11:63e54f6e7939 92 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 93 if(entity_collision(player, *enemies[i])){
el17sm 11:63e54f6e7939 94 goto gameover;
el17sm 11:63e54f6e7939 95 }
el17sm 11:63e54f6e7939 96 };
el17sm 11:63e54f6e7939 97
el17sm 10:1a3499f6b583 98 // Player Movement
el17sm 10:1a3499f6b583 99 Vector2D mapped_coord = gamepad.get_mapped_coord();
el17sm 10:1a3499f6b583 100 player.move(mapped_coord.x, mapped_coord.y);
el17sm 10:1a3499f6b583 101 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 102
el17sm 10:1a3499f6b583 103 // Enemy Movement
el17sm 11:63e54f6e7939 104 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 105 enemies[i]->update_prev_pos();
el17sm 11:63e54f6e7939 106 enemies[i]->move(pos_x, pos_y);
el17sm 11:63e54f6e7939 107 };
el17sm 11:63e54f6e7939 108 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 109 enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i));
el17sm 11:63e54f6e7939 110 enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i));
el17sm 11:63e54f6e7939 111 };
el17sm 10:1a3499f6b583 112
el17sm 10:1a3499f6b583 113 // Entity Collision Detection
el17sm 10:1a3499f6b583 114
el17sm 10:1a3499f6b583 115 // MiniMap Generation
el17sm 10:1a3499f6b583 116 // MiniMap Screen Detection
el17sm 10:1a3499f6b583 117
el17sm 10:1a3499f6b583 118 // Pause Detection
el17sm 10:1a3499f6b583 119 if(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 120 lcd.clear();
el17sm 10:1a3499f6b583 121 lcd.printString("Paused", 0, 0);
el17sm 10:1a3499f6b583 122 lcd.refresh();
el17sm 10:1a3499f6b583 123 wait(0.05);
el17sm 10:1a3499f6b583 124 while(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 125 };
el17sm 10:1a3499f6b583 126 wait(0.05);
el17sm 10:1a3499f6b583 127 while(!gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 128 };
el17sm 10:1a3499f6b583 129 wait(0.05);
el17sm 10:1a3499f6b583 130 while(gamepad.check_event(Gamepad::START_PRESSED)){
el17sm 10:1a3499f6b583 131 };
el17sm 10:1a3499f6b583 132 }
el17sm 10:1a3499f6b583 133
el17sm 10:1a3499f6b583 134
el17sm 10:1a3499f6b583 135 // screen update
el17sm 10:1a3499f6b583 136 lcd.clear();
el17sm 10:1a3499f6b583 137 lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
el17sm 11:63e54f6e7939 138 lcd.drawSpriteTransparent(pos_x-player.get_offset_x(),
el17sm 11:63e54f6e7939 139 pos_y-player.get_offset_y(),
el17sm 10:1a3499f6b583 140 player.get_sprite_height(),
el17sm 10:1a3499f6b583 141 player.get_sprite_width(),
el17sm 10:1a3499f6b583 142 (int *)sprite_player[player.get_face()][(int)(player.get_moving()*(counter/5)%4)]);
el17sm 11:63e54f6e7939 143 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 144 lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
el17sm 11:63e54f6e7939 145 enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
el17sm 11:63e54f6e7939 146 enemies[i]->get_sprite_height(),
el17sm 11:63e54f6e7939 147 enemies[i]->get_sprite_width(),
el17sm 11:63e54f6e7939 148 (int *)sprite_headless[enemies[i]->get_face()][(int)(enemies[i]->get_moving()*(counter/5)%4)]);
el17sm 11:63e54f6e7939 149 };
el17sm 10:1a3499f6b583 150 lcd.refresh();
el17sm 10:1a3499f6b583 151 wait_ms(1000/20);
el17sm 10:1a3499f6b583 152 counter++;
el17sm 10:1a3499f6b583 153
el17sm 10:1a3499f6b583 154 }
el17sm 11:63e54f6e7939 155 gameover:
el17sm 11:63e54f6e7939 156 lcd.clear();
el17sm 11:63e54f6e7939 157 lcd.printString("Game Over", 0, 0);
el17sm 11:63e54f6e7939 158 lcd.printString("Retry?", 0, 1);
el17sm 11:63e54f6e7939 159 lcd.refresh();
el17sm 11:63e54f6e7939 160 player.~Player();
el17sm 11:63e54f6e7939 161 for (int i = 0; i < no_of_enemies; i++){
el17sm 11:63e54f6e7939 162 enemies[i]->~Entity();
el17sm 11:63e54f6e7939 163 };
el17sm 11:63e54f6e7939 164 wait(0.05);
el17sm 11:63e54f6e7939 165 while(!gamepad.check_event(Gamepad::A_PRESSED)){
el17sm 11:63e54f6e7939 166 };
el17sm 11:63e54f6e7939 167 wait(0.05);
el17sm 11:63e54f6e7939 168 while(gamepad.check_event(Gamepad::A_PRESSED)){
el17sm 10:1a3499f6b583 169 };
el17sm 10:1a3499f6b583 170 };
el17sm 10:1a3499f6b583 171 }