Game.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Bird from "./Bird";
  2. import MatchPanel from "../Common/MatchPanel/MatchPanel";
  3. import TouchPanel from "./TouchPanel";
  4. import { Tool } from "./JCLibrary";
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class Game extends cc.Component {
  8. @property({type:cc.Prefab})
  9. private matchPanelPrefab = null;
  10. @property({type:cc.Prefab})
  11. private birdPrefab = null;
  12. @property({type:cc.AudioClip})
  13. private bgMusic = null;
  14. @property(cc.String)
  15. public serverUrl:string = "";
  16. @property(cc.Boolean)
  17. public appGame:boolean = false;
  18. public birds:Bird[] = new Array(2);
  19. //flag
  20. public isGameOver:boolean;
  21. public onLoad():void{
  22. window.gm = this;
  23. // this.serverUrl = "ws://192.168.101.14:9999/birdServer";
  24. cc.director.getPhysicsManager().enabled = true;
  25. cc.director.getCollisionManager().enabled = true;
  26. cc.audioEngine.playMusic(this.bgMusic, true);
  27. if (Tool.openInWebview()) {
  28. this.appGame = true;
  29. } else {
  30. this.appGame = false;
  31. }
  32. }
  33. addMatchPanel() {
  34. this.node.addChild(cc.instantiate(this.matchPanelPrefab));
  35. window.matchPanel.node.on(MatchPanel.EVENT_MATCH_SUCCESS, () => {
  36. this.startGame();
  37. });
  38. }
  39. startGame():void{
  40. window.gc.startMode();
  41. this.createBird(0);
  42. this.createBird(1);
  43. window.fruitGroove.show();
  44. window.progressBar.show();
  45. window.topBar.show();
  46. this.node.addComponent(TouchPanel);
  47. this.isGameOver = false;
  48. window.player.call('checkAI');
  49. }
  50. createBird(index:number):void{
  51. this.birds[index] = (cc.instantiate(this.birdPrefab) as cc.Node).getComponent(Bird);
  52. this.birds[index].init(index);
  53. this.node.addChild(this.birds[index].node);
  54. }
  55. controlFlyUp(hitPower: number = 30) {
  56. window.appGame.jumpCount++;
  57. this.birds[window.myPlayerInfo.index].flyUp(hitPower);
  58. let x = Math.floor(this.birds[window.myPlayerInfo.index].node.x);
  59. let y = Math.floor(this.birds[window.myPlayerInfo.index].node.y);
  60. window.player.call('flyUp',[x,y,hitPower]);
  61. }
  62. update(){
  63. if(this.isGameOver==false){
  64. let myBirdNode = this.birds[window.myPlayerInfo.index].node;
  65. window.player.call('updateBirdPosition',[
  66. Math.floor(myBirdNode.x),
  67. Math.floor(myBirdNode.y)
  68. ]);
  69. }
  70. }
  71. gameOver(winnerIndex:number):void{
  72. if(this.isGameOver)return;
  73. this.isGameOver = true;
  74. this.birds[0].stop();
  75. this.birds[1].stop();
  76. window.fruitGroove.hide();
  77. window.settlePanel.show(winnerIndex);
  78. }
  79. }