ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_ll16j23s

Dependencies:   mbed ll16j23s_test_docs

Committer:
JoeShotton
Date:
Wed May 27 03:18:16 2020 +0000
Revision:
14:2dfe04ced21c
Parent:
13:7b7ec5db56b2
Final Version

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 13:7b7ec5db56b2 5 head_x = 42;
JoeShotton 13:7b7ec5db56b2 6 head_y = 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 13:7b7ec5db56b2 21 body_x.clear();
JoeShotton 13:7b7ec5db56b2 22 body_x.push_back(42);
JoeShotton 13:7b7ec5db56b2 23 body_y.clear();
JoeShotton 13:7b7ec5db56b2 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 13:7b7ec5db56b2 30 if ((head_x % 2) + (head_y % 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 13:7b7ec5db56b2 63 head_x += _fsm[_move_state].delta_x; // increments x value based on fsm state value
JoeShotton 13:7b7ec5db56b2 64 head_y += _fsm[_move_state].delta_y; // increments y value based on fsm state value
JoeShotton 10:a2d643b3c782 65
JoeShotton 13:7b7ec5db56b2 66 head_x = ((head_x % 84) + 84) % 84; // wraps x back to within range 0-83
JoeShotton 13:7b7ec5db56b2 67 head_y = ((head_y % 48) + 48) % 48; // wraps y back to within range 0-47
JoeShotton 10:a2d643b3c782 68
JoeShotton 13:7b7ec5db56b2 69 //printf("head_x: %d\n", head_x);
JoeShotton 13:7b7ec5db56b2 70 //printf("head_y: %d\n", head_y);
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 13:7b7ec5db56b2 85 if ((head_x % 2) + (head_y % 2) == 0) { //only updates body when cell aligned
JoeShotton 13:7b7ec5db56b2 86 body_x.insert(body_x.begin(), head_x); //sets first array element to head coordinates
JoeShotton 13:7b7ec5db56b2 87 body_y.insert(body_y.begin(), head_y);
JoeShotton 10:a2d643b3c782 88
JoeShotton 13:7b7ec5db56b2 89 body_x.erase(body_x.begin() + _length, body_x.end()); //erases all elements from position after tail (ie [_length])
JoeShotton 13:7b7ec5db56b2 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 14:2dfe04ced21c 95 //printf("L increase: %d\n", _length_increase);
JoeShotton 10:a2d643b3c782 96 }
JoeShotton 4:ea3fa51c4386 97 }
JoeShotton 4:ea3fa51c4386 98
JoeShotton 8:bcc3403d7e79 99
JoeShotton 10:a2d643b3c782 100 void SnakeBody::draw_body(N5110 &lcd)
JoeShotton 10:a2d643b3c782 101 {
JoeShotton 13:7b7ec5db56b2 102 lcd.drawRect(head_x, head_y, 2, 2, FILL_BLACK); //draws square at head (so new head coords displayed even when not cell-aligned)
JoeShotton 10:a2d643b3c782 103 for(int i = 0; i < _length - 3; i++) { //iterates across vector to draw sqaures at every position
JoeShotton 13:7b7ec5db56b2 104 lcd.drawRect(body_x[i], body_y[i], 2, 2, FILL_BLACK);
JoeShotton 4:ea3fa51c4386 105 }
JoeShotton 4:ea3fa51c4386 106 }
JoeShotton 4:ea3fa51c4386 107
JoeShotton 10:a2d643b3c782 108 void SnakeBody::snake_snake_collision(Gamepad &pad, bool &_death)
JoeShotton 10:a2d643b3c782 109 {
JoeShotton 10:a2d643b3c782 110 if (_move_state > 0) { //if body has started to move
JoeShotton 10:a2d643b3c782 111 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 13:7b7ec5db56b2 112 if (head_x == body_x[i] && head_y == body_y[i]) { //checks if head coord is the same as any of the body coords
JoeShotton 10:a2d643b3c782 113 //printf("S-S Collison \n");
JoeShotton 10:a2d643b3c782 114 _move_state = 0;
JoeShotton 9:0571880085cc 115 _death = true;
JoeShotton 8:bcc3403d7e79 116 }
JoeShotton 4:ea3fa51c4386 117 }
JoeShotton 10:a2d643b3c782 118 }
JoeShotton 9:0571880085cc 119 }
JoeShotton 9:0571880085cc 120
JoeShotton 10:a2d643b3c782 121 void SnakeBody::add_length(int increase) {
JoeShotton 10:a2d643b3c782 122 _length_increase += increase;
JoeShotton 9:0571880085cc 123 }
JoeShotton 9:0571880085cc 124
JoeShotton 10:a2d643b3c782 125 void SnakeBody::run(Gamepad &pad, N5110 &lcd, bool &_death)
JoeShotton 10:a2d643b3c782 126 {
JoeShotton 9:0571880085cc 127 snake_movement(pad);
JoeShotton 9:0571880085cc 128 draw_body(lcd);
JoeShotton 9:0571880085cc 129 snake_snake_collision(pad, _death);
JoeShotton 10:a2d643b3c782 130 //printf("Body running!");
JoeShotton 10:a2d643b3c782 131 }
JoeShotton 10:a2d643b3c782 132
JoeShotton 10:a2d643b3c782 133 void SnakeBody::reset()
JoeShotton 10:a2d643b3c782 134 {
JoeShotton 13:7b7ec5db56b2 135 head_x = 42;
JoeShotton 13:7b7ec5db56b2 136 head_y = 24;
JoeShotton 14:2dfe04ced21c 137 for(int i = 1; i < body_y.size(); i++) {
JoeShotton 13:7b7ec5db56b2 138 body_y.at(i) = 50; //moves all body coordinates off-screen
JoeShotton 10:a2d643b3c782 139 }
JoeShotton 10:a2d643b3c782 140 _length = 4;
JoeShotton 10:a2d643b3c782 141 _length_increase = 4;
JoeShotton 10:a2d643b3c782 142 _move_state = 0;
JoeShotton 4:ea3fa51c4386 143 }