GameMain.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { _decorator, Button, Collider2D, Component, Contact2DType, EPhysics2DDrawFlags, EventTouch, instantiate, IPhysics2DContact, Layout, log, Node, PhysicsSystem2D, Prefab, Sprite, SpriteAtlas, SpriteFrame, tween, UIOpacity, v3, Vec3 } from 'cc';
  2. import { GameManager } from '../Manager/GameManager';
  3. import { ColliderTag, Global } from '../Common/Global';
  4. import { AudioManage } from '../Manager/AudioManage';
  5. import { BallItem } from './BallItem';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('GameMain')
  8. export class GameMain extends Component {
  9. @property(Node)
  10. GouLanZi: Node = null;
  11. @property(Sprite)
  12. TargetColor: Sprite = null;
  13. @property(Node)
  14. Balls: Node = null;
  15. @property(Node)
  16. TargetBall: Node = null;
  17. @property(Node)
  18. WriteBall: Node = null;
  19. @property(Node)
  20. Mesh: Node = null;
  21. @property(SpriteAtlas)
  22. ItemsAtlas: SpriteAtlas = null;
  23. _targetColorIndex: number = 0;
  24. start() {
  25. // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Shape;
  26. PhysicsSystem2D.instance.enable = true;
  27. this.node.on(Node.EventType.TOUCH_MOVE, this.touchFun, this);
  28. // PhysicsSystem2D.instance
  29. }
  30. /**
  31. * 初始化逻辑
  32. * 每次GameManager.StartGame()时都会调用
  33. */
  34. initUI() {
  35. this.Balls.removeAllChildren();
  36. this._targetColorIndex = Global.getRandom(1, (this.ItemsAtlas.getSpriteFrames().length - 1) / 2 - 1);
  37. this.TargetColor.spriteFrame = this.ItemsAtlas.getSpriteFrame("Bar_" + this._targetColorIndex);
  38. this.schedule(this.tnUpdateTimer, GameManager.getInstance().curLevelData.targetUpdateTimer);
  39. this.schedule(this.wnUpdateTimer, GameManager.getInstance().curLevelData.wbUpdateTimer);
  40. }
  41. tnUpdateTimer() {
  42. let n = instantiate(this.TargetBall)
  43. if (n) {
  44. n.active = true;
  45. }
  46. n.getChildByName("BallSp").getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame("Ball_" + this._targetColorIndex);
  47. this.Balls.addChild(n);
  48. let index = 0;
  49. do {
  50. index = Global.getRandom(0, 6) - 3;
  51. } while (!this.canCreateBall(index));
  52. n.getComponent(BallItem)._index = index;
  53. n.position = v3(index * 166, 800, 0);
  54. this.tweenFun(n, true);
  55. }
  56. wnUpdateTimer() {
  57. let n = instantiate(this.WriteBall)
  58. if (n) {
  59. n.active = true;
  60. }
  61. let color;
  62. do {
  63. color = Global.getRandom(0, (this.ItemsAtlas.getSpriteFrames().length - 1) / 2 - 1);
  64. } while (color == this._targetColorIndex);
  65. n.getChildByName("BallSp").getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame("Ball_" + color);
  66. this.Balls.addChild(n);
  67. let index = 0;
  68. do {
  69. index = Global.getRandom(0, 6) - 3;
  70. } while (!this.canCreateBall(index));
  71. n.getComponent(BallItem)._index = index;
  72. n.position = v3(index * 166, 800, 0);
  73. this.tweenFun(n, false);
  74. }
  75. canCreateBall(index: number): boolean {
  76. let can = true;
  77. for (let i = 0; i < this.Balls.children.length; i++) {
  78. if (this.Balls.children[i].getComponent(BallItem)._index == index) {
  79. can = false;
  80. break;
  81. }
  82. }
  83. log(index + " " + can);
  84. return can
  85. }
  86. touchFun(e) {
  87. let deltaX = e.getUIDelta().x
  88. this.GouLanZi.position = new Vec3(this.GouLanZi.position.x + deltaX, this.GouLanZi.position.y, this.GouLanZi.position.z);
  89. if (this.GouLanZi.position.x < -570) {
  90. this.GouLanZi.position = new Vec3(-570, this.GouLanZi.position.y, this.GouLanZi.position.z);
  91. }
  92. if (this.GouLanZi.position.x > 570) {
  93. this.GouLanZi.position = new Vec3(570 + deltaX, this.GouLanZi.position.y, this.GouLanZi.position.z);
  94. }
  95. }
  96. tweenFun(node: Node, isTar): void {
  97. tween(node).to(GameManager.getInstance().curLevelData.dropTime,
  98. { position: new Vec3(node.position.x, -800, node.position.z), angle: Math.round(Math.random() * 180 - 90) },
  99. { easing: "quadIn" }).call(() => {
  100. if (node && node.parent) {
  101. node.parent.removeChild(node);
  102. node.destroy();
  103. }
  104. }).start();
  105. }
  106. /**
  107. * 游戏结束后清理
  108. */
  109. gameOver() {
  110. this.unschedule(this.tnUpdateTimer);
  111. this.unschedule(this.wnUpdateTimer);
  112. this.Balls.removeAllChildren();
  113. }
  114. update(deltaTime: number) {
  115. }
  116. }