#include "MyLockonController.h"

#include "EffectLockon001_Main.h"
#include "EffectLockon001_Sub.h"
#include "jp/gecchi/VioletVreath/actor/my/MyLockonController.h"

using namespace GgafCore;
using namespace GgafDxCore;
using namespace GgafLib;
using namespace VioletVreath;

/** 侾僆僾僔儑儞摉偨傝偺嵟戝壜擻儘僢僋僆儞悢 */
int MyLockonController::max_lockon_num_ = 8;
int MyLockonController::lockon_num_ = 0;


MyLockonController::MyLockonController(const char* prm_name) :
        GgafDummyActor(prm_name, nullptr) {
    _class_name = "MyLockonController";
    pRingTarget_ = NEW GgafLinkedListRing<GgafDxCore::GgafDxGeometricActor>();
    pMainLockonEffect_ = NEW EffectLockon001_Main("MAIN");
    pMainLockonEffect_->inactivate();
    addSubLast(pMainLockonEffect_);
    for (int i = 1; i < MyLockonController::max_lockon_num_; i++) {
        std::string name = "SubLockon("+XTOS(i)+")";
        EffectLockon001_Sub* pSubLockon = NEW EffectLockon001_Sub(name.c_str());
        pSubLockon->inactivate();
        addSubLast(pSubLockon);
    }
}

void MyLockonController::initialize() {
}
void MyLockonController::onReset() {
    //儘僢僋僆儞僞乕僎僢僩偺儕僗僩傪嬻偵偡傞
    pRingTarget_->removeAll();
    //儘僢僋僆儞傾僋僞乕偺儕僙僢僩
    EffectLockon001* pEffectLockon001;
    for (int i = 0; i < MyLockonController::max_lockon_num_; i++) {
        pEffectLockon001 = (EffectLockon001*)(getSub(i));
        pEffectLockon001->releaseLockon();
        pEffectLockon001->inactivate();
    }
}

void MyLockonController::onActive() {
    onReset();
}


void MyLockonController::processBehavior() {
    //儘僢僋僆儞僞乕僎僢僩惗懚妋擣
    GgafDxGeometricActor* pMainLockonTarget = pRingTarget_->getCurrent(); //儊僀儞儘僢僋僆儞偺僞乕僎僢僩
    GgafDxGeometricActor* pTarget = pRingTarget_->getCurrent();  //僞乕僎僢僩僇乕僜儖
    GgafMainActor* pLockonEffect_Active = (GgafMainActor*)getSubFirst();         //儘僢僋僆儞僄僼僃僋僩僇乕僜儖
    int n = pRingTarget_->length();
    for (int i = 0; i < n; i++) {
        if (pTarget->isActiveInTheTree() && pTarget->getStatus()->get(STAT_Stamina) > 0) {
            //僞乕僎僢僩偑懚柦側傜偽
            //僄僼僃僋僩傾僋僞乕偺僞乕僎僢僩峏怴丄僄僼僃僋僩傪僘儖偭偲偟傑偡
            ((EffectLockon001*)pLockonEffect_Active)->lockon(pTarget);
            if (!pLockonEffect_Active->isActiveInTheTree()) {
                pLockonEffect_Active->activate();
            }
            pLockonEffect_Active = pLockonEffect_Active->getNext(); //師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊
            pTarget = pRingTarget_->next();                         //師夞張棟僞乕僎僢僩傪師傊
        } else {
            //僞乕僎僢僩偑巰朣帪
            if (pTarget == pMainLockonTarget) {
                //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱偁偭偨応崌
                if (pRingTarget_->length() == 1) {
                    //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱儕儞僌偺嵟屻偺堦偮偺応崌
                    pRingTarget_->remove();                                    //僞乕僎僢僩敳偒弌偟
                    ((EffectLockon001*)pLockonEffect_Active)->releaseLockon(); //儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                    pTarget = nullptr;
                    break;
                } else {
                    //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱儕儞僌偺嵟屻偺堦偮偱偼柍偄応崌
                    pRingTarget_->remove();               //僞乕僎僢僩敳偒弌偟
                    pTarget = pRingTarget_->getCurrent(); //師夞張棟僞乕僎僢僩傪師傊乮remove()偵傛傝師偺僞乕僎僢僩偼getCurrent()偲側傞乯
                    pMainLockonTarget = pTarget;          //儊僀儞儘僢僋僆儞偺僞乕僎僢僩傪峏怴
                    //儊僀儞儘僢僋僆儞僄僼僃僋僩傪捈嬤儘僢僋僆儞傊栠偡偟
                    //儊僀儞儘僢僋僆儞僄僼僃僋僩偺師偺僄僼僃僋僩傪夝曻偡傞偲偄偆摦嶌傪偟偨偄
                    ((EffectLockon001*)pLockonEffect_Active->getNext())->releaseLockon(); //師偺儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                    pLockonEffect_Active->getNext()->moveLastImmed();               //師偺儘僢僋僆儞僄僼僃僋僩傪枛旜傊
                    //乽師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊乿偺張棟懳徾堏摦偼晄梫丅師偵張棟偟偨偄儘僢僋僆儞僄僼僃僋僩傾僋僞乕偦偺傑傑偱偁傞偨傔丅
                }
            } else {
                //巰朣僞乕僎僢僩偑僒僽儘僢僋僆儞偺僞乕僎僢僩偱偁偭偨応崌
                pRingTarget_->remove();               //僞乕僎僢僩敳偒弌偟
                pTarget = pRingTarget_->getCurrent(); //師夞張棟僞乕僎僢僩傪師傊乮remove()偵傛傝師偺僞乕僎僢僩偼getCurrent()偲側傞乯
                ((EffectLockon001*)pLockonEffect_Active)->releaseLockon(); //儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                pLockonEffect_Active = pLockonEffect_Active->getNext();    //師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊
                pLockonEffect_Active->getPrev()->moveLastImmed();    //崱夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕傪枛旜傊
            }
        }
    }
}

