import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import { ScenceManager } from "./ScenceManager";
import { ColliderLay } from "./Runner";

export class Rigbody extends Laya.Script3D
{
    m_sprite:Sprite3D;
    m_life:number = 1;
    m_time:number = 0;
    m_resistance = 0.03;
    onAwake()
    {
        this.m_sprite = this.owner as Sprite3D;
        Laya.timer.once(this.m_life*1000,this,this.OnEnd);
        Laya.timer.frameLoop(1,this,this.Update);
    }

    private OnEnd()
    {
        Laya.timer.clearAll(this);
        this.destroy();
    }

    OnLand = true;
    hit:Laya.HitResult = new Laya.HitResult();
    CheckPlaneCollider()
    {
        var startpoint = new Vector3(this.m_sprite.transform.position.x,this.m_sprite.transform.position.y,this.m_sprite.transform.position.z);
        var dir = new Vector3(0,-1,0);
        var ray:Laya.Ray = new Laya.Ray(startpoint,dir);
        if(ScenceManager.Instance.GameScence.physicsSimulation.rayCast(ray,this.hit,0.5))
        {
            if(this.hit.collider.collisionGroup != ColliderLay.Plane)return;
            var dis = this.m_sprite.transform.position.y - this.hit.point.y;
            if(this.OnLand)
            {
                if (dis > 0.15)
                {
                    this.OnLand = false;
                    return;
                }
                else
                {
                    this.m_sprite.transform.position = new Vector3(this.m_sprite.transform.position.x,this.hit.point.y + 0.1,this.m_sprite.transform.position.z);
                }
            }
            else
            {
                if (dis < 0.01)
                {
                    this.OnLand = true;
                    this.OnPlane();
                }
            }
        }
        else
        {
            this.OnLand = false;
        }
    }

    OnPlane()
    {

    }

    offestY = 0;
    g = 1;
    gravity()
    {
        this.offestY -= this.g*this.m_time*this.m_time;

        // var rig = this.m_sprite.addComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D;
        // rig.angularVelocity
    }

    
    curdir:Vector3 = new Vector3();
    AddVelocity(_dir:Vector3,_speed:number)
    {
        Vector3.normalize(_dir,this.curdir);
        Vector3.scale(this.curdir,_speed,this.curdir);
        Laya.timer.frameLoop(1,this,this.Move);
    }

    Update()
    {
        this.m_time += 0.012;
        this.gravity();
        this.CheckPlaneCollider();
    }

    curPos:Vector3 = new Vector3();
    Move()
    {
        
        var mpos = this.m_sprite.transform.position;
        Vector3.add(mpos,this.curdir,this.curPos);
        
        //Vector3.lerp(mpos,this.curPos,0.1,this.curPos);

        if(this.OnLand)
        {
            this.curPos = new Vector3(this.curPos.x,this.m_sprite.transform.position.y,this.curPos.z);
        }
        else
        {
            //console.log(this.offestY);
            Vector3.add(this.curPos,new Vector3(0,this.offestY,0),this.curPos);
        }
        this.m_sprite.transform.position = this.curPos;
        
        Vector3.lerp(this.curdir,new Vector3(0,0,0),this.m_resistance,this.curdir);
        if(Vector3.scalarLength(this.curdir) < 0.01)
        {
            Laya.timer.clear(this,this.Move);
        }
    }
}