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-21
Revision:
10:1a3499f6b583
Parent:
7:4aaa37a711a1
Child:
11:63e54f6e7939

File content as of revision 10:1a3499f6b583:

/*
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){
    if (((b.get_pos_x() < a.get_pos_x()) && (a.get_pos_x() < b.get_pos_x() + b.get_hitbox_width())) ||
    ((b.get_pos_x() < a.get_pos_x() + a.get_hitbox_width()) && (a.get_pos_x() + a.get_hitbox_width() < b.get_pos_x() + b.get_hitbox_width())))
    {
        if (((b.get_pos_y() < a.get_pos_y()) && (a.get_pos_y() < b.get_pos_y() + b.get_hitbox_height())) ||
        ((b.get_pos_y() < a.get_pos_y() + a.get_hitbox_height()) && (a.get_pos_y() + a.get_hitbox_height() < b.get_pos_y() + b.get_hitbox_height())))
        {
            return true;
        }          
    }
    return false;
}

int main()
{
    lcd.init();
    lcd.setContrast(0.45);
    gamepad.init();
    while(1){
        Player player(39, 27);
        Headless headless(20, 20);
        while(1){
            // 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
            headless.move(player.get_pos_x(), player.get_pos_y());
            
            // Entity Collision Detection
            // Damage action
            if(entity_collision(player, headless)){
                break;
            }
            
            // MiniMap Generation
            // 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(player.get_pos_x()-player.get_offset_x(),
                                      player.get_pos_y()-player.get_offset_y(),
                                      player.get_sprite_height(),
                                      player.get_sprite_width(),
                                      (int *)sprite_player[player.get_face()][(int)(player.get_moving()*(counter/5)%4)]);
            lcd.drawSpriteTransparent(headless.get_pos_x()-headless.get_offset_x(),
                                      headless.get_pos_y()-headless.get_offset_y(),
                                      headless.get_sprite_height(),
                                      headless.get_sprite_width(),
                                      (int *)sprite_headless[headless.get_face()][(int)(headless.get_moving()*(counter/5)%4)]);
            lcd.refresh();
            wait_ms(1000/20);
            counter++;
            
        }
        lcd.clear();
        lcd.printString("Game Over", 0, 0);
        lcd.printString("Retry?", 0, 1);
        lcd.refresh();
        player.~Player();
        headless.~Headless();
        wait(0.05);
        while(!gamepad.check_event(Gamepad::A_PRESSED)){
        };
        wait(0.05);
        while(gamepad.check_event(Gamepad::A_PRESSED)){
        };
    };
}