void MyLockonController::processJudgement() {
}

void MyLockonController::onInactive() {
    onReset();
}

void MyLockonController::lockon(GgafDxGeometricActor* prm_pTarget) {
    if (MyLockonController::lockon_num_ == 0) {
        return; //尰嵼偺儘僢僋僆儞壜擻悢偑0
    }

    if (pRingTarget_->indexOf(prm_pTarget) == -1) {
        //儘僢僋僆儞嵪傒偵柍偄偺偱丄儘僢僋僆儞偡傞
        if (pRingTarget_->length() >= MyLockonController::lockon_num_) { //僞乕僎僢僩儕僗僩偑婛偵枮堳偺応崌
            //儘僢僋僆儞僞乕僎僢僩儘乕僥乕僩
            pRingTarget_->prev();           //僇儗儞僩傪嵟廔梫慺傊
            pRingTarget_->set(prm_pTarget, false); //嵟廔梫慺偩偭偨偲偙傠偵儊僀儞僞乕僎僢僩忋彂偒
            //儘僢僋僆儞僄僼僃僋僩傾僋僞乕偼偦偺傑傑
            //偙傟偵傛傝丄processBehavior() 偺僞乕僎僢僩懚柦帪偺偟傚傝偵傛傝
            //僘儖僢偲偢傟傞丅
        } else {   //傑偩僞乕僎僢僩儕僗僩偵壗傕側偄丄埥偄偼梋桾偑偁傞乮捛壛壜擻側応崌乯
            //僞乕僎僢僩儘乕僥乕僩
            pRingTarget_->addPrev(prm_pTarget, false);
            pRingTarget_->prev();
            //儘僢僋僆儞僄僼僃僋僩
            if (pRingTarget_->length() == 1) {
                //嵟弶偺儘僢僋僆儞僞乕僎僢僩捛壛帪乮儊僀儞儘僢僋僆儞僞乕僎僢僩乯
                GgafMainActor* pLockonEffect = (GgafMainActor*)getSubFirst(); //儊僀儞儘僢僋僆僄僼僃僋僩
                pLockonEffect->activate();
                ((EffectLockon001*)pLockonEffect)->lockon(prm_pTarget);

                //嵟弶偺儘僢僋僆儞僞乕僎僢僩埲奜偺捛壛帪乮僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
            } else if (pRingTarget_->length() > 1) {
                //Sub儘僢僋僆儞捛壛帪
                if (pRingTarget_->length() >= 3) {
                    //俁屄栚偺埲崀僞乕僎僢僩捛壛帪乮俀屄栚埲崀偺僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
                    //儘僢僋僆儞僄僼僃僋僩偺摿庩側儘乕僥乕僩傪峴偆丅乮仸嵟屻偺僐儊儞僩亙捛壛偺応崌亜嶲徠乯
                    //Last傪愗傝弌偡
                    GgafMainActor* pLockonEffect = (GgafMainActor*)getSubFirst()->getPrev(); //Last
                    GgafMainActor* pLockonEffect_Next = pLockonEffect->getNext(); //Main儘僢僋僆儞偲側傞
                    GgafMainActor* pLockonEffect_Prev = pLockonEffect->getPrev();
                    pLockonEffect_Prev->_pNext = pLockonEffect_Next;
                    pLockonEffect_Next->_pPrev = pLockonEffect_Prev;
                    pLockonEffect_Prev->_is_last_flg = true;
                    pLockonEffect->_is_last_flg = false;
                    //First->Next 偺娫偵擖傟傞
                    GgafMainActor* pMainLockonEffect = (GgafMainActor*)getSubFirst();
                    GgafMainActor* pMainLockonEffect_Next = (GgafMainActor*)getSubFirst()->getNext();
                    pMainLockonEffect->_pNext = pLockonEffect;
                    pLockonEffect->_pPrev = pMainLockonEffect;
                    pLockonEffect->_pNext = pMainLockonEffect_Next;
                    pMainLockonEffect_Next->_pPrev = pLockonEffect;
                    pLockonEffect->activate(); //僒僽儘僢僋僆儞桳岠偵
                    //僒僽儘僢僋僆儞僄僼僃僋僩儘僢僋僆儞両
                    ((EffectLockon001*)pLockonEffect)->lockon(pRingTarget_->getNext());
                } else {
                    //俀屄栚偺僞乕僎僢僩捛壛帪乮嵟弶偺僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
                    GgafMainActor* pLockonEffect = (GgafMainActor*)getSubFirst()->getPrev(); //俀偮側偺偱寢嬊Next偺埵抲
                    pLockonEffect->activate(); //僒僽儘僢僋僆儞桳岠偵
                    ((EffectLockon001*)pLockonEffect)->lockon(pRingTarget_->getNext());
                }
            }
        }
    } else {
        //prm_pTarget 偼婛偵儘僢僋僆儞嵪傒
    }
}

