GameSystem.ts 5.1 KB

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