GameManager.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }
  47. protected lateUpdate(): void {
  48. this.socketPlayer.uploadInputs();
  49. }
  50. createBoys() {
  51. for (let i = 0; i < 4; i++) {
  52. let box = window.gcr.boxs[i];
  53. let boyPosition = box.position.add(cc.v2(0,box.height/2+10));
  54. if (window.gameSystem.playerInfos[i]) {
  55. this.addBoy(i, boyPosition, false);
  56. } else {
  57. this.addBoy(i, boyPosition, window.gameSystem.isBotSupporter());
  58. }
  59. }
  60. }
  61. /**
  62. * 时间格式化
  63. * @param seconds 秒数
  64. */
  65. public static timeFormat(seconds:number):string{
  66. let minute = Math.floor(seconds/60);
  67. let second = Math.floor(seconds%60);
  68. return (minute<10?'0':'')+minute+':'+(second<10?'0':'')+second;
  69. }
  70. addBoy(index:number,position:cc.Vec2,isBot:boolean){
  71. let node = cc.instantiate(window.rs.pf_boy);
  72. node.setPosition(position);
  73. let boy = node.addComponent(Boy);
  74. boy.index = index;
  75. window.gcr.boyGroup.addChild(node);
  76. if (isBot) {
  77. console.log("添加机器人");
  78. node.addComponent(Bot);
  79. } else {
  80. console.log("添加boy");
  81. }
  82. this.boys.push(boy);
  83. if (window.gameSystem.masterId === index) window.boy = boy;
  84. }
  85. /**游戏结束 */
  86. gameOver(winBoyIndex: number){
  87. if (this.isGameOver) return;
  88. this.isGameOver = true;
  89. this.scheduleOnce(()=>{
  90. console.log("胜利的人", winBoyIndex);
  91. SettlePanel.Show(PIFS.matchPlayerInfos[winBoyIndex]);
  92. }, window.gameSystem.countDown === 0 ? 0 : 1);
  93. }
  94. rankingTag:number = 4;
  95. removeBoy(boy:Boy){
  96. boy.rankingNum = this.rankingTag;
  97. this.rankingTag-=1;
  98. // console.log("removeBoy------------",boy.index);
  99. let newBoys = new Array();
  100. this.boys.forEach((elem)=>{
  101. if(elem!=boy){
  102. newBoys.push(elem);
  103. }
  104. });
  105. this.boys = newBoys;
  106. boy.node.destroy();
  107. }
  108. }