Class used to run the maze game loop.

Committer:
el15mh
Date:
Wed Apr 19 20:16:56 2017 +0000
Revision:
2:cbce5d35e7d6
Parent:
1:5a44ce88c5e2
Child:
3:4b82cb2e0618
maze game with working wall barriers;

Who changed what in which revision?

UserRevisionLine numberNew 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 2:cbce5d35e7d6 88
el15mh 2:cbce5d35e7d6 89 // printf("position = (%f, %f) \n", _position.x, _position.y);
el15mh 1:5a44ce88c5e2 90
el15mh 2:cbce5d35e7d6 91 int leftSide = int(_position.x - _radius);
el15mh 2:cbce5d35e7d6 92 int rightSide = int(_position.x + _radius);
el15mh 2:cbce5d35e7d6 93 int topSide = int(_position.y - _radius);
el15mh 2:cbce5d35e7d6 94 int lowerSide = int (_position.y + _radius);
el15mh 1:5a44ce88c5e2 95
el15mh 2:cbce5d35e7d6 96 // check position of ball
el15mh 2:cbce5d35e7d6 97 // if distance between outermost pixel and centre changes
el15mh 2:cbce5d35e7d6 98 // there must be a pixel in adjacent position
el15mh 2:cbce5d35e7d6 99 if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){
el15mh 2:cbce5d35e7d6 100
el15mh 2:cbce5d35e7d6 101 // set velocity to 0
el15mh 2:cbce5d35e7d6 102 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 103 // ball can't move any further left
el15mh 2:cbce5d35e7d6 104 _position.x += 1;
el15mh 2:cbce5d35e7d6 105 printf("left side hit \n");
el15mh 1:5a44ce88c5e2 106 }
el15mh 0:afee1085c5ef 107
el15mh 2:cbce5d35e7d6 108 if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){
el15mh 2:cbce5d35e7d6 109
el15mh 2:cbce5d35e7d6 110 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 111 _position.x -= 1;
el15mh 2:cbce5d35e7d6 112 printf("right side hit \n");
el15mh 1:5a44ce88c5e2 113 }
el15mh 1:5a44ce88c5e2 114
el15mh 2:cbce5d35e7d6 115
el15mh 2:cbce5d35e7d6 116 if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){
el15mh 2:cbce5d35e7d6 117
el15mh 1:5a44ce88c5e2 118 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 119 _position.y += 1;
el15mh 2:cbce5d35e7d6 120 printf("top side hit \n");
el15mh 1:5a44ce88c5e2 121 }
el15mh 1:5a44ce88c5e2 122
el15mh 2:cbce5d35e7d6 123 if (lowerSide + _mazeArray[int(_position.x)][lowerSide + 1] != lowerSide){
el15mh 2:cbce5d35e7d6 124
el15mh 1:5a44ce88c5e2 125 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 126 _position.y -= 1;
el15mh 2:cbce5d35e7d6 127 printf("low side hit \n");
el15mh 1:5a44ce88c5e2 128 }
el15mh 1:5a44ce88c5e2 129
el15mh 2:cbce5d35e7d6 130 _ball.setPosition(_position);
el15mh 1:5a44ce88c5e2 131 _ball.setVelocity(_velocity);
el15mh 1:5a44ce88c5e2 132 }
el15mh 1:5a44ce88c5e2 133
el15mh 1:5a44ce88c5e2 134
el15mh 1:5a44ce88c5e2 135 void MazeEngine::update(N5110 &lcd)
el15mh 1:5a44ce88c5e2 136 {
el15mh 1:5a44ce88c5e2 137 // changes the location of the ball according to inputs
el15mh 1:5a44ce88c5e2 138 _ball.update(position);
el15mh 1:5a44ce88c5e2 139
el15mh 1:5a44ce88c5e2 140 // reverts the changes if the proposed new position sends ball
el15mh 1:5a44ce88c5e2 141 // into the wall
el15mh 2:cbce5d35e7d6 142 // checkWallCollision(lcd);
el15mh 2:cbce5d35e7d6 143 }
el15mh 2:cbce5d35e7d6 144
el15mh 2:cbce5d35e7d6 145 void MazeEngine::getMazeArray(N5110 &lcd)
el15mh 2:cbce5d35e7d6 146 {
el15mh 2:cbce5d35e7d6 147 for (int i = 0; i < 84; i++){
el15mh 2:cbce5d35e7d6 148 for (int j = 0; j < 48; j++){
el15mh 2:cbce5d35e7d6 149
el15mh 2:cbce5d35e7d6 150 int pixel = lcd.getPixel(i, j);
el15mh 2:cbce5d35e7d6 151
el15mh 2:cbce5d35e7d6 152 if (pixel == 1){
el15mh 2:cbce5d35e7d6 153
el15mh 2:cbce5d35e7d6 154 _mazeArray[i][j] = 1;
el15mh 2:cbce5d35e7d6 155 }
el15mh 2:cbce5d35e7d6 156 else {
el15mh 2:cbce5d35e7d6 157
el15mh 2:cbce5d35e7d6 158 _mazeArray[i][j] = 0;
el15mh 2:cbce5d35e7d6 159 }
el15mh 2:cbce5d35e7d6 160 }
el15mh 2:cbce5d35e7d6 161
el15mh 2:cbce5d35e7d6 162 }
el15mh 2:cbce5d35e7d6 163 /*
el15mh 2:cbce5d35e7d6 164 for (int i = 0; i < 84; i++){
el15mh 2:cbce5d35e7d6 165 for (int j = 0; j < 48; j++){
el15mh 2:cbce5d35e7d6 166
el15mh 2:cbce5d35e7d6 167 printf("%f", _mazeArray[i][j]);
el15mh 2:cbce5d35e7d6 168 }
el15mh 2:cbce5d35e7d6 169 }
el15mh 2:cbce5d35e7d6 170 */
el15mh 0:afee1085c5ef 171 }
el15mh 0:afee1085c5ef 172
el15mh 0:afee1085c5ef 173 void MazeEngine::draw(N5110 &lcd)
el15mh 0:afee1085c5ef 174 {
el15mh 2:cbce5d35e7d6 175 _maze.draw(lcd);
el15mh 2:cbce5d35e7d6 176 getMazeArray(lcd);
el15mh 2:cbce5d35e7d6 177
el15mh 0:afee1085c5ef 178 _ball.draw(lcd);
el15mh 0:afee1085c5ef 179 }
el15mh 0:afee1085c5ef 180