GameSystem.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Bot from "../../Game/Bot";
  2. /**
  3. * 游戏逻辑系统(游戏逻辑和显示分离)
  4. */
  5. export default class GameSystem {
  6. public masterId: number = -1;
  7. public playerInfos: Map<number, any>;
  8. public leavingPlayerIds: number[] = []
  9. public boys: BoyEntity[] = [];
  10. public countDown: number = 120 * 1000;
  11. public isGameStart: boolean = false;
  12. public isGameOver: boolean = false;
  13. public onMatchComplete(id, playerInfos, uuid, timestamp) {
  14. this.isGameStart = true;
  15. this.masterId = id;
  16. this.playerInfos = playerInfos;
  17. }
  18. public onGameStart() {
  19. this._createBoys();
  20. window.gm.createBoys();
  21. }
  22. public onFrameSync(frames: any[]) {
  23. for (let frame of frames) {
  24. if (frame.t === 1) {
  25. this._handleBoyState(frame);
  26. continue;
  27. }
  28. if (frame.t === 2) {
  29. this._handleBoyBeKilled(frame);
  30. continue;
  31. }
  32. if (frame.t === 100) {
  33. this._handlePlayerLeave(frame)
  34. continue;
  35. }
  36. if (frame.t === 101) {
  37. this._handleTimePass(frame);
  38. continue;
  39. }
  40. }
  41. }
  42. /**帧同步处理-角色状态 */
  43. private _handleBoyState(frame) {
  44. let id = frame.id;
  45. let boy = this.boys.find(boy => boy.id === id);
  46. if (boy) {
  47. boy.position = cc.v2(parseFloat(frame.x), parseFloat(frame.y));
  48. boy.angle = parseFloat(frame.a);
  49. boy.scaleX = parseFloat(frame.sx);
  50. }
  51. }
  52. /**帧同步处理-角色被杀 */
  53. private _handleBoyBeKilled(frame) {
  54. let id = frame.id;
  55. let killerId = frame.ki;
  56. let deadPosition = cc.v2(parseFloat(frame.x), parseFloat(frame.y));
  57. let boy = this.boys.find(boy => boy.id === id);
  58. if (killerId === 100 && !boy.dead) { //被大锯子杀死
  59. boy.dead = true;
  60. boy.deadPosition = deadPosition;
  61. if (this._isOnlyOneAlive()) this._setGameOver();
  62. } else { //被其他角色杀死
  63. let killer = this.boys.find(boy => boy.id === killerId);
  64. if (!killer.dead && !boy.dead) {
  65. boy.dead = true;
  66. boy.deadPosition = deadPosition;
  67. if (this._isOnlyOneAlive()) this._setGameOver();
  68. }
  69. }
  70. }
  71. /**帧同步处理-玩家离开 */
  72. private _handlePlayerLeave(frame) {
  73. let playerId = frame.pi;
  74. this.leavingPlayerIds.push(playerId);
  75. console.log("Player Leave", playerId);
  76. /**
  77. * 如果我现在是机器人支持者
  78. * 将对(离开的玩家角色,机器人角色)检测并添加机器人脚本
  79. */
  80. if (this.isBotSupporter()) {
  81. window.gm.boys.forEach(boy => {
  82. if (this.leavingPlayerIds.indexOf(boy.index) > -1 || !this.playerInfos[boy.index]) {
  83. if (!boy.getComponent(Bot)) boy.addComponent(Bot);
  84. }
  85. });
  86. }
  87. }
  88. /**帧同步处理-时间流逝 */
  89. private _handleTimePass(frame) {
  90. if (this.isGameOver) return;
  91. this.countDown -= frame.dt;
  92. if (this.countDown <= 0) {
  93. this.countDown = 0;
  94. this._setGameOver();
  95. }
  96. }
  97. /**我是不是机器人支持者 */
  98. public isBotSupporter() {
  99. for (let i = 0; i < this.masterId; i++) {
  100. if (this.leavingPlayerIds.indexOf(i) === -1) return false;
  101. }
  102. return true;
  103. }
  104. /**创建游戏角色 */
  105. private _createBoys() {
  106. for (let i = 0; i < 4; i++) {
  107. let boy = new BoyEntity();
  108. boy.id = i;
  109. let box = window.gc.boxs[i];
  110. boy.position = box.position.add(cc.v2(0, box.height/2+10));
  111. boy.angle = 0;
  112. boy.scaleX = 1.8;
  113. boy.dead = false;
  114. this.boys.push(boy);
  115. }
  116. }
  117. /**是不是只有一个人活着 */
  118. private _isOnlyOneAlive() {
  119. let aliveCount = 0;
  120. this.boys.forEach(boy => {
  121. if (!boy.dead) aliveCount++;
  122. });
  123. return aliveCount === 1;
  124. }
  125. /**设置游戏结束 */
  126. private _setGameOver() {
  127. if (this.isGameOver) return;
  128. this.isGameOver = true;
  129. window.gm.gameOver();
  130. }
  131. }
  132. class BoyEntity {
  133. id: number;
  134. position: cc.Vec2;
  135. angle: number;
  136. scaleX: number;
  137. dead: boolean;
  138. deadPosition: cc.Vec2;
  139. }