Class used to run the maze game loop.
MazeEngine.cpp@1:5a44ce88c5e2, 2017-04-17 (annotated)
- Committer:
- el15mh
- Date:
- Mon Apr 17 08:47:00 2017 +0000
- Revision:
- 1:5a44ce88c5e2
- Parent:
- 0:afee1085c5ef
- Child:
- 2:cbce5d35e7d6
project with wall collision function (not working);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15mh | 0:afee1085c5ef | 1 | /* |
el15mh | 0:afee1085c5ef | 2 | |
el15mh | 0:afee1085c5ef | 3 | @file mazeEngine.cpp |
el15mh | 0:afee1085c5ef | 4 | |
el15mh | 0:afee1085c5ef | 5 | (c) Max Houghton 19.03.2017 |
el15mh | 0:afee1085c5ef | 6 | Roller Maze Project, ELEC2645, Univeristy of Leeds |
el15mh | 0:afee1085c5ef | 7 | |
el15mh | 0:afee1085c5ef | 8 | */ |
el15mh | 0:afee1085c5ef | 9 | |
el15mh | 0:afee1085c5ef | 10 | #include "MazeEngine.h" |
el15mh | 0:afee1085c5ef | 11 | |
el15mh | 0:afee1085c5ef | 12 | MazeEngine::MazeEngine() |
el15mh | 0:afee1085c5ef | 13 | { |
el15mh | 0:afee1085c5ef | 14 | |
el15mh | 0:afee1085c5ef | 15 | } |
el15mh | 0:afee1085c5ef | 16 | |
el15mh | 0:afee1085c5ef | 17 | MazeEngine::~MazeEngine() |
el15mh | 0:afee1085c5ef | 18 | { |
el15mh | 0:afee1085c5ef | 19 | |
el15mh | 0:afee1085c5ef | 20 | } |
el15mh | 0:afee1085c5ef | 21 | |
el15mh | 1:5a44ce88c5e2 | 22 | void MazeEngine::init(int mazeIndex, |
el15mh | 1:5a44ce88c5e2 | 23 | int x, |
el15mh | 1:5a44ce88c5e2 | 24 | int y, |
el15mh | 1:5a44ce88c5e2 | 25 | int radius, |
el15mh | 1:5a44ce88c5e2 | 26 | bool control, |
el15mh | 1:5a44ce88c5e2 | 27 | bool colour) |
el15mh | 0:afee1085c5ef | 28 | { |
el15mh | 0:afee1085c5ef | 29 | // maze parameters |
el15mh | 0:afee1085c5ef | 30 | _mazeIndex = mazeIndex; |
el15mh | 0:afee1085c5ef | 31 | |
el15mh | 1:5a44ce88c5e2 | 32 | // method of controlling |
el15mh | 1:5a44ce88c5e2 | 33 | _control = control; |
el15mh | 1:5a44ce88c5e2 | 34 | // _control = false; // set as false just for debugging |
el15mh | 1:5a44ce88c5e2 | 35 | |
el15mh | 1:5a44ce88c5e2 | 36 | // style of ball |
el15mh | 1:5a44ce88c5e2 | 37 | _colour = colour; |
el15mh | 1:5a44ce88c5e2 | 38 | |
el15mh | 0:afee1085c5ef | 39 | // ball parameters |
el15mh | 0:afee1085c5ef | 40 | _radius = radius; |
el15mh | 0:afee1085c5ef | 41 | _x = x; |
el15mh | 0:afee1085c5ef | 42 | _y = y; |
el15mh | 0:afee1085c5ef | 43 | |
el15mh | 0:afee1085c5ef | 44 | // initialise ball & maze with parameters |
el15mh | 1:5a44ce88c5e2 | 45 | _ball.init(_x, _y, _radius, _colour); |
el15mh | 0:afee1085c5ef | 46 | _maze.init(_mazeIndex); |
el15mh | 0:afee1085c5ef | 47 | } |
el15mh | 0:afee1085c5ef | 48 | |
el15mh | 0:afee1085c5ef | 49 | void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device) |
el15mh | 0:afee1085c5ef | 50 | { |
el15mh | 1:5a44ce88c5e2 | 51 | if (_control){ // if control is true (default), input comes from joystick |
el15mh | 1:5a44ce88c5e2 | 52 | readJoystickInput(pad); |
el15mh | 1:5a44ce88c5e2 | 53 | } |
el15mh | 1:5a44ce88c5e2 | 54 | else { // if control is false, input comes from accelerometer |
el15mh | 1:5a44ce88c5e2 | 55 | readAccelerometer(device); |
el15mh | 1:5a44ce88c5e2 | 56 | } |
el15mh | 1:5a44ce88c5e2 | 57 | } |
el15mh | 1:5a44ce88c5e2 | 58 | |
el15mh | 1:5a44ce88c5e2 | 59 | void MazeEngine::readJoystickInput(Gamepad &pad) |
el15mh | 1:5a44ce88c5e2 | 60 | { |
el15mh | 1:5a44ce88c5e2 | 61 | // position is a 2D struct for (x,y) |
el15mh | 1:5a44ce88c5e2 | 62 | position = pad.get_mapped_coord(); |
el15mh | 1:5a44ce88c5e2 | 63 | |
el15mh | 1:5a44ce88c5e2 | 64 | // printf("Joystick inputs: %.2f, %.2f \n", position.x, position.y); |
el15mh | 1:5a44ce88c5e2 | 65 | } |
el15mh | 1:5a44ce88c5e2 | 66 | |
el15mh | 1:5a44ce88c5e2 | 67 | void MazeEngine::readAccelerometer(FXOS8700CQ &device) |
el15mh | 1:5a44ce88c5e2 | 68 | { |
el15mh | 1:5a44ce88c5e2 | 69 | // acquire values from device |
el15mh | 1:5a44ce88c5e2 | 70 | Data values = device.get_values(); |
el15mh | 1:5a44ce88c5e2 | 71 | |
el15mh | 1:5a44ce88c5e2 | 72 | // values are between 0 and 90° |
el15mh | 1:5a44ce88c5e2 | 73 | float pitch = device.getPitchAngle(); |
el15mh | 1:5a44ce88c5e2 | 74 | float roll = device.getRollAngle(); |
el15mh | 1:5a44ce88c5e2 | 75 | |
el15mh | 1:5a44ce88c5e2 | 76 | position.x = pitch / -60; // divide by 60 for increase sensitivity |
el15mh | 1:5a44ce88c5e2 | 77 | position.y = roll / -60; |
el15mh | 0:afee1085c5ef | 78 | } |
el15mh | 0:afee1085c5ef | 79 | |
el15mh | 1:5a44ce88c5e2 | 80 | void MazeEngine::checkWallCollision(N5110 &lcd) |
el15mh | 0:afee1085c5ef | 81 | { |
el15mh | 1:5a44ce88c5e2 | 82 | // acquire position of ball |
el15mh | 1:5a44ce88c5e2 | 83 | // if position on each side of ball is a pixel |
el15mh | 1:5a44ce88c5e2 | 84 | // block movement in that direction |
el15mh | 1:5a44ce88c5e2 | 85 | |
el15mh | 1:5a44ce88c5e2 | 86 | Vector2D _position = _ball.getPosition(); |
el15mh | 1:5a44ce88c5e2 | 87 | Vector2D _velocity = _ball.getVelocity(); |
el15mh | 1:5a44ce88c5e2 | 88 | |
el15mh | 1:5a44ce88c5e2 | 89 | int leftSide = _position.x - _radius; |
el15mh | 1:5a44ce88c5e2 | 90 | int rightSide = _position.x + _radius; |
el15mh | 1:5a44ce88c5e2 | 91 | int topSide = _position.y - _radius; |
el15mh | 1:5a44ce88c5e2 | 92 | int lowerSide = _position.y + _radius; |
el15mh | 1:5a44ce88c5e2 | 93 | |
el15mh | 1:5a44ce88c5e2 | 94 | bool topPixel = lcd.getPixel(_position.x, topSide - 1); |
el15mh | 1:5a44ce88c5e2 | 95 | bool lowerPixel = lcd.getPixel(_position.x, lowerSide + 1); |
el15mh | 1:5a44ce88c5e2 | 96 | bool leftPixel = lcd.getPixel(_position.y, leftSide - 1); |
el15mh | 1:5a44ce88c5e2 | 97 | bool rightPixel = lcd.getPixel(_position.y, rightSide + 1); |
el15mh | 1:5a44ce88c5e2 | 98 | |
el15mh | 1:5a44ce88c5e2 | 99 | if ((topSide + topPixel) != topSide){ // if the sum is changed by pixel it must be present |
el15mh | 1:5a44ce88c5e2 | 100 | _velocity.y = 0; |
el15mh | 1:5a44ce88c5e2 | 101 | printf("Top side hit wall"); |
el15mh | 1:5a44ce88c5e2 | 102 | } |
el15mh | 0:afee1085c5ef | 103 | |
el15mh | 1:5a44ce88c5e2 | 104 | if ((lowerSide + lowerPixel) != lowerSide ){ |
el15mh | 1:5a44ce88c5e2 | 105 | _velocity.y = 0; |
el15mh | 1:5a44ce88c5e2 | 106 | printf("Low side hit wall"); |
el15mh | 1:5a44ce88c5e2 | 107 | } |
el15mh | 1:5a44ce88c5e2 | 108 | |
el15mh | 1:5a44ce88c5e2 | 109 | if ((leftSide + leftPixel) != leftSide){ // if the sum is changed by pixel it must be present |
el15mh | 1:5a44ce88c5e2 | 110 | _velocity.y = 0; |
el15mh | 1:5a44ce88c5e2 | 111 | printf("Left side hit wall"); |
el15mh | 1:5a44ce88c5e2 | 112 | } |
el15mh | 1:5a44ce88c5e2 | 113 | |
el15mh | 1:5a44ce88c5e2 | 114 | if ((rightSide + rightPixel) != rightSide ){ |
el15mh | 1:5a44ce88c5e2 | 115 | _velocity.y = 0; |
el15mh | 1:5a44ce88c5e2 | 116 | printf("Right side hit wall"); |
el15mh | 1:5a44ce88c5e2 | 117 | } |
el15mh | 1:5a44ce88c5e2 | 118 | |
el15mh | 1:5a44ce88c5e2 | 119 | _ball.setVelocity(_velocity); |
el15mh | 1:5a44ce88c5e2 | 120 | } |
el15mh | 1:5a44ce88c5e2 | 121 | |
el15mh | 1:5a44ce88c5e2 | 122 | |
el15mh | 1:5a44ce88c5e2 | 123 | void MazeEngine::update(N5110 &lcd) |
el15mh | 1:5a44ce88c5e2 | 124 | { |
el15mh | 1:5a44ce88c5e2 | 125 | // changes the location of the ball according to inputs |
el15mh | 1:5a44ce88c5e2 | 126 | _ball.update(position); |
el15mh | 1:5a44ce88c5e2 | 127 | |
el15mh | 1:5a44ce88c5e2 | 128 | // reverts the changes if the proposed new position sends ball |
el15mh | 1:5a44ce88c5e2 | 129 | // into the wall |
el15mh | 1:5a44ce88c5e2 | 130 | checkWallCollision(lcd); |
el15mh | 0:afee1085c5ef | 131 | } |
el15mh | 0:afee1085c5ef | 132 | |
el15mh | 0:afee1085c5ef | 133 | void MazeEngine::draw(N5110 &lcd) |
el15mh | 0:afee1085c5ef | 134 | { |
el15mh | 0:afee1085c5ef | 135 | _ball.draw(lcd); |
el15mh | 0:afee1085c5ef | 136 | _maze.draw(lcd); |
el15mh | 0:afee1085c5ef | 137 | } |
el15mh | 0:afee1085c5ef | 138 | |
el15mh | 0:afee1085c5ef | 139 |