Created
December 7, 2023 16:02
-
-
Save ealbinu/99498add8129c138298838b869903c47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { _decorator, Component, Node, Input, input, EventKeyboard, KeyCode } from 'cc'; | |
| const { ccclass, property } = _decorator; | |
| @ccclass('InputManager') | |
| export class InputManager extends Component { | |
| private static _instance: InputManager; | |
| private _keyStates: Map<number, boolean> = new Map(); | |
| onLoad(){ | |
| console.log('INPUT MANAGER') | |
| if(InputManager._instance){ | |
| this.destroy() | |
| return; | |
| } | |
| InputManager._instance = this; | |
| input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this); | |
| input.on(Input.EventType.KEY_UP, this.onKeyUp, this); | |
| } | |
| private onKeyDown(event: EventKeyboard){ | |
| this._keyStates.set(event.keyCode, true) | |
| console.log(this._keyStates) | |
| } | |
| private onKeyUp(event: EventKeyboard){ | |
| this._keyStates.set(event.keyCode, false) | |
| } | |
| public static isKeyPressed(keyCode: number): boolean { | |
| return !!InputManager._instance?._keyStates.get(keyCode) | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { _decorator, Component, EventKeyboard, Input, input, KeyCode, deltaTime , Vec3 } from 'cc'; | |
| import { InputManager } from './InputManager'; | |
| const { ccclass, property } = _decorator; | |
| @ccclass('SomeOtherScript') | |
| export class SomeOtherScript extends Component { | |
| @property | |
| public speed: number = 500; | |
| private targetPosition: Vec3 = new Vec3(); | |
| private moveDirection: Vec3 = new Vec3(); | |
| private newPosition: Vec3 = new Vec3(); | |
| onLoad(){ | |
| Vec3.copy(this.targetPosition, this.node.position); | |
| } | |
| update(deltaTime:number){ | |
| this.moveDirection.x = 0 | |
| this.moveDirection.y = 0 | |
| if(InputManager.isKeyPressed(KeyCode.ARROW_UP)){ | |
| this.moveDirection.y += 1 | |
| } | |
| if(InputManager.isKeyPressed(KeyCode.ARROW_DOWN)){ | |
| this.moveDirection.y -= 1 | |
| } | |
| if(InputManager.isKeyPressed(KeyCode.ARROW_LEFT)){ | |
| this.moveDirection.x -= 1 | |
| } | |
| if(InputManager.isKeyPressed(KeyCode.ARROW_RIGHT)){ | |
| this.moveDirection.x += 1 | |
| } | |
| ///* | |
| //OPTIM | |
| Vec3.normalize(this.moveDirection, this.moveDirection); | |
| Vec3.scaleAndAdd(this.targetPosition, this.targetPosition, this.moveDirection, this.speed * deltaTime) | |
| const lerpFactor = 1 - Math.pow(0.001, deltaTime); | |
| Vec3.lerp(this.newPosition, this.node.position, this.targetPosition, lerpFactor); | |
| this.node.setPosition(this.newPosition); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment