A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Entity/Bosses/Skull/Skull.cpp

Committer:
el17sm
Date:
2019-05-08
Revision:
46:f09711580d4a
Parent:
44:cc4cecfc639f
Child:
51:4d0cd75e7ed3

File content as of revision 46:f09711580d4a:

#include "Skull.h"
#include <complex>
Skull::Skull(float pos_x, float pos_y)
{
    hp = 20;
    attack = 1;
    
    _dash = false;
    _dash_counter = 0;
    
    moving = true;
    hitbox.width = 19;
    hitbox.height = 9;
    
    sprite_size.width = 21;
    sprite_size.height = 23;
    sprite_size.offset_x = -1;
    sprite_size.offset_y = -14;
    
    _shadow.width = 19;
    _shadow.height = 5;
    _shadow.offset_x = 0;
    _shadow.offset_y = 5;
    
    position.x = pos_x;
    position.y = pos_y;
    update_prev_pos();
    
    frame.count = 0;
    frame.number = 0;
    frame.max = 2;
    face = 2;
    
    velocity = 0.2;
    _hp_drop_chance = 0;
}

void Skull::move(float player_x, float player_y, char * map, bool * doorways)
{
    // Approaching Movement
    if (_dash_counter < DASH_DELAY) {
        _dash = false;
        std::complex<double> pos_diff(player_x - position.x, player_y - position.y);     // defining difference in position as a vector
        position.x += velocity * pos_diff.real() / std::abs(pos_diff);
        position.y += velocity * pos_diff.imag() / std::abs(pos_diff);
        
        // Setting Face
        if (abs(pos_diff.real()) > abs(pos_diff.imag())) {
            if (pos_diff.real() > 0) {
                face = 1;
            } else {
                face = 3;
            }
        } else {
            if (pos_diff.imag() > 0) {
                face = 2;
            } else {
                face = 0;
            }
        }
    } else if (_dash_counter < DASH_DELAY + 28){
        _dash = true;
        velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)];
        if (face == 0){
            position.y -= velocity;
        } else if (face == 1){
            position.x += velocity;
        } else if (face == 2){
            position.y += velocity;
        } else if (face == 3){
            position.x -= velocity;
        }
    } else {
        _dash_counter = 0;
        velocity = 0.2;
    }
    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));
    
    _dash_counter++;
    increment_frames();
}

void Skull::draw(N5110 &lcd)
{
    update_offsets();
    lcd.drawSpriteTransparent(position.x+_shadow.offset_x,
                              position.y+_shadow.offset_y,
                              _shadow.height,
                              _shadow.width,
                              (char *)skull_shadow_sprite[frame.number]);
    lcd.drawSpriteTransparent(position.x+sprite_size.offset_x,
                              position.y+sprite_size.offset_y,
                              sprite_size.height,
                              sprite_size.width,
                              (char *)skull_sprite[face][_dash]);
}

void Skull::take_damage(int damage)
{
    hp -= 1;
}

// Private Functions

void Skull::increment_frames()
{
    if (frame.number < frame.max) {
        frame.count++;
    } else {
        frame.count = 0;
    }
    frame.number = (frame.count/20) % frame.max;
}

void Skull::update_offsets()
{
    if (frame.number == 0) {
        sprite_size.offset_y = -14;
    } else if (frame.number == 1) {
        sprite_size.offset_y = -15;
    }
}