#include <StdAfx.h>
#include "mummy.h"
#include <trng_core.h>
#include "utility.h"
#include "Class/TRClass.h"

extern TYPE_InitialiseCreature InitialiseCreature;
extern TYPE_CreatureCollision CreatureCollision;
extern TYPE_CreatureActive CreatureActive;
extern TYPE_phd_atan phd_atan;
extern TYPE_CreatureJoint CreatureJoint;
extern TYPE_CreatureAnimation CreatureAnimation;

#define MUMMY_HITPOINTS 18

enum MUMMY_ANIM {
    MUMMY_EMPTY = 0,
    MUMMY_STOP = 1,
    MUMMY_DEATH = 2,
};

void InitTR1_Mummy(short item_num)
{
    InitialiseCreature(item_num);
}

void CtrlTR1_Mummy(short item_num)
{
    if (!CreatureActive(item_num))
        return;

    ITEM_INFO* item = &Ptr_VetItems[item_num];
    short head = 0;

    if (item->current_anim_state == MUMMY_STOP) {
        head = phd_atan(
                   LaraItem->pos.z_pos - item->pos.z_pos, LaraItem->pos.x_pos - item->pos.x_pos)
            - item->pos.y_rot;
        if (head < -FRONT_ARC)
            head = -FRONT_ARC;
        else if (head > FRONT_ARC)
            head = FRONT_ARC;

        if (item->hit_points <= 0 || item->touch_bits) {
            item->goal_anim_state = MUMMY_DEATH;
        }
    }

    CreatureJoint(item, 0, head);
    CreatureAnimation(item_num, 0, 0);
}

void InitialiseTR1_Mummy()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR1_MUMMY1 + index].set_name("Mummy");
        tomb[TR1_MUMMY1 + index].initialise(TR1_MUMMY1 + index, 0, 0);
        tomb[TR1_MUMMY1 + index].set_shadow_size(0);
        tomb[TR1_MUMMY1 + index].set_func_initialise(InitTR1_Mummy);
        tomb[TR1_MUMMY1 + index].set_func_collide(CreatureCollision);
        tomb[TR1_MUMMY1 + index].set_func_control(CtrlTR1_Mummy);
        tomb[TR1_MUMMY1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR1_MUMMY1 + index].set_health(MUMMY_HITPOINTS);
        tomb[TR1_MUMMY1 + index].set_radius_and_pivot(0, 0);
        tomb[TR1_MUMMY1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR1_MUMMY1 + index].set_creature(TRUE);
        tomb[TR1_MUMMY1 + index].set_rotation_meshtree(2, ROT_Y);
        tomb[TR1_MUMMY1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}
