import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import StreetManager, { ThingArr } from "../Manager/StreetManager";
import { Scene3DManager } from "./Scene3DManager";
import { GamePool } from "./GamePool";
import MainRoleManager from "../Manager/MainRoleManager";
import { Mathf } from "../Util/Mathf";
import { GlobalVariable } from "../game_module/GlobalVariable";


export class Street extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;

    private child: Transform3D;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;

    }

    private tarTran: Transform3D;
    private thing: ThingArr;
    SetBlock(thing: ThingArr) {
        this.tran.localPositionZ = Number(thing.z);
        this.tran.localPositionX = Number(thing.x);
        this.tran.rotationEuler = Mathf.VecZero;

        this.thing = thing;
        if (!MainRoleManager.Instance.player) return;
        this.tarTran = MainRoleManager.Instance.player.transform;

        this.SetDate();

    }


    private ttttt: Transform3D;
    private rotate: Vector3 = Mathf.VecZero;
    private speed: Vector3 = Mathf.VecZero;
    SetDate() {
        if (this.thing.type == 5) {
            this.ttttt = (this.selfobj.getChildAt(0) as Sprite3D).transform;
        } else {
            this.ttttt = this.tran;
            if (this.thing.type == 4) {
                StreetManager.Instance.AddCubeBlock(this.selfobj);
            }
        }
        if (this.thing.moveType == 1) {
            Laya.timer.frameLoop(3, this, this.Cheack);
            return;
        } else if (this.thing.moveType == 2) {
            this.rotate.x = 0;
            this.rotate.y = -this.thing.moveSpeed;
            this.rotate.z = 0;
            Laya.timer.frameLoop(2, this, this.Move2);
        } else if (this.thing.moveType == 3) {
            this.rotate.x = 0;
            this.rotate.y = this.thing.moveSpeed;
            this.rotate.z = 0;
            Laya.timer.frameLoop(2, this, this.Move2);
        } else if (this.thing.moveType == 4) {
            this.rotate.x = 0;
            this.rotate.y = 0;
            this.rotate.z = this.thing.moveSpeed;
            Laya.timer.frameLoop(1, this, this.Move3);
        } else if (this.thing.moveType == 5) {
            this.rotate.x = 0;
            this.rotate.y = 0;
            this.rotate.z = -this.thing.moveSpeed;
            Laya.timer.frameLoop(1, this, this.Move3);
        } else if (this.thing.moveType == 6) {
            this.speed.x = -this.thing.moveSpeed;
            this.speed.y = 0;
            this.speed.z = 0;
            Laya.timer.frameLoop(1, this, this.Move4);
        } else if (this.thing.moveType == 7) {
            this.speed.x = this.thing.moveSpeed;
            this.speed.y = 0;
            this.speed.z = 0;
            Laya.timer.frameLoop(1, this, this.Move4);
        } else if (this.thing.moveType == 8) {

        } else if (this.thing.moveType == 9) {

        }
    }
    //  正逆旋转
    Move2() {
        this.ttttt.rotate(this.rotate);
        this.Cheack();
    }
    //左右摆动
    Move3() {
        this.ttttt.rotate(this.rotate);
        //console.log(this.ttttt.localRotationEulerZ);
        if (Math.abs(this.ttttt.localRotationEulerZ) >= 50) this.rotate.z = -this.rotate.z;
        this.Cheack();
    }
    Move4() {
        this.ttttt.translate(this.speed);
        if (Math.abs(this.ttttt.localPositionX) >= GlobalVariable.blockLRmoveMaxDis) this.speed.x = -this.speed.x;
        this.Cheack();
    }

    Cheack() {
        if (this.tarTran.localPositionZ - this.tran.localPositionZ >= 10) {
            console.log("回收block", this.selfobj.name);
            this.Des(); return;
        }
    }

    // CubeBlockAtked(sp: Sprite3D) {
    //     sp.active = false;
    // }

    Des() {
        if(this.thing.type){
            StreetManager.Instance.ResetCubBlock(this.selfobj.id);
        }
        Laya.timer.clearAll(this);
        StreetManager.Instance.RemoveBlock(this.selfobj);
        GamePool.Instance.SetDisableObject(this.selfobj);
    }

    onDestroy() {

    }
}