testing documentation

Dependencies:   mbed ll16j23s_test_docs

Committer:
JoeShotton
Date:
Tue May 26 23:57:47 2020 +0000
Revision:
12:33a5cff31339
Parent:
10:a2d643b3c782
documentation test 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JoeShotton 3:fcd6d70e9694 1 #include "SnakeBody.h"
JoeShotton 3:fcd6d70e9694 2
JoeShotton 3:fcd6d70e9694 3 SnakeBody::SnakeBody()
JoeShotton 3:fcd6d70e9694 4 {
JoeShotton 4:ea3fa51c4386 5 _x_head = 42;
JoeShotton 4:ea3fa51c4386 6 _y_head = 24;
JoeShotton 8:bcc3403d7e79 7 _length = 4;
JoeShotton 10:a2d643b3c782 8 _length_increase = 4;
JoeShotton 4:ea3fa51c4386 9 _angle = -1.0;
JoeShotton 10:a2d643b3c782 10 _move_state = 0;
JoeShotton 4:ea3fa51c4386 11 _d = 0;
JoeShotton 3:fcd6d70e9694 12 }
JoeShotton 3:fcd6d70e9694 13
JoeShotton 10:a2d643b3c782 14 SnakeBody::~SnakeBody()
JoeShotton 10:a2d643b3c782 15 {
JoeShotton 4:ea3fa51c4386 16 //destructor
JoeShotton 4:ea3fa51c4386 17 }
JoeShotton 4:ea3fa51c4386 18
JoeShotton 10:a2d643b3c782 19 void SnakeBody::init()
JoeShotton 10:a2d643b3c782 20 {
JoeShotton 6:6c9453397f4a 21 _body_x.clear();
JoeShotton 6:6c9453397f4a 22 _body_x.push_back(42);
JoeShotton 6:6c9453397f4a 23 _body_y.clear();
JoeShotton 6:6c9453397f4a 24 _body_y.push_back(24);
JoeShotton 3:fcd6d70e9694 25 }
JoeShotton 3:fcd6d70e9694 26
JoeShotton 10:a2d643b3c782 27 void SnakeBody::update_direction()
JoeShotton 10:a2d643b3c782 28 {
JoeShotton 10:a2d643b3c782 29
JoeShotton 4:ea3fa51c4386 30 if ((_x_head % 2) + (_y_head % 2) == 0) { // only allows changing movement when the snake is cell-aligned (ie x and y are even)
JoeShotton 4:ea3fa51c4386 31 // partition 360 into segments and check which segment the angle is in
JoeShotton 10:a2d643b3c782 32 printf("D: %d\n", _d);
JoeShotton 4:ea3fa51c4386 33 if (_angle < 0.0f) { //check for -1.0 angle
JoeShotton 10:a2d643b3c782 34 _d = 0;
JoeShotton 4:ea3fa51c4386 35 } else if (_angle < 45.0f) { //North quadrant
JoeShotton 10:a2d643b3c782 36 _d = 1;
JoeShotton 4:ea3fa51c4386 37 } else if (_angle < 135.0f) { //East quadrant
JoeShotton 10:a2d643b3c782 38 _d = 2;
JoeShotton 4:ea3fa51c4386 39 } else if (_angle < 225.0f) { //South quadrant
JoeShotton 10:a2d643b3c782 40 _d = 3;
JoeShotton 4:ea3fa51c4386 41 } else if (_angle < 315.0f) { //West quadrant
JoeShotton 10:a2d643b3c782 42 _d = 4;
JoeShotton 4:ea3fa51c4386 43 } else { //NW half - quadrant
JoeShotton 4:ea3fa51c4386 44 _d = 1;
JoeShotton 4:ea3fa51c4386 45 }
JoeShotton 3:fcd6d70e9694 46 }
JoeShotton 4:ea3fa51c4386 47 };
JoeShotton 4:ea3fa51c4386 48
JoeShotton 10:a2d643b3c782 49 void SnakeBody::update_position()
JoeShotton 10:a2d643b3c782 50 {
JoeShotton 4:ea3fa51c4386 51
JoeShotton 4:ea3fa51c4386 52 Direction _fsm[5] = {
JoeShotton 4:ea3fa51c4386 53 {0, 0, {0,1,2,3,4}}, // Centred
JoeShotton 4:ea3fa51c4386 54 {0, -1, {1,1,2,1,4}}, // North, will not go to centre or south
JoeShotton 4:ea3fa51c4386 55 {1, 0, {2,1,2,3,2}}, // East, will not go to centre or west
JoeShotton 4:ea3fa51c4386 56 {0, 1, {3,3,2,3,4}}, // South, will not go to centre or north
JoeShotton 4:ea3fa51c4386 57 {-1, 0, {4,1,4,3,4}} // West, will not go to centre or east
JoeShotton 4:ea3fa51c4386 58 };
JoeShotton 4:ea3fa51c4386 59
JoeShotton 10:a2d643b3c782 60 _move_state = _fsm[_move_state].nextState[_d]; // adjusts fsm state based on direction
JoeShotton 10:a2d643b3c782 61 //printf("State: %d\n", _move_state);
JoeShotton 10:a2d643b3c782 62
JoeShotton 10:a2d643b3c782 63 _x_head += _fsm[_move_state].delta_x; // increments x value based on fsm state value
JoeShotton 10:a2d643b3c782 64 _y_head += _fsm[_move_state].delta_y; // increments y value based on fsm state value
JoeShotton 10:a2d643b3c782 65
JoeShotton 4:ea3fa51c4386 66 _x_head = ((_x_head % 84) + 84) % 84; // wraps x back to within range 0-83
JoeShotton 4:ea3fa51c4386 67 _y_head = ((_y_head % 48) + 48) % 48; // wraps y back to within range 0-47
JoeShotton 10:a2d643b3c782 68
JoeShotton 4:ea3fa51c4386 69 //printf("x_head: %d\n", _x_head);
JoeShotton 4:ea3fa51c4386 70 //printf("y_head: %d\n", _y_head);
JoeShotton 4:ea3fa51c4386 71 };
JoeShotton 4:ea3fa51c4386 72
JoeShotton 10:a2d643b3c782 73 void SnakeBody::snake_movement(Gamepad &pad)
JoeShotton 10:a2d643b3c782 74 {
JoeShotton 10:a2d643b3c782 75
JoeShotton 10:a2d643b3c782 76 _angle = pad.get_angle(); //finds joystick angle
JoeShotton 10:a2d643b3c782 77 //printf("Angle: %f\n", _angle);
JoeShotton 10:a2d643b3c782 78 update_direction(); //converts angle into a integer direction
JoeShotton 10:a2d643b3c782 79 update_position(); //takes integer direction and feeds it into FSM to move snake in correct direction
JoeShotton 10:a2d643b3c782 80 update_body(); //feeds head coords into body vectors and remove old ones
JoeShotton 4:ea3fa51c4386 81 }
JoeShotton 4:ea3fa51c4386 82
JoeShotton 10:a2d643b3c782 83 void SnakeBody::update_body()
JoeShotton 10:a2d643b3c782 84 {
JoeShotton 10:a2d643b3c782 85 if ((_x_head % 2) + (_y_head % 2) == 0) { //only updates body when cell aligned
JoeShotton 10:a2d643b3c782 86 _body_x.insert(_body_x.begin(), _x_head); //sets first array element to head coordinates
JoeShotton 8:bcc3403d7e79 87 _body_y.insert(_body_y.begin(), _y_head);
JoeShotton 10:a2d643b3c782 88
JoeShotton 10:a2d643b3c782 89 _body_x.erase(_body_x.begin() + _length, _body_x.end()); //erases all elements from position after tail (ie [_length])
JoeShotton 10:a2d643b3c782 90 _body_y.erase(_body_y.begin() + _length, _body_y.end()); //up to end of vector
JoeShotton 8:bcc3403d7e79 91 }
JoeShotton 10:a2d643b3c782 92 if(_length_increase > 0) { //converts length increase into length, one unit at a time
JoeShotton 9:0571880085cc 93 _length++;
JoeShotton 9:0571880085cc 94 _length_increase--;
JoeShotton 10:a2d643b3c782 95 }
JoeShotton 4:ea3fa51c4386 96 }
JoeShotton 4:ea3fa51c4386 97
JoeShotton 8:bcc3403d7e79 98
JoeShotton 10:a2d643b3c782 99 void SnakeBody::draw_body(N5110 &lcd)
JoeShotton 10:a2d643b3c782 100 {
JoeShotton 10:a2d643b3c782 101 lcd.drawRect(_x_head,_y_head,2,2,FILL_BLACK); //draws square at head (so new head coords displayed even when not cell-aligned)
JoeShotton 10:a2d643b3c782 102 for(int i = 0; i < _length - 3; i++) { //iterates across vector to draw sqaures at every position
JoeShotton 4:ea3fa51c4386 103 lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK);
JoeShotton 4:ea3fa51c4386 104 }
JoeShotton 4:ea3fa51c4386 105 }
JoeShotton 4:ea3fa51c4386 106
JoeShotton 10:a2d643b3c782 107 void SnakeBody::snake_snake_collision(Gamepad &pad, bool &_death)
JoeShotton 10:a2d643b3c782 108 {
JoeShotton 10:a2d643b3c782 109 if (_move_state > 0) { //if body has started to move
JoeShotton 10:a2d643b3c782 110 for(int i = 3; i < _length - 3; i++) { //only checks from 3rd cell onwards since head can't collide with very start of body
JoeShotton 8:bcc3403d7e79 111 if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords
JoeShotton 10:a2d643b3c782 112 //printf("S-S Collison \n");
JoeShotton 10:a2d643b3c782 113 _move_state = 0;
JoeShotton 9:0571880085cc 114 _death = true;
JoeShotton 8:bcc3403d7e79 115 }
JoeShotton 4:ea3fa51c4386 116 }
JoeShotton 10:a2d643b3c782 117 }
JoeShotton 9:0571880085cc 118 }
JoeShotton 9:0571880085cc 119
JoeShotton 10:a2d643b3c782 120 void SnakeBody::add_length(int increase) {
JoeShotton 10:a2d643b3c782 121 _length_increase += increase;
JoeShotton 9:0571880085cc 122 }
JoeShotton 9:0571880085cc 123
JoeShotton 10:a2d643b3c782 124 void SnakeBody::run(Gamepad &pad, N5110 &lcd, bool &_death)
JoeShotton 10:a2d643b3c782 125 {
JoeShotton 9:0571880085cc 126 snake_movement(pad);
JoeShotton 9:0571880085cc 127 draw_body(lcd);
JoeShotton 9:0571880085cc 128 snake_snake_collision(pad, _death);
JoeShotton 10:a2d643b3c782 129 //printf("Body running!");
JoeShotton 10:a2d643b3c782 130 }
JoeShotton 10:a2d643b3c782 131
JoeShotton 10:a2d643b3c782 132 void SnakeBody::reset()
JoeShotton 10:a2d643b3c782 133 {
JoeShotton 10:a2d643b3c782 134 _x_head = 42;
JoeShotton 10:a2d643b3c782 135 _y_head = 24;
JoeShotton 10:a2d643b3c782 136 for(int i = 3; i < _body_y.size(); i++) {
JoeShotton 10:a2d643b3c782 137 _body_y.at(i) = 50;
JoeShotton 10:a2d643b3c782 138 }
JoeShotton 10:a2d643b3c782 139 _length = 4;
JoeShotton 10:a2d643b3c782 140 _length_increase = 4;
JoeShotton 10:a2d643b3c782 141 _move_state = 0;
JoeShotton 4:ea3fa51c4386 142 }