Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

My_game_clases/Objects.cpp

Committer:
thestudent
Date:
2020-04-18
Revision:
10:f5b920a6a71a
Parent:
9:4b11ee1155ad
Child:
11:4722bf70b2be

File content as of revision 10:f5b920a6a71a:

//includes
#include "Objects.h"
#include "Gamepad.h"
#include "N5110.h"
#include "mbed.h"

int _cannon[6][6] = {
    { 0,0,1,1,0,0 },
    { 0,0,1,1,0,0 },
    { 0,1,1,1,1,0 },
    { 0,1,1,1,1,0 },
    { 1,1,1,1,1,1 },
    { 1,1,1,1,1,1 },
};

Objects::Objects()
{
    _cannon_pos = 0; //sets the cannon postion to 0
    _initial_shot_pos = 37;// sets the vertical position of the shot which is one above the cannon
    _shot_incrementer = 1; // default shot incrementer in y direction
    _shot_y_pos.push_back(_initial_shot_pos); // creates the first shot(was needed here for shot function)
    _shot_x_pos.push_back(_cannon_pos + 2);
    _radiuss = 2; // sets the initial ball radiuss
};

void Objects::draw_base(N5110 &lcd)
{
    lcd.drawRect(0,46,84,2,FILL_BLACK);
};
void Objects::cannon_position(Gamepad &pad)
{
    //reads the sensor
    float joy_mag = pad.get_mag();
    float joy_angle = pad.get_angle();
    // checks the joystics position and determines the speed of cannon movement
    if(joy_mag < 0.5 && _cannon_pos > 1 && joy_angle > 180.0) {
        _cannon_pos += -1;
    } else if(joy_mag < 1.01 && _cannon_pos > 0 && joy_angle > 180.0) {
        _cannon_pos += -2;
    } else if(joy_mag < 0.5 && _cannon_pos < 78 && joy_angle > 0.0) {
        _cannon_pos += 1;
    } else if (joy_mag <= 1.01 && _cannon_pos < 77 && joy_angle > 0.0) {
        _cannon_pos += 2;
    } else {
        _cannon_pos = _cannon_pos;
    }
}
void Objects::draw_cannon(N5110 &lcd)
{

    lcd.drawSprite(_cannon_pos,40,6,6,(int *)_cannon);

}
void Objects::draw_shots(N5110 &lcd)
{

    for (int i = 0; i < _shot_y_pos.size(); i++) {
        lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK);
       // printf( " %d || %d \n",_shot_x_pos.size(),_shot_y_pos.size());
        _shot_y_pos[i] -= _shot_incrementer; // moves the shots upwards
    }
    
    //adds another shot if the distance between
    //y pos of the initial pos and previous shot pos is more than 7
    if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos || _shot_y_pos.size() < 1) {
        _shot_y_pos.push_back(_initial_shot_pos);
        _shot_x_pos.push_back(_cannon_pos + 2);
    }
    //erasing a shot if the y position is below 0
    if(_shot_y_pos[0] <= 0) {
        _shot_y_pos.erase(_shot_y_pos.begin());
        _shot_x_pos.erase(_shot_x_pos.begin());
    }
   
}
void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r)
{   
    lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK);
    //lcd.setPixel( ball_x,  ball_y,0);
}

int Objects::get_size(){
 return _shot_y_pos.size();
};

int Objects::get_x_value(int i){
    return _shot_x_pos[i];
}

int Objects::get_y_value(int i){
    return _shot_y_pos[i];
}
void Objects::erase_shot(int i){
     _shot_y_pos.erase(_shot_y_pos.begin() + i);
     _shot_x_pos.erase(_shot_x_pos.begin() + i);
}
int Objects::get_x_cannon(){
    return _cannon_pos;
}