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

Committer:
el15mh
Date:
Wed May 03 21:13:45 2017 +0000
Revision:
2:bcb96ab2848b
Parent:
1:ba8bb10ebd5a
Child:
3:569a3f2786ec
fully working program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 2:bcb96ab2848b 1 /*
el15mh 2:bcb96ab2848b 2
el15mh 2:bcb96ab2848b 3 @file Ball.cpp
el15mh 2:bcb96ab2848b 4
el15mh 2:bcb96ab2848b 5 (c) Max Houghton 02.14.17
el15mh 2:bcb96ab2848b 6 Roller Maze Project, ELEC2645, Univeristy of Leeds
el15mh 2:bcb96ab2848b 7
el15mh 2:bcb96ab2848b 8 */
el15mh 0:4c58ed26b6ea 9
el15mh 0:4c58ed26b6ea 10 #include "Ball.h"
el15mh 0:4c58ed26b6ea 11
el15mh 0:4c58ed26b6ea 12 Ball::Ball()
el15mh 0:4c58ed26b6ea 13 {
el15mh 0:4c58ed26b6ea 14
el15mh 0:4c58ed26b6ea 15 }
el15mh 0:4c58ed26b6ea 16
el15mh 0:4c58ed26b6ea 17 Ball::~Ball()
el15mh 0:4c58ed26b6ea 18 {
el15mh 0:4c58ed26b6ea 19
el15mh 0:4c58ed26b6ea 20 }
el15mh 0:4c58ed26b6ea 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 2:bcb96ab2848b 37 void Ball::update(Vector2D position)
el15mh 1:ba8bb10ebd5a 38 {
el15mh 2:bcb96ab2848b 39 velocity = position;
el15mh 2:bcb96ab2848b 40
el15mh 2:bcb96ab2848b 41 checkForInterference(velocity);
el15mh 2:bcb96ab2848b 42
el15mh 1:ba8bb10ebd5a 43 // new coordinates for the centre of the ball
el15mh 1:ba8bb10ebd5a 44 _x += velocity.x;
el15mh 2:bcb96ab2848b 45 _y -= velocity.y; // +ve y joystick motion => -ve ball motion
el15mh 2:bcb96ab2848b 46 }
el15mh 2:bcb96ab2848b 47
el15mh 2:bcb96ab2848b 48 // goes into main 'draw' function in engine
el15mh 2:bcb96ab2848b 49 // main draw function in engine then called by menu during 'playGame' function
el15mh 2:bcb96ab2848b 50 void Ball::draw(N5110 &lcd)
el15mh 2:bcb96ab2848b 51 {
el15mh 2:bcb96ab2848b 52 if (_colour){// true colour => outline
el15mh 2:bcb96ab2848b 53 lcd.drawCircle(_x, _y, _radius, FILL_TRANSPARENT);
el15mh 2:bcb96ab2848b 54 }
el15mh 2:bcb96ab2848b 55 else { // false colour => solid
el15mh 2:bcb96ab2848b 56 lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
el15mh 2:bcb96ab2848b 57 }
el15mh 1:ba8bb10ebd5a 58
el15mh 1:ba8bb10ebd5a 59 }
el15mh 0:4c58ed26b6ea 60
el15mh 2:bcb96ab2848b 61 void Ball::setVelocity(Vector2D v)
el15mh 2:bcb96ab2848b 62 {
el15mh 2:bcb96ab2848b 63 velocity.x = v.x;
el15mh 2:bcb96ab2848b 64 velocity.y = v.y;
el15mh 2:bcb96ab2848b 65 }
el15mh 2:bcb96ab2848b 66
el15mh 2:bcb96ab2848b 67 void Ball::setPosition(Vector2D p)
el15mh 2:bcb96ab2848b 68 {
el15mh 2:bcb96ab2848b 69 _x = p.x;
el15mh 2:bcb96ab2848b 70 _y = p.y;
el15mh 2:bcb96ab2848b 71 }
el15mh 2:bcb96ab2848b 72
el15mh 2:bcb96ab2848b 73 Vector2D Ball::getVelocity()
el15mh 2:bcb96ab2848b 74 {
el15mh 2:bcb96ab2848b 75 Vector2D _velocity = {velocity.x, velocity.y};
el15mh 2:bcb96ab2848b 76
el15mh 2:bcb96ab2848b 77 return _velocity;
el15mh 2:bcb96ab2848b 78 }
el15mh 2:bcb96ab2848b 79
el15mh 2:bcb96ab2848b 80 Vector2D Ball::getPosition()
el15mh 1:ba8bb10ebd5a 81 {
el15mh 2:bcb96ab2848b 82 Vector2D p = {_x,_y};
el15mh 2:bcb96ab2848b 83
el15mh 2:bcb96ab2848b 84 return p;
el15mh 2:bcb96ab2848b 85 }
el15mh 2:bcb96ab2848b 86
el15mh 2:bcb96ab2848b 87 Vector2D Ball::checkForInterference(Vector2D velocity)
el15mh 2:bcb96ab2848b 88 {
el15mh 2:bcb96ab2848b 89 // +ve x
el15mh 2:bcb96ab2848b 90 if ((velocity.x < 0.1f) && (velocity.x > 0.0f)){
el15mh 2:bcb96ab2848b 91 velocity.x = 0.0f;
el15mh 2:bcb96ab2848b 92 }
el15mh 2:bcb96ab2848b 93 // -ve x
el15mh 2:bcb96ab2848b 94 if ((velocity.x > -0.1f) && (velocity.x < 0.0f)){
el15mh 2:bcb96ab2848b 95 velocity.x = 0.0f;
el15mh 2:bcb96ab2848b 96 }
el15mh 2:bcb96ab2848b 97
el15mh 2:bcb96ab2848b 98 // +ve y
el15mh 2:bcb96ab2848b 99 if ((velocity.y < 0.1f) && (velocity.y > 0.0f)){
el15mh 2:bcb96ab2848b 100 velocity.y = 0.0f;
el15mh 2:bcb96ab2848b 101 }
el15mh 2:bcb96ab2848b 102 // -ve y
el15mh 2:bcb96ab2848b 103 if ((velocity.y > -0.1f) && (velocity.y < 0.0f)){
el15mh 2:bcb96ab2848b 104 velocity.y = 0.0f;
el15mh 2:bcb96ab2848b 105 }
el15mh 2:bcb96ab2848b 106
el15mh 2:bcb96ab2848b 107 return velocity;
el15mh 2:bcb96ab2848b 108 }