import { Role } from "./Role";
import { 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 { Bullet } from "./Bullet";
import Game_Tool from "../game_tool/Game_Tool";
export class Player extends Role {
    constructor() {
        super();
    }

    onAwake() {
        super.onAwake();
        this._type = CheckType.Player;
        this.forw = new Vector3(0, 0, 0);
        this._corss = new Vector3(0, 0, 0);
    }

    private forw: Vector3;
    private _ray: Ray;
    private _hit: HitResult;

    private _corss: Vector3;

    private go: Sprite3D;
    private goS: Bullet;
    CreateRay() {
        this._transform.getForward(this.forw);
        this.forw = new Vector3(-this.forw.x, -this.forw.y, -this.forw.z);
        if (!this._ray) {
            this._ray = new Ray(this._bulletPos.transform.position, this.forw);
            this._hit = new HitResult();
        } else {
            this._ray.origin = this._bulletPos.transform.position;
            this._ray.direction = this.forw;
        }
        if (!this.go) {
            this.go = BulletController.Instance.CreateBullet();
            var gs = this.go.getComponent(Bullet);
            if (!gs) this.goS = this.go.addComponent(Bullet);
        }
        this.go.transform.position = this._bulletPos.transform.position;
        this.go.transform.rotation = this._transform.rotation;
        //  this.go.transform.getForward(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(this._bulletPos.transform.position, poi);
            this.go.transform.localScaleZ = dis;
            var nor = this._hit.normal;
            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();
        }
        //  console.log(go.transform.localScaleZ);
    }

    RotationSelf(rota: Vector3) {
        this._transform.rotate(rota);
        this.CreateRay();
    }
}