import { _decorator, Component, Node,Collider2D,RigidBody2D, input, Input, EventMouse,log,Vec2,PhysicsSystem2D,PHYSICS_2D_PTM_RATIO,v2,Contact2DType,IPhysics2DContact } from 'cc';
const { ccclass, property } = _decorator;

PhysicsSystem2D.instance.gravity = v2(0, -60 * PHYSICS_2D_PTM_RATIO);
// 跳跃状态
enum jumpStatus{
    up,
    down,
    static,
};
@ccclass('PlayerController')
export class PlayerController extends Component {

    // 是否接收到跳跃指令
    private _startJump: boolean = false;

    start() {
        let rb = this.getComponent(RigidBody2D)
        rb.enabledContactListener = true;
        rb.fixedRotation = true;
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            // collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
            // collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
            // collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        }
        // 注册全局碰撞回调函数
        // if (PhysicsSystem2D.instance) {
        //     PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        // }
    }
    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        // console.log('onBeginContact');
        let rb = this.getComponent(RigidBody2D)
        let lv = rb.linearVelocity
        if(lv.y<0){
            if(otherCollider.node.name==='wall'){
                this._startJump = false
                this.node.emit('JumpFull');
            }
            if(otherCollider.node.name==='platform'){
                this._startJump = false
                this.node.emit('JumpEnd');
            }
        }

    }
    // onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    //     // 只在两个碰撞体结束接触时被调用一次
    //     console.log('onEndContact');
    // }
    // onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    //     // 每次将要处理碰撞体接触逻辑时被调用
    //     console.log('onPreSolve');
    // }
    // onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    //     // 每次处理完碰撞体接触逻辑时被调用
    //     console.log('onPostSolve');
    // }

    update(deltaTime: number) {
        this.checkStatus()
    }
    setInputActive(active: boolean) {
        if (active) {
            input.on(Input.EventType.TOUCH_START, this.onMouseUp, this);
        } else {
            input.off(Input.EventType.TOUCH_START, this.onMouseUp, this);
        }
    }
    onMouseUp() {
        this.jump();
    }
    jump() {
        if(this._startJump)return
        this._startJump = true
        log('jump')
        let rb = this.getComponent(RigidBody2D)
        rb.linearVelocity = new Vec2(0,0)
        rb.applyForceToCenter(new Vec2(0,800*PHYSICS_2D_PTM_RATIO),true)
        // rb.applyLinearImpulseToCenter(new Vec2(0,15*PHYSICS_2D_PTM_RATIO),true)
        
    }
    checkStatus(){
        let rb = this.getComponent(RigidBody2D)
        let lv = rb.linearVelocity
        if(Math.abs(lv.y)<0.1){
            this._jumpStatus = jumpStatus.static
        }else if(lv.y>0){
            this._jumpStatus = jumpStatus.up
        }else if(lv.y<0){
            this._jumpStatus = jumpStatus.down
        }
    }
    set _jumpStatus (value: jumpStatus) {
        switch(value) {
            case jumpStatus.up:
                // 更改穿透属性
                this.getComponent(Collider2D).sensor = true
                this.getComponent(Collider2D).apply()
                break;
            case jumpStatus.down:
                // 更改穿透属性
                this.getComponent(Collider2D).sensor = false
                this.getComponent(Collider2D).apply()
                break;
            case jumpStatus.static:
                break;
        }
    }
}

