#include "stdafx.h"
#include "VreathMagic.h"

#include "jp/ggaf/dxcore/actor/supporter/GgafDxKuroko.h"
#include "jp/ggaf/dxcore/actor/supporter/GgafDxScaler.h"
#include "jp/gecchi/VioletVreath/actor/my/MagicMeter/magic/effect/EffectVreathMagic001.h"
#include "jp/gecchi/VioletVreath/actor/my/MyShip.h"
#include "jp/gecchi/VioletVreath/God.h"
#include "jp/gecchi/VioletVreath/scene/Universe/World/GameScene/MyShipScene.h"

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

VreathMagic::VreathMagic(const char* prm_name, int* prm_pMP)
    : Magic(prm_name, prm_pMP,
            5,                       //max_level
            1000      , 1.0, 0.9,    //婎杮杺朄僐僗僩, 亄侾儗儀儖枅偺僐僗僩憹壛棪  , 旘傃儗儀儖帪偺僐僗僩嶍尭棪
            60        , 1.0, 0.9,    //婎杮塺彞帪娫  , 亄侾儗儀儖枅偺塺彞帪娫憹壛棪, 旘傃儗儀儖帪偺塺彞帪娫嶍尭棪
            60        , 1.0, 0.9,    //婎杮敪摦帪娫  , 亄侾儗儀儖枅偺敪摦帪娫憹壛棪, 旘傃儗儀儖帪偺敪摦帪娫嶍尭棪
            60*60*3   , 0.3,         //婎杮帩懕帪娫  , 亄侾儗儀儖枅偺帩懕帪娫偺忔棪
            0         , 0.0          //婎杮堐帩僐僗僩, 亄侾儗儀儖枅偺堐帩僐僗僩偺忔棪
        ) {

    //嵍楍慖戰壜擻忬懺丄塃楍慖戰晄壜忬懺
    //    | 0          |  1         |  2 Lockon嘮 |  3 Lockon嘮 |  4 Vreath嘮|  5 Vreath嘮|  6 Tractor嘮|  7 Tractor嘮|
    //    | 8          |  9         | 10 Lockon嘫 | 11 Lockon嘫 | 12 Vreath嘫| 13 Vreath嘫| 14 Tractor嘫| 15 Tractor嘫|
    //    | 16         | 17         | 18 Lockon嘪 | 19 Lockon嘪 | 20 Vreath嘪| 21 Vreath嘪| 22 Tractor嘪| 23 Tractor嘪|
    //    | 24         | 25         | 26 Lockon嘩 | 27 Lockon嘩 | 28 Vreath嘩| 29 Vreath嘩| 30 Tractor嘩| 31 Tractor嘩|
    //    | 32         | 33         | 34 Lockon嘨 | 35 Lockon嘨 | 36 Vreath嘨| 37 Vreath嘨| 38 Tractor嘨| 39 Tractor嘨|
    //    | 40 Option嘳| 41 Option嘳| 42 Lockon嘦 | 43 Lockon嘦 | 44 Vreath嘦| 45 Vreath嘦| 46 Tractor嘦| 47 Tractor嘦|
    //    | 48 Option嘰| 49 Option嘰| 50 Lockon嘥 | 51 Lockon嘥 | 52 Vreath嘥| 53 Vreath嘥| 54 Tractor嘥| 55 Tractor嘥|
    //    | 56 Option嘯| 57 Option嘯| 58 Empty    | 59 Empty    | 60 Empty   | 61 Empty   | 62 Empty    | 63 Empty    |
    //    | 64 Option嘮| 65 Option嘮| 66 Torpedo嘮| 67 Torpedo嘮| 68 Laser嘮 | 69 Laser嘮 | 70 Speed嘮  | 71 Speed嘮  |
    //    | 72 Option嘫| 73 Option嘫| 74 Torpedo嘫| 75 Torpedo嘫| 76 Laser嘫 | 77 Laser嘫 | 78 Speed嘫  | 79 Speed嘫  |
    //    | 80 Option嘪| 81 Option嘪| 82 Torpedo嘪| 83 Torpedo嘪| 84 Laser嘪 | 85 Laser嘪 | 86 Speed嘪  | 87 Speed嘪  |
    //    | 88 Option嘩| 89 Option嘩| 90 Torpedo嘩| 91 Torpedo嘩| 92 Laser嘩 | 93 Laser嘩 | 94 Speed嘩  | 95 Speed嘩  |
    //    | 96 Option嘨| 97 Option嘨| 98 Torpedo嘨| 99 Torpedo嘨|100 Laser嘨 |101 Laser嘨 |102 Speed嘨  |103 Speed嘨  |
    //    |104 Option嘦|105 Option嘦|106 Torpedo嘦|107 Torpedo嘦|108 Laser嘦 |109 Laser嘦 |110 Speed嘦  |111 Speed嘦  |
    //    |112 Option嘥|113 Option嘥|114 Torpedo嘥|115 Torpedo嘥|116 Laser嘥 |117 Laser嘥 |118 Speed嘥  |119 Speed嘥  |
    //    |120 Empty   |121 Empty   |122 Empty    |123 Empty    |124 Empty   |125 Empty   |126 Empty    |127 Empty    |

//    lvinfo_[7].pno_ =  4;
//    lvinfo_[6].pno_ = 12;
    lvinfo_[5].pno_ = 20;
    lvinfo_[4].pno_ = 28;
    lvinfo_[3].pno_ = 36;
    lvinfo_[2].pno_ = 44;
    lvinfo_[1].pno_ = 52;
    lvinfo_[0].pno_ = 60;

    pEffect_ = NEW EffectVreathMagic001("EffectVreathMagic001");
    pEffect_->inactivate();
    addSubGroup(pEffect_);

    for (int i = 0; i <= MMETER_MAX_LEVEL; i++) {
        beat_time_[i]               = 0;
        apaInt_vreath_per_frame_[i] = 0;
        aInt_total_vreath_[i]       = 0;
        r_add_vreath_[i]            = 0.0;
    }
    //侾夞偺屇媧帪娫僥乕僽儖
    beat_time_[0] = 0.0;
    beat_time_[1] = 180;
    beat_time_[2] = beat_time_[1]/2; //偩傫偩傫
    beat_time_[3] = beat_time_[2]/2; //屇媧偑
    beat_time_[4] = beat_time_[3]/2; //憗偔
    beat_time_[5] = beat_time_[4]/2; //側傞両
//    beat_time_[6] = beat_time_[5]/2;
//    beat_time_[7] = beat_time_[6]/2;

    //枅僼儗乕儉壛嶼偝傟傞Vreath忔棪僥乕僽儖
    for (int lv = 0; lv <= max_level_; lv++) {
        //r_add_vreath_ = - sqrt((max_lv-1)^2 - (lv-1)^2) + max_lv
        //max_level=7 偺応崌
        //lv =7.0 偱 棪 7.0
        //lv =1.0 偱 棪 1.0
        //偲側傞傛偆側1/4墌
        r_add_vreath_[lv] =  -1.0*sqrt((double)(((max_level_-1)*(max_level_-1)) - ((lv-1)*(lv-1)))) + max_level_;
    }

    for (int lv = 0; lv <= max_level_; lv++) {
        if (beat_time_[lv] == 0) {
            apaInt_vreath_per_frame_[lv] = NEW int[1];
            apaInt_vreath_per_frame_[lv][0] = 0;
        } else {
            apaInt_vreath_per_frame_[lv] = NEW int[beat_time_[lv]];
            //慜敿
            for (frame f = 0; f <= beat_time_[lv]/2; f++) {
                int t = f*3600.0/beat_time_[lv];
                apaInt_vreath_per_frame_[lv][f] = MY_SHIP_VREATH_COST*2.0*UTIL::SMOOTH_DV[t];
            }
            //屻敿
            for (frame f = beat_time_[lv]/2 + 1; f < beat_time_[lv]; f++) {
                apaInt_vreath_per_frame_[lv][f] = 0; //媧傢側偄亖vreath偼枅僼儗乕儉MY_SHIP_VREATH_COST嶍傜傟傞
            }
        }
    }
    //奺儗儀儖偺憤屇媧検(aInt_total_vreath_[])傪寁嶼
    for (int lv = 0; lv <= max_level_; lv++) {
        if (beat_time_[lv] == 0) {
            aInt_total_vreath_[lv] = 0;
        } else {
            int sum_v_onebeat = 0; //1屇媧偺懅偺検
            for (frame f = 0; f < beat_time_[lv]; f++) {
                sum_v_onebeat += apaInt_vreath_per_frame_[lv][f] * r_add_vreath_[lv];
            }
            for (frame f = 0; f < beat_time_[lv]; f++) {
                sum_v_onebeat -= MY_SHIP_VREATH_COST;
            }
            //屇媧夞悢傪忔偠偰憤屇媧検傪寁嶼
            aInt_total_vreath_[lv] = sum_v_onebeat * ((1.0 * lvinfo_[lv].time_of_effect_) / beat_time_[lv]);
        }
    }
}

