Harry Rance 200925395 Embedded Systems Project

Dependencies:   mbed

Committer:
harryrance
Date:
Wed May 03 16:24:01 2017 +0000
Revision:
6:dca8b5e2ebe5
Parent:
3:43970d8d642e
Full project complete with Doxygen commenting throughout; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harryrance 1:95d7dd44bb0d 1 #include "Bullet.h"
harryrance 6:dca8b5e2ebe5 2 //Constructor
harryrance 1:95d7dd44bb0d 3 Bullet::Bullet()
harryrance 1:95d7dd44bb0d 4 {
harryrance 1:95d7dd44bb0d 5
harryrance 1:95d7dd44bb0d 6 }
harryrance 6:dca8b5e2ebe5 7 //Destructor
harryrance 1:95d7dd44bb0d 8 Bullet::~Bullet()
harryrance 1:95d7dd44bb0d 9 {
harryrance 1:95d7dd44bb0d 10
harryrance 1:95d7dd44bb0d 11 }
harryrance 6:dca8b5e2ebe5 12 //Initialise function - initialises all starting game parameters defined in the header file.
harryrance 3:43970d8d642e 13 void Bullet::initialise(int x_origin, int y_origin, int speed, int button_check, int coins, int score)
harryrance 1:95d7dd44bb0d 14 {
harryrance 1:95d7dd44bb0d 15 _x = x_origin;
harryrance 1:95d7dd44bb0d 16 _y = y_origin;
harryrance 1:95d7dd44bb0d 17 _button_check = button_check;
harryrance 1:95d7dd44bb0d 18 _speed = speed;
harryrance 3:43970d8d642e 19 _coins = coins;
harryrance 3:43970d8d642e 20 _score = score;
harryrance 1:95d7dd44bb0d 21
harryrance 1:95d7dd44bb0d 22 }
harryrance 6:dca8b5e2ebe5 23 //Checks if the A button has been pressed. If so, change the _button_check and _speed variables.
harryrance 1:95d7dd44bb0d 24 void Bullet::check_button_press(Gamepad &pad)
harryrance 1:95d7dd44bb0d 25 {
harryrance 1:95d7dd44bb0d 26 if (pad.check_event(Gamepad::A_PRESSED)){
harryrance 1:95d7dd44bb0d 27 _button_check = 1;
harryrance 2:50feb42b982c 28 _speed = 2;
harryrance 1:95d7dd44bb0d 29 }
harryrance 1:95d7dd44bb0d 30 }
harryrance 6:dca8b5e2ebe5 31 //Draw function - draws the bullet on the screen
harryrance 1:95d7dd44bb0d 32 void Bullet::draw(N5110 &lcd)
harryrance 1:95d7dd44bb0d 33 {
harryrance 1:95d7dd44bb0d 34 _y_origin = _y;
harryrance 1:95d7dd44bb0d 35 _x_origin = _x;
harryrance 1:95d7dd44bb0d 36
harryrance 6:dca8b5e2ebe5 37 if (_button_check){ //draw bullet only if the A buttonhas been pressed
harryrance 1:95d7dd44bb0d 38 lcd.setPixel(_x_origin,_y_origin);
harryrance 1:95d7dd44bb0d 39 lcd.setPixel(_x_origin,_y_origin-1);
harryrance 1:95d7dd44bb0d 40 }
harryrance 6:dca8b5e2ebe5 41 if (_y < 10){ //resets the bullet if it moves above a certain point on the screen to its original y position.
harryrance 6:dca8b5e2ebe5 42 _button_check = 0; //also reset the button check variable in order to allow another bullet to be fired.
harryrance 6:dca8b5e2ebe5 43 _speed = 0; //sets the speed to 0 so that the velocity does not update and bullet can be redrawn at the origin.
harryrance 6:dca8b5e2ebe5 44 _y = 42; //y origin
harryrance 2:50feb42b982c 45 }
harryrance 1:95d7dd44bb0d 46 }
harryrance 6:dca8b5e2ebe5 47 //Update function - updates the velocity and direction of movement for the bullet.
harryrance 1:95d7dd44bb0d 48 void Bullet::update()
harryrance 1:95d7dd44bb0d 49 {
harryrance 1:95d7dd44bb0d 50 _x += _velocity.x;
harryrance 1:95d7dd44bb0d 51 _y -= _velocity.y;
harryrance 1:95d7dd44bb0d 52
harryrance 1:95d7dd44bb0d 53 int direction = 0;
harryrance 1:95d7dd44bb0d 54
harryrance 1:95d7dd44bb0d 55 if(direction == 0)
harryrance 1:95d7dd44bb0d 56 {
harryrance 1:95d7dd44bb0d 57 _velocity.y = _speed;
harryrance 1:95d7dd44bb0d 58 }
harryrance 1:95d7dd44bb0d 59 }
harryrance 6:dca8b5e2ebe5 60 //sets the x and y velocity of the bullet.
harryrance 1:95d7dd44bb0d 61 void Bullet::set_velocity(Vector2D v)
harryrance 1:95d7dd44bb0d 62 {
harryrance 1:95d7dd44bb0d 63 _velocity.x = v.x;
harryrance 1:95d7dd44bb0d 64 _velocity.y = v.y;
harryrance 1:95d7dd44bb0d 65 }
harryrance 6:dca8b5e2ebe5 66 //sets the x and y position of the bullet.
harryrance 1:95d7dd44bb0d 67 void Bullet::set_position(Vector2D p)
harryrance 1:95d7dd44bb0d 68 {
harryrance 1:95d7dd44bb0d 69 _x = p.x;
harryrance 1:95d7dd44bb0d 70 _y = p.y;
harryrance 1:95d7dd44bb0d 71 }
harryrance 6:dca8b5e2ebe5 72 //returns a struct containing x and y members of the bullet velocity.
harryrance 1:95d7dd44bb0d 73 Vector2D Bullet::get_velocity()
harryrance 1:95d7dd44bb0d 74 {
harryrance 1:95d7dd44bb0d 75 Vector2D v = {_velocity.x,_velocity.y};
harryrance 1:95d7dd44bb0d 76
harryrance 1:95d7dd44bb0d 77 return v;
harryrance 1:95d7dd44bb0d 78 }
harryrance 6:dca8b5e2ebe5 79 //returns a struct containing x and y members of the bullet position.
harryrance 1:95d7dd44bb0d 80 Vector2D Bullet::get_position()
harryrance 1:95d7dd44bb0d 81 {
harryrance 1:95d7dd44bb0d 82 Vector2D p = {_x,_y};
harryrance 1:95d7dd44bb0d 83
harryrance 1:95d7dd44bb0d 84 return p;
harryrance 1:95d7dd44bb0d 85 }
harryrance 6:dca8b5e2ebe5 86 //increments the score variable.
harryrance 2:50feb42b982c 87 void Bullet::add_score()
harryrance 2:50feb42b982c 88 {
harryrance 2:50feb42b982c 89 _score++;
harryrance 2:50feb42b982c 90 }
harryrance 6:dca8b5e2ebe5 91 //returns the score variable integer.
harryrance 2:50feb42b982c 92 int Bullet::get_score()
harryrance 2:50feb42b982c 93 {
harryrance 2:50feb42b982c 94 return _score;
harryrance 2:50feb42b982c 95 }
harryrance 6:dca8b5e2ebe5 96 //adds 10 to the coins integer variable.
harryrance 2:50feb42b982c 97 void Bullet::add_coins()
harryrance 2:50feb42b982c 98 {
harryrance 2:50feb42b982c 99 _coins += 10;
harryrance 2:50feb42b982c 100 }
harryrance 6:dca8b5e2ebe5 101 //decrements the coins variable by 100 for when a life is purchased.
harryrance 3:43970d8d642e 102 void Bullet::dec_coins_for_life()
harryrance 3:43970d8d642e 103 {
harryrance 3:43970d8d642e 104 _coins -= 100;
harryrance 3:43970d8d642e 105 }
harryrance 6:dca8b5e2ebe5 106 //decrements the coins variable by 50 if a ship is purchased.
harryrance 3:43970d8d642e 107 void Bullet::dec_coins_for_x()
harryrance 3:43970d8d642e 108 {
harryrance 3:43970d8d642e 109 _coins -= 50;
harryrance 3:43970d8d642e 110 }
harryrance 6:dca8b5e2ebe5 111 //decrements the coins variable by 100 if a ship is purchased.
harryrance 3:43970d8d642e 112 void Bullet::dec_coins_for_y()
harryrance 3:43970d8d642e 113 {
harryrance 3:43970d8d642e 114 _coins -= 100;
harryrance 3:43970d8d642e 115 }
harryrance 6:dca8b5e2ebe5 116 //decrements the coins variable by 150 if a ship is purchased.
harryrance 3:43970d8d642e 117 void Bullet::dec_coins_for_b()
harryrance 3:43970d8d642e 118 {
harryrance 3:43970d8d642e 119 _coins -= 150;
harryrance 3:43970d8d642e 120 }
harryrance 6:dca8b5e2ebe5 121 //decrements the coins variable by 200 if a ship is purchased.
harryrance 3:43970d8d642e 122 void Bullet::dec_coins_for_r()
harryrance 3:43970d8d642e 123 {
harryrance 3:43970d8d642e 124 _coins -= 200;
harryrance 3:43970d8d642e 125 }
harryrance 6:dca8b5e2ebe5 126 //returns the coins integer variable to display user balance on the screen.
harryrance 2:50feb42b982c 127 int Bullet::get_coins()
harryrance 2:50feb42b982c 128 {
harryrance 2:50feb42b982c 129 return _coins;
harryrance 2:50feb42b982c 130 }
harryrance 1:95d7dd44bb0d 131