void MyLockonController::releaseAllLockon() {
    if (pRingTarget_->length() == 0) {
        return;
    }
    pRingTarget_->removeAll();
    //儘僢僋僆儞僄僼僃僋僩傪僀儞傾僋僥傿僽偵偡傞
    GgafMainActor* pLockonEffect = (GgafMainActor*)getSubFirst();
    while (pLockonEffect) {
        ((EffectLockon001*)pLockonEffect)->releaseLockon();
        if (pLockonEffect->isLast()) {
            break;
        } else {
            pLockonEffect = pLockonEffect->getNext();
        }
    }
}

MyLockonController::~MyLockonController() {
    GGAF_DELETE(pRingTarget_);
}

void MyLockonController::dumpTarget(GgafDxGeometricActor* pMain) {
    if (pRingTarget_->getFromFirst(0) == nullptr) {
        _TEXT_("nullptr\n");
        return;
    } else {
        _TEXT_("佁");
    }
    for (int i = 0; i < pRingTarget_->length(); i++) {
        if (pRingTarget_->getFromFirst(i) == pMain) {
            _TEXT_("両");
        }
        _TEXT_(pRingTarget_->getFromFirst(i)->getName()<<"["<<i<<"]佁");
    }
    _TEXT_(" ... avtive="<<pRingTarget_->getCurrent()->getName()<<"\n");
}

