Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MotionSensor
Revision 50:2c5cb92a5361, committed 2019-05-09
- Comitter:
- el17sm
- Date:
- Thu May 09 06:33:28 2019 +0000
- Parent:
- 49:3f83ed62d123
- Child:
- 51:4d0cd75e7ed3
- Commit message:
- Room Engine fully commented;
Changed in this revision
--- a/Entity/Player/Player.cpp Thu May 09 04:24:30 2019 +0000
+++ b/Entity/Player/Player.cpp Thu May 09 06:33:28 2019 +0000
@@ -123,7 +123,7 @@
}
}
-bool Player::update_bullets(char * map, bool * doorways)
+bool Player::delete_out_of_bounds_bullets(char * map, bool * doorways)
{
bool result = false;
for (int i = 0; i < bullets_max; i++) {
--- a/Entity/Player/Player.h Thu May 09 04:24:30 2019 +0000
+++ b/Entity/Player/Player.h Thu May 09 06:33:28 2019 +0000
@@ -40,7 +40,7 @@
virtual void take_damage(int damage);
virtual void draw(N5110 &lcd);
void draw_bullets(N5110 &lcd, int j);
- bool update_bullets(char * map, bool * doorways);
+ bool delete_out_of_bounds_bullets(char * map, bool * doorways);
void delete_bullets();
void buttons(bool button_A, bool button_B, bool button_Y, bool button_X);
--- a/RoomEngine/RoomEngine.cpp Thu May 09 04:24:30 2019 +0000
+++ b/RoomEngine/RoomEngine.cpp Thu May 09 06:33:28 2019 +0000
@@ -18,11 +18,32 @@
{
player = current_player;
room = current_room;
- set_input(0,0,0,0,0,0,0,0);
- update_player_position(check_player_room_position());
+ set_input(true,0,0,0,0,0,0,0); // Displays health throughout entrance_scene
+ update_player_position(check_player_room_position()); // Moving the player such that he enters opposite of his exit
room->load();
}
+void RoomEngine::update_player_position(int side)
+{
+ switch(side) { // Depending on which side it exits from, player is repositioned
+ case 0 : // N
+ player->set_position(39, 49);
+ break;
+ case 1 : // E
+ player->set_position(0 - player->get_hitbox_width(), 25);
+ break;
+ case 2 : // S
+ player->set_position(39, 0 - player->get_hitbox_height());
+ break;
+ case 3 : // W
+ player->set_position(85, 25);
+ break;
+ case 4 : // Default where an exit does not exist (starting position)
+ player->set_position(39, 30);
+ break;
+ }
+}
+
void RoomEngine::entrance_scene(N5110 &lcd, Gamepad &gamepad)
{
int side = check_player_room_position();
@@ -45,7 +66,173 @@
}
}
-void RoomEngine::exit_scene(N5110 &lcd, Gamepad &gamepad)
+int RoomEngine::check_player_room_position() // returns 0,1,2,3 if the player exits the respective directions, returns 4 if the player is in the room
+{
+ if (player->get_pos_y() < 0) {
+ return 0;
+ }
+ else if (player->get_pos_x() > WIDTH - (player->get_hitbox_width())) {
+ return 1;
+ }
+ else if (player->get_pos_y() > HEIGHT - (player->get_hitbox_height())) {
+ return 2;
+ }
+ else if (player->get_pos_x() < 0) {
+ return 3;
+ }
+ else {
+ return 4;
+ }
+}
+
+void RoomEngine::read_input(Gamepad &gamepad) // Updates member variables for easier access
+{
+ _L = gamepad.check_event(Gamepad::L_PRESSED);
+ _R = gamepad.check_event(Gamepad::R_PRESSED);
+ _A = gamepad.check_event(Gamepad::A_PRESSED);
+ _B = gamepad.check_event(Gamepad::B_PRESSED);
+ _X = gamepad.check_event(Gamepad::X_PRESSED);
+ _Y = gamepad.check_event(Gamepad::Y_PRESSED);
+ mapped_coord = gamepad.get_mapped_coord();
+}
+
+void RoomEngine::update(int &number_of_enemies_killed) // Updates all HP, deletes dead entities, increment score (kills),
+{ // checks wall collisions (exclusive for entity:walls), movements for all entities and player action detection
+ room->update_doorways();
+ check_damage();
+ check_enemies_death(number_of_enemies_killed);
+ check_walls_collision();
+ move();
+ player->buttons(_A, _B, _Y, _X);
+}
+
+void RoomEngine::check_damage()
+{
+ check_damage_player(); // damage player if collide with enemy / heal if collide with hearts
+ check_damage_enemies(); // damage enemy if collide with player's bullets
+}
+
+
+void RoomEngine::check_enemies_death(int &number_of_enemies_killed) // Deletes dead enemies, spawn hearts based on chance(defined in each enemy) and increments kill score
+{
+ // Enemy Death
+ for (int i = 0; i < MAX_ENEMIES; i++) {
+ if((room->valid_enemies[i]) && (room->enemies[i]->get_hp() <= 0)) {
+ if ((rand() % 100) < room->enemies[i]->get_hp_drop_chance()){
+ for (int j = 0; j < MAX_ENEMIES; j++) {
+ if (!room->valid_collectibles[j]) {
+ room->collectibles[j] = new Health(room->enemies[i]->get_pos_x(), room->enemies[i]->get_pos_y()); // Spawn a health drop
+ room->valid_collectibles[j] = true;
+ break;
+ }
+ }
+ }
+ delete room->enemies[i];
+ room->valid_enemies[i] = false;
+ number_of_enemies_killed++;
+ }
+ }
+}
+
+void RoomEngine::check_walls_collision() // Undo moves of player and enemy if colliding with entity:wall and kills bullets when colliding with entity:wall
+{ // Currently unused (room types 0 and 10 do not spawn entity:wall)
+ // Enemy
+ for (int i = 0; i < MAX_ENEMIES; i++) {
+ if(room->valid_enemies[i]) { // Undo move of every valid enemy upon collision with entity:wall
+ room->enemies[i]->undo_move_x(entity_move_check_x(room->enemies[i], room->walls, 2, 10, room->valid_walls));
+ room->enemies[i]->undo_move_y(entity_move_check_y(room->enemies[i], room->walls, 2, 10, room->valid_walls));
+ }
+ }
+ // Player
+ player->undo_move_x(entity_move_check_x(player, room->walls, 2, 10, room->valid_walls));
+ player->undo_move_y(entity_move_check_y(player, room->walls, 2, 10, room->valid_walls));
+ // Bullets
+ for (int i = 0; i < bullets_max; i++) {
+ for (int j = 0; j < 2; j++) { // given that both walls and bullets are valid, check if they're colliding
+ if ((player->valid_bullets[i]) && (room->valid_walls[j]) && (entity_collision(*player->bullets_array[i], *room->walls[j]))) {
+ delete player->bullets_array[i]; player->valid_bullets[i] = false;
+ }
+ }
+ }
+}
+
+void RoomEngine::move()
+{
+ move_player();
+ move_enemies();
+}
+
+void RoomEngine::render(N5110 &lcd, Gamepad &gamepad)
+{
+ pause_detection(lcd, gamepad);
+ lcd.clear();
+ draw(lcd);
+ lcd.refresh();
+ wait_ms(1000/40); // setting FPS
+}
+
+void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad)
+{
+ if(gamepad.check_event(Gamepad::START_PRESSED)) {
+ draw_health(lcd);
+ char * paused_screen = lcd.readScreen(); // Saves current screen
+ int pause_timer = 0;
+ lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite);
+ // Ensures that Start button is toggled twice to exit loop
+ while(gamepad.check_event(Gamepad::START_PRESSED)) {
+ draw_pause_screen(lcd, paused_screen, pause_timer);
+ }
+ while(!gamepad.check_event(Gamepad::START_PRESSED)) {
+ draw_pause_screen(lcd, paused_screen, pause_timer);
+ }
+ while(gamepad.check_event(Gamepad::START_PRESSED)) {
+ draw_pause_screen(lcd, paused_screen, pause_timer);
+ }
+ }
+}
+
+void RoomEngine::draw_pause_screen(N5110 &lcd, char * paused_screen, int &pause_timer)
+{
+ lcd.clear();
+ lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen);
+ if (pause_timer % 20 < 10) { // Draws pause sprite only 10/20 frames of pause_timer, this way pause blinks
+ lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite);
+ }
+ lcd.refresh();
+ pause_timer++;
+ wait_ms(1000/40);
+}
+
+void RoomEngine::draw(N5110 &lcd) // Draws everything onto the screen
+{
+ room->draw_room(lcd);
+ for(int j = 0; j < HEIGHT; j++) { // To create a 3D illusion, all entities are drawn at the order of it's hitbox height (highest to lowest)
+ if (j == player->get_pos_y()) {
+ player->draw(lcd);
+ }
+ player->draw_bullets(lcd, j);
+ room->draw(lcd, j);
+ }
+
+ room->draw_room_overlay(lcd); // Special overlay such as side doorways and boss doorways
+
+ if (_L) { // Displaying Hearts if L is pressed
+ draw_health(lcd);
+ }
+}
+
+void RoomEngine::draw_health(N5110 &lcd)
+{
+ for (int i = 0; i < player->get_hp(); i++){ // For every health, draw a heart at x pos i*10
+ lcd.drawSpriteTransparent(i*10,
+ 0,
+ player->get_hearts_height(),
+ player->get_hearts_width(),
+ player->get_hearts_sprite());
+ }
+}
+
+void RoomEngine::exit_scene(N5110 &lcd, Gamepad &gamepad) // Plays an exit scene
{
int side = check_player_room_position();
@@ -67,7 +254,7 @@
lcd.setContrast(0.75);
}
-void RoomEngine::update_current_room()
+void RoomEngine::update_current_room() // Increments room coord depending on direction
{
switch(check_player_room_position()) {
case 0 :
@@ -87,55 +274,6 @@
}
}
-void RoomEngine::read_input(Gamepad &gamepad)
-{
- _L = gamepad.check_event(Gamepad::L_PRESSED);
- _R = gamepad.check_event(Gamepad::R_PRESSED);
- _A = gamepad.check_event(Gamepad::A_PRESSED);
- _B = gamepad.check_event(Gamepad::B_PRESSED);
- _X = gamepad.check_event(Gamepad::X_PRESSED);
- _Y = gamepad.check_event(Gamepad::Y_PRESSED);
- mapped_coord = gamepad.get_mapped_coord();
-}
-
-void RoomEngine::update(int &number_of_enemies_killed)
-{
- room->update_doorways();
- check_damage();
- check_enemies_death(number_of_enemies_killed);
- check_walls_collision();
- move();
- player->buttons(_A, _B, _Y, _X);
-}
-
-void RoomEngine::render(N5110 &lcd, Gamepad &gamepad)
-{
- pause_detection(lcd, gamepad);
- lcd.clear();
- draw(lcd);
- lcd.refresh();
- wait_ms(1000/40); // setting FPS
-}
-
-int RoomEngine::check_player_room_position() // returns 0,1,2,3 if the player exits the respective directions, returns 4 if the player is in the room
-{
- if (player->get_pos_y() < 0) {
- return 0;
- }
- else if (player->get_pos_x() > WIDTH - (player->get_hitbox_width())) {
- return 1;
- }
- else if (player->get_pos_y() > HEIGHT - (player->get_hitbox_height())) {
- return 2;
- }
- else if (player->get_pos_x() < 0) {
- return 3;
- }
- else {
- return 4;
- }
-}
-
// Public Accessors
int RoomEngine::get_room_x()
@@ -166,26 +304,26 @@
mapped_coord.y = y;
}
-// Private Functions
+// Methods
bool RoomEngine::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))) {
- 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))) {
+ if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) || // if Entity A's x of left side of the hitbox is inside Entity B's hitbox x range
+ ((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 Entity A's x of right side of the hitbox is inside Entity B's hitbox x range
+ if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) || // if Entity A's y of top side of the hitbox is inside Entity B's hitbox y range
+ ((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))) { // if Entity A's y of bottom side of the hitbox is inside Entity B's hitbox y range
return true;
}
}
return 0;
}
-// returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction
+// returns true if the hitbox of "entity a" collides with any hitboxes of entities within "array" as "entity a" moves on the x direction
float RoomEngine::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++) {
+ for (int i = 0; i < no_of_enemies; i++) { // For every enemy = Entity B
if ((valid_enemies[i]) && (i != current_entity)) { // only check if entity b exists and entity a is not the same as entity b
- 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)) ||
+ 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)) || // Same as entity_collision, except that Entity B's x position is its previous x position
((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))) {
@@ -197,12 +335,12 @@
return 0;
}
-// returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction
+// returns true if the hitbox of "entity a" collides with any hitboxes of entities within "array" as "entity a" moves on the y direction
float RoomEngine::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++) {
+ for (int i = 0; i < no_of_enemies; i++) { // For every enemy = Entity B
if ((valid_enemies[i]) && (i != current_entity)) { // only check if entity b exists and entity a is not the same as entity b
- 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)) ||
+ 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)) || // Same as entity_collision, except that Entity B's y position is its previous y position
((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))) {
@@ -214,22 +352,16 @@
return 0;
}
-void RoomEngine::check_damage()
-{
- check_damage_player();
- check_damage_enemies();
-}
-
void RoomEngine::check_damage_player()
{
for (int i = 0; i < MAX_ENEMIES; i++) {
- if (room->valid_enemies[i]) {
+ if (room->valid_enemies[i]) { // Checking each valid enemy
if(entity_collision(*player, *room->enemies[i])) {
player->take_damage(room->enemies[i]->get_attack());
break; // only let 1 enemy damage player at a time
}
}
- if (room->valid_collectibles[i]) {
+ if (room->valid_collectibles[i]) { // Checking each valid collectible (hearts, coins)
if(entity_collision(*player, *room->collectibles[i])) {
player->take_damage(room->collectibles[i]->get_attack());
delete room->collectibles[i];
@@ -244,13 +376,11 @@
{
for (int i = 0; i < bullets_max; i++) {
if (player->valid_bullets[i]) {
- if (player->update_bullets(room->get_current_map_2d(), room->get_doorways())) {
- } else {
+ if (!player->delete_out_of_bounds_bullets(room->get_current_map_2d(), room->get_doorways())) { // Delete any bullet that goes out the screen or hits walls, if none, then
for (int j = 0; j < MAX_ENEMIES; j++) {
- if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) {
+ if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) { // Delete bullets and damage enemy if player bullets collide with any enemy
room->enemies[j]->take_damage(player->get_attack());
- player->valid_bullets[i] = false;
- delete player->bullets_array[i];
+ delete player->bullets_array[i]; player->valid_bullets[i] = false;
break;
}
}
@@ -259,55 +389,6 @@
}
}
-void RoomEngine::check_enemies_death(int &number_of_enemies_killed)
-{
- // Enemy Death
- for (int i = 0; i < MAX_ENEMIES; i++) {
- if((room->valid_enemies[i]) && (room->enemies[i]->get_hp() <= 0)) {
- if ((rand() % 100) < room->enemies[i]->get_hp_drop_chance()){
- for (int j = 0; j < MAX_ENEMIES; j++) {
- if (!room->valid_collectibles[j]) {
- room->collectibles[j] = new Health(room->enemies[i]->get_pos_x(), room->enemies[i]->get_pos_y()); // Spawn a health drop
- room->valid_collectibles[j] = true;
- break;
- }
- }
- }
- delete room->enemies[i];
- room->valid_enemies[i] = false;
- number_of_enemies_killed++;
- }
- }
-}
-
-void RoomEngine::check_walls_collision()
-{
- // Enemy
- for (int i = 0; i < MAX_ENEMIES; i++) {
- if(room->valid_enemies[i]) {
- room->enemies[i]->undo_move_x(entity_move_check_x(room->enemies[i], room->walls, 2, 10, room->valid_walls));
- room->enemies[i]->undo_move_y(entity_move_check_y(room->enemies[i], room->walls, 2, 10, room->valid_walls));
- }
- }
- // Player
- player->undo_move_x(entity_move_check_x(player, room->walls, 2, 10, room->valid_walls));
- player->undo_move_y(entity_move_check_y(player, room->walls, 2, 10, room->valid_walls));
- // Bullets
- for (int i = 0; i < bullets_max; i++) {
- for (int j = 0; j < 2; j++) {
- if ((player->valid_bullets[i]) && (room->valid_walls[j]) && (entity_collision(*player->bullets_array[i], *room->walls[j]))) {
- delete player->bullets_array[i]; player->valid_bullets[i] = false;
- }
- }
- }
-}
-
-void RoomEngine::move()
-{
- move_player();
- move_enemies();
-}
-
void RoomEngine::move_player()
{
player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d(), room->get_doorways());
@@ -325,91 +406,8 @@
// Entity Collision Repulsion
for (int i = 0; i < MAX_ENEMIES; i++) {
if (room->valid_enemies[i]) {
- room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies));
- room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies));
+ room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); // add 1 x position if collide
+ room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); // add 1 y position if collide
}
}
-}
-
-void RoomEngine::update_player_position(int side)
-{
- switch(side) {
- case 0 :
- player->set_position(39, 49);
- break;
- case 1 :
- player->set_position(0 - player->get_hitbox_width(), 25);
- break;
- case 2 :
- player->set_position(39, 0 - player->get_hitbox_height());
- break;
- case 3 :
- player->set_position(85, 25);
- break;
- case 4 :
- player->set_position(39, 37);
- break;
- }
-}
-
-void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad)
-{
- if(gamepad.check_event(Gamepad::START_PRESSED)) {
- draw_health(lcd);
- char * paused_screen = lcd.readScreen();
- int pause_timer = 2;
- lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite);
- while(gamepad.check_event(Gamepad::START_PRESSED)) {
- draw_pause_screen(lcd, paused_screen, pause_timer);
- }
- pause_timer += 2;
- while(!gamepad.check_event(Gamepad::START_PRESSED)) {
- draw_pause_screen(lcd, paused_screen, pause_timer);
- }
- pause_timer += 2;
- while(gamepad.check_event(Gamepad::START_PRESSED)) {
- draw_pause_screen(lcd, paused_screen, pause_timer);
- }
- }
-}
-
-void RoomEngine::draw_pause_screen(N5110 &lcd, char * paused_screen, int &pause_timer)
-{
- lcd.clear();
- lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen);
- if (pause_timer % 10 <= 4) {
- lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite);
- }
- lcd.refresh();
- pause_timer++;
- wait_ms(1000/40);
-}
-
-void RoomEngine::draw(N5110 &lcd)
-{
- room->draw_room(lcd);
- //lcd.drawSprite(0,0,screen_height,screen_width,(char *)level_map[1]);
- for(int j = 0; j < HEIGHT; j++) {
- if (j == player->get_pos_y()) {
- player->draw(lcd);
- }
- player->draw_bullets(lcd, j);
- room->draw(lcd, j);
- }
-
- room->draw_room_overlay(lcd);
- if (_L) {
- draw_health(lcd);
- }
-}
-
-void RoomEngine::draw_health(N5110 &lcd)
-{
- for (int i = 0; i < player->get_hp(); i++){
- lcd.drawSpriteTransparent(i*10,
- 0,
- player->get_hearts_height(),
- player->get_hearts_width(),
- player->get_hearts_sprite());
- }
}
\ No newline at end of file
--- a/RoomEngine/RoomEngine.h Thu May 09 04:24:30 2019 +0000
+++ b/RoomEngine/RoomEngine.h Thu May 09 06:33:28 2019 +0000
@@ -23,14 +23,15 @@
// Functions
void load(Player *current_player, Room *current_room);
void entrance_scene(N5110 &lcd, Gamepad &gamepad);
- void exit_scene(N5110 &lcd, Gamepad &gamepad);
void update_current_room();
+ int check_player_room_position();
+
void read_input(Gamepad &gamepad);
void update(int &number_of_enemies_killed);
void render(N5110 &lcd, Gamepad &gamepad);
- int check_player_room_position();
+ void exit_scene(N5110 &lcd, Gamepad &gamepad);
// Accessor
int get_room_x();
--- a/Title/Title.cpp Thu May 09 04:24:30 2019 +0000
+++ b/Title/Title.cpp Thu May 09 06:33:28 2019 +0000
@@ -85,7 +85,7 @@
player.move(1, 0, (char *)level_map[0][0], (bool *)sprite_transparent_player); // Adding animation of walking
player.undo_move_x(true); // Keeping the player in place
player.buttons(false, true, false, false); // Instructing player to shoot right
- player.update_bullets((char *)level_map[0][0], (bool *)sprite_transparent_player); // Move the bullets and delete those out of the screen
+ player.delete_out_of_bounds_bullets((char *)level_map[0][0], (bool *)sprite_transparent_player); // Move the bullets and delete those out of the screen
wait_ms(1000/40);
}
wait(0.05);
--- a/main.cpp Thu May 09 04:24:30 2019 +0000
+++ b/main.cpp Thu May 09 06:33:28 2019 +0000
@@ -15,7 +15,7 @@
init();
while(1) { // Gameloop
- boss_room_exist = false;
+ boss_room_exist = false; // Game initialize variables
number_of_enemies_killed = 0;
total_time = 0;
@@ -25,7 +25,7 @@
room_engine = new RoomEngine(global_contrast);
game_loop();
- game_unload();
+ game_unload(); // Deletion of player, rooms, roomengine
}
}
@@ -47,17 +47,17 @@
boss_room_number = 5 + rand() % 4; // Boss room appears after travelling 5-8 rooms
boss_room_counter = 0;
while(1) { // Room Loop
- room_entrance();
+ room_entrance(); // Generation of new room, loading of room and entrance scene
while(room_engine->check_player_room_position() == INSIDE) { // actions inside the Room
room_engine->read_input(gamepad);
room_engine->update(number_of_enemies_killed);
room_engine->render(lcd, gamepad);
minimap_detection();
- total_time++;
+ total_time++; // Incrementing time score
if (player->get_hp() <= 0) {goto gameover;} // gameover if player health depleted
if ((rooms[room_y][room_x]->get_room_type() == 10) && !(rooms[room_y][room_x]->enemies_exist())){goto winner;} // wins game if room is a boss room, and the boss is dead
}
- room_exit();
+ room_exit(); // Exit scene, deletion of existing entities and update of room coordinates
}
}
gameover : { game_over(); goto displaystats;}
@@ -67,7 +67,7 @@
void room_entrance()
{
- update_room_coords();
+ update_room_coords(); // Accessor to room_engine room coords
if (!valid_rooms[room_y][room_x]){generate_room();} // generate a new room if player enters a nonexistent room
room_engine->load(player, rooms[room_y][room_x]);
room_engine->entrance_scene(lcd, gamepad);
@@ -78,28 +78,28 @@
room_engine->exit_scene(lcd, gamepad);
rooms[room_y][room_x]->unload();
player->delete_bullets();
- room_engine->update_current_room();
+ room_engine->update_current_room(); // Increments room coord
}
void generate_room()
{
- valid_rooms[room_y][room_x] = true;
- if (boss_room_counter == 0) {
- rooms[room_y][room_x] = new Room(0, 0);
+ valid_rooms[room_y][room_x] = true; // Sets the room coord to be valid
+ if (boss_room_counter == 0) { // if first room
+ rooms[room_y][room_x] = new Room(0, 0); // no enemies
rooms[room_y][room_x]->set_doorway(0, true);
} else {
- rooms[room_y][room_x] = new Room(rand() % 4, 0);
+ rooms[room_y][room_x] = new Room(rand() % 4, 0); // random enemies (0-3)
if (boss_room_counter < boss_room_number) {no_of_doorways = 2 + (rand() % 100 < 10) + (rand() % 100 < 10);} // 20% chance of adding doorways
- else {no_of_doorways = 0;}
+ else {no_of_doorways = 1;} // If boss room exist, ensure all unexplored rooms branch out into a dead end
while (count_doorways() < no_of_doorways) {
rooms[room_y][room_x]->set_doorway(rand() % 4, true); // Setting random doorways until number of desired doorways
}
update_definite_doorways();
- for (int i = 0; i < 4; i++) {
+ for (int i = 0; i < 4; i++) { // Sets the definitive doorways
if (cannot[i]){rooms[room_y][room_x]->set_doorway(i, false);}
if (have_to[i]){rooms[room_y][room_x]->set_doorway(i, true);}
}
- if ((boss_room_counter >= boss_room_number) && (!boss_room_exist)) {
+ if ((boss_room_counter >= boss_room_number) && (!boss_room_exist)) { // Sets the doorway towards the boss room to be a boss doorway
rooms[room_y][room_x]->set_boss_doorway(available_boss_room());
boss_room_exist = true;
}
@@ -109,8 +109,6 @@
void update_room_coords()
{
- prev_room_x = room_x;
- prev_room_y = room_y;
room_y = room_engine->get_room_y();
room_x = room_engine->get_room_x();
}
@@ -122,33 +120,38 @@
for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
if (valid_rooms[j][i]) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]);
- if (rooms[j][i]->get_room_type() == 10) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]);
- } else if (rooms[j][i]->enemies_exist()) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]);
- }
- if (rooms[j][i]->get_doorway(0)) {
- lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(1)) {
- lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(2)) {
- lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(3)) {
- lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
- }
+ draw_minimap(j,i);
}
}
}
- lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]);
+ lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]); // draws your current room
lcd.refresh();
wait_ms(1000/40);
};
}
+void draw_minimap(int j, int i)
+{
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]); // draws normal room
+ if (rooms[j][i]->get_room_type() == 10) {
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]); // draws boss room
+ } else if (rooms[j][i]->enemies_exist()) {
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]); // draws mob room
+ }
+ if (rooms[j][i]->get_doorway(0)) { // Drawing all doorways
+ lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(1)) {
+ lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(2)) {
+ lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(3)) {
+ lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+ }
+}
+
void game_over()
{
lcd.clear();
@@ -187,7 +190,7 @@
int available_boss_room()
{
- if (!valid_rooms[room_y - 1][room_x]){
+ if (!valid_rooms[room_y - 1][room_x]){ // if any of the adjacent room is invalid, it is replaced with a boss room
rooms[room_y][room_x]->set_doorway(0, true);
set_boss_room(room_y - 1, room_x, 0);
return 0;
@@ -203,7 +206,7 @@
rooms[room_y][room_x]->set_doorway(3, true);
set_boss_room(room_y, room_x - 1, 3);
return 3; }
- delete rooms[room_y - 1][room_x];
+ delete rooms[room_y - 1][room_x]; // if all adjacent room is valid, mutate the top room into a boss room (default case)
rooms[room_y][room_x]->set_doorway(3, true);
set_boss_room(room_y - 1, room_x, 0);
return 0;
@@ -213,8 +216,8 @@
{
valid_rooms[room_y][room_x] = true;
rooms[room_y][room_x] = new Room(0, 10);
- rooms[room_y][room_x]->set_doorway(opposite(side), true);
- if ((opposite(side) != 0) && (valid_rooms[room_y - 1][room_x])){
+ rooms[room_y][room_x]->set_doorway(opposite(side), true); // Sets a definite doorway on the side player comes from
+ if ((opposite(side) != 0) && (valid_rooms[room_y - 1][room_x])){ // Deletes any existing doorways unless player entered from that side
if (rooms[room_y - 1][room_x]->get_doorway(2)){
rooms[room_y - 1][room_x]->set_doorway(2, false);
}}
@@ -234,16 +237,17 @@
void game_unload()
{
+ // Deletes every generated rooms
for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
- for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
- if (valid_rooms[j][j]){
- delete rooms[j][i];
- valid_rooms[i][j] = false;
- }
+ for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+ if (valid_rooms[j][j]){
+ delete rooms[j][i];
+ valid_rooms[i][j] = false;
}
}
- delete room_engine;
- delete player;
+ }
+ delete room_engine;
+ delete player;
}
// Functions
@@ -280,33 +284,33 @@
if (room_y == 1) { // if room to be generated is on the border, then doorway cannot exist
cannot[0] = true;
have_to[0] = false;
- } else if (valid_rooms[room_y - 1][room_x]){ // if room to be generated is on the border, then doorway cannot exist
- if (rooms[room_y - 1][room_x]->get_doorway(2)){
+ } else if (valid_rooms[room_y - 1][room_x]){ // if room to be generated has an existing room above it
+ if (rooms[room_y - 1][room_x]->get_doorway(2)){ // if room to be generated has an existing doorway, then doorway must exist
have_to[0] = true;
cannot[0] = false;
- } else {
+ } else { // if room to be generated does not have an existing doorway, then doorway must not exist
have_to[0] = false;
cannot[0] = true;
}
- } else {
+ } else { // else the room does not have any definite doorways set on it's north
have_to[0] = false;
cannot[0] = false;
}
}
void update_definite_doorways_right()
{
- if (room_x == 10) {
+ if (room_x == 10) { // if room to be generated is on the border, then doorway cannot exist
cannot[1] = true;
have_to[1] = false;
- } else if (valid_rooms[room_y][room_x + 1]){
- if (rooms[room_y][room_x + 1]->get_doorway(3)){
+ } else if (valid_rooms[room_y][room_x + 1]){ // if room to be generated has an existing room on it's right
+ if (rooms[room_y][room_x + 1]->get_doorway(3)){ // if room to be generated has an existing doorway, then doorway must exist
have_to[1] = true;
cannot[1] = false;
- } else {
+ } else { // if room to be generated does not have an existing doorway, then doorway must not exist
have_to[1] = false;
cannot[1] = true;
}
- } else {
+ } else { // else the room does not have any definite doorways set on it's right
have_to[1] = false;
cannot[1] = false;
}
@@ -314,18 +318,18 @@
void update_definite_doorways_down()
{
- if (room_y == 10) {
+ if (room_y == 10) { // if room to be generated is on the border, then doorway cannot exist
cannot[2] = true;
have_to[2] = false;
- } else if (valid_rooms[room_y + 1][room_x]){
- if (rooms[room_y + 1][room_x]->get_doorway(0)){
+ } else if (valid_rooms[room_y + 1][room_x]){ // if room to be generated has an existing room below it
+ if (rooms[room_y + 1][room_x]->get_doorway(0)){ // if room to be generated has an existing doorway, then doorway must exist
have_to[2] = true;
cannot[2] = false;
- } else {
+ } else { // if room to be generated does not have an existing doorway, then doorway must not exist
have_to[2] = false;
cannot[2] = true;
}
- } else {
+ } else { // else the room does not have any definite doorways set on it's south
have_to[2] = false;
cannot[2] = false;
}
@@ -333,18 +337,18 @@
void update_definite_doorways_left()
{
- if (room_x == 1) {
+ if (room_x == 1) { // if room to be generated is on the border, then doorway cannot exist
cannot[3] = true;
have_to[3] = false;
- } else if (valid_rooms[room_y][room_x - 1]){
- if (rooms[room_y][room_x - 1]->get_doorway(1)){
+ } else if (valid_rooms[room_y][room_x - 1]){ // if room to be generated has an existing room on it's left
+ if (rooms[room_y][room_x - 1]->get_doorway(1)){ // if room to be generated has an existing doorway, then doorway must exist
have_to[3] = true;
cannot[3] = false;
- } else {
+ } else { // if room to be generated does not have an existing doorway, then doorway must not exist
have_to[3] = false;
cannot[3] = true;
}
- } else {
+ } else { // else the room does not have any definite doorways set on it's left
have_to[3] = false;
cannot[3] = false;
}
--- a/main.h Thu May 09 04:24:30 2019 +0000
+++ b/main.h Thu May 09 06:33:28 2019 +0000
@@ -24,8 +24,6 @@
int total_time;
int no_of_doorways;
int boss_room_counter = 0;
-int prev_room_x = MAX_ROOMS_MAP_X;
-int prev_room_y = MAX_ROOMS_MAP_Y;
int room_x = MAX_ROOMS_MAP_X;
int room_y = MAX_ROOMS_MAP_Y;
bool have_to[4] = {false, false, false, false};
@@ -49,6 +47,7 @@
void generate_room();
void update_room_coords();
void minimap_detection();
+void draw_minimap(int j, int i);
void game_over();
void win();