HsGame.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { _decorator, CCInteger, Collider2D, Component, instantiate, Label, log, Node, Prefab, Tween, tween, UITransform, v3, Vec3, Animation, sp } from 'cc';
  2. import { EventCustom } from '../../scripts/Common/EventCustom';
  3. import { CosntValue } from '../../Common/Scripts/CosntValue';
  4. import { HsGoodsItem } from './HsGoodsItem';
  5. import { HsPlayer } from './HsPlayer';
  6. import { AudioManage } from '../../scripts/Manager/AudioManage';
  7. import { GameManager } from '../../scripts/Manager/GameManager';
  8. import { Global } from '../../scripts/Common/Global';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('HsGame')
  11. export class HsGame extends Component {
  12. @property(Node)
  13. Bg: Node = null;
  14. @property(Node)
  15. Goods: Node = null;
  16. @property(Prefab)
  17. GoodsItem: Prefab = null;
  18. @property(Node)
  19. Player: Node = null;
  20. @property(Node)
  21. TouchNode: Node = null;
  22. @property(CCInteger)
  23. MoveTime: number = 10;
  24. @property(Prefab)
  25. scoreLabelPerfab: Prefab = null;
  26. // _lv: number;
  27. // set Lv(a: number) {
  28. // this._lv = a;
  29. // let rle = new EventCustom(CosntValue.EventType_RefreshLevel, true, this._lv);
  30. // this.node.dispatchEvent(rle);
  31. // let e = new EventCustom(CosntValue.EventType_ShowTips, true, "恭喜,通过关卡");
  32. // this.node.dispatchEvent(e);
  33. // }
  34. // _sumPoint: number = 0;
  35. // _point: number;
  36. // set Point(a: number) {
  37. // this._point = a;
  38. // let e = new EventCustom(CosntValue.EventType_RefreshScore, true, a);
  39. // this.node.dispatchEvent(e);
  40. // if (a >= this._gotoNextPoint) {
  41. // Tween.stopAllByTarget(this.Bg);
  42. // for (let i = 0; i < this.Goods.children.length; i++) {
  43. // Tween.stopAllByTarget(this.Goods.children[i])
  44. // }
  45. // AudioManager.instance.playSound(CosntValue.Audio_Hs_Win);
  46. // if (this._lv == CosntValue.MaxLv) {
  47. // let e = new EventCustom(CosntValue.EventType_GameAllOver, true);
  48. // this.node.dispatchEvent(e);
  49. // } else {
  50. // this.Bg.setPosition(v3());
  51. // this._lv++;
  52. // let e = new EventCustom(CosntValue.EventType_NextLv, true, this._lv);
  53. // this.node.dispatchEvent(e);
  54. // this.scheduleOnce(this.init, 3);
  55. // }
  56. // }
  57. // }
  58. _hp: number = 3;
  59. // _gotoNextPoint: number = 100;
  60. start() {
  61. CosntValue.currGame = "Hs";
  62. this.TouchNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  63. this.rec();
  64. }
  65. rec() {
  66. this.node.on("ContactSomething", (e) => {
  67. if (e.data.getComponent(HsGoodsItem)._goodType == 1) {
  68. let spine = this.Player.getChildByName("A").getComponent(sp.Skeleton);
  69. spine.timeScale = 3.5;
  70. let track = spine.setAnimation(0, "animation", false);
  71. if (track) {
  72. // 注册动画的结束回调
  73. spine.setCompleteListener((trackEntry) => {
  74. let name = trackEntry.animation ? trackEntry.animation.name : '';
  75. if (name === "animation") {
  76. spine.timeScale = 1;
  77. e.data.active = false;
  78. }
  79. });
  80. }
  81. spine.addAnimation(0, "yzou", true);
  82. AudioManage.instance.playSound(Global.Audio_Hs_Cg);
  83. // this._sumPoint += e.data.getComponent(HsGoodsItem)._point;
  84. // this.Point = this._point + e.data.getComponent(HsGoodsItem)._point;
  85. let score = e.data.getComponent(HsGoodsItem)._point;
  86. GameManager.getInstance().addScore(score);
  87. if (GameManager.getInstance().curLevelScore >= GameManager.getInstance().curLevelData.targetScore) {
  88. this.stopTween();
  89. }
  90. const scoreLabel = instantiate(this.scoreLabelPerfab);
  91. scoreLabel.setParent(this.Goods);
  92. const pos = e.data.getPosition();
  93. scoreLabel.setPosition(pos.x + 100, pos.y);
  94. const label = scoreLabel.getComponentInChildren(Label);
  95. const lableAni = scoreLabel.getComponent(Animation)
  96. label.string = '+' + score;
  97. lableAni.play('addScore');
  98. } else if (e.data.getComponent(HsGoodsItem)._goodType == 2) {
  99. AudioManage.instance.playSound(Global.Audio_Hs_Sb);
  100. this._hp--;
  101. let spine = this.Player.getChildByName("A").getComponent(sp.Skeleton);
  102. spine.timeScale = 1.5;
  103. let track = spine.setAnimation(0, "shuaidao", false);
  104. if (track) {
  105. // 注册动画的结束回调
  106. spine.setCompleteListener((trackEntry) => {
  107. let name = trackEntry.animation ? trackEntry.animation.name : '';
  108. if (name === "shuaidao") {
  109. spine.timeScale = 1;
  110. e.data.active = false;
  111. }
  112. });
  113. }
  114. spine.addAnimation(0, "yzou", true);
  115. let tipe = new EventCustom(Global.EventType_ShowTips, true, "触碰到障碍物,生命值减1,剩余生命值为" + this._hp);
  116. this.node.dispatchEvent(tipe);
  117. e.data.active = false;
  118. if (this._hp <= 0) {
  119. let e = new EventCustom(Global.EventType_ShowTips, true, "很遗憾,生命值为0游戏失败");
  120. this.node.dispatchEvent(e);
  121. GameManager.getInstance().gameFail();
  122. // this.scheduleOnce(() => {
  123. // let e = new EventCustom(CosntValue.EventType_GameOver, true);
  124. // this.node.dispatchEvent(e);
  125. // }, 2);
  126. }
  127. }
  128. });
  129. }
  130. protected onEnable(): void {
  131. this.TouchNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  132. //this.Lv = CosntValue.StartLv;
  133. // this.init();
  134. }
  135. protected onDisable(): void {
  136. this.TouchNode.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  137. }
  138. onTouchMove(e) {
  139. if (this.Player.getChildByPath("A").getComponent(sp.Skeleton).getCurrent(0).animation.name == "yzou") {
  140. let posDelta = e.getDelta();
  141. let dir = posDelta.y;
  142. if (dir > 1 || dir < -1) {
  143. this.Player.getComponent(HsPlayer).move(dir);
  144. }
  145. }
  146. }
  147. init() {
  148. // this._gotoNextPoint = 100;
  149. this._hp = 3;
  150. this.Player.getChildByName("A").getComponent(sp.Skeleton).addAnimation(0, "yzou", true);
  151. let moveTime = this.MoveTime - GameManager.getInstance().curLevel;
  152. //背景移动
  153. this.Bg.setPosition(v3());
  154. let t = tween(this.Bg).set({ position: v3() }).by(moveTime, { position: new Vec3(-(this.Bg.getComponent(UITransform).width / 2), 0, 0) });
  155. tween(this.Bg).repeatForever(t).start();
  156. // let t = tween(this.Bg).set({ position: v3(1819, 0, 0) }).to(moveTime, { position: new Vec3(-1819, 0, 0) });
  157. // tween(this.Bg).repeatForever(t).start();
  158. let tips = "关卡开始,请上下滑动屏幕,捡起贝壳";
  159. // if (GameManager.getInstance().curLevel > 4) {
  160. // tips += "商场人流量增大,注意躲避其他客人"
  161. // }
  162. let tipe = new EventCustom(Global.EventType_ShowTips, true, tips);
  163. this.node.dispatchEvent(tipe);
  164. this.Goods.removeAllChildren();
  165. this.createGoods();
  166. this.scheduleOnce(() => {
  167. this.createGoods();
  168. }, moveTime * 0.5)
  169. }
  170. createGoods() {
  171. log(123);
  172. let g = instantiate(this.GoodsItem);
  173. this.Goods.addChild(g);
  174. // let rx = CosntValue.randomInt(100, 300)
  175. let x = this.node.getComponent(UITransform).width / 2;
  176. let y = CosntValue.randomInt(0, 1) == 0 ? 120 : -30;
  177. g.setPosition(x, y);
  178. let tx = -(this.node.getComponent(UITransform).width / 2);
  179. let max = GameManager.getInstance().curLevel > 5 ? 5 : GameManager.getInstance().curLevel;
  180. let type = CosntValue.randomInt(1, max);
  181. g.getComponent(HsGoodsItem).GoodType = type;
  182. let moveTime = this.MoveTime - GameManager.getInstance().curLevel;
  183. tween(g).to(moveTime, { position: new Vec3(tx, y, 0) }).call(() => {
  184. g.destroy();
  185. this.createGoods();
  186. }).start();
  187. }
  188. stopTween() {
  189. Tween.stopAllByTarget(this.Bg);
  190. for (let i = 0; i < this.Goods.children.length; i++) {
  191. Tween.stopAllByTarget(this.Goods.children[i])
  192. }
  193. this.Goods.removeAllChildren();
  194. }
  195. update(deltaTime: number) {
  196. }
  197. }