import { Role } from "./Role";
import GameDefined, { CheckType } from "../game_module/GameDefined";
import Vector3 = Laya.Vector3;
import Ray = Laya.Ray;
import HitResult = Laya.HitResult;
import GameSceneController from "./GameSceneController";
import BulletController from "./BulletController";
import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import Game_Tool from "../game_tool/Game_Tool";
export class Bullet extends Laya.Script {
    constructor() {
        super();
    }

    private _transform: Transform3D;
    private self: Sprite3D;
    onAwake() {
        this.self = this.owner as Sprite3D;
        this.forw = new Vector3(0, 0, 0);
        this._transform = this.self.transform;
        this._corss = new Vector3(0, 0, 0);
    }

    private forw: Vector3;
    private _ray: Ray;
    private _hit: HitResult;
    private go: Sprite3D;
    private goS: Bullet;
    private _corss: Vector3;
    CreateRay(pos: Vector3, _rotation) {
        if (!this.go) {
            this.go = BulletController.Instance.CreateBullet();
            var gs = this.go.getComponent(Bullet);
            if (!gs) this.goS = this.go.addComponent(Bullet);
        } else {
            this.go.active = true;
        }
        this.go.transform.position = pos.clone();
        this.go.transform.rotationEuler = _rotation;
        // this._transform.getForward(this.forw);
        this.go.transform.getForward(this.forw);
        this.forw = new Vector3(-this.forw.x, -this.forw.y, -this.forw.z);
        var temp=this.forw.clone();
        Vector3.normalize(temp, temp);
        //    this.forw = new Vector3(this.forW.x * this.speed, this.forW.y * this.speed, this.forW.z * this.speed);


       // var temp = new Vector3(-this.forw.x * 0.01, -this.forw.y * 0.01, -this.forw.z * 0.01);

        pos.x = pos.x + temp.x*0.01;
        pos.y = pos.y + temp.y*0.01;
        pos.z = pos.z + temp.z*0.01;

        if (!this._ray) {
            this._ray = new Ray(pos, this.forw);
            this._hit = new HitResult();
        } else {
            this._ray.origin = pos;
            this._ray.direction = this.forw;
        }


        var isS = GameSceneController.Instance.game_scene.physicsSimulation.rayCast(this._ray, this._hit);
        if (isS) {
            var poi = this._hit.point;
            var dis = Vector3.distance(pos, poi);
            this.go.transform.localScaleZ = dis;
            var other = this._hit.collider.owner as Sprite3D;
            if (other.layer == GameDefined.player_layer) {
                this.goS.SetNextNotActive();
            } else {
                var nor = this._hit.normal;
                console.log(nor);
                Vector3.cross(this.forw, nor, this._corss);//大于0在左方 小于0在右方
                var a = Game_Tool.getReflectAng(this.forw);
                var ang = this._transform.rotationEuler.clone();
                this._corss.y >= 0 ? ang.y = ang.y + 2 * a : ang.y = ang.y - 2 * a;
                this.goS.CreateRay(poi, ang);
            }

        } else {
            this.go.transform.localScaleZ = 50;
            this.goS.SetNextNotActive();
        }
    }

    public SetNextNotActive() {
        if (this.go) {
            this.go.active = false;
            if (this.goS) this.goS.SetNextNotActive();
        }
    }
}