//
// Generated by dtk
// Translation Unit: d_bg_s_movebg_actor.cpp
//

#include "d/d_bg_s_movebg_actor.h"
#include "d/d_bg_w.h"
#include "d/d_com_inf_game.h"
#include "m_Do/m_Do_mtx.h"

/* 800A56B8-800A56FC       .text __ct__16dBgS_MoveBgActorFv */
dBgS_MoveBgActor::dBgS_MoveBgActor() {
    mpBgW = NULL;
}

/* 800A56FC-800A571C       .text CheckCreateHeap__FP10fopAc_ac_c */
static int CheckCreateHeap(fopAc_ac_c* i_actor) {
    return static_cast<dBgS_MoveBgActor*>(i_actor)->MoveBGCreateHeap();
}

const char* dBgS_MoveBgActor::m_name;

int dBgS_MoveBgActor::m_dzb_id;

MoveBGActor_SetFunc dBgS_MoveBgActor::m_set_func;

/* 800A571C-800A57F4       .text MoveBGCreateHeap__16dBgS_MoveBgActorFv */
int dBgS_MoveBgActor::MoveBGCreateHeap() {
    if (!CreateHeap()) {
        return 0;
    }

    mpBgW = new dBgW();
    if (mpBgW && !mpBgW->Set((cBgD_t*)dComIfG_getObjectRes(m_name, m_dzb_id), cBgW::MOVE_BG_e, &mBgMtx)) {
        if (m_set_func != NULL) {
            mpBgW->SetCrrFunc(m_set_func);
        }
    } else {
        mpBgW = NULL;
        return 0;
    }

    return 1;
}

/* 800A57F4-800A58F4       .text
 * MoveBGCreate__16dBgS_MoveBgActorFPCciPFP4dBgWPvR13cBgS_PolyInfobP4cXyzP5csXyzP5csXyz_vUl */
int dBgS_MoveBgActor::MoveBGCreate(const char* i_arcName, int i_dzb_id,
                                   MoveBGActor_SetFunc i_setFunc, u32 i_heapSize) {
    mDoMtx_stack_c::transS(current.pos.x, current.pos.y, current.pos.z);
    mDoMtx_stack_c::YrotM(shape_angle.y);
    mDoMtx_stack_c::scaleM(mScale.x, mScale.y, mScale.z);
    MTXCopy(mDoMtx_stack_c::get(), mBgMtx);

    m_name = i_arcName;
    m_dzb_id = i_dzb_id;
    m_set_func = i_setFunc;

    if (!fopAcM_entrySolidHeap(this, CheckCreateHeap, i_heapSize)) {
        return cPhs_ERROR_e;
    }

    if (dComIfG_Bgsp()->Regist(mpBgW, this)) {
        return cPhs_ERROR_e;
    }

    // return Create() ? cPhs_COMPLEATE_e : cPhs_ERROR_e;  // using enums here changes code gen
    return Create() ? 4 : 5;
}

/* 800A58F4-800A5980       .text MoveBGDelete__16dBgS_MoveBgActorFv */
int dBgS_MoveBgActor::MoveBGDelete() {
    int ret = Delete();

    if (mpBgW != NULL && mpBgW->ChkUsed()) {
        dComIfG_Bgsp()->Release(mpBgW);
    }
    return ret;
}

/* 800A5980-800A5A3C       .text MoveBGExecute__16dBgS_MoveBgActorFv */
int dBgS_MoveBgActor::MoveBGExecute() {
    Mtx* new_mtx = NULL;

    int ret = Execute(&new_mtx);
    if (new_mtx == NULL) {
        mDoMtx_stack_c::transS(current.pos.x, current.pos.y, current.pos.z);
        mDoMtx_stack_c::YrotM(shape_angle.y);
        mDoMtx_stack_c::scaleM(mScale.x, mScale.y, mScale.z);
        MTXCopy(mDoMtx_stack_c::get(), mBgMtx);
    } else {
        MTXCopy(*new_mtx, mBgMtx);
    }

    mpBgW->Move();

    return ret;
}