//亂旛朰榐亃
//乽儘僢僋僆儞僞乕僎僢僩(揋)偲儘僢僋僆儞僄僼僃僋僩(徠弨)偺娭學乿(2010/09/14)
//
//儘僢僋僆儞帪丄儘僢僋僆儞儕儕乕僗帪偺
//儘僢僋僆儞僞乕僎僢僩偲儘僢僋僆儞僄僼僃僋僩偺娭學傪儊儌偲偟偰巆偡丅
//
//俵      乧 儊僀儞儘僢僋僆儞僄僼僃僋僩
//S0乣S3  乧 僒僽儘僢僋僆儞僄僼僃僋僩
//t1乣t5  乧 儘僢僋僆儞僞乕僎僢僩
//
//儊僀儞儘僢僋僆儞僄僼僃僋僩(俵)偺応強偵埵抲偡傞儘僢僋僆儞僞乕僎僢僩傪
//乽儊僀儞儘僢僋僆儞僞乕僎僢僩乿
//僒僽儘僢僋僆儞僄僼僃僋僩(S0乣S3)偺応強偵埵抲偡傞儘僢僋僆儞僞乕僎僢僩傪
//乽僒僽儘僢僋僆儞僞乕僎僢僩乿
//偲屇傇偙偲偵偡傞丅

//亙捛壛偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
//          伀            t1儘僢僋僆儞
// 佁t1佁
// 僄僼僃僋僩傾僋僞乕曄壔柍偟
//          伀            t2捛壛儘僢僋僆儞
// 佁t2佁t1佁
// 佁俵佁S3佁S0佁S1佁S2佁
//          伀            t3捛壛儘僢僋僆儞
// 佁t3佁t2佁t1佁
// 佁俵佁S2佁S3佁S0佁S1佁
//          伀            t4捛壛儘僢僋僆儞
// 佁t4佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁
//          伀            t5捛壛儘僢僋僆儞
// 佁t5佁t4佁t3佁t2佁t1佁
// 佁俵佁S0佁S1佁S2佁S3佁

// 亙枮堳偱捛壛偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕偼憖嶌晄梫
//
// 佁t5佁t4佁t3佁t2佁t1佁
// 佁t6佁t5佁t4佁t3佁t2佁   僘儖偭
// 佁t7佁t6佁t5佁t4佁t3佁   僘儖偭
// 佁t8佁t7佁t6佁t5佁t4佁   僘儖偭
// 佁t9佁t8佁t7佁t6佁t5佁   僘儖偭偲偟傑偡

//亙巰朣偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t5佁t4佁t3佁t2佁
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒曄壔柍偟乮S3偑枛旜傊乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t5佁t4佁t3佁t1佁
// 佁俵佁S0佁S1佁S3佁S2佁    丒丒丒S2偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t5佁t4佁t2佁t1佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t4)
// 佁t5佁t3佁t2佁t1佁
// 佁俵佁S2佁S3佁S1佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t5)
// 佁t4佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒 S0偑枛旜傊乮拲堄乯堏摦偟偰lockonRelase()



//亙愗傟傞応崌2亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t4佁t3佁t1佁
// 佁俵佁S0佁S1佁S3佁S2佁    丒丒丒S2偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t4佁t3佁t1佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t4佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t4)
// 佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊乮拲堄乯堏摦偟偰lockonRelase()



//亙愗傟傞応崌3亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t3佁t2佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t3佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()



//亙愗傟傞応崌4亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t2佁t1佁
//          伀    remove(t1)
// 佁t2佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t2佁t1佁
//          伀    remove(t2)
// 佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜僿堏摦偟偰lockonRelase()


//亙愗傟傞応崌5亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t1佁
//          伀    remove(t1)
//
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒曄壔柍偟偱M傪lockonRelase()
