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-05-05
Revision:
31:ab24d028ddfd
Parent:
30:ec915d24d3e9
Child:
32:fe6359ef9916

File content as of revision 31:ab24d028ddfd:

/*
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
*/

// Pre-Processor
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "math.h"
#include "sprites.h"
#include "Entity.h"
#include "Player.h"
#include "Headless.h"
#include "Snake.h"
#include "RoomEngine.h"
#include <time.h> 

#define INSIDE 4
#define MAX_ROOMS_MAP_X 11
#define MAX_ROOMS_MAP_Y 11

// Variables
int title_count = 0;
int cursor_timer = 20;
int title_option = 0;
float global_contrast = 0.5;

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

// Prototypes
void title_screen();
void draw_title_screen(N5110 &lcd);
void title_options_joystick(Gamepad &gamepad);

void title_option_option();
void title_option_credit();
void title_option_hi_scores();
void game_loop();
void game_over();
Player *player;
Room *rooms[MAX_ROOMS_MAP_Y][MAX_ROOMS_MAP_X];
RoomEngine *room_engine;

// Functions
int main()
{
    // Initialize
    lcd.init();
    lcd.setContrast(global_contrast); 
    gamepad.init();
    
    while(1) { // Gameloop
        
        player = new Player(5, 36);
        title_screen();
        delete player;
        
        srand(title_count);
        player = new Player(39, 27);
        room_engine = new RoomEngine(global_contrast);
        
        game_loop();
        game_over();
        delete room_engine;
        delete player;
    }
}

void draw_title_screen(N5110 &lcd)
{
    lcd.drawSprite(11, 4, 15, 44, (int *)title_name_0);
    lcd.drawSpriteTransparent(19, 14, 17, 53, (int *)title_name_1);
    lcd.drawCircle(79, 7, 10, FILL_BLACK);
    lcd.drawCircle(81, 5, 8, FILL_WHITE);
    lcd.drawSprite(56, 6, 11, 5, (int *)star_sprite[abs(((title_count/20) % 7) - 3)]);
    lcd.drawSprite(12, 34, 8, 8, (int *)button_A_sprite);
    lcd.drawSprite(22, 37, 3, 2, (int *)arrow_left_sprite);
    lcd.drawSprite(59, 37, 3, 2, (int *)arrow_right_sprite);
    lcd.drawSprite(69, 31, 12, 6, (int *)sprite_player[(title_count/40) % 4][(title_count/10) % 4]);
    lcd.drawSprite(26, 35, 9, 32, (int *)title_options_sprite[title_option]);
}

void title_options_joystick(Gamepad &gamepad)
{
    if ((gamepad.get_direction() == 3) && (cursor_timer > 20)) {    // Detect Joystick going right
        cursor_timer = 0;
        if (title_option >= 3) {
            title_option = 0;
        } else {
            title_option++;
        }
    } else if ((gamepad.get_direction() == 7) && (cursor_timer > 20)) { // Detect Joystick going left
        cursor_timer = 0;
        if (title_option <= 0) {
            title_option = 3;
        } else {
            title_option--;
        }
    }
    cursor_timer++;
}

void title_screen()
{
    while(1){  // Title Screen Loop
        title_option = 0;
        while(!gamepad.check_event(Gamepad::A_PRESSED)){ 
            lcd.clear();
            draw_title_screen(lcd);
            lcd.refresh();
            
            title_options_joystick(gamepad);
            
            title_count++;
            wait_ms(1000/40); // 1s/framerate
        }
        while(gamepad.check_event(Gamepad::A_PRESSED)){}
        if(title_option == 0) {
            break;
        } else if (title_option == 1) {
            while(!gamepad.check_event(Gamepad::A_PRESSED)) {
                title_option_option();
            }
            wait(0.05);
            while(gamepad.check_event(Gamepad::A_PRESSED)) {
            }
        } else if (title_option == 2) {
            title_option_credit();
            wait(0.05);
            while(!gamepad.check_event(Gamepad::A_PRESSED)) {
            }
            wait(0.05);
            while(gamepad.check_event(Gamepad::A_PRESSED)) {
            }
        } else if (title_option == 3) {
            
        }
    }
}

void title_option_option()
{
    global_contrast = gamepad.read_pot();
    lcd.setContrast(global_contrast);
    lcd.clear();
    lcd.printString("Set contrast", 0, 0);
    lcd.printString("using the", 0, 1);
    lcd.printString("potentiometer", 0, 2);
    player->draw(lcd);
    lcd.refresh();
    player->move(1, 0, (int *)level_map[0][0], (bool *)sprite_transparent_player);
    player->buttons(false, true, false, false);
    player->update_bullets((int *)level_map[0][0], (bool *)sprite_transparent_player);
    wait_ms(1000/40);
}

void title_option_credit()
{
    lcd.clear();
    lcd.setContrast(global_contrast);
    lcd.printString("Made by:", 0, 0);
    lcd.printString("Steven Mahasin", 0, 1);
    lcd.printString("201192939", 0, 2);
    lcd.refresh();
}
void title_option_hi_scores()
{
    
}

void game_loop()
{
    while(1) {  // Floor Loop 
        for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
            for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
                rooms[j][i] = new Room(2);
            }
        }
    
        // Rooms Generation
        while(1) {  // Room Loop
            rooms[room_engine->get_room_y()][room_engine->get_room_x()]->load();
            room_engine->load(player, rooms[room_engine->get_room_y()][room_engine->get_room_x()]);
            
            room_engine->entrance_scene(lcd, gamepad);
            while(room_engine->check_player_room_position() == INSIDE) {  // Room actions
                room_engine->read_input(gamepad);
                room_engine->update();
                room_engine->render(lcd, gamepad);
                if  (player->get_hp() <= 0) {
                    goto gameover;
                }
            }
            room_engine->exit_scene(lcd, gamepad);
            rooms[room_engine->get_room_y()][room_engine->get_room_x()]->unload();
            player->delete_bullets();
            room_engine->update_current_room();
        }
    }
    gameover : {
        game_over();
    }
}

void game_over()
{
    while(1){   // Game Over Screen Loop
                lcd.clear();
                lcd.setContrast(global_contrast);
                lcd.printString("Game Over", 0, 0);
                lcd.printString("Retry?", 0, 1);
                lcd.refresh();
                wait(0.05);
                while(!gamepad.check_event(Gamepad::A_PRESSED)) {
                }
                wait(0.05);
                while(gamepad.check_event(Gamepad::A_PRESSED)) {
                }
                break;
            }
}