#include <trx/core/math.h>
#include <trx/game/const.h>
#include <trx/game/effects.h>
#include <trx/game/objects.h>

static void M_Control(const int16_t effect_num)
{
    EFFECT *const effect = Effect_Get(effect_num);
    const OBJECT *const obj = Object_Get(effect->object_id);

    effect->counter--;
    if (effect->counter % 4 == 0) {
        effect->frame_num--;
        if (effect->frame_num <= obj->mesh_count) {
            effect->frame_num = 0;
        }
    }

    if (effect->counter == 0 || effect->fall_speed > 0) {
        Effect_Destroy(effect_num);
        return;
    }

    effect->pos.x += (effect->speed * Math_Sin(effect->rot.y)) >> W2V_SHIFT;
    effect->pos.z += (effect->speed * Math_Cos(effect->rot.y)) >> W2V_SHIFT;
    if (effect->fall_speed != 0) {
        effect->pos.y += effect->fall_speed;
        effect->fall_speed += GRAVITY;
    }
}

static void M_Setup(OBJECT *const obj)
{
    obj->effect_control_func = M_Control;
    obj->semi_transparent = true;
}

REGISTER_OBJECT(O_WATER_SPRITE, M_Setup)
