import { NodeUtil } from "../GameUtils/NodeUtil";
import { AssetManager } from "../Resources/AssetManager";
import { ResourcesPath } from "../Resources/ResourcesPath";
import { LevelManager } from "../Manager/LevelManager";

import Vector3 = Laya.Vector3;
import { Transform3DHelper } from "../GameUtils/Vector3Helper";

export default class WallBehav extends Laya.Script3D{
    constructor(){
        super();
    }
    public _gameobject:Laya.Sprite3D;
    public _hitres:Laya.HitResult = new Laya.HitResult();
    public crash:boolean = false;
    onAwake(){
        this._gameobject = this.owner as Laya.Sprite3D;
    }
    onStart(){
        var isFowardHit = false;
        var startPos:Vector3 = new Vector3(this._gameobject.transform.position.x,this._gameobject.transform.position.y,this._gameobject.transform.position.z);
        let _up = new Laya.Vector3(0,1,0);
        var FowardRay:Laya.Ray = new Laya.Ray(startPos,_up);
         if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(FowardRay,this._hitres,1)){
             if(this._hitres.collider.collisionGroup == 2){
                 let _wall = this._hitres.collider.owner.parent as Laya.Sprite3D;
                 if(this.crash){
                     NodeUtil.SetColliderInactive(this._gameobject);
                     let _pre = AssetManager.Instance.getPref(ResourcesPath.Yan);
                     let yan = Laya.Sprite3D.instantiate(_pre);
                     LevelManager.Instance.spritepool.addChild(yan);
                     yan.transform.position = this._gameobject.transform.position.clone();
                     yan.transform.scale = new Laya.Vector3(0.5, 0.5, 0.5);
                     this.destroy();
                     this._gameobject.destroy();
                 }
                 else{
                    this.FallDown();
                 }
                 let _behav = _wall.addComponent(WallBehav) as WallBehav;
             }
         }
         else{
            if(this.crash){
                NodeUtil.SetColliderInactive(this._gameobject);
                let _pre = AssetManager.Instance.getPref(ResourcesPath.Yan);
                let yan = Laya.Sprite3D.instantiate(_pre);
                LevelManager.Instance.spritepool.addChild(yan);
                yan.transform.position = this._gameobject.transform.position.clone();
                yan.transform.scale = new Laya.Vector3(0.5, 0.5, 0.5);
                this.destroy();
                this._gameobject.destroy();
            }
            // else{
            //    this.FallDown();
            // }
         }
    }
    onUpdate(){
        if(this.crash){
            NodeUtil.SetColliderInactive(this._gameobject);
            let _pre = AssetManager.Instance.getPref(ResourcesPath.Yan);
            let yan = Laya.Sprite3D.instantiate(_pre);
            LevelManager.Instance.spritepool.addChild(yan);
            yan.transform.position = this._gameobject.transform.position.clone();
            yan.transform.scale = new Laya.Vector3(0.5, 0.5, 0.5);
            this.destroy();
            this._gameobject.destroy();
            return;
        }
        this.Down();
    }
    Crash(){
        NodeUtil.SetColliderInactive(this._gameobject);
        let _pre = AssetManager.Instance.getPref(ResourcesPath.Yan);
        let yan = Laya.Sprite3D.instantiate(_pre);
        LevelManager.Instance.spritepool.addChild(yan);
        yan.transform.position = this._gameobject.transform.position.clone();
        yan.transform.scale = new Laya.Vector3(0.5, 0.5, 0.5);
        this.destroy();
        this._gameobject.destroy();
    }
    _hitres2:Laya.HitResult = new Laya.HitResult();

    FallDown(){
        Laya.timer.loop(1,this,this.Down);
    }
    Down(){
        var isFowardHit = false;
        var startPos:Vector3 = new Vector3(this._gameobject.transform.position.x,this._gameobject.transform.position.y+0.1,this._gameobject.transform.position.z);
        let _down = new Laya.Vector3(0,-0.1,0);
        var FowardRay:Laya.Ray = new Laya.Ray(startPos,_down);
         if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(FowardRay,this._hitres2,0.2)){
             if(this._hitres2 != null && (this._hitres2.collider.collisionGroup == 1 || this._hitres2.collider.collisionGroup == 2)){
                Laya.timer.clear(this,this.Down);
             }
             else{
                let _posy = this._gameobject.transform.position.y -=0.03;
                this._gameobject.transform.position = new Laya.Vector3(this._gameobject.transform.position.x,_posy,this._gameobject.transform.position.z); 
             }
         }
         else{
            let _posy = this._gameobject.transform.position.y -=0.03;
            this._gameobject.transform.position = new Laya.Vector3(this._gameobject.transform.position.x,_posy,this._gameobject.transform.position.z);
         }
    }

}