Project Submission (late)
Dependencies: mbed
Player/Player.cpp@0:72f372170a73, 2019-05-10 (annotated)
- Committer:
- el17tc
- Date:
- Fri May 10 08:07:10 2019 +0000
- Revision:
- 0:72f372170a73
- Child:
- 3:83e79d31930c
Save at functioning version;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:72f372170a73 | 9 | pos = UP; |
el17tc | 0:72f372170a73 | 10 | direction = DOWN; |
el17tc | 0:72f372170a73 | 11 | } |
el17tc | 0:72f372170a73 | 12 | |
el17tc | 0:72f372170a73 | 13 | bool Player::checkLocation(Vector2Di origin, double angle, int checkVal) { |
el17tc | 0:72f372170a73 | 14 | Vector2Di peekDirection = direction; |
el17tc | 0:72f372170a73 | 15 | peekDirection.rotateVector(angle); |
el17tc | 0:72f372170a73 | 16 | origin.addVector(peekDirection); |
el17tc | 0:72f372170a73 | 17 | if (maze->mazeMatrix[origin.y][origin.x] == checkVal) { |
el17tc | 0:72f372170a73 | 18 | // eg if index == 1 that indicates a solid cell |
el17tc | 0:72f372170a73 | 19 | // or if index == 2 that is the maze exit |
el17tc | 0:72f372170a73 | 20 | return true; |
el17tc | 0:72f372170a73 | 21 | } else { |
el17tc | 0:72f372170a73 | 22 | return false; |
el17tc | 0:72f372170a73 | 23 | } |
el17tc | 0:72f372170a73 | 24 | } |
el17tc | 0:72f372170a73 | 25 | |
el17tc | 0:72f372170a73 | 26 | void Player::walk() { |
el17tc | 0:72f372170a73 | 27 | if (checkLocation(pos,0,0)) { |
el17tc | 0:72f372170a73 | 28 | printf("v = (%i, %i)\nwalking\n", pos.x, pos.y); |
el17tc | 0:72f372170a73 | 29 | pos.addVector(direction); |
el17tc | 0:72f372170a73 | 30 | printf("v = (%i, %i)\n", pos.x, pos.y); |
el17tc | 0:72f372170a73 | 31 | } |
el17tc | 0:72f372170a73 | 32 | }; |
el17tc | 0:72f372170a73 | 33 | |
el17tc | 0:72f372170a73 | 34 | void Player::turnLeft() { direction.rotateVector(-PI/2); }; |
el17tc | 0:72f372170a73 | 35 | |
el17tc | 0:72f372170a73 | 36 | void Player::turnRight() { direction.rotateVector(PI/2); }; |
el17tc | 0:72f372170a73 | 37 | |
el17tc | 0:72f372170a73 | 38 | void Player::stepBack() { |
el17tc | 0:72f372170a73 | 39 | if (checkLocation(pos,PI,0)) { |
el17tc | 0:72f372170a73 | 40 | pos.addVector(-direction); |
el17tc | 0:72f372170a73 | 41 | } |
el17tc | 0:72f372170a73 | 42 | }; |