import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import { Scene3DManager } from "./Scene3DManager";
import { Mathf } from "../Util/Mathf";
import { GlobalVariable } from "../game_module/GlobalVariable";
import { GamePool } from "./GamePool";
import { ColliderGroup } from "./ColliderAndMusicType";
import { Mound } from "./Mound";
import { ParticleManager } from "./ParticleManager";
import { Opponent } from "./Opponent";
import { Street } from "./Street";
import StreetManager from "../Manager/StreetManager";
import GameManager from "./GameManager";

export class MainRoleShot extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;
    private rayPoint: Transform3D;
    private speed: Vector3 = Mathf.VecZero;
    private _forward=new Vector3(0,0,1);
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.rayPoint = (this.selfobj.getChildByName("rayPoint") as Sprite3D).transform;
        this.speed.z = GlobalVariable.bulletspeed;
    }
    private tarTran: Transform3D;
    StartMove(tarTran, isover = false) {
        this.tarTran = tarTran;
        this.over = isover;
        Laya.timer.frameLoop(1, this, this._MOVE);

    }
    _MOVE() {
        this.tran.translate(this.speed);
        let d = Vector3.distance(this.tarTran.position, this.tran.position);
        if (d >= GlobalVariable.bulletMaxDis) {
            this.Des();
            return;
        }
        this.CreateRay();
    }



    private _scene: Laya.Scene3D;
    private ray: Laya.Ray;
    private outHit: Laya.HitResult;

    lineSprite: Laya.PixelLineSprite3D;
    private forward: Vector3;

    private over = false;
    private CreateRay() {
        let origP = this.rayPoint.position.clone();
        if (!this._scene) {
            this._scene = Scene3DManager.Instance._mainscene;
        }
        if (!this.ray) {
            if (!this.forward) this.forward = Mathf.VecZero;
            this.ray = new Laya.Ray(Mathf.VecZero,this._forward);
            this.outHit = new Laya.HitResult();
        }
        this.ray.origin = origP;
        if (this.over) {
            this.tran.getForward(this.forward);
            this.forward.x = -this.forward.x;
            this.forward.y = -this.forward.y;
            this.forward.z = -this.forward.z;
            this.ray.direction = this.forward;
            // if (this.lineSprite) this.lineSprite.destroy();
            // this.lineSprite = this._scene.addChild(new Laya.PixelLineSprite3D(1)) as Laya.PixelLineSprite3D;
            // this.lineSprite.addLine(origP, new Vector3(origP.x+this.forward.x * 10, origP.y+this.forward.y * 10, origP.z+this.forward.z * 10), Laya.Color.RED, Laya.Color.RED);
            // let isHItF = this._scene.physicsSimulation.raycastFromTo(origP, new Vector3(origP.x, origP.y, origP.z + 3), this.outHit);
        }else{
            
            this.ray.direction=this._forward;
            // this.forward= this.ray.direction .clone();
            // if (this.lineSprite) this.lineSprite.destroy();
            // this.lineSprite = this._scene.addChild(new Laya.PixelLineSprite3D(1)) as Laya.PixelLineSprite3D;
            // this.lineSprite.addLine(origP, new Vector3(origP.x+this.forward.x * 10, origP.y+this.forward.y * 10, origP.z+this.forward.z * 10), Laya.Color.RED, Laya.Color.RED);
            // let isHItF = this._scene.physicsSimulation.raycastFromTo(origP, new Vector3(origP.x, origP.y, origP.z + 3), this.outHit);
        }
        let isHItF = this._scene.physicsSimulation.rayCast(this.ray, this.outHit, GlobalVariable.bulletRayLength);
        if (isHItF) {
            let obj = this.outHit.collider.owner as Sprite3D;
            //console.log("子弹碰撞", obj.layer);
            if (obj.layer == ColliderGroup.mound) {
                let tower = obj.getComponent(Mound) as Mound;
                if (tower) {
                    tower.Atked(this.outHit.point);
                }
            } else if (obj.layer == ColliderGroup.opponent) {
                let tower = obj.getComponent(Opponent) as Opponent;
                if (tower) {
                    tower.Atked(this.outHit.point);
                }
            } else if (obj.layer == ColliderGroup.obstacle1 || obj.layer == ColliderGroup.obstacle2 || obj.layer == ColliderGroup.obstacle3) {
                StreetManager.Instance.CubeBlockAtked(obj);
                GameManager.Instance.blockCount++;
            } else {
                ParticleManager.Instance.CreateHitEffect(this.outHit.point);
            }
            this.Des();

            return;
        }
    }


    Des() {
        //console.log("子弹回收")
        Laya.timer.clearAll(this);
        GamePool.Instance.SetDisableObject(this.selfobj);
        GamePool.bulletCount--;
        //console.log("子弹回收",GamePool.bulletCount)
    }

    onDestroy() {

    }
}