ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Thu Apr 25 15:08:52 2019 +0000
Revision:
3:aa82968b7a8e
Parent:
2:888634fff8ff
Child:
15:807eba7c7811
Completed Dodge game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll16o2l 2:888634fff8ff 1 #include "Objects.h"
ll16o2l 1:2d3139578aca 2
ll16o2l 1:2d3139578aca 3 // nothing doing in the constructor and destructor
ll16o2l 2:888634fff8ff 4 Objects::Objects()
ll16o2l 1:2d3139578aca 5 {
ll16o2l 1:2d3139578aca 6
ll16o2l 1:2d3139578aca 7 }
ll16o2l 1:2d3139578aca 8
ll16o2l 2:888634fff8ff 9 Objects::~Objects()
ll16o2l 1:2d3139578aca 10 {
ll16o2l 1:2d3139578aca 11
ll16o2l 1:2d3139578aca 12 }
ll16o2l 1:2d3139578aca 13
ll16o2l 3:aa82968b7a8e 14 /**
ll16o2l 3:aa82968b7a8e 15 * This method will be used to initialise the objects variables.
ll16o2l 3:aa82968b7a8e 16 * Saves global variables to local variables.
ll16o2l 3:aa82968b7a8e 17 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 18 * @param size, speed
ll16o2l 3:aa82968b7a8e 19 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 20 */
ll16o2l 2:888634fff8ff 21 void Objects::init(int size,int speed)
ll16o2l 1:2d3139578aca 22 {
ll16o2l 1:2d3139578aca 23 _size = size;
ll16o2l 1:2d3139578aca 24
ll16o2l 2:888634fff8ff 25 _x_edge = WIDTH - _size/2; // Edge of horizontal
ll16o2l 2:888634fff8ff 26 _y_edge = HEIGHT - _size/2; // Edge of vertical
ll16o2l 2:888634fff8ff 27
ll16o2l 2:888634fff8ff 28
ll16o2l 2:888634fff8ff 29 _x = rand() % _x_edge; // Generate random position on the screen - 0 to the edge
ll16o2l 2:888634fff8ff 30 _y = rand() % _y_edge; // Generate random position on the screen - 0 to the edge
ll16o2l 1:2d3139578aca 31
ll16o2l 1:2d3139578aca 32 srand(time(NULL));
ll16o2l 1:2d3139578aca 33 int direction = rand() % 4; // randomise initial direction.
ll16o2l 1:2d3139578aca 34
ll16o2l 1:2d3139578aca 35 // 4 possibilities. Get random modulo and set velocities accordingly
ll16o2l 1:2d3139578aca 36 if (direction == 0) {
ll16o2l 1:2d3139578aca 37 _velocity.x = speed;
ll16o2l 1:2d3139578aca 38 _velocity.y = speed;
ll16o2l 1:2d3139578aca 39 } else if (direction == 1) {
ll16o2l 1:2d3139578aca 40 _velocity.x = speed;
ll16o2l 1:2d3139578aca 41 _velocity.y = -speed;
ll16o2l 1:2d3139578aca 42 } else if (direction == 2) {
ll16o2l 1:2d3139578aca 43 _velocity.x = speed;
ll16o2l 1:2d3139578aca 44 _velocity.y = speed;
ll16o2l 1:2d3139578aca 45 } else {
ll16o2l 1:2d3139578aca 46 _velocity.x = -speed;
ll16o2l 1:2d3139578aca 47 _velocity.y = -speed;
ll16o2l 1:2d3139578aca 48 }
ll16o2l 1:2d3139578aca 49 }
ll16o2l 1:2d3139578aca 50
ll16o2l 3:aa82968b7a8e 51 /**
ll16o2l 3:aa82968b7a8e 52 * This method will be used to draw the objects to the LCD.
ll16o2l 3:aa82968b7a8e 53 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 54 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 55 */
ll16o2l 2:888634fff8ff 56 void Objects::draw(N5110 &lcd)
ll16o2l 1:2d3139578aca 57 {
ll16o2l 2:888634fff8ff 58 lcd.drawCircle(_x,_y,_size,FILL_BLACK); // x,y,radius,black fill
ll16o2l 1:2d3139578aca 59 }
ll16o2l 1:2d3139578aca 60
ll16o2l 3:aa82968b7a8e 61 /**
ll16o2l 3:aa82968b7a8e 62 * This method will be used to update the objects position.
ll16o2l 3:aa82968b7a8e 63 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 64 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 65 */
ll16o2l 2:888634fff8ff 66 void Objects::update()
ll16o2l 1:2d3139578aca 67 {
ll16o2l 1:2d3139578aca 68 _x += _velocity.x;
ll16o2l 1:2d3139578aca 69 _y += _velocity.y;
ll16o2l 1:2d3139578aca 70 }
ll16o2l 1:2d3139578aca 71
ll16o2l 3:aa82968b7a8e 72 /**
ll16o2l 3:aa82968b7a8e 73 * This method will be used to set the velocity of the objects.
ll16o2l 3:aa82968b7a8e 74 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 75 * @param v
ll16o2l 3:aa82968b7a8e 76 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 77 */
ll16o2l 2:888634fff8ff 78 void Objects::set_velocity(Vector2D v)
ll16o2l 1:2d3139578aca 79 {
ll16o2l 1:2d3139578aca 80 _velocity.x = v.x;
ll16o2l 1:2d3139578aca 81 _velocity.y = v.y;
ll16o2l 1:2d3139578aca 82 }
ll16o2l 1:2d3139578aca 83
ll16o2l 3:aa82968b7a8e 84 /**
ll16o2l 3:aa82968b7a8e 85 * This method will be used to return the velocity when called.
ll16o2l 3:aa82968b7a8e 86 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 87 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 88 */
ll16o2l 2:888634fff8ff 89 Vector2D Objects::get_velocity()
ll16o2l 1:2d3139578aca 90 {
ll16o2l 1:2d3139578aca 91 Vector2D v = {_velocity.x,_velocity.y};
ll16o2l 1:2d3139578aca 92 return v;
ll16o2l 1:2d3139578aca 93 }
ll16o2l 1:2d3139578aca 94
ll16o2l 3:aa82968b7a8e 95 /**
ll16o2l 3:aa82968b7a8e 96 * This method will be used to set the postion of the objects.
ll16o2l 3:aa82968b7a8e 97 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 98 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 99 */
ll16o2l 3:aa82968b7a8e 100 void Objects::set_pos(Vector2D p)
ll16o2l 3:aa82968b7a8e 101 {
ll16o2l 3:aa82968b7a8e 102 _x = p.x;
ll16o2l 3:aa82968b7a8e 103 _y = p.y;
ll16o2l 3:aa82968b7a8e 104 }
ll16o2l 3:aa82968b7a8e 105
ll16o2l 3:aa82968b7a8e 106
ll16o2l 3:aa82968b7a8e 107 /**
ll16o2l 3:aa82968b7a8e 108 * This method will be used to return the position when called.
ll16o2l 3:aa82968b7a8e 109 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 110 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 111 */
ll16o2l 2:888634fff8ff 112 Vector2D Objects::get_pos()
ll16o2l 1:2d3139578aca 113 {
ll16o2l 1:2d3139578aca 114 Vector2D p = {_x,_y};
ll16o2l 1:2d3139578aca 115 return p;
ll16o2l 1:2d3139578aca 116 }