CommandManager.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * 命令控制类
  3. * @author ituuz
  4. * @description 负责控制和维护命令。
  5. */
  6. import BaseCommand from "../base/BaseCommand";
  7. import SimpleCommand from "../command/SimpleCommand";
  8. import SyncMacroCommand from "../command/SyncMacroCommand";
  9. import AsyncMacroCommand from "../command/AsyncMacroCommand";
  10. export default class CommandManager {
  11. // 实例
  12. private static _instance: CommandManager = new CommandManager();
  13. /**
  14. * @constructor
  15. * @private
  16. */
  17. private constructor () {
  18. }
  19. /**
  20. * 单例获取类
  21. */
  22. public static getInstance(): CommandManager {
  23. return this._instance;
  24. }
  25. /**
  26. * 执行命令
  27. * @param {{new (): BaseCommand}} command 命令对象
  28. * @param {Object} body 命令参数
  29. */
  30. public __executeCommand__(command: {new (): BaseCommand}, body?: any): void {
  31. if (cc.js.isChildClassOf(command, SimpleCommand)) {
  32. let cmd: SimpleCommand = new command() as SimpleCommand;
  33. cmd.execute(body);
  34. } else if (cc.js.isChildClassOf(command, SyncMacroCommand)) {
  35. // TODO: 同步按顺序执行的命令组合宏
  36. } else if (cc.js.isChildClassOf(command, AsyncMacroCommand)) {
  37. let cmd: AsyncMacroCommand = new command() as AsyncMacroCommand;
  38. // 初始化宏
  39. cmd["initialize"]();
  40. // 执行
  41. cmd["asyncExecute"]();
  42. } else {
  43. console.log(command.prototype + " 不是可执行的命令!");
  44. }
  45. }
  46. /**
  47. * 撤销命令
  48. * @param {{new (): BaseCommand}} command 命令对象
  49. * @param {Object} body 命令参数
  50. */
  51. public __undoCommand__(command: {new (): BaseCommand}, body?: any): void {
  52. if (cc.js.isChildClassOf(command, SimpleCommand)) {
  53. let cmd: SimpleCommand = new command() as SimpleCommand;
  54. cmd.undo(body);
  55. } else if (cc.js.isChildClassOf(command, SyncMacroCommand)) {
  56. // TODO: 同步按顺序撤销的命令组合宏
  57. } else if (cc.js.isChildClassOf(command, AsyncMacroCommand)) {
  58. let cmd: AsyncMacroCommand = new command() as AsyncMacroCommand;
  59. // 初始化宏
  60. cmd["initialize"]();
  61. // 执行
  62. cmd["asyncUndo"]();
  63. } else {
  64. console.log(command.prototype + " 不是可执行的命令!");
  65. }
  66. }
  67. }