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
Entity/Mobs/Snake/Snake.cpp
- Committer:
- el17sm
- Date:
- 2019-05-09
- Revision:
- 51:4d0cd75e7ed3
- Parent:
- 44:cc4cecfc639f
- Child:
- 54:03ddad11d202
File content as of revision 51:4d0cd75e7ed3:
#include "Snake.h"
#include "math.h"
#include <complex>
// Constructor
Snake::Snake(float pos_x, float pos_y)
{
_hp_drop_chance = 10; // out of 100
_prev_face = 0;
face = 0;
hp = 4;
attack = 1;
hitbox.width = 4;
hitbox.height = 7;
position.x = pos_x;
position.y = pos_y;
sprite_size.width = 6;
sprite_size.height = 12;
sprite_size.offset_x = -1;
sprite_size.offset_y = -6;
frame.count = 0;
frame.number = 0;
frame.max = 6;
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 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 = hitbox_width;
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;
frame.max = max_frame;
}
}
// Functions
void Snake::move(float player_x, float player_y, char * map, bool * doorways)
{
float diff_x = player_x - position.x; // defining difference in position as a vector
float diff_y = player_y - position.y;
velocity = snake_velocity_pattern[_velocity_index]; // Creating slithering effect, changing velocity of movement
update_prev_face();
// Setting Face
if (_velocity_index == 0) {
if (abs(diff_x) > abs(diff_y)) {
if (diff_x > 0) {
face = 1;
} else {
face = 3;
}
} else {
if (diff_y > 0) {
face = 2;
} else {
face = 0;
}
}
}
// Movement
if (face == 0) {
position.y -= velocity;
update_hitbox(4, 7, 6, 12, -1, -6, 6);
} else if (face == 1) {
position.x += velocity;
update_hitbox(7, 4, 12, 7, -6, -4, 4);
} else if (face == 2) {
position.y += velocity;
update_hitbox(4, 7, 6, 12, -1, -5, 6);
} else if (face == 3) {
position.x -= velocity;
update_hitbox(7, 4, 12, 7, 0, -4, 4);
}
undo_move_x(entity_to_map_collision_test(position.x, prev_pos.y, map, doorways));
undo_move_y(entity_to_map_collision_test(prev_pos.x, position.y, map, doorways));
frame.count++;
if (frame.count >= 10) {
frame.count = 0;
_velocity_index++;
frame.number++;
if (_velocity_index >= 6) {
_velocity_index = 0;
}
if (frame.number >= frame.max) {
frame.number = 0;
}
}
}
void Snake::take_damage(int damage)
{
hp -= damage;
}
char * Snake::get_frame()
{
if(face == 0) {
return (char *) sprite_snake_y[0][frame.number];
} else if(face == 1) {
return (char *) sprite_snake_x[0][frame.number];
} else if(face == 2) {
return (char *) sprite_snake_y[1][frame.number];
} else if(face == 3) {
return (char *) sprite_snake_x[1][frame.number];
}
return 0;
}
void Snake::draw(N5110 &lcd)
{
lcd.drawSpriteTransparent(get_pos_x()+sprite_size.offset_x,
get_pos_y()+sprite_size.offset_y,
sprite_size.height,
sprite_size.width,
get_frame());
}