Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Revision:
17:99e533f7f2fb
Parent:
16:ddb203a74dfc
Child:
18:3c030560e31d
--- a/Entity/Snake/Snake.cpp	Wed Apr 24 21:21:37 2019 +0000
+++ b/Entity/Snake/Snake.cpp	Wed Apr 24 23:56:32 2019 +0000
@@ -2,8 +2,10 @@
 #include "math.h"
 #include <complex>
 
+// Constructor
 Snake::Snake(float pos_x, float pos_y) {
     moving = true;
+    _prev_face = 0;
     face = 0;
     hp = 4;
     hitbox.width = 6;
@@ -17,15 +19,32 @@
     frame.count = 0;
     frame.number = 0;
     frame.max = 6;
-    velocity = 0.1;
+    velocity = 0;
+    _velocity_index = 0;
+}
+// Member Function
+void Snake::update_prev_face(){_prev_face = face;}
+
+// Member Mutator
+void Snake::update_hitbox(int hitbox_width, int hitbox_height, int hitbox_offset_x, int hitbox_offset_y, int sprite_size_width, int sprite_size_height, int sprite_size_offset_x, int sprite_size_offset_y){    // Offset, Hitbox and Frame Count update
+    if (_prev_face != face){
+        hitbox.width = sprite_size.offset_y;
+        hitbox.height = hitbox_height;
+        sprite_size.width = sprite_size_width;
+        sprite_size.height = sprite_size_height;
+        sprite_size.offset_x = sprite_size_offset_x;
+        sprite_size.offset_y = sprite_size_offset_y;
+    }
 }
 
+// Functions
 void Snake::move(float player_x, float player_y) {
     std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector
-    velocity = velocity_pattern[frame.number]; // Creating slithering effect, changing velocity of movement
+    velocity = velocity_pattern[_velocity_index]; // Creating slithering effect, changing velocity of movement
+    update_prev_face();
     
     // Setting Face
-    if (frame.number == 0) {
+    if (_velocity_index == 0) {
         if (abs(pos_diff.real()) > abs(pos_diff.imag())) {
             if (pos_diff.real() > 0) {
                 face = 1;
@@ -44,32 +63,59 @@
         }
     }
     
-    // Movement, Offset and  Update
+    // Movement
     if (face == 0){
         position.y += velocity;
+        update_hitbox(4, 7, 0, 0, 6, 12, 1, 6);
     }
     else if (face == 1){
         position.x += velocity;
+        update_hitbox(7, 4, 0, 0, 12, 7, 3, 4);
     }
     else if (face == 2){
         position.y -= velocity;
+        update_hitbox(4, 7, 0, 0, 6, 12, 1, 5);
     }
     else if (face == 3){
         position.x -= velocity;
+        update_hitbox(7, 4, 0, 0, 12, 7, 2, 4);
     }
     
     undo_move_x(matrix_collision_test(position.x, prev_pos.y, 0));
     undo_move_y(matrix_collision_test(prev_pos.x, position.y, 0));
     
-    if (frame.number < frame.max) {
+    if (frame.count < 5) {
         frame.count++;
     }
     else {
         frame.count = 0;
+        if (_velocity_index < 6) {
+            _velocity_index++;
+        }
+        else {
+            _velocity_index = 0;
+        }
+        if (frame.number < frame.max) {
+            frame.number++;
+        }
+        else {
+            frame.number = 0;
+        }
     }
-    frame.number = (frame.count/5) % frame.max;
 }
 
 int * Snake::get_frame() {
-    return (int *) sprite_snake[face][frame.number];
+    if(face == 0) {
+        return (int *) sprite_snake_y[1][frame.number];
+        }
+    else if(face == 1) {
+        return (int *) sprite_snake_x[0][frame.number];
+        }
+    else if(face == 2) {
+        return (int *) sprite_snake_y[0][frame.number];
+    }
+    else if(face == 3) {
+        return (int *) sprite_snake_x[1][frame.number];
+    }
+    return 0;
 }
\ No newline at end of file