ELEC2645 (2018/19) / Mbed 2 deprecated henririgby98

Dependencies:   mbed

SpaceInvader/SpaceInvader.h

Committer:
henririgby98
Date:
2019-05-09
Revision:
16:3cb5c59ae7e8
Parent:
5:0da65740cd5e
Child:
20:477d2ee5e461

File content as of revision 16:3cb5c59ae7e8:

#ifndef SPACEINVADER_H
#define SPACEINVADER_H

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

/** SpaceInvader class

@brief Class for Spaceinvader

@version 1.0

@author Henri Rigby

@date May 2019

@code

#include "SpaceInvader.h"
SpaceInvader::SpaceInvader()
{

}

SpaceInvader::~SpaceInvader()
{

}
    
void SpaceInvader::init(int height,int width)
{
    _x = WIDTH/2 - width/2 - 2;  //sets x position to be in centre of lcd display
    _y = HEIGHT/2 - height/2 + 1;  //sets y position to be in centre of lcd display
    _height = height;
    _width = width;
    _speed = 1; //sets _speed to equal 1
}

void SpaceInvader::draw(N5110 &lcd)
{
    int invader[8][11] =   {  //sprite for spaceinvader
    { 0,0,1,0,0,0,0,0,1,0,0 },
    { 0,0,0,1,0,0,0,1,0,0,0 },
    { 0,0,1,1,1,1,1,1,1,0,0 },
    { 0,1,1,0,1,1,1,0,1,1,0 },
    { 1,1,1,1,1,1,1,1,1,1,1 },
    { 1,0,1,1,1,1,1,1,1,0,1 },
    { 1,0,1,0,0,0,0,0,1,0,1 },
    { 0,0,0,1,1,0,1,1,0,0,0 },
};
    lcd.drawSprite(_x,_y,_width,_height,(int *)invader);  //draws spaceinvader sprite
}

void SpaceInvader::update(Direction d,float mag)
{
    _speed = int(mag*7.0f);  //sets _speed to equal 7 * the magnitude of joystick 
    if (d == N) {
        _y-=_speed;  //moves spaceinvader up
    } else if (d == S) {
        _y+=_speed;  //moves spaceinvader down
    } else if (d == W) {
        _x-=_speed;  //moves spaceinvader left
    } else if (d == E) {
        _x+=_speed;  //moves spaceinvader right
    } else if (d == NW) {
        _y-=((2*_speed)^1/2)/2;  //equation prevents spaceinvader strafing when moving diagonally
        _x-=((2*_speed)^1/2)/2;  //moves spaceinvader up and to the left
    } else if (d == SW) {
        _y+=((2*_speed)^1/2)/2;
        _x-=((2*_speed)^1/2)/2;  //moves spaceinvader down and to the left
    } else if (d == NE) {
        _y-=((2*_speed)^1/2)/2;
        _x+=((2*_speed)^1/2)/2;  //moves spaceinvader up and to the right
    } else if (d == SE) {
        _y+=((2*_speed)^1/2)/2;
        _x+=((2*_speed)^1/2)/2;  //moves spaceinvader down and to the right
    }
    if (_x < 1) {
        _x = 1;  //prevents spaceinvader going offscreen to the left
    }
    if (_x > WIDTH - _width - 4) {
        _x = WIDTH - _width - 4;  //prevents spaceinvader going offscreen to the right
    }
    if (_y < 1) {
        _y = 1;  //prevents spaceinvader going offscreen at the top
    }
    if (_y > HEIGHT - _height + 2) {
        _y = HEIGHT - _height + 2;  //prevents spaceinvader going offscreen at the bottom
    }
}

Vector2D SpaceInvader::get_pos() {
    Vector2D p = {_x,_y};  //sets the value of position by taking both x and y coordinates
    return p;    
}

@endcode
*/

class SpaceInvader
{
public:

    SpaceInvader();
    ~SpaceInvader();
     /** 
    * @brief Sets spaceinvader starting position and speed
    * @param sets position and speed @details Sets starting position and speed of the spaceinvader
    */
    void init(int height,int width);
    /** 
    * @brief Draw the spaceinvader 
    * @return draws spaceinvader @details Uses designed sprite to draw a spaceinvader
    */
    void draw(N5110 &lcd);
    /** 
    * @brief Updates the position of the spaceinvader
    * @param sets position @details Uses movement of joystick to set position of spaceinvader
    */
    void update(Direction d,float mag);
    /** 
    * @brief Gets the position
    * @return position @details returns the position of both the x and y coordinates
    */
    Vector2D get_pos();

private:

    int _height;
    int _width;
    int _x;
    int _y;
    int _speed;

};
#endif