Class containing methods to draw a ball within the maze game with the specified position, size and fill style parameters.

Committer:
el15mh
Date:
Thu May 04 14:01:56 2017 +0000
Revision:
3:569a3f2786ec
Parent:
2:bcb96ab2848b
Doxygen commenting added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 0:4c58ed26b6ea 1 #include "Ball.h"
el15mh 0:4c58ed26b6ea 2
el15mh 3:569a3f2786ec 3 /**
el15mh 3:569a3f2786ec 4 * @details - constructor
el15mh 3:569a3f2786ec 5 */
el15mh 0:4c58ed26b6ea 6 Ball::Ball()
el15mh 0:4c58ed26b6ea 7 {
el15mh 0:4c58ed26b6ea 8
el15mh 0:4c58ed26b6ea 9 }
el15mh 0:4c58ed26b6ea 10
el15mh 3:569a3f2786ec 11 /**
el15mh 3:569a3f2786ec 12 * @details - destructor
el15mh 3:569a3f2786ec 13 */
el15mh 0:4c58ed26b6ea 14 Ball::~Ball()
el15mh 0:4c58ed26b6ea 15 {
el15mh 0:4c58ed26b6ea 16
el15mh 0:4c58ed26b6ea 17 }
el15mh 0:4c58ed26b6ea 18
el15mh 3:569a3f2786ec 19 /**
el15mh 3:569a3f2786ec 20 * @details - Initiases the ball.
el15mh 3:569a3f2786ec 21 */
el15mh 2:bcb96ab2848b 22 void Ball::init(float x, // x coordinate for centre
el15mh 2:bcb96ab2848b 23 float y, // y coordinate for centre
el15mh 2:bcb96ab2848b 24 int radius, // radius size
el15mh 2:bcb96ab2848b 25 bool colour) // type of ball
el15mh 1:ba8bb10ebd5a 26 {
el15mh 1:ba8bb10ebd5a 27 // properties of ball are set with values passed down from engine
el15mh 1:ba8bb10ebd5a 28 _x = x;
el15mh 1:ba8bb10ebd5a 29 _y = y;
el15mh 1:ba8bb10ebd5a 30 _radius = radius;
el15mh 2:bcb96ab2848b 31 _colour = colour;
el15mh 0:4c58ed26b6ea 32
el15mh 1:ba8bb10ebd5a 33 // printf("INPUT: x = %i, y = %i, radius = %i \n", x, y, radius);
el15mh 1:ba8bb10ebd5a 34 // printf("DRAW FUNCTION: x = %f, y = %f, radius = %f \n", _x, _y, _radius);
el15mh 0:4c58ed26b6ea 35 }
el15mh 0:4c58ed26b6ea 36
el15mh 3:569a3f2786ec 37 /**
el15mh 3:569a3f2786ec 38 * @details - Updates position of the ball
el15mh 3:569a3f2786ec 39 */
el15mh 2:bcb96ab2848b 40 void Ball::update(Vector2D position)
el15mh 1:ba8bb10ebd5a 41 {
el15mh 2:bcb96ab2848b 42 velocity = position;
el15mh 2:bcb96ab2848b 43
el15mh 3:569a3f2786ec 44 // smooths out small signal fluctuations from accelerometer
el15mh 3:569a3f2786ec 45 // used by joystick output also but with neglible effect
el15mh 2:bcb96ab2848b 46 checkForInterference(velocity);
el15mh 2:bcb96ab2848b 47
el15mh 1:ba8bb10ebd5a 48 // new coordinates for the centre of the ball
el15mh 1:ba8bb10ebd5a 49 _x += velocity.x;
el15mh 2:bcb96ab2848b 50 _y -= velocity.y; // +ve y joystick motion => -ve ball motion
el15mh 2:bcb96ab2848b 51 }
el15mh 2:bcb96ab2848b 52
el15mh 3:569a3f2786ec 53 /**
el15mh 3:569a3f2786ec 54 * @details - Function draws the ball
el15mh 3:569a3f2786ec 55 */
el15mh 2:bcb96ab2848b 56 void Ball::draw(N5110 &lcd)
el15mh 2:bcb96ab2848b 57 {
el15mh 3:569a3f2786ec 58 // goes into main 'draw' function in engine
el15mh 3:569a3f2786ec 59 // main draw function in engine then called by menu during 'playGame' function
el15mh 2:bcb96ab2848b 60 if (_colour){// true colour => outline
el15mh 2:bcb96ab2848b 61 lcd.drawCircle(_x, _y, _radius, FILL_TRANSPARENT);
el15mh 2:bcb96ab2848b 62 }
el15mh 2:bcb96ab2848b 63 else { // false colour => solid
el15mh 2:bcb96ab2848b 64 lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
el15mh 2:bcb96ab2848b 65 }
el15mh 1:ba8bb10ebd5a 66
el15mh 1:ba8bb10ebd5a 67 }
el15mh 0:4c58ed26b6ea 68
el15mh 3:569a3f2786ec 69 /**
el15mh 3:569a3f2786ec 70 * @details - Sets the velocity of the ball
el15mh 3:569a3f2786ec 71 */
el15mh 2:bcb96ab2848b 72 void Ball::setVelocity(Vector2D v)
el15mh 2:bcb96ab2848b 73 {
el15mh 2:bcb96ab2848b 74 velocity.x = v.x;
el15mh 2:bcb96ab2848b 75 velocity.y = v.y;
el15mh 2:bcb96ab2848b 76 }
el15mh 2:bcb96ab2848b 77
el15mh 3:569a3f2786ec 78 /**
el15mh 3:569a3f2786ec 79 * @details - Sets the position of the ball
el15mh 3:569a3f2786ec 80 */
el15mh 2:bcb96ab2848b 81 void Ball::setPosition(Vector2D p)
el15mh 2:bcb96ab2848b 82 {
el15mh 2:bcb96ab2848b 83 _x = p.x;
el15mh 2:bcb96ab2848b 84 _y = p.y;
el15mh 2:bcb96ab2848b 85 }
el15mh 2:bcb96ab2848b 86
el15mh 3:569a3f2786ec 87 /**
el15mh 3:569a3f2786ec 88 * @details - Returns a struct containing current ball velocity information
el15mh 3:569a3f2786ec 89 */
el15mh 2:bcb96ab2848b 90 Vector2D Ball::getVelocity()
el15mh 2:bcb96ab2848b 91 {
el15mh 2:bcb96ab2848b 92 Vector2D _velocity = {velocity.x, velocity.y};
el15mh 2:bcb96ab2848b 93
el15mh 2:bcb96ab2848b 94 return _velocity;
el15mh 2:bcb96ab2848b 95 }
el15mh 2:bcb96ab2848b 96
el15mh 3:569a3f2786ec 97 /**
el15mh 3:569a3f2786ec 98 * @details - Returns a struct containing current ball position information
el15mh 3:569a3f2786ec 99 */
el15mh 2:bcb96ab2848b 100 Vector2D Ball::getPosition()
el15mh 1:ba8bb10ebd5a 101 {
el15mh 2:bcb96ab2848b 102 Vector2D p = {_x,_y};
el15mh 2:bcb96ab2848b 103
el15mh 2:bcb96ab2848b 104 return p;
el15mh 2:bcb96ab2848b 105 }
el15mh 2:bcb96ab2848b 106
el15mh 3:569a3f2786ec 107 /**
el15mh 3:569a3f2786ec 108 * @details - Smooths out data from accelerometer and returns values in struct
el15mh 3:569a3f2786ec 109 */
el15mh 2:bcb96ab2848b 110 Vector2D Ball::checkForInterference(Vector2D velocity)
el15mh 2:bcb96ab2848b 111 {
el15mh 2:bcb96ab2848b 112 // +ve x
el15mh 2:bcb96ab2848b 113 if ((velocity.x < 0.1f) && (velocity.x > 0.0f)){
el15mh 2:bcb96ab2848b 114 velocity.x = 0.0f;
el15mh 2:bcb96ab2848b 115 }
el15mh 2:bcb96ab2848b 116 // -ve x
el15mh 2:bcb96ab2848b 117 if ((velocity.x > -0.1f) && (velocity.x < 0.0f)){
el15mh 2:bcb96ab2848b 118 velocity.x = 0.0f;
el15mh 2:bcb96ab2848b 119 }
el15mh 2:bcb96ab2848b 120
el15mh 2:bcb96ab2848b 121 // +ve y
el15mh 2:bcb96ab2848b 122 if ((velocity.y < 0.1f) && (velocity.y > 0.0f)){
el15mh 2:bcb96ab2848b 123 velocity.y = 0.0f;
el15mh 2:bcb96ab2848b 124 }
el15mh 2:bcb96ab2848b 125 // -ve y
el15mh 2:bcb96ab2848b 126 if ((velocity.y > -0.1f) && (velocity.y < 0.0f)){
el15mh 2:bcb96ab2848b 127 velocity.y = 0.0f;
el15mh 2:bcb96ab2848b 128 }
el15mh 3:569a3f2786ec 129
el15mh 2:bcb96ab2848b 130 return velocity;
el15mh 2:bcb96ab2848b 131 }