ELEC2645 (2018/19) / Mbed 2 deprecated el17cr

Dependencies:   mbed

Falldown/Falldown.h

Committer:
el17cr
Date:
2019-05-09
Revision:
7:cf469c3505a2
Parent:
6:85314a3d69cd
Child:
8:8d9c5a7e57d3

File content as of revision 7:cf469c3505a2:

#ifndef FALLDOWN_H
#define FALLDOWN_H

#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Ball.h"
#include "Ground.h"


/** Falldown class

@brief Class for creating the Falldown object, initialising the game, checking for collisions, and determining the status of the game depending on these collions

@version 1.0

@author Connor Rainey

@date May 2019

@code

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Falldown.h"

// These values cannot be changed
#define GROUND_HEIGHT 2
#define BALL_SIZE 3


// define initial ground width, ground speed and level
int ground_width = 40;
double ground_speed = 0.3;
int level = 1;

// struct holds data for reading joystick
struct UserInput {
    Direction d;
    float mag;
};

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
Gamepad pad; //gamepad library
Falldown falldown; // falldown class

void init();
void update_game(UserInput input);
void render();
void welcome();
void game_over();
void game_complete();


int main()
{
    init();
    welcome();
    render();
    wait(0.1); //delay required before clearing the display and constructing the next frame

    // start game loop
    while(1) {
        falldown.read_input(pad);
        falldown.update(pad);
        render();
        wait(0.1);
        // check goal funtion returns a 1 if ball reaches the bottom of the screen
        if (falldown.check_goal() == 1) {
            level = level++; // increment level if goal reached
            ground_width = ground_width + 1; // increases ground width
            ground_speed = ground_speed + 0.1; // increase ground speed
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);

            // if on final level, and goal reached
        } else if ((level == 2) && (falldown.check_complete() == 1)) {
            game_complete(); // call game complete function
            wait(0.5);
            level = 1; // if game complete function is broken and player chooses to play again, re-set values for level, ground width and ground speed
            ground_width = 40;
            ground_speed = 0.5;
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);
        
        //
        } else if (falldown.check_end() == 1)  {
            game_over(); // call game over function
            level = 1; // if game over function is broken and player chooses to play again, re-set values for level, ground width and ground speed
            ground_width = 40;
            ground_speed = 0.5;
            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
            wait(0.1);
        }
    }
}

@endcode
*/

class Falldown
{

public:
    // constructor
    /**
     * @brief Constructs the Falldown object
     * @details simple construtor that creates a trivial calibration
     */
    Falldown();
    // destructor
    /**
     * @brief Destructs the Falldown object
     * @details deallocates memory location for the ground object
     */
    ~Falldown();
    /**
     * @brief initialises falldown object
     * @param ground_height @details the height of the ground
     * @param ground_width @details the width of the ground
     * @param ball_size @details the size of the ball
     * @param ground_speed @details 
     */
    void init(int ground_height, int ground_width, int ball_size, float ground_speed);
    /**
     * @brief reads input from gamepad
     * @param Gamepad &pad @details The gamepad library
     */
    void read_input(Gamepad &pad);
    /**
     * @brief updates the falldown object
     * @param Gamepad &pad @details The gamepad library
     */
    void update(Gamepad &pad);
    /**
     * @brief draws the falldown object
     * @param N5110 &lcd @details The library for the screen in which falldown is drawn on
     */
    void draw(N5110 &lcd);
    /**
     * @brief checks if the ball has reached the bottom of the screen
     * @returns 1 @details if statement is satisfied this will return a 1 subsequently causing the game to move the next level
     */
    int check_goal();
    /**
     * @brief checks if the ball has reached the top of the game map 
     * @returns 1 @details if the statement is satisfied this will return a 1 subsequently and display the game over screen
     */
    int check_end();
    /**
    * @brief checks if the goal has been reached
    * @returns 1 @details if statement is satisfied a 1 is returned and subsequently display the game complete screen
    */
    int check_complete();




private:

    void check_wall_collisions(Gamepad &pad);
    void check_Ground_collisions(Gamepad &pad);
    void check_sideground_collisions(Gamepad &pad);

    Ground _ground;
    Ball _ball;

    int _ground_width;
    int _ground_height;
    double _ground_speed;

    int _ball_size;
    int _groundy;

    int _bally;

    Direction _d;
    float _mag;

};

#endif