import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import { Scene3DManager } from "./Scene3DManager";
import { MathTool } from "../Util/MathTool";
import { GlobalParam } from "../Net/GlobalParam";
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";
import Sprite = Laya.Sprite;

export class MainRoleShot extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;
    private rayPoint: Transform3D;
    private speed: Vector3 = MathTool.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 = GlobalParam.shootspeed;
    }
    
    Run() {
        this.tran.translate(this.speed);
        let d = Vector3.distance(this.tarTran.position, this.tran.position);
        if (d >= GlobalParam.bulletMaxDis) {
            this.RecoverBullet();
            return;
        }
        this.RayReset();
    }

    RecoverBullet() {
        Laya.timer.clearAll(this);
        GamePool.Instance.RecoverModel(this.selfobj);
        GamePool.bulletCount--;
    }

    /*************************************************** */
    private sp: Sprite;
    private drawSomething(): void {
        this.sp = new Sprite();
        Laya.stage.addChild(this.sp);
        //画线
        this.sp.graphics.drawLine(10, 58, 146, 58, "#ff0000", 3);
        //画连续直线
        this.sp.graphics.drawLines(176, 58, [0, 0, 39, -50, 78, 0, 117, 50, 156, 0], "#ff0000", 5);
        //画曲线
        this.sp.graphics.drawCurves(352, 58, [0, 0, 19, -100, 39, 0, 58, 100, 78, 0, 97, -100, 117, 0, 136, 100, 156, 0], "#ff0000", 5);
        //画矩形
        this.sp.graphics.drawRect(10, 166, 166, 90, "#ffff00");
        //画多边形
        this.sp.graphics.drawPoly(264, 166, [0, 0, 60, 0, 78.48, 57, 30, 93.48, -18.48, 57], "#ffff00");
        //画三角形
        this.sp.graphics.drawPoly(400, 166, [0, 100, 50, 0, 100, 100], "#ffff00");
        //画圆
        this.sp.graphics.drawCircle(98, 332, 50, "#00ffff");
        //画扇形
        this.sp.graphics.drawPie(240, 290, 100, 10, 60, "#00ffff");
        //绘制圆角矩形，自定义路径
        this.sp.graphics.drawPath(400, 310, [["moveTo", 5, 0], ["lineTo", 105, 0], ["arcTo", 110, 0, 110, 5, 5], ["lineTo", 110, 55], ["arcTo", 110, 60, 105, 60, 5], ["lineTo", 5, 60], ["arcTo", 0, 60, 0, 55, 5], ["lineTo", 0, 5], ["arcTo", 0, 0, 5, 0, 5], ["closePath"]], {fillStyle: "#00ffff"});
    }
    /*************************************************** */

    
    private _scene: Laya.Scene3D;
    private ray: Laya.Ray;
    private outHit: Laya.HitResult;
    lineSprite: Laya.PixelLineSprite3D;
    private forward: Vector3;
    private over = false;
    private RayReset() {
        let origP = this.rayPoint.position.clone();
        if (!this._scene) {
            this._scene = Scene3DManager.Instance.MainScene;
        }
        if (!this.ray) {
            if (!this.forward) this.forward = MathTool.VecZero;
            this.ray = new Laya.Ray(MathTool.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;
        }else{
            this.ray.direction=this._forward;
        }
        let isHItF = this._scene.physicsSimulation.rayCast(this.ray, this.outHit, GlobalParam.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.Injured(this.outHit.point);
                }
            } else if (obj.layer == ColliderGroup.opponent) {
                let tower = obj.getComponent(Opponent) as Opponent;
                if (tower) {
                    tower.Injured(this.outHit.point);
                }
            } else if (obj.layer == ColliderGroup.obstacle1 || obj.layer == ColliderGroup.obstacle2 || obj.layer == ColliderGroup.obstacle3) {
                StreetManager.Instance.StreetAtkedCube(obj);
                GameManager.Instance.blockCount++;
            } else {
                ParticleManager.Instance.AttackEffect(this.outHit.point);
            }
            this.RecoverBullet();
            return;
        }
    }

    private tarTran: Transform3D;
    Start(tarTran, isover = false) {
        this.tarTran = tarTran;
        this.over = isover;
        Laya.timer.frameLoop(1, this, this.Run);

    }

    
}