GameManager.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import CCGIF from "./lib/gif/CCGIF";
  2. import { GIFCache } from "./lib/gif/GIF";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass
  5. export default class GameManager extends cc.Component {
  6. public static Instance: GameManager = null;
  7. @property(cc.Node)
  8. CharaNode: cc.Node = null;
  9. @property(cc.Node)
  10. TargetCharaNode: cc.Node = null;
  11. @property(cc.Prefab)
  12. CharaPre: cc.Prefab = null;
  13. @property(cc.Node)
  14. lineNode: cc.Node = null;
  15. @property(cc.Label)
  16. scoreLabel: cc.Label = null;
  17. @property(cc.Node)
  18. GifNode: cc.Node = null;
  19. @property(cc.Prefab)
  20. CharaNumber: cc.Prefab = null;
  21. @property(cc.Node)
  22. CharaNumberList: cc.Node = null;
  23. TargetChara: cc.Node = null;
  24. @property(cc.SpriteFrame)
  25. AllChara: cc.SpriteFrame[] = [];
  26. score: number = 0;
  27. endOne: number = 0;//限制次数
  28. CharaHeigth: number = 0;//下面水果中最高的高度
  29. CharaSize:number = 1.5
  30. CharaNumberRec:number[] = [];
  31. // LIFE-CYCLE CALLBACKS:
  32. onLoad() {
  33. cc.view.enableAutoFullScreen(false);
  34. if (GameManager.Instance != null) {
  35. GameManager.Instance.destroy();
  36. }
  37. GameManager.Instance = this;
  38. cc.director.getPhysicsManager().enabled = true;//物理游戏,开启物理
  39. }
  40. start() {
  41. this.GifNode.getComponent(CCGIF).preload().then(()=>this.GifNode.getComponent(CCGIF).play(true));
  42. cc.tween(this.lineNode.children[0])
  43. .to(0.3, { opacity: 255 })
  44. .to(0.3, { opacity: 0 })
  45. .union()
  46. .repeatForever()
  47. .start()
  48. this.lineNode.children[0].active = false;
  49. for (const index in this.AllChara) {
  50. if (Object.prototype.hasOwnProperty.call(this.AllChara, index)) {
  51. const Chara = this.AllChara[index];
  52. var CharaNum = cc.instantiate(this.CharaNumber);
  53. CharaNum.setParent(this.CharaNumberList);
  54. CharaNum.children[0].getComponent(cc.Sprite).spriteFrame = Chara;
  55. this.CharaNumberRec[index]=0
  56. }
  57. }
  58. this.updateCharaNumber();
  59. this.createOneChara(0, cc.v2(0, 550))
  60. }
  61. update(dt) {
  62. GameManager.Instance.CharaHeigth = GameManager.Instance.findHighestChara();
  63. this.scoreLabel.string = this.score.toString();
  64. this.checkRedLineAlert();
  65. }
  66. /**
  67. * 生成一个水果
  68. * @param _CharaNum 水果类型 0是葡萄 以此类推
  69. */
  70. createOneChara(_CharaNum: number, _pos: cc.Vec2 ) {
  71. for (const index in this.TargetCharaNode.children) {
  72. if (Object.prototype.hasOwnProperty.call(this.TargetCharaNode.children, index)) {
  73. const childNode = this.TargetCharaNode.children[index];
  74. if(childNode.position.y>=500) childNode.destroy();
  75. }
  76. }
  77. let Chara = cc.instantiate(this.CharaPre);//实例化一个预制体
  78. Chara.setParent(this.TargetCharaNode)//更改Chara父节点
  79. Chara.getComponent(cc.Sprite).spriteFrame = this.AllChara[_CharaNum];//更改Chara的图片
  80. Chara.getComponent("CharaCollision").CharaNumber = _CharaNum;//Chara碰撞回调脚本,里面有一个当前水果类型 如果是葡萄 CharaNumber为0 用于相同合成检测
  81. Chara.setPosition(_pos);//设置坐标
  82. Chara.setScale(0)//出生时scale设置为0
  83. Chara.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static;//刚体类型设置为Static防止下落
  84. Chara.getComponent(cc.PhysicsCircleCollider).radius = 0;//碰撞器半径先设置位0,下落时再改到相应水果大小
  85. Chara.setContentSize(this.resizeChara(Chara.getContentSize(),_CharaNum));
  86. Chara.getComponent(cc.PhysicsCircleCollider).apply();//保存碰撞器更改的参数
  87. //水果生成时执行一个缩放动作
  88. cc.tween(Chara)
  89. .to(0.5, { scale: 1 }, { easing: 'backOut' })
  90. .call(() => {
  91. this.TargetChara = Chara;//将我们控制的水果TargetChara设置为刚生成的Chara
  92. })
  93. .start()
  94. }
  95. /**
  96. * 生成高一级的水果
  97. * @param _CharaNum 水果
  98. */
  99. createLevelUpChara(_CharaNum , _pos) {
  100. //AudioManager.Instance.Play(6, false, 1)
  101. let Chara = cc.instantiate(this.CharaPre);
  102. Chara.parent = this.CharaNode;
  103. Chara.getComponent(cc.Sprite).spriteFrame = this.AllChara[_CharaNum];
  104. Chara.getComponent("CharaCollision").CharaNumber = _CharaNum;
  105. Chara.position = _pos;
  106. Chara.scale = 0;
  107. Chara.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, -100);
  108. Chara.getComponent(cc.PhysicsCircleCollider).radius = Chara.height / 2;
  109. Chara.setContentSize(this.resizeChara(Chara.getContentSize(),_CharaNum));
  110. Chara.getComponent(cc.PhysicsCircleCollider).apply();
  111. cc.tween(Chara)
  112. .to(0.5, { scale: 1 }, { easing: 'backOut' })
  113. .call(() => {
  114. if (Chara.getComponent(cc.PhysicsCircleCollider) != null) {
  115. if (Chara.getComponent(cc.PhysicsCircleCollider).radius != Chara.height / 2) Chara.getComponent(cc.PhysicsCircleCollider).radius = Chara.height / 2;
  116. if (Chara.getComponent(cc.RigidBody).type != cc.RigidBodyType.Dynamic) Chara.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
  117. Chara.getComponent(cc.PhysicsCircleCollider).apply();
  118. }
  119. })
  120. .start()
  121. }
  122. randomOneChara(){
  123. let randomIndexArray:number[] = [];
  124. for (let index = 0; index < GameManager.Instance.AllChara.length*0.5; index++) {
  125. randomIndexArray.push(index);
  126. for (let i = 0; i < GameManager.Instance.CharaNumberRec[index]; i++) {
  127. randomIndexArray.push(index)
  128. randomIndexArray.push(index)
  129. }
  130. }
  131. // GameManager.Instance.createOneChara(Math.floor(Math.random() * GameManager.Instance.AllChara.length*0.5), cc.v2(0, 550));
  132. GameManager.Instance.createOneChara(randomIndexArray[Math.floor(Math.random() * randomIndexArray.length)], cc.v2(0, 550));
  133. }
  134. resizeChara(size: cc.Size,_CharaNum: number){
  135. return new cc.Size(size.width*this.CharaSize*Math.pow((_CharaNum+1)/this.AllChara.length,4/5),size.height*this.CharaSize*Math.pow((_CharaNum+1)/this.AllChara.length,4/5))
  136. }
  137. updateCharaNumber(){
  138. for (const index in this.CharaNumberRec) {
  139. if (Object.prototype.hasOwnProperty.call(this.CharaNumberRec, index)) {
  140. const value = this.CharaNumberRec[index];
  141. console.log(this.CharaNumberList.children[index].children[1].getComponent(cc.Label).string);
  142. this.CharaNumberList.children[index].children[1].getComponent(cc.Label).string=value.toString();
  143. }
  144. }
  145. }
  146. checkRedLineAlert(){
  147. // 红线是否开启预警
  148. if (this.lineNode.children[0].y - GameManager.Instance.CharaHeigth < 200 && this.lineNode.children[0].y - GameManager.Instance.CharaHeigth >= 0) {
  149. this.lineNode.children[0].active = true;
  150. }
  151. if (this.lineNode.children[0].y - GameManager.Instance.CharaHeigth > 200) {
  152. this.lineNode.children[0].active = false;
  153. }
  154. }
  155. // 结束
  156. end() {
  157. if (this.endOne == 0) {
  158. let i = 0;
  159. for (let j = this.CharaNode.children.length - 1; j >= 0; j--) {
  160. i++;
  161. setTimeout(() => {
  162. GameManager.Instance.score += this.CharaNode.children[j].getComponent("CharaCollision").CharaNumber + 1;
  163. this.CharaNode.children[j].active = false;
  164. }, i * 100);
  165. }
  166. this.TargetCharaNode.active = true;
  167. for (let i = 0; i < this.TargetCharaNode.children.length; i++) {
  168. this.TargetCharaNode.children[i].active = false;
  169. }
  170. this.endOne++;
  171. }
  172. }
  173. // 获取下落水果中位置最高的
  174. findHighestChara() {
  175. if(this.CharaNode.children.length>0){
  176. let highest = this.CharaNode.children[0].y;
  177. for (let i = 1; i < this.CharaNode.children.length; i++) {
  178. let high = this.CharaNode.children[i].y + this.CharaNode.children[i].width / 2;
  179. if (highest < high) {
  180. highest = high;
  181. }
  182. }
  183. return highest;
  184. }else{
  185. return 0;
  186. }
  187. }
  188. }