import { _decorator, Component, Node, EventKeyboard, input, Input, KeyCode, Vec3, RigidBody, Prefab, instantiate } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('player')
export class player extends Component {
    @property({ type: Prefab })
    testPrefab: Prefab;
    private pos: Vec3 = new Vec3();
    private rigidBody: any = null;
    private isMove = false;
    start() {
        console.time('第一次创建 Time')
        let test1 = instantiate(this.testPrefab);
        this.node.parent.addChild(test1)
        console.timeEnd('第一次创建 Time')

        console.time('第二次创建 Time')
        let test2 = instantiate(this.testPrefab);
        this.node.parent.addChild(test2)
        console.timeEnd('第二次创建 Time')

        this.rigidBody = this.node.getComponent(RigidBody);
        input.on(Input.EventType.KEY_DOWN, (event: EventKeyboard) => {
            switch (event.keyCode) {
                case KeyCode.KEY_W:
                    this.isMove = true;
                    break;

            }
        }, this);
        input.on(Input.EventType.KEY_UP, (event: EventKeyboard) => {
            switch (event.keyCode) {
                case KeyCode.KEY_W:
                    this.isMove = false;
                    break;

            }
        }, this);
        this.pos = this.node.position;
    }

    update(deltaTime: number) {
        if (this.isMove) {
            this.rigidBody.setLinearVelocity(new Vec3(0, -10, -0.5));
            // this.pos.z-= 0.02;
            // this.node.position=this.pos;
        } else {
            this.rigidBody.setLinearVelocity(new Vec3(0, -10, 0));
        }


    }
}

