Project Submission (late)

Dependencies:   mbed

Committer:
el17tc
Date:
Fri May 10 14:52:28 2019 +0000
Revision:
3:83e79d31930c
Parent:
0:72f372170a73
final commit, API is added.; I'm not sure if there is a specific statement of academic integrity wanted but- I declare that this work is 100% my own, I have not plagiarised any work or attempted to fabricate any part of it for extra marks.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17tc 0:72f372170a73 1 #include "Player.h"
el17tc 0:72f372170a73 2 #include "mbed.h"
el17tc 0:72f372170a73 3
el17tc 0:72f372170a73 4 const Vector2Di UP = {0,1};
el17tc 0:72f372170a73 5 const Vector2Di DOWN = {0,-1};
el17tc 0:72f372170a73 6
el17tc 0:72f372170a73 7 Player::Player(Maze* mazePtr) {
el17tc 0:72f372170a73 8 maze = mazePtr;
el17tc 3:83e79d31930c 9 pos = UP; // {0,1} is a temporary value to initialise the variable
el17tc 3:83e79d31930c 10 direction = DOWN; // due to matrices index increasing as you go down
el17tc 3:83e79d31930c 11 // and their acces being [row][column], the standard (x,y) co-ordinate system is
el17tc 3:83e79d31930c 12 // effectively reversed so even though the direction is down, from our perspective
el17tc 3:83e79d31930c 13 // (if we were looking down on the maze) it would be up.
el17tc 0:72f372170a73 14 }
el17tc 0:72f372170a73 15
el17tc 3:83e79d31930c 16 /* Function used to check the value of a cell in the maze from any given cell
el17tc 3:83e79d31930c 17 (not necessarily the player's current position)
el17tc 3:83e79d31930c 18 this function is used a lot in the Drawer (along with other information from the player)
el17tc 3:83e79d31930c 19 so it is important the function is not bound to the player's current location
el17tc 3:83e79d31930c 20 */
el17tc 0:72f372170a73 21 bool Player::checkLocation(Vector2Di origin, double angle, int checkVal) {
el17tc 0:72f372170a73 22 Vector2Di peekDirection = direction;
el17tc 0:72f372170a73 23 peekDirection.rotateVector(angle);
el17tc 0:72f372170a73 24 origin.addVector(peekDirection);
el17tc 0:72f372170a73 25 if (maze->mazeMatrix[origin.y][origin.x] == checkVal) {
el17tc 0:72f372170a73 26 // eg if index == 1 that indicates a solid cell
el17tc 0:72f372170a73 27 // or if index == 2 that is the maze exit
el17tc 0:72f372170a73 28 return true;
el17tc 0:72f372170a73 29 } else {
el17tc 0:72f372170a73 30 return false;
el17tc 0:72f372170a73 31 }
el17tc 0:72f372170a73 32 }
el17tc 0:72f372170a73 33
el17tc 0:72f372170a73 34 void Player::walk() {
el17tc 0:72f372170a73 35 if (checkLocation(pos,0,0)) {
el17tc 0:72f372170a73 36 printf("v = (%i, %i)\nwalking\n", pos.x, pos.y);
el17tc 0:72f372170a73 37 pos.addVector(direction);
el17tc 0:72f372170a73 38 printf("v = (%i, %i)\n", pos.x, pos.y);
el17tc 0:72f372170a73 39 }
el17tc 0:72f372170a73 40 };
el17tc 0:72f372170a73 41
el17tc 0:72f372170a73 42 void Player::turnLeft() { direction.rotateVector(-PI/2); };
el17tc 0:72f372170a73 43
el17tc 0:72f372170a73 44 void Player::turnRight() { direction.rotateVector(PI/2); };
el17tc 0:72f372170a73 45
el17tc 0:72f372170a73 46 void Player::stepBack() {
el17tc 0:72f372170a73 47 if (checkLocation(pos,PI,0)) {
el17tc 0:72f372170a73 48 pos.addVector(-direction);
el17tc 0:72f372170a73 49 }
el17tc 0:72f372170a73 50 };