Game.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Boy from "./Boy";
  2. import Bot from "./Bot";
  3. import MatchPanel from "../Panel/MatchPanel";
  4. import SettlePanel from "../Panel/SettlePanel";
  5. import Camera from "./Camera";
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class Game extends cc.Component {
  9. @property({
  10. type: cc.Float,
  11. range: [0, 1, 0.1],
  12. slide: true,
  13. })
  14. botStrength = 1;
  15. @property({
  16. type: cc.Label,
  17. })
  18. text_countDown:cc.Label = null;
  19. boys:Boy[] = new Array();
  20. mainCamera:cc.Node;
  21. isGameOver:boolean = false;
  22. onLoad(){
  23. window.gm = this;
  24. cc.director.getCollisionManager().enabled = true;
  25. cc.director.getPhysicsManager().enabled = true;
  26. }
  27. /**一局游戏时间,单位:秒s */
  28. gameTotalTime:number = 120;
  29. /**游戏倒计时,单位:秒s */
  30. countDown :number = 120;
  31. start(){
  32. console.log("开始匹配");
  33. // this.node.addChild(cc.instantiate(window.rs.pf_matchPanel));
  34. // this.node.getChildByName("MatchPanel").on(MatchPanel.EventType_MatchFinish,()=>{
  35. console.log("匹配成功");
  36. //游戏开始开启倒计时
  37. this.countDown = this.gameTotalTime;
  38. this.schedule(this.timerGameTimeCountDown, 1);
  39. for(let i=0;i<4;i++){
  40. let box = window.gc.boxs[i];
  41. let boyPosition = box.position.add(cc.v2(0,box.height/2+10));
  42. if(i==0){
  43. this.addBoy(i,boyPosition,false);
  44. }else{
  45. this.addBoy(i,boyPosition,true);
  46. }
  47. }
  48. // });
  49. this.mainCamera = this.node.getChildByName('Main Camera');
  50. this.mainCamera.addComponent(Camera);
  51. }
  52. /**游戏倒计时 */
  53. timerGameTimeCountDown(){
  54. if(this.isGameOver){
  55. console.log("取消计时器1111");
  56. this.unschedule(this.timerGameTimeCountDown);
  57. // return;
  58. }
  59. if(this.countDown>0){
  60. this.countDown--;
  61. }else{
  62. console.log("取消计时器2222");
  63. //游戏倒计时为0,强制结束游戏
  64. this.unschedule(this.timerGameTimeCountDown);
  65. this.gameOver();
  66. }
  67. // console.log("游戏倒计时:",this.countDown);
  68. this.text_countDown.string = this.countDown+"'";
  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.gc.boyGroup.addChild(node);
  76. if(isBot){
  77. console.log("添加机器人");
  78. node.addComponent(Bot);
  79. }else{
  80. console.log("添加boy");
  81. window.boy = boy;
  82. }
  83. this.boys.push(boy);
  84. }
  85. /**游戏结束 */
  86. gameOver(){
  87. this.isGameOver = true;
  88. this.scheduleOnce(()=>{
  89. console.log("胜利的人",this.boys[0].index);
  90. this.showSettlePanel(MatchPanel.palyerInfos[this.boys[0].index]);
  91. },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. showSettlePanel(playerInfo:PalyerInfo){
  111. let node = cc.instantiate(window.rs.pf_settlePanel);
  112. node.setPosition(this.mainCamera.getPosition());
  113. let settlePanel = node.getComponent(SettlePanel);
  114. settlePanel.setPlayerInfo(playerInfo);
  115. this.node.addChild(node);
  116. node.on(SettlePanel.EventType_Restart,()=>{
  117. this.node.runAction(
  118. cc.sequence(
  119. cc.fadeOut(1),
  120. cc.callFunc(()=>{
  121. // cc.director.loadScene("Game");
  122. cc.director.loadScene("Pairing");
  123. })
  124. )
  125. );
  126. });
  127. cc.audioEngine.playEffect(window.rs.ac_win,false);
  128. }
  129. }