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 22:59:12 2019 +0000
Revision:
12:a1c1991835ca
Parent:
11:63e54f6e7939
Child:
13:d04a6caba40d
Sprite is now controlled and stored inside each entity's files;

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