Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MotionSensor
Diff: Entity/Snake/Snake.cpp
- Revision:
- 22:7abf4581bc9b
- Parent:
- 18:3c030560e31d
- Child:
- 23:5a8f75e93508
--- a/Entity/Snake/Snake.cpp Thu Apr 25 03:32:36 2019 +0000
+++ b/Entity/Snake/Snake.cpp Thu Apr 25 05:53:30 2019 +0000
@@ -3,7 +3,8 @@
#include <complex>
// Constructor
-Snake::Snake(float pos_x, float pos_y) {
+Snake::Snake(float pos_x, float pos_y)
+{
moving = true;
_prev_face = 0;
face = 0;
@@ -23,11 +24,15 @@
_velocity_index = 0;
}
// Member Function
-void Snake::update_prev_face(){_prev_face = face;}
+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, int max_frame){ // Offset, Hitbox and Frame Count update
- if (_prev_face != face){
+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, int max_frame) // Offset, Hitbox and Frame Count update
+{
+ if (_prev_face != face) {
frame.number = 0;
hitbox.width = sprite_size.offset_y;
hitbox.height = hitbox_height;
@@ -40,83 +45,70 @@
}
// Functions
-void Snake::move(float player_x, float player_y) {
+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[_velocity_index]; // Creating slithering effect, changing velocity of movement
update_prev_face();
-
+
// Setting Face
if (_velocity_index == 0) {
if (abs(pos_diff.real()) > abs(pos_diff.imag())) {
if (pos_diff.real() > 0) {
face = 1;
- }
- else {
+ } else {
face = 3;
}
- }
- else {
+ } else {
if (pos_diff.imag() > 0) {
face = 0;
- }
- else {
+ } else {
face = 2;
}
}
}
-
+
// Movement
- if (face == 0){
+ if (face == 0) {
position.y += velocity;
update_hitbox(4, 7, 0, 0, 6, 12, 1, 6, 6);
- }
- else if (face == 1){
+ } else if (face == 1) {
position.x += velocity;
update_hitbox(7, 4, 0, 0, 12, 7, 3, 4, 4);
- }
- else if (face == 2){
+ } else if (face == 2) {
position.y -= velocity;
update_hitbox(4, 7, 0, 0, 6, 12, 1, 5, 6);
- }
- else if (face == 3){
+ } else if (face == 3) {
position.x -= velocity;
update_hitbox(7, 4, 0, 0, 12, 7, 2, 4, 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.count < 5) {
- frame.count++;
- }
- else {
+
+ frame.count++;
+ if (frame.count >= 5) {
frame.count = 0;
- if (_velocity_index < 6) {
- _velocity_index++;
- }
- else {
+ _velocity_index++;
+ frame.number++;
+ if (_velocity_index >= 6) {
_velocity_index = 0;
}
- if (frame.number < frame.max) {
- frame.number++;
- }
- else {
+ if (frame.number >= frame.max) {
frame.number = 0;
}
}
}
-int * Snake::get_frame() {
+int * Snake::get_frame()
+{
if(face == 0) {
return (int *) sprite_snake_y[1][frame.number];
- }
- else if(face == 1) {
+ } else if(face == 1) {
return (int *) sprite_snake_x[0][frame.number];
- }
- else if(face == 2) {
+ } else if(face == 2) {
return (int *) sprite_snake_y[0][frame.number];
- }
- else if(face == 3) {
+ } else if(face == 3) {
return (int *) sprite_snake_x[1][frame.number];
}
return 0;