A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
Diff: main.cpp
- Revision:
- 21:be18f33da757
- Parent:
- 19:bfe410c82b45
diff -r bfe410c82b45 -r be18f33da757 main.cpp --- a/main.cpp Thu Apr 25 03:56:09 2019 +0000 +++ b/main.cpp Thu Apr 25 05:27:43 2019 +0000 @@ -23,30 +23,28 @@ int counter = 0; -bool entity_collision(Entity &a, Entity &b){ // returns true if the two entity hitboxes collide +bool entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide +{ if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) || - ((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))) - { + ((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))) { if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) || - ((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))) - { + ((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))) { return true; - } + } } return false; } // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction -bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){ - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ - if(i != current_entity){ +bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) +{ + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { + if(i != current_entity) { 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)) || - ((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))) - { + ((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))) { 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)) || - ((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))) - { + ((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))) { return true; } } @@ -57,18 +55,17 @@ } // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction -bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){ - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ - if(i != current_entity){ +bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) +{ + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { + if(i != current_entity) { 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)) || - ((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))) - { + ((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))) { 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)) || - ((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))) - { + ((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))) { return true; - } + } } } } @@ -81,103 +78,107 @@ lcd.init(); lcd.setContrast(0.45); gamepad.init(); - while(1){ + while(1) { Player player(39, 27); int no_of_enemies = 3; bool valid_enemies[no_of_enemies]; - for (int i = 0; i < no_of_enemies; i++){valid_enemies[i] = false;} + for (int i = 0; i < no_of_enemies; i++) { + valid_enemies[i] = false; + } Entity *enemies[no_of_enemies]; enemies[0] = new Snake(20, 20); valid_enemies[0] = true; // enemies[1] = new Snake(20, 30); // valid_enemies[1] = true; -// enemies[2] = new Snake(60, 30); -// valid_enemies[2] = true; - - while(1){ + enemies[2] = new Snake(60, 30); + valid_enemies[2] = true; + + while(1) { int pos_x = player.get_pos_x(); int pos_y = player.get_pos_y(); // Damage action - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ - if(entity_collision(player, *enemies[i])){ + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { + if(entity_collision(player, *enemies[i])) { goto gameover; } } - }; - for (int i = 0; i < bullets_max; i++){ - if (player.valid_bullets[i]){ - for (int j = 0; j < no_of_enemies; j++){ - if (valid_enemies[j]){ - if(entity_collision(*player.bullets_array[i], *enemies[j])){ - enemies[j]->take_damage(player.get_attack()); - player.valid_bullets[i] = false; - player.bullets_array[i]->~Bullets; - goto multiple_damage_prevention; + } + for (int i = 0; i < bullets_max; i++) { + if (player.valid_bullets[i]) { + if (player.bullets_array[i]->out_of_bounds_check()) { + player.valid_bullets[i] = false; + player.bullets_array[i]->~Bullets; + } else { + for (int j = 0; j < no_of_enemies; j++) { + if (valid_enemies[j]) { + if(entity_collision(*player.bullets_array[i], *enemies[j])) { + enemies[j]->take_damage(player.get_attack()); + player.valid_bullets[i] = false; + player.bullets_array[i]->~Bullets; + break; + } } } - if (player.bullets_array[i]->out_of_bounds_check()){ - player.valid_bullets[i] = false; - player.bullets_array[i]->~Bullets; - } - }; - multiple_damage_prevention:{} + } } - }; - + } + // Player Movement Vector2D mapped_coord = gamepad.get_mapped_coord(); player.move(mapped_coord.x, mapped_coord.y); 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)); - + // Enemy Death - for (int i = 0; i < no_of_enemies; i++){ - if(enemies[i]->death_check()){ - valid_enemies[i] = false; - enemies[i]->~Entity; + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { + if(enemies[i]->death_check()) { + valid_enemies[i] = false; + enemies[i]->~Entity; + } } } - + // Enemy Movement - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { enemies[i]->update_prev_pos(); enemies[i]->move(pos_x, pos_y); } - }; - for (int i = 0; i < bullets_max; i++){ - if (player.valid_bullets[i]){ + } + for (int i = 0; i < bullets_max; i++) { + if (player.valid_bullets[i]) { player.bullets_array[i]->move(1, 0); } - }; - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ + } + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i, valid_enemies)); enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i, valid_enemies)); } - }; - + } + // Entity Collision Detection - + // MiniMap Screen Detection - + // Pause Detection - if(gamepad.check_event(Gamepad::START_PRESSED)){ + if(gamepad.check_event(Gamepad::START_PRESSED)) { lcd.clear(); lcd.printString("Paused", 0, 0); lcd.refresh(); wait(0.05); - while(gamepad.check_event(Gamepad::START_PRESSED)){ - }; + while(gamepad.check_event(Gamepad::START_PRESSED)) { + } wait(0.05); - while(!gamepad.check_event(Gamepad::START_PRESSED)){ - }; + while(!gamepad.check_event(Gamepad::START_PRESSED)) { + } wait(0.05); - while(gamepad.check_event(Gamepad::START_PRESSED)){ - }; + while(gamepad.check_event(Gamepad::START_PRESSED)) { + } } - - + + // screen update lcd.clear(); lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]); @@ -186,44 +187,46 @@ player.get_sprite_height(), player.get_sprite_width(), player.get_frame()); - for (int i = 0; i < no_of_enemies; i++){ - if (valid_enemies[i]){ + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(), enemies[i]->get_pos_y()-enemies[i]->get_offset_y(), enemies[i]->get_sprite_height(), enemies[i]->get_sprite_width(), enemies[i]->get_frame()); } - }; - for (int i = 0; i < bullets_max; i++){ - if (player.valid_bullets[i]){ + } + for (int i = 0; i < bullets_max; i++) { + if (player.valid_bullets[i]) { lcd.drawSpriteTransparent(player.bullets_array[i]->get_pos_x()-player.bullets_array[i]->get_offset_x(), player.bullets_array[i]->get_pos_y()-player.bullets_array[i]->get_offset_y(), player.bullets_array[i]->get_sprite_height(), player.bullets_array[i]->get_sprite_width(), player.bullets_array[i]->get_frame()); } - }; + } lcd.refresh(); wait_ms(1000/20); // setting FPS counter++; - + } - gameover:{ +gameover: { lcd.clear(); lcd.printString("Game Over", 0, 0); lcd.printString("Retry?", 0, 1); lcd.refresh(); player.~Player(); - for (int i = 0; i < no_of_enemies; i++){ - enemies[i]->~Entity(); - }; + for (int i = 0; i < no_of_enemies; i++) { + if (valid_enemies[i]) { + enemies[i]->~Entity(); + } + } wait(0.05); - while(!gamepad.check_event(Gamepad::A_PRESSED)){ - }; + while(!gamepad.check_event(Gamepad::A_PRESSED)) { + } wait(0.05); - while(gamepad.check_event(Gamepad::A_PRESSED)){ - }; + while(gamepad.check_event(Gamepad::A_PRESSED)) { + } } - }; + } } \ No newline at end of file