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

Dependencies:   mbed MotionSensor

main.cpp

Committer:
el17sm
Date:
2019-04-23
Revision:
12:a1c1991835ca
Parent:
11:63e54f6e7939
Child:
13:d04a6caba40d

File content as of revision 12:a1c1991835ca:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Steven Mahasin
Username: el17sm
Student ID Number: 201192939
Date: 11/04/2019
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "math.h"
#include "sprites.h"
#include "Entity.h"
#include "Player.h"
#include "Headless.h"

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad gamepad;

int counter = 0;

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)))
    {
        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)))
        {
            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){
    for (int i = 0; i < no_of_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)))
            {
                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)))
                {
                    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 x direction
bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
    for (int i = 0; i < no_of_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)))
            {
                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)))
                {
                    return true;
                }          
            }
        }
    }
    return false;
}

int main()
{
    lcd.init();
    lcd.setContrast(0.45);
    gamepad.init();
    while(1){
        Player player(39, 27);
        int no_of_enemies = 3;
        Entity *enemies[no_of_enemies];
        Headless enemy1(20, 20);
        Headless enemy2(20, 30);
        Headless enemy3(60, 30);
        enemies[0] = &enemy1;
        enemies[1] = &enemy2;
        enemies[2] = &enemy3;
        
        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(entity_collision(player, *enemies[i])){
                    goto gameover;
                }
            };
            
            // 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 Movement
            for (int i = 0; i < no_of_enemies; i++){
                enemies[i]->update_prev_pos();
                enemies[i]->move(pos_x, pos_y);
            };
            for (int i = 0; i < no_of_enemies; i++){
                enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i));
                enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i));
            };
            
            // Entity Collision Detection
            
            // MiniMap Screen Detection
            
            // Pause Detection
            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)){
                };
                wait(0.05);
                while(!gamepad.check_event(Gamepad::START_PRESSED)){
                };
                wait(0.05);
                while(gamepad.check_event(Gamepad::START_PRESSED)){
                };
            }
            
            
            // screen update
            lcd.clear();
            lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
            lcd.drawSpriteTransparent(pos_x-player.get_offset_x(),
                                      pos_y-player.get_offset_y(),
                                      player.get_sprite_height(),
                                      player.get_sprite_width(),
                                      player.get_frame());
            for (int i = 0; i < no_of_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());
            };
            
            lcd.refresh();
            wait_ms(1000/20); // setting FPS
            counter++;
            
        }
        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();
            };
            wait(0.05);
            while(!gamepad.check_event(Gamepad::A_PRESSED)){
            };
            wait(0.05);
            while(gamepad.check_event(Gamepad::A_PRESSED)){
        };
    };
}