Prints objects on to a Nokia N5110 LCD display and makes them 'fall' down the display.

Dependents:   Game_Controller_Project

Objects.cpp

Committer:
Nathanj94
Date:
2017-05-04
Revision:
15:bb213a2e282b
Parent:
13:f5e96e0c6a2e

File content as of revision 15:bb213a2e282b:

#include "Objects.h"

Objects::Objects()
{
    
}

Objects::~Objects()
{
    
}

//INITILISATION FUNCTION//
//Sets x and y entry points, sets speed and sets the shape of the object
void Objects::init(int speed)
{
    undraw_var = 0; //initialise variable that determines which shape to undraw (0 is unused in-game)
    objects_speed = speed;
    y_ref = 8; //leave Bank 0 clear to write #lives and score
    
    srand(time(NULL));
    objects_ep = rand() % 7;
    if(objects_ep == 0) { //randomly select one of these entry points
        x_ref = 3;
    } else if (objects_ep == 1) {
        x_ref = 15;
    } else if (objects_ep == 2) {
        x_ref = 27;
    } else if (objects_ep == 3) {
        x_ref = 39;
    } else if (objects_ep == 4) {
        x_ref = 51;
    } else if (objects_ep == 5) {
        x_ref = 63;
    } else {
        x_ref = 75;
    }
    
    objects_shape = rand() % 5; //randomly select the shape of the object
    //printf("object init\n");
}


//UPDATE FUNCTIONS//

//Move the object down the screen on each loop
void Objects::move(int speed)
{
    int objects_speed = speed; //speed just an int value corresponding to number of pixels y_ref changes by each loop
    y_ref += objects_speed;
}

//Return the value of the variable that corresponds to which score function should be called from Basket
int Objects::get_score_var()
{
    return score_var;
}


//DISPLAY FUNCTIONS//

//Draw object on the screen buffer
void Objects::draw(N5110 &lcd) 
{
    if(objects_shape == 1) { //this is the variable set in Objects::init(int speed)
        fruit.draw_strawberry(x_ref,y_ref,lcd); //call this draw function from Fruit...
        undraw_var = 1; //...set a value to this variable so the same objects in undrawn...
        score_var = 1; //...set a value to this variable so the correct score is added if this object is caught.
    } else if (objects_shape == 2) {
        fruit.draw_pineapple(x_ref,y_ref,lcd);
        undraw_var = 2;
        score_var = 2;
    } else if (objects_shape == 3) {
        fruit.draw_pear(x_ref,y_ref,lcd);
        undraw_var = 3;
        score_var = 3;
    } else if (objects_shape == 4) {
        fruit.draw_melon(x_ref,y_ref,lcd);
        undraw_var = 4;
        score_var = 4;
    } else {
        fruit.draw_antifruit(x_ref,y_ref,lcd);
        undraw_var = 5;
        score_var = 5;
    }
}

//Clear object from the screen buffer
void Objects::undraw(N5110 &lcd)
{
    if(undraw_var == 1) { //this is one of the variables set in Objects::draw(N5110 &lcd)
        fruit.undraw_strawberry(x_ref,y_ref,lcd); //this ensures that the object on-screen will be removed correctly
    } else if (undraw_var == 2) {
        fruit.undraw_pineapple(x_ref,y_ref,lcd);
    } else if (undraw_var == 3) {
        fruit.undraw_pear(x_ref,y_ref,lcd);
    } else if (undraw_var == 4) {
        fruit.undraw_melon(x_ref,y_ref,lcd);
    } else {
        fruit.undraw_antifruit(x_ref,y_ref,lcd);
    }
}

int Objects::get_undraw_var()
{
    return undraw_var;
}

int Objects::get_x()
{
    return x_ref;
}

int Objects::get_y()
{
    return y_ref;
}