

import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import Vector3 = Laya.Vector3;
import {  MusicType } from "./ColliderAndMusicType";
import { MathTool } from "../Util/MathTool";
import {CameraManager} from "../Manager/CameraManager";
import { GlobalParam } from "../Net/GlobalParam";
import { Scene3DManager } from "./Scene3DManager";
import {ConstructManager} from "../Manager/ConstructManager";
import {PlaneManager} from "../Manager/PlaneManager";
import StreetManager from "../Manager/StreetManager";
import Quaternion = Laya.Quaternion;
import { ParticleManager } from "./ParticleManager";
import {MainRoleManager} from "../Manager/MainRoleManager";
import {RoleManager} from "../Manager/RoleManager";
import Sprite = Laya.Sprite;

export  class MainRole extends Laya.Script3D {
    public selfobj: Sprite3D;
    
    public animator: Laya.Animator;
    public _speed: Vector3;
    private rayPoint: Transform3D;
    private shootPos: Transform3D;
    public body: Sprite3D;
    public forW: Vector3;
    private quaternion1: Quaternion;
    public _transform: Transform3D;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this._transform = this.selfobj.transform;
        this.body = this.selfobj.getChildAt(0) as Sprite3D;
        this.animator = this.body.getComponent(Laya.Animator);
        this.rayPoint = (this.selfobj.getChildByName("rayPoint") as Sprite3D).transform;
        this.shootPos = (this.selfobj.getChildByName("bulletPos") as Sprite3D).transform;
        this._speed = MathTool.VecZero;
        this._speed.z = GlobalParam.playerspeed;
        this.forW = MathTool.VecZero;
        this.quaternion1 = new Quaternion();
    }

    private isarrive = false;
    public isdead = false;
    Move() {
        if (this.isarrive) {
            if (this.isdead) return;
            this.RayReset(); return;
        }
        this._transform.translate(this._speed);
        let pz = this._transform.localPositionZ;
        ConstructManager.Instance.CheckMainRolePos(pz);
        PlaneManager.Instance.CheckPlayerRoadPos(pz);
        StreetManager.Instance.CheckPlayerRoadPos(pz);
        CameraManager.Instance.CameraFollow(pz);
        if (this.isdead) return;
        this.RayReset();
    }

    Near() {
        this.isarrive = true;
    }

    Start() {
        this.isdead = false;
        this.isarrive = false;
        Laya.timer.frameLoop(1, this, this.Move);
    }

    /*********************************************************** */
    private drawPentagram(): void {
        var canvas: Sprite = new Sprite();
        Laya.stage.addChild(canvas);
        var path: Array<number> = [];
        path.push(0, -130);
        path.push(33, -33);
        path.push(137, -30);
        path.push(55, 32);
        path.push(85, 130);
        path.push(0, 73);
        path.push(-85, 130);
        path.push(-55, 32);
        path.push(-137, -30);
        path.push(-33, -33);

        canvas.graphics.drawPoly(Laya.stage.width / 2, Laya.stage.height / 2, path, "#FF7F50");
    }
    /*********************************************************** */
    

    public PlayAni(clip: string, speed, callback?) {
        this.animator.play(clip);
        //this.animator.play(clip);
        if (!speed) speed = 1;
        this.animator.speed = speed;
    }
    Slider(x) {
        //if (this.isdead) return;
        this._transform.localPositionX += x;
        let px = this._transform.localPositionX;
        let abs = Math.abs(px);
        if (abs > GlobalParam.playerMoveMaxX) {
            if (px > 0) this._transform.localPositionX = GlobalParam.playerMoveMaxX;
            else this._transform.localPositionX = -GlobalParam.playerMoveMaxX
        }
    }

    CreateBullet() {
        if (this.isdead) return null;
        return this.shootPos;
    }

    Dead(pos) {
        this.isdead = true;
        this.body.active = false;
        ParticleManager.Instance.BloodEffect(pos);
        MainRoleManager.Instance.CheckGameOver(true);
    }

    Reset() {
        this.body.active = true;
        this.PlayAni(MusicType.Idle, 1);
        Laya.timer.clearAll(this);
    }

    private _scene;
    private rayF: Laya.Ray;
    private outHitF: Laya.HitResult;
    private rayL: Laya.Ray;
    private outHitL: Laya.HitResult;
    private rayR: Laya.Ray;
    private outHitR: Laya.HitResult;
    private RayReset() {
        let origP = this.rayPoint.position;
        if (!this._scene) {
            this._scene = Scene3DManager.Instance.MainScene;
        }
        //前面的射线
        if (!this.rayF) {
            this.rayF = new Laya.Ray(MathTool.VecZero, new Vector3(0, 0, 1));
            this.outHitF = new Laya.HitResult();
        }
        this.rayF.origin = origP;
        let isHItF = this._scene.physicsSimulation.rayCast(this.rayF, this.outHitF, GlobalParam.playerRayLengthF);
        if (isHItF) {
            let obj = this.outHitF.collider.owner as Sprite3D;
            //console.log("前面的射线碰撞");
            this.Dead(this.outHitF.point);
            return;
        }
        //左边的射线
        if (!this.rayL) {
            this.rayL = new Laya.Ray(MathTool.VecZero, new Vector3(1, 0, 0));
            this.outHitL = new Laya.HitResult();
        }
        this.rayL.origin = origP;
        let isHItL = this._scene.physicsSimulation.rayCast(this.rayL, this.outHitL, GlobalParam.playerRayLengthLR);
        if (isHItL) {
            let obj = this.outHitL.collider.owner as Sprite3D;
            //console.log("左边的射线碰撞")
            this.Dead(this.outHitL.point);
            return;
        }
        //右边的射线
        if (!this.rayR) {
            this.rayR = new Laya.Ray(MathTool.VecZero, new Vector3(-1, 0, 0));
            this.outHitR = new Laya.HitResult();
        }
        this.rayR.origin = origP;
        let isHItR = this._scene.physicsSimulation.rayCast(this.rayR, this.outHitR, GlobalParam.playerRayLengthLR);
        if (isHItR) {
            let obj = this.outHitR.collider.owner as Sprite3D;
            //  console.log("右边的射线碰撞");
            this.Dead(this.outHitR.point);
            return;
        }

    }

    /******************************************************** */
    private maskSp:Sprite;
    private bg2:Sprite;
    private setup():void
		{
			var bg:Sprite = new Sprite();
			bg.loadImage("res/bg2.png");
			Laya.stage.addChild(bg);

			this.bg2 = new Sprite();
			this.bg2.loadImage("res/bg2.png");
			Laya.stage.addChild(this.bg2);
			this.bg2.scale(3, 3);
			
			//创建mask
			this.maskSp = new Sprite();
			this.maskSp.loadImage("res/mask.png");
			this.maskSp.pivot(50, 50);

			//设置mask
			this.bg2.mask = this.maskSp;
        }
    /******************************************************** */

    private isFristShoot = true;
    _attack() {
        if (this.isdead) return;
        if (this.enemyTar == null) {
            this.enemyTar = RoleManager.Instance.GetOpponent();
            // if (this.enemyTar == null) return;
            // if (!this.enemyTar.active) this.enemyTar = RoleManager.Instance.PlayerGetEnemy();
            if(this.enemyTar != null){
                this.tp = this.enemyTar.transform.position.clone();
                Quaternion.lookAt(this.tp, this._transform.position, this._up, this.quaternion1);
                this.quaternion1.invert(this.quaternion1);
                this._transform.rotation = this.quaternion1.clone();
            }
        } else {
            if (!this.enemyTar.active) {
                this.enemyTar = RoleManager.Instance.GetOpponent();
                if(this.enemyTar != null){
                    this.tp = this.enemyTar.transform.position.clone();
                    Quaternion.lookAt(this.tp, this._transform.position, this._up, this.quaternion1);
                    this.quaternion1.invert(this.quaternion1);
                    this._transform.rotation = this.quaternion1.clone();
                }
            }
            if (this.enemyTar == null) {
                MainRoleManager.Instance.CreateOver(true);
                this.isFristShoot = true;
                return;
            }
            if (this.isFristShoot) {
                this.isFristShoot = false;
                MainRoleManager.Instance.CreateShoot(true);
            }
            this.tp = this.enemyTar.transform.position.clone();
            Quaternion.lookAt(this.tp, this._transform.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this._transform.rotation = this.quaternion1.clone();
        }
    }

    private r;
    private dancepos:Vector3;
    TiaoWu(sp: Transform3D) {
        this.PlayAni(MusicType.RunAndGun, 2);
        this.dancepos = sp.position.clone();
        this.r = sp.rotationEuler.clone();
        Laya.timer.frameLoop(1, this, this.PlayWuDao);
    }
    PlayWuDao() {
        Vector3.subtract(this.dancepos, this._transform.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * 0.15, this.forW.y * 0.15, this.forW.z * 0.15);
        this._transform.translate(this.forW);
        if (Vector3.distance(this._transform.position, this.dancepos) < 0.15) {
            Laya.timer.clear(this, this.PlayWuDao);
            this._transform.position = this.dancepos.clone();
            this._transform.rotationEuler = this.r.clone();
            this.PlayAni(MusicType.Dance, 1);
            return;
        }
        Quaternion.lookAt(this.dancepos, this._transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        //Quaternion.slerp(this.tran.rotation, this.quaternion1, 0.1, this.quaternion1);
        this._transform.rotation = this.quaternion1.clone();
    }

    _up = new Vector3(0, 1, 0);
    //准备防御
    Defense(pos) {
        if (this.isdead) return;
        //  Laya.timer.clearAll(this);
        this.tp = pos;
        Quaternion.lookAt(pos, this._transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this._transform.rotation = this.quaternion1.clone();
        this.PlayAni(MusicType.CrouchFiring, 1);
        Laya.timer.frameLoop(1, this, this._Defense);
    }
    private ts = new Vector3(0, 0, 0.01);
    private tp: Vector3;
    _Defense() {
        Vector3.subtract(this.tp, this._transform.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * 0.15, this.forW.y * 0.15, this.forW.z * 0.15);
        this._transform.translate(this.forW);
        if (Vector3.distance(this._transform.position, this.tp) < 0.1) {
            Laya.timer.clear(this, this._Defense);
            return;
        }
        Quaternion.lookAt(this.tp, this._transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        //Quaternion.slerp(this.tran.rotation, this.quaternion1, 0.1, this.quaternion1);
        this._transform.rotation = this.quaternion1.clone();
    }


    private enemyTar: Sprite3D;
    Attack() {
        Laya.timer.clear(this, this._Defense);
        this.isFristShoot = true;

        this.enemyTar = null;
        Laya.timer.frameLoop(1, this, this._attack)
    }

}