KeyBoardControl.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import BasicObject from "../../../Engine/Basic/BasicObject";
  2. import EventManager from "../../../Engine/Event/EventManager";
  3. import GameEventName from "../../Event/GameEventName";
  4. import GameManager from "../../Game/GameManager";
  5. import { CMD } from "../../SDK/sdk";
  6. /**
  7. * 键盘操作组件
  8. */
  9. export default class KeyBoardControl extends BasicObject {
  10. /**键盘值记录 */
  11. private codes: Map<number, number>;
  12. constructor () {
  13. super();
  14. this.codes = new Map<number, number>();
  15. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  16. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
  17. }
  18. _destroy () {
  19. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  20. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
  21. super._destroy();
  22. }
  23. onKeyDown (event) {
  24. let bool = true;
  25. switch(event.keyCode) {
  26. case cc.macro.KEY.left:
  27. this.codes.set(cc.macro.KEY.left, cc.macro.KEY.left);
  28. this.codes.delete(cc.macro.KEY.right);
  29. break;
  30. case cc.macro.KEY.right:
  31. this.codes.set(cc.macro.KEY.right, cc.macro.KEY.right);
  32. this.codes.delete(cc.macro.KEY.left);
  33. break;
  34. case cc.macro.KEY.up:
  35. this.codes.set(cc.macro.KEY.up, cc.macro.KEY.up);
  36. this.codes.delete(cc.macro.KEY.down);
  37. break;
  38. case cc.macro.KEY.down:
  39. this.codes.set(cc.macro.KEY.down, cc.macro.KEY.down);
  40. this.codes.delete(cc.macro.KEY.up);
  41. break;
  42. }
  43. 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) {
  44. bool = !this.codes.has(event.keyCode);
  45. this.codes.set(event.keyCode, event.keyCode);
  46. }
  47. if (bool == true) {
  48. this.sendJoystickEvent();
  49. }
  50. }
  51. onKeyUp (event) {
  52. this.codes.delete(event.keyCode);
  53. this.sendJoystickEvent();
  54. }
  55. private sendJoystickEvent (): void {
  56. this.codes.forEach((value: number, key: number) => {
  57. switch(value) {
  58. case cc.macro.KEY.left:
  59. EventManager.sendEvent(CMD.ENTER_KEY.toString());
  60. break;
  61. case cc.macro.KEY.right:
  62. EventManager.sendEvent(CMD.CANCLE_KEY.toString());
  63. break;
  64. case cc.macro.KEY.up:
  65. EventManager.sendEvent(CMD.BACK_LEFT.toString());
  66. break;
  67. case cc.macro.KEY.down:
  68. EventManager.sendEvent(CMD.BACK_RIGHT.toString());
  69. break;
  70. case cc.macro.KEY.z:
  71. EventManager.sendEvent(GameManager.ActionEvent.LeftJumpEvent);
  72. break;
  73. case cc.macro.KEY.x:
  74. EventManager.sendEvent(GameManager.ActionEvent.RightJumpEvent);
  75. break;
  76. case cc.macro.KEY.c:
  77. EventManager.sendEvent(GameManager.ActionEvent.VerticalJumpEvent);
  78. break;
  79. case cc.macro.KEY.v:
  80. EventManager.sendEvent(GameManager.ActionEvent.JackJumpEvent);
  81. break;
  82. }
  83. });
  84. }
  85. }