void VreathMagic::onReset() {
    Magic::onReset();
    pEffect_->inactivate();
}

void VreathMagic::processCastBegin(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->positionAs(P_MYSHIP);
        pEffect_->setAlpha(0.9);
        pEffect_->_pKuroko->setFaceAngVelo(AXIS_Z, 100);
        pEffect_->pScaler_->setScale(1000);
        pEffect_->activate();
    }
}

void VreathMagic::processCastingBehavior(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->positionAs(P_MYSHIP);
        pEffect_->pScaler_->addScale(10);
    }
}

void VreathMagic::processCastingCancel(int prm_now_level) {
    pEffect_->inactivate();
}

void VreathMagic::processCastFinish(int prm_now_level, int prm_new_level, int prm_result_invoke) {
}

void VreathMagic::processInvokeBegin(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->pScaler_->setScale(1000);
        pEffect_->_pKuroko->setFaceAngVelo(AXIS_Z, 3000);
    }
}

void VreathMagic::processInvokingBehavior(int prm_now_level, int prm_new_level) {
    if (prm_new_level > prm_now_level) {
        pEffect_->pScaler_->addScale(100);
    }
}

void VreathMagic::processInvokingCancel(int prm_now_level) {
    pEffect_->inactivate();
}

void VreathMagic::processInvokeFinish(int prm_now_level, int prm_new_level, int prm_result_effect) {
    pEffect_->inactivate();
}

void VreathMagic::processEffectBegin(int prm_last_level, int prm_now_level) {
    if ( prm_now_level > 0) {
    } else {
        //儗儀儖0傊儗儀儖僟僂儞帪
        pEffect_->inactivate();
    }
}
void VreathMagic::processEffectingBehavior(int prm_last_level, int prm_now_level) {
    if (beat_time_[prm_now_level] > 0) {
        frame f = getActiveFrame() % beat_time_[prm_now_level];
#ifdef MY_DEBUG
        if (f > beat_time_[prm_now_level]) {
            throwGgafCriticalException("VreathMagic::processEffectingBehavior 斖埻奜偱偡丅 f="<<f<<" beat_time_["<<prm_now_level<<"]");
        }
#endif
        int add_vreath = apaInt_vreath_per_frame_[prm_now_level][f] * r_add_vreath_[prm_now_level];
        P_MYSHIP->_pStatus->plus(STAT_Stamina, add_vreath);
    } else {

    }
}

int VreathMagic::calcTotalVreath(int prm_now_level, int prm_target_up_level) {
    int sum_vreath = 0;
    for (int lv = prm_now_level+1; lv <= prm_target_up_level; lv++) {
        sum_vreath += aInt_total_vreath_[lv];
    }
    return sum_vreath;
}

VreathMagic::~VreathMagic() {
    for (int lv = 0; lv <= max_level_; lv++) {
        GGAF_DELETEARR(apaInt_vreath_per_frame_[lv]);
    }
}
