Chukwuebuka Nwibe
/
el16ccn
el16ccn_maze
MazeEngine/MazeEngine.cpp@9:73888767b4db, 2018-05-08 (annotated)
- Committer:
- el16ccn
- Date:
- Tue May 08 14:17:41 2018 +0000
- Revision:
- 9:73888767b4db
- Parent:
- 7:cfd0854eee25
2nd publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el16ccn | 1:062d8e7aa3b6 | 1 | #include "MazeEngine.h" |
el16ccn | 1:062d8e7aa3b6 | 2 | |
el16ccn | 1:062d8e7aa3b6 | 3 | MazeEngine::MazeEngine() |
el16ccn | 1:062d8e7aa3b6 | 4 | { |
el16ccn | 1:062d8e7aa3b6 | 5 | |
el16ccn | 1:062d8e7aa3b6 | 6 | } |
el16ccn | 1:062d8e7aa3b6 | 7 | |
el16ccn | 1:062d8e7aa3b6 | 8 | MazeEngine::~MazeEngine() |
el16ccn | 1:062d8e7aa3b6 | 9 | { |
el16ccn | 1:062d8e7aa3b6 | 10 | |
el16ccn | 1:062d8e7aa3b6 | 11 | } |
el16ccn | 1:062d8e7aa3b6 | 12 | |
el16ccn | 4:17816de20d6b | 13 | void MazeEngine::init(int mazeModel, int x, int y, int radius, bool colour, bool control) //define game parameters |
el16ccn | 1:062d8e7aa3b6 | 14 | { |
el16ccn | 1:062d8e7aa3b6 | 15 | |
el16ccn | 4:17816de20d6b | 16 | //define parameters for ball |
el16ccn | 1:062d8e7aa3b6 | 17 | _radius = radius; |
el16ccn | 1:062d8e7aa3b6 | 18 | _x = x; |
el16ccn | 1:062d8e7aa3b6 | 19 | _y = y; |
el16ccn | 4:17816de20d6b | 20 | _colour = colour; |
el16ccn | 4:17816de20d6b | 21 | _control = control; |
el16ccn | 1:062d8e7aa3b6 | 22 | |
el16ccn | 4:17816de20d6b | 23 | //define parameters for maze |
el16ccn | 2:28cdb445f121 | 24 | _mazeModel = mazeModel; |
el16ccn | 1:062d8e7aa3b6 | 25 | |
el16ccn | 4:17816de20d6b | 26 | // initialise with given parameters |
el16ccn | 4:17816de20d6b | 27 | _ball.init(_x, _y, _radius, _colour); |
el16ccn | 2:28cdb445f121 | 28 | _maze.init(_mazeModel); |
el16ccn | 1:062d8e7aa3b6 | 29 | |
el16ccn | 7:cfd0854eee25 | 30 | _ball.init(8, 5, _radius, _colour); |
el16ccn | 7:cfd0854eee25 | 31 | |
el16ccn | 1:062d8e7aa3b6 | 32 | } |
el16ccn | 1:062d8e7aa3b6 | 33 | |
el16ccn | 9:73888767b4db | 34 | //reads joystick input ans sets them to control ball movememnt |
el16ccn | 4:17816de20d6b | 35 | void MazeEngine::readInput(Gamepad &pad) |
el16ccn | 4:17816de20d6b | 36 | { |
el16ccn | 4:17816de20d6b | 37 | _control = true; |
el16ccn | 4:17816de20d6b | 38 | getJoystickInput(pad); |
el16ccn | 4:17816de20d6b | 39 | |
el16ccn | 4:17816de20d6b | 40 | } |
el16ccn | 4:17816de20d6b | 41 | |
el16ccn | 4:17816de20d6b | 42 | |
el16ccn | 4:17816de20d6b | 43 | void MazeEngine::update(N5110 &lcd) |
el16ccn | 4:17816de20d6b | 44 | { |
el16ccn | 4:17816de20d6b | 45 | if (checkFinish()){ |
el16ccn | 4:17816de20d6b | 46 | |
el16ccn | 4:17816de20d6b | 47 | return; |
el16ccn | 5:b4651ea21918 | 48 | |
el16ccn | 4:17816de20d6b | 49 | } |
el16ccn | 4:17816de20d6b | 50 | |
el16ccn | 4:17816de20d6b | 51 | //update ball position based on input (using joystick) |
el16ccn | 4:17816de20d6b | 52 | _ball.update(position); |
el16ccn | 4:17816de20d6b | 53 | |
el16ccn | 4:17816de20d6b | 54 | |
el16ccn | 4:17816de20d6b | 55 | |
el16ccn | 4:17816de20d6b | 56 | // reverts the changes if the proposed new ball position is a wall |
el16ccn | 4:17816de20d6b | 57 | checkCollision(lcd); |
el16ccn | 4:17816de20d6b | 58 | } |
el16ccn | 4:17816de20d6b | 59 | |
el16ccn | 4:17816de20d6b | 60 | |
el16ccn | 4:17816de20d6b | 61 | |
el16ccn | 4:17816de20d6b | 62 | //Acquires maze array for wall collision checks |
el16ccn | 4:17816de20d6b | 63 | void MazeEngine::getMazeArray(N5110 &lcd) |
el16ccn | 4:17816de20d6b | 64 | { |
el16ccn | 4:17816de20d6b | 65 | |
el16ccn | 4:17816de20d6b | 66 | for (int i = 0; i < 84; i++){ |
el16ccn | 4:17816de20d6b | 67 | for (int j = 0; j < 48; j++){ |
el16ccn | 4:17816de20d6b | 68 | |
el16ccn | 4:17816de20d6b | 69 | int pixel = lcd.getPixel(i, j); |
el16ccn | 4:17816de20d6b | 70 | |
el16ccn | 4:17816de20d6b | 71 | if (pixel == 1){ |
el16ccn | 4:17816de20d6b | 72 | |
el16ccn | 4:17816de20d6b | 73 | _mazeArray[i][j] = 1; |
el16ccn | 4:17816de20d6b | 74 | } |
el16ccn | 4:17816de20d6b | 75 | else { |
el16ccn | 4:17816de20d6b | 76 | |
el16ccn | 4:17816de20d6b | 77 | _mazeArray[i][j] = 0; |
el16ccn | 4:17816de20d6b | 78 | } |
el16ccn | 4:17816de20d6b | 79 | } |
el16ccn | 4:17816de20d6b | 80 | |
el16ccn | 4:17816de20d6b | 81 | } |
el16ccn | 4:17816de20d6b | 82 | |
el16ccn | 4:17816de20d6b | 83 | } |
el16ccn | 4:17816de20d6b | 84 | |
el16ccn | 4:17816de20d6b | 85 | |
el16ccn | 4:17816de20d6b | 86 | |
el16ccn | 4:17816de20d6b | 87 | |
el16ccn | 4:17816de20d6b | 88 | void MazeEngine::checkCollision(N5110 &lcd) |
el16ccn | 1:062d8e7aa3b6 | 89 | { |
el16ccn | 4:17816de20d6b | 90 | |
el16ccn | 4:17816de20d6b | 91 | //get position of ball for wall collision check |
el16ccn | 4:17816de20d6b | 92 | Vector2D _position = _ball.getPosition(); |
el16ccn | 4:17816de20d6b | 93 | Vector2D _velocity = _ball.getSpeed(); |
el16ccn | 5:b4651ea21918 | 94 | printf("position = (%f, %f) \n", _position.x, _position.y); |
el16ccn | 5:b4651ea21918 | 95 | |
el16ccn | 4:17816de20d6b | 96 | |
el16ccn | 4:17816de20d6b | 97 | int leftSide = int(_position.x - _radius); //outermost left side pixel x centre minus radius |
el16ccn | 4:17816de20d6b | 98 | int rightSide = int(_position.x + _radius); //outermost right side pixel |
el16ccn | 4:17816de20d6b | 99 | int topSide = int(_position.y - _radius); //outermost top pixel |
el16ccn | 4:17816de20d6b | 100 | int bottomSide = int (_position.y + _radius); //outermost bottom pixel |
el16ccn | 4:17816de20d6b | 101 | |
el16ccn | 4:17816de20d6b | 102 | |
el16ccn | 4:17816de20d6b | 103 | // check ball position |
el16ccn | 4:17816de20d6b | 104 | // if distance between outermost pixel and centre != original distance |
el16ccn | 4:17816de20d6b | 105 | // there must be a pixel in adjacent position |
el16ccn | 4:17816de20d6b | 106 | |
el16ccn | 4:17816de20d6b | 107 | |
el16ccn | 4:17816de20d6b | 108 | if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){ |
el16ccn | 4:17816de20d6b | 109 | |
el16ccn | 4:17816de20d6b | 110 | _velocity.x = 0;//set velocity = 0 |
el16ccn | 4:17816de20d6b | 111 | _position.x -= 1; // ball set to no further right movement |
el16ccn | 4:17816de20d6b | 112 | } |
el16ccn | 4:17816de20d6b | 113 | |
el16ccn | 4:17816de20d6b | 114 | |
el16ccn | 4:17816de20d6b | 115 | if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){ |
el16ccn | 4:17816de20d6b | 116 | |
el16ccn | 4:17816de20d6b | 117 | _velocity.y = 0;//set velocity = 0 |
el16ccn | 4:17816de20d6b | 118 | _position.y += 1; // ball set to no further top movement |
el16ccn | 4:17816de20d6b | 119 | } |
el16ccn | 4:17816de20d6b | 120 | |
el16ccn | 4:17816de20d6b | 121 | if (bottomSide + _mazeArray[int(_position.x)][bottomSide + 1] != bottomSide){ |
el16ccn | 4:17816de20d6b | 122 | |
el16ccn | 4:17816de20d6b | 123 | _velocity.y = 0; //set velocity = 0 |
el16ccn | 4:17816de20d6b | 124 | _position.y -= 1;// ball set to no further bottom movement |
el16ccn | 4:17816de20d6b | 125 | } |
el16ccn | 4:17816de20d6b | 126 | |
el16ccn | 4:17816de20d6b | 127 | if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){ |
el16ccn | 4:17816de20d6b | 128 | |
el16ccn | 4:17816de20d6b | 129 | _velocity.x = 0; // set velocity = 0 |
el16ccn | 4:17816de20d6b | 130 | _position.x += 1; // ball set to no further left movement |
el16ccn | 5:b4651ea21918 | 131 | printf("left side hit \n"); |
el16ccn | 4:17816de20d6b | 132 | } |
el16ccn | 4:17816de20d6b | 133 | |
el16ccn | 4:17816de20d6b | 134 | _ball.setPosition(_position); |
el16ccn | 4:17816de20d6b | 135 | _ball.setSpeed(_velocity); |
el16ccn | 4:17816de20d6b | 136 | |
el16ccn | 4:17816de20d6b | 137 | } |
el16ccn | 4:17816de20d6b | 138 | |
el16ccn | 1:062d8e7aa3b6 | 139 | |
el16ccn | 9:73888767b4db | 140 | |
el16ccn | 9:73888767b4db | 141 | //gets joystick input as mapped coordinate(x,y) |
el16ccn | 4:17816de20d6b | 142 | void MazeEngine::getJoystickInput(Gamepad &pad) |
el16ccn | 4:17816de20d6b | 143 | { |
el16ccn | 4:17816de20d6b | 144 | position = pad.get_mapped_coord(); |
el16ccn | 4:17816de20d6b | 145 | |
el16ccn | 4:17816de20d6b | 146 | } |
el16ccn | 4:17816de20d6b | 147 | |
el16ccn | 4:17816de20d6b | 148 | |
el16ccn | 9:73888767b4db | 149 | |
el16ccn | 9:73888767b4db | 150 | //check if maze has been completed |
el16ccn | 9:73888767b4db | 151 | //boolean variable where true = completed and false = not completed |
el16ccn | 9:73888767b4db | 152 | //only true if x (x coordinate of ball centre) > 83 (maximum x for lcd buffer) |
el16ccn | 4:17816de20d6b | 153 | bool MazeEngine::checkFinish() |
el16ccn | 4:17816de20d6b | 154 | { |
el16ccn | 4:17816de20d6b | 155 | Vector2D position = _ball.getPosition(); |
el16ccn | 9:73888767b4db | 156 | |
el16ccn | 4:17816de20d6b | 157 | |
el16ccn | 4:17816de20d6b | 158 | if (position.x > 86){ |
el16ccn | 4:17816de20d6b | 159 | _finish = true; |
el16ccn | 4:17816de20d6b | 160 | return _finish; |
el16ccn | 4:17816de20d6b | 161 | } |
el16ccn | 4:17816de20d6b | 162 | |
el16ccn | 4:17816de20d6b | 163 | |
el16ccn | 4:17816de20d6b | 164 | return _finish; |
el16ccn | 4:17816de20d6b | 165 | } |
el16ccn | 4:17816de20d6b | 166 | |
el16ccn | 4:17816de20d6b | 167 | |
el16ccn | 9:73888767b4db | 168 | //draw needed maze and ball for particular level and ball type |
el16ccn | 5:b4651ea21918 | 169 | void MazeEngine::draw(N5110 &lcd, Visuals &visual) |
el16ccn | 1:062d8e7aa3b6 | 170 | { |
el16ccn | 1:062d8e7aa3b6 | 171 | _ball.draw(lcd); |
el16ccn | 5:b4651ea21918 | 172 | _maze.draw(lcd,visual); |
el16ccn | 1:062d8e7aa3b6 | 173 | } |