12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import BasicObject from "../../../Engine/Basic/BasicObject";
- import EventManager from "../../../Engine/Event/EventManager";
- import GameEventName from "../../Event/GameEventName";
- import GameManager from "../../Game/GameManager";
- import { CMD } from "../../SDK/sdk";
- /**
- * 键盘操作组件
- */
- export default class KeyBoardControl extends BasicObject {
- /**键盘值记录 */
- private codes: Map<number, number>;
- constructor () {
- super();
- this.codes = new Map<number, number>();
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
- }
- _destroy () {
- cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
- cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
- super._destroy();
- }
- onKeyDown (event) {
- let bool = true;
- switch(event.keyCode) {
- case cc.macro.KEY.left:
- this.codes.set(cc.macro.KEY.left, cc.macro.KEY.left);
- this.codes.delete(cc.macro.KEY.right);
- break;
- case cc.macro.KEY.right:
- this.codes.set(cc.macro.KEY.right, cc.macro.KEY.right);
- this.codes.delete(cc.macro.KEY.left);
- break;
- case cc.macro.KEY.up:
- this.codes.set(cc.macro.KEY.up, cc.macro.KEY.up);
- this.codes.delete(cc.macro.KEY.down);
- break;
- case cc.macro.KEY.down:
- this.codes.set(cc.macro.KEY.down, cc.macro.KEY.down);
- this.codes.delete(cc.macro.KEY.up);
- break;
- }
- if (event.keyCode == cc.macro.KEY.z || event.keyCode == cc.macro.KEY.x || event.keyCode == cc.macro.KEY.c || event.keyCode == cc.macro.KEY.v) {
- bool = !this.codes.has(event.keyCode);
- this.codes.set(event.keyCode, event.keyCode);
- }
- if (bool == true) {
- this.sendJoystickEvent();
- }
- }
- onKeyUp (event) {
- this.codes.delete(event.keyCode);
- this.sendJoystickEvent();
- }
- private sendJoystickEvent (): void {
- this.codes.forEach((value: number, key: number) => {
- switch(value) {
- case cc.macro.KEY.left:
- EventManager.sendEvent(CMD.ENTER_KEY.toString());
- break;
- case cc.macro.KEY.right:
- EventManager.sendEvent(CMD.CANCLE_KEY.toString());
- break;
- case cc.macro.KEY.up:
- EventManager.sendEvent(CMD.BACK_LEFT.toString());
- break;
- case cc.macro.KEY.down:
- EventManager.sendEvent(CMD.BACK_RIGHT.toString());
- break;
- case cc.macro.KEY.z:
- EventManager.sendEvent(GameManager.ActionEvent.LeftJumpEvent);
- break;
- case cc.macro.KEY.x:
- EventManager.sendEvent(GameManager.ActionEvent.RightJumpEvent);
- break;
- case cc.macro.KEY.c:
- EventManager.sendEvent(GameManager.ActionEvent.VerticalJumpEvent);
- break;
- case cc.macro.KEY.v:
- EventManager.sendEvent(GameManager.ActionEvent.JackJumpEvent);
- break;
- }
- });
- }
- }
|