
import Joystick_mag, { SpeedType } from "../../../joystick/Joystick_mag";
import Joystick from "../../../util/Joystick";
import { FFAttr } from "../../data/FFCalAttr";
import { __SkillData } from "../../data/sdata/SManage";
import BObject from "../bullet/BObject";
import FSprite, { SpriteActionType } from "./FSprite";
import MBomb from "./skill/mainSkill/MBomb";
import MFWind from "./skill/mainSkill/MFWind";
import MMagic from "./skill/mainSkill/MMagic";
import MShotgun from "./skill/mainSkill/MShotgun";
import SkillBase from "./skill/SkillBase";

/**
 * 主角
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class MSprite extends FSprite {
    /**
     * 技能1
     * 武器技能
     */
    public skill1: SkillBase
    /**
     * 技能2
     * 职业技能
     */
    public skill2: SkillBase

    private musicWalkId = -1;
    private muiscRunId = -1;

    /**
     * 移动速度
     */
    private speedN = 120000;

    public start(){
        super.start()
        this.spineEventListener()
    }

    public setAttrData(attrData: FFAttr) {
        super.setAttrData(attrData)
        // attrData.weaponSkill = 1205
        let _skillData = this.ff.main.sManage.getSkillById(attrData.weaponSkill)
        if (_skillData) {
            if (_skillData.type == 1) {//散弹
                let skill1: MShotgun = this.node.addComponent(MShotgun) as any
                skill1.count = _skillData.value2
                skill1.bcount = _skillData.value3
                this.loadSkill(_skillData, skill1)
                this.skill1 = skill1
            } else if (_skillData.type == 2) {//丢炸弹
                let skill1: MBomb = this.node.addComponent(MBomb) as any
                this.loadSkill(_skillData, skill1)
                this.skill1 = skill1
            } else if (_skillData.type == 3) {//法术
                let skill1 = this.node.addComponent(MMagic) as any
                this.loadSkill(_skillData, skill1)
                this.skill1 = skill1
            } else if (_skillData.type == 4) {//龙卷风
                let skill1 = this.node.addComponent(MFWind) as any
                this.loadSkill(_skillData, skill1)
                this.skill1 = skill1
            }
            this.skill1._skillData = _skillData
        }

    }

    private loadSkill(_skillData: __SkillData, skill1) {
        cc.resources.load('icon/skills/' + _skillData.effect + '/bullet', cc.Prefab, (err, prefab: cc.Prefab) => {
            if (err) {
                cc.error(err);
            } else {
                skill1.mBullet = prefab
            }
        });
    }


    public setJoystick(mJoystick: Joystick_mag) {
        mJoystick.setListen((speedType: SpeedType, moveVec?: cc.Vec2) => {
            if (speedType == SpeedType.STOP) {
                this.stopMove()
            } else {
                this.startMove(moveVec)
            }
            this.playMusic();
        });
    }
    update(dt) {
        if (this.gamePause) {
            return;
        }
        if (this.isWalk) {
            this.mRigidBody.applyLinearImpulse(
                cc.v2(this.moveV2.x * this.speedN * dt, this.moveV2.y * this.speedN * dt),
                this.mRigidBody.getWorldCenter(),
                true
            );
        }
    }
    /**
 * @param v2 开始向量移动
 */
    public startMove(v2: cc.Vec2) {
        if (!this.isWalk) {
            if (!this.currentShooting) {
                this.spine.setAnimation(0, SpriteActionType.move, true)
            }
        }
        this.isWalk = true
        this.moveV2 = v2;
        //有怪物的时候，面向怪物
        let enemy = this.findEnemy(2000);
        if(enemy.sprite){
            let abs = Math.abs(this.spine.node.scaleX);
            if(this.node.x > enemy.sprite.node.x){
                this.spine.node.scaleX = -abs;
            }else{
                this.spine.node.scaleX = abs;
            }
        }else{
            let abs = Math.abs(this.spine.node.scaleX);
            if (v2.x <= 0.001 && v2.x >= -0.001) {
            } else if (v2.x > 0) {
                this.spine.node.scaleX = abs;
            } else {
                this.spine.node.scaleX = -abs;
            }
        }
    }

    /**
     * 停止移动
     */
    public stopMove() {
        this.isWalk = false
        if (!this.currentShooting) {
            this.spine.setAnimation(0, SpriteActionType.stand, true)
        }
    }
    /**
     * 设置是否跑
     * @param isRuning 
     */
    public setRuning(isRuning: boolean) {
        // super.setRuning(isRuning);
        this.playMusic();
    }

    private playMusic() {
        if (this.isWalk) {
            let setting = this.ff.main.player.setting;
            if (setting.music_ON_OFF_2) {
                if (this.isRuning) {
                    if (this.muiscRunId == -1) {
                        this.muiscRunId = cc.audioEngine.play(this.ff.mRun, true, setting.music_Slider_2);
                    }
                    if (this.musicWalkId != -1) {
                        cc.audioEngine.stop(this.musicWalkId);
                        this.musicWalkId = -1;
                    }
                } else {
                    if (this.musicWalkId == -1) {
                        this.musicWalkId = cc.audioEngine.play(this.ff.mWalk, true, setting.music_Slider_2);
                    }
                    if (this.muiscRunId != -1) {
                        cc.audioEngine.stop(this.muiscRunId);
                        this.muiscRunId = -1;
                    }
                }
            }
        } else {
            if (this.musicWalkId != -1) {
                cc.audioEngine.stop(this.musicWalkId);
                this.musicWalkId = -1;
            }
            if (this.muiscRunId != -1) {
                cc.audioEngine.stop(this.muiscRunId);
                this.muiscRunId = -1;
            }
        }
    }
    public setPause(pause: boolean) {
        this.gamePause = pause
        if (this.gamePause) {
            this.isWalk = false
            this.currentShooting = false
            this.isShooting = false
            this.spine.setAnimation(0, SpriteActionType.stand, true)
        }
    }

    /**
     * 当前动作是否处于射击状态
     */
    private currentShooting = false;
    /**
     * 设置攻击
     * @param isShooting 
     */
    public setShooting(isShooting: boolean) {
        // cc.log('isShooting : ',isShooting);
        this.isShooting = isShooting;
        if (this.isShooting && !this.currentShooting) {
            this.currentShooting = true;
            this.shooting()
        }
    }

    private shooting() {
        this.playAction(SpriteActionType.atk, false, () => {
            if (this.isShooting) {
                this.shooting()
            } else {
                this.endShooting()
            }
        });
    }
    /**
     * 射击结束后
     */
    private endShooting() {
        this.currentShooting = false;
        if(this.mWeapon1){
            this.mWeapon1.rotation = 180;
        }
        if (this.isWalk) {
            this.spine.setAnimation(0, SpriteActionType.move, true)
        } else {
            this.spine.setAnimation(0, SpriteActionType.stand, true)
        }
    }

    /**
        * 注册帧事件
        */
    private spineEventListener() {
        this.spine.setEventListener((trackEntry, event) => {
            if (this.isValid && !this.gamePause) {
                // var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                // cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
                if (this.node.isValid && event.data.name == 'hit') {
                    let node = cc.instantiate(this.mBullet);
                    node.group = 'bullet';

                    let x = this.node.x + this.mAtkSite.worldX
                    let y = this.node.y + this.mAtkSite.worldY

                    let pos = cc.v2(x, y);
                    node.setPosition(pos);

                    let bObject = node.getComponent(BObject);
                    bObject.setSprite(this);


                    let csprite: FSprite = this.findEnemy(2000).sprite;

                    node.parent = this.map.mSprites;
                    if (csprite && csprite.isValid) {
                        bObject.fire(csprite.node);
                        // this.addSendEffect(bObject.mStartEffect, csprite.node);
                    } else {//没有目标的时候,向朝向开火
                        bObject.fireAngleV2(this.moveV2);
                        // this.addSendEffect(bObject.mStartEffect, null);
                    }
                    let angle = Math.atan2(this.moveV2.y, this.moveV2.x);
                    this.setWeaponAngle(angle);
                    if (this.fireCallback) {
                        this.fireCallback();
                    }
                    if (this.node == this.ff.mainSprite.node) {
                        if (this.hitCount > 4) {
                            this.ff.shockMap();
                            this.hitCount = 0;
                        } else {
                            this.hitCount++;
                        }
                    }
                    this.ff.main.playerEffectByPath('music/magic_1001_fs');

                }
            }
        });
    }

}
