GameManager.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Boy from "./Boy";
  2. import Bot from "./Bot";
  3. import SettlePanel from "../Views/SettlePanel";
  4. import Camera from "./Camera";
  5. import SocketPlayer from "../FrameSync/Game/SocketPlayer";
  6. import GameSystem from "../FrameSync/Game/GameSystem";
  7. import PIFS from "../PIFS";
  8. const {ccclass, property} = cc._decorator;
  9. @ccclass
  10. export default class GameManager extends cc.Component {
  11. @property({
  12. type: cc.Float,
  13. range: [0, 1, 0.1],
  14. slide: true,
  15. })
  16. botStrength = 1;
  17. @property({
  18. type: cc.Label,
  19. })
  20. text_countDown:cc.Label = null;
  21. boys:Boy[] = new Array();
  22. mainCamera:cc.Node;
  23. isGameOver:boolean = false;
  24. socketPlayer: SocketPlayer = null;
  25. onLoad(){
  26. window.gm = this;
  27. cc.director.getCollisionManager().enabled = true;
  28. cc.director.getPhysicsManager().enabled = true;
  29. window.gameSystem = new GameSystem();
  30. }
  31. start(){
  32. //获取相机,添加相机跟踪脚本
  33. this.mainCamera = this.node.getChildByName('Main Camera');
  34. this.mainCamera.addComponent(Camera);
  35. //添加匹配界面
  36. cc.find("Canvas").addChild(cc.instantiate(window.rs.pf_matchView));
  37. //触发联机
  38. this.socketPlayer = new SocketPlayer();
  39. }
  40. protected onDestroy(): void {
  41. this.socketPlayer.close();
  42. window.gm = null;
  43. }
  44. protected update(): void {
  45. this.text_countDown.string = GameManager.timeFormat(window.gameSystem.countDown / 1000);
  46. this.socketPlayer.uploadFrames();
  47. }
  48. createBoys() {
  49. for (let i = 0; i < 4; i++) {
  50. let box = window.gc.boxs[i];
  51. let boyPosition = box.position.add(cc.v2(0,box.height/2+10));
  52. if (window.gameSystem.playerInfos[i]) {
  53. this.addBoy(i, boyPosition, false);
  54. } else {
  55. this.addBoy(i, boyPosition, window.gameSystem.isBotSupporter());
  56. }
  57. }
  58. }
  59. /**
  60. * 时间格式化
  61. * @param seconds 秒数
  62. */
  63. public static timeFormat(seconds:number):string{
  64. let minute = Math.floor(seconds/60);
  65. let second = Math.floor(seconds%60);
  66. return (minute<10?'0':'')+minute+':'+(second<10?'0':'')+second;
  67. }
  68. addBoy(index:number,position:cc.Vec2,isBot:boolean){
  69. let node = cc.instantiate(window.rs.pf_boy);
  70. node.setPosition(position);
  71. let boy = node.addComponent(Boy);
  72. boy.index = index;
  73. window.gc.boyGroup.addChild(node);
  74. if (isBot) {
  75. console.log("添加机器人");
  76. node.addComponent(Bot);
  77. } else {
  78. console.log("添加boy");
  79. }
  80. this.boys.push(boy);
  81. if (window.gameSystem.masterId === index) window.boy = boy;
  82. }
  83. /**游戏结束 */
  84. gameOver(){
  85. if (this.isGameOver) return;
  86. let winBoyIndex = this.boys[0].index;
  87. this.isGameOver = true;
  88. this.scheduleOnce(()=>{
  89. console.log("胜利的人", winBoyIndex);
  90. SettlePanel.Show(PIFS.matchPlayerInfos[winBoyIndex]);
  91. }, window.gameSystem.countDown === 0 ? 0 : 1);
  92. }
  93. rankingTag:number = 4;
  94. removeBoy(boy:Boy){
  95. boy.rankingNum = this.rankingTag;
  96. this.rankingTag-=1;
  97. // console.log("removeBoy------------",boy.index);
  98. let newBoys = new Array();
  99. this.boys.forEach((elem)=>{
  100. if(elem!=boy){
  101. newBoys.push(elem);
  102. }
  103. });
  104. this.boys = newBoys;
  105. boy.node.destroy();
  106. if(this.boys.length==1){
  107. this.gameOver();
  108. }
  109. }
  110. }