GameMain.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { Animation, BoxCollider2D, CCClass, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, NodeEventType, Tween, UITransform, Vec2, Vec3, _decorator, animation, instantiate, tween, v2, v3 } from 'cc';
  2. import { GameManager } from '../../Manager/GameManager';
  3. import { Player } from './Player';
  4. import { AudioManage } from '../../Manager/AudioManage';
  5. import { Global } from '../../Common/Global';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('GameMain')
  8. export class GameMain extends Component {
  9. @property(Node)
  10. jingchaNode: Node = null;
  11. @property(Node)
  12. jingchaImg: Node = null;
  13. @property(Node)
  14. player: Node = null;
  15. @property(Node)
  16. mainNode: Node = null;
  17. private _moveDirection: Vec2 | undefined;
  18. private jc_w: number = 0;//player一半的宽度
  19. private jc_h: number = 0;//player一半的高度
  20. private collider;
  21. //是否在移动中
  22. private isMove = false;
  23. private isGameover = false;
  24. private _tween = new Tween();
  25. jingcha: Node = null;
  26. /**当前剩余平民数量 */
  27. private pingminNum: number = 0;
  28. onLoad() {
  29. this.jc_w = this.jingchaNode.getComponent(UITransform).contentSize.width / 2;
  30. this.jc_h = this.jingchaNode.getComponent(UITransform).contentSize.height / 2;
  31. this.jingchaNode.name = 'jingcha';
  32. this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
  33. }
  34. start() {
  35. this.move();
  36. }
  37. //冲刺
  38. private jingchaMove(e): void {
  39. //冲刺时打开碰撞
  40. if (this.collider) {
  41. this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  42. }
  43. this.node.off(NodeEventType.TOUCH_START, this.jingchaMove, this);
  44. let uiposition = e.getUILocation();
  45. let pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(uiposition.x, uiposition.y, 0))
  46. let _x;
  47. let _y;
  48. let time = _x
  49. this.isMove = true;
  50. if (pos.x > this.jingcha.position.x) {
  51. _x = pos.x - this.jingcha.position.x;
  52. this.jingcha.setScale(v3(1, 1, 1));
  53. } else {
  54. _x = this.jingcha.position.x - pos.x;
  55. this.jingcha.setScale(v3(-1, 1, 1));
  56. }
  57. if (pos.y > this.jingcha.position.y) {
  58. _y = pos.y - this.jingcha.position.y;
  59. } else {
  60. _y = this.jingcha.position.y - pos.y;
  61. }
  62. if (Math.abs(_x) > Math.abs(_y)) {
  63. time = Math.abs(_x);
  64. } else {
  65. time = Math.abs(_y)
  66. }
  67. this._tween = new Tween(this.jingcha).to(time / (GameManager.getInstance().speed * 10), { position: new Vec3(pos.x, pos.y, 0) }).call(() => {
  68. this.isMove = false;
  69. this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  70. this.move();
  71. // this.scheduleOnce(() => {
  72. this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
  73. // }, .5)
  74. }).start();
  75. }
  76. private updateZindex() {
  77. this.mainNode.children.sort((a, b) => {
  78. return b.position.y - a.position.y;
  79. })
  80. }
  81. startGame(): void {
  82. this.isGameover = false;
  83. this.pingminNum = 0;
  84. this.mainNode.removeAllChildren();
  85. this.jingcha = instantiate(this.jingchaNode);
  86. this.mainNode.addChild(this.jingcha);
  87. this.collider = this.jingcha.getComponent(BoxCollider2D);
  88. this.move();
  89. //刷新平民的数量
  90. for (let i = 0; i < GameManager.getInstance().maxPingminNum; i++) {
  91. let node = instantiate(this.player);
  92. node.active = true;
  93. node.getComponent(Player).setData("pingmin");
  94. this.mainNode.addChild(node);
  95. this.pingminNum++;
  96. node.position = v3(GameManager.getInstance().getRand(-560, 560), GameManager.getInstance().getRand(-430, 430), 0);
  97. }
  98. //刷新小偷的数量
  99. for (let i = 0; i < GameManager.getInstance().playerNum; i++) {
  100. let node = instantiate(this.player);
  101. node.active = true;
  102. node.getComponent(Player).setData("xiaotou");
  103. this.mainNode.addChild(node);
  104. node.position = v3(GameManager.getInstance().getRand(-560, 560), GameManager.getInstance().getRand(-430, 430), 0);
  105. }
  106. this.jingcha.position = v3(this.jingcha.position.x, this.jingcha.position.y, 0)
  107. }
  108. public move() {
  109. let x = GameManager.getInstance().getRand(-1, 1);
  110. let y = GameManager.getInstance().getRand(-1, 1);
  111. if (x == 0) {
  112. x = 0.5;
  113. }
  114. if (y == 0) {
  115. y = 0.5;
  116. }
  117. this._moveDirection = v2(x, y);
  118. }
  119. //碰撞事件
  120. onBeginContact(hero_collier: Collider2D, box_collier: Collider2D, contact: IPhysics2DContact | null) {
  121. let anim = hero_collier.node.getChildByName("jingcha").getComponent(Animation);
  122. let anim2 = box_collier.node.getChildByName("player").getComponent(Animation);
  123. anim.play("siwang");
  124. anim2.play("siwang");
  125. anim2.on(Animation.EventType.FINISHED, (Event, state) => {
  126. if (box_collier.node.name == "xiaotou") {
  127. //在此加分
  128. console.log("撞击小偷");
  129. AudioManage.instance.playSound(Global.Audio_Success);
  130. GameManager.getInstance().addScore(GameManager.getInstance().AddScoreNum);
  131. //删除一个小偷就要增加一个
  132. // let node = instantiate(this.player);
  133. // node.active = true;
  134. // node.getComponent(Player).setData("xiaotou");
  135. // this.mainNode.addChild(node);
  136. // node.position = v3(-560, GameManager.getInstance().getRand(-430, 430), 0);
  137. } else {
  138. //在此减分
  139. console.log("撞击平民");
  140. AudioManage.instance.playSound(Global.Audio_GameFail);
  141. GameManager.getInstance().minusScore(GameManager.getInstance().MinusScoreNum);
  142. this.pingminNum--;
  143. if (this.pingminNum <= 0) {
  144. //失败
  145. // GameManager.getInstance().saveDataToServer(1);
  146. AudioManage.instance.pauseMusic();
  147. GameManager.getInstance().gameFail();
  148. }
  149. }
  150. box_collier.node.removeFromParent();
  151. box_collier.node.destroy();
  152. this.move();
  153. }, this);
  154. this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  155. this._tween.stop().removeSelf();
  156. this.isMove = false;
  157. // this.scheduleOnce(() => {
  158. this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
  159. anim.getComponent(Animation).play("animation");
  160. // }, .5)
  161. }
  162. gameOver() {
  163. this.isGameover = true;
  164. this.mainNode.removeAllChildren();
  165. }
  166. update(deltaTime: number) {
  167. if (!this._moveDirection || this.isGameover) return;
  168. //改变层级
  169. this.updateZindex();
  170. if (this.isMove) {
  171. return;
  172. }
  173. // 计算移动位置
  174. this.jingcha.translate(v3(this._moveDirection.x * GameManager.getInstance().speed * deltaTime, this._moveDirection.y * GameManager.getInstance().speed * deltaTime, 0));
  175. if (this._moveDirection.x > 0) {
  176. this.jingcha.setScale(v3(1, 1, 1));
  177. } else {
  178. this.jingcha.setScale(v3(-1, 1, 1));
  179. }
  180. if (this.jingcha.position.x > this.node.getComponent(UITransform).contentSize.width / 2 - this.jc_w) {
  181. this._moveDirection = v2(-GameManager.getInstance().getRand(0.8, 1), this._moveDirection.y);
  182. }
  183. else if (this.jingcha.position.x < -(this.node.getComponent(UITransform).contentSize.width / 2 - this.jc_w)) {
  184. this._moveDirection = v2(GameManager.getInstance().getRand(0.8, 1), this._moveDirection.y);
  185. }
  186. if (this.jingcha.position.y > this.node.getComponent(UITransform).contentSize.height / 2 - this.jc_h) {
  187. this._moveDirection = v2(this._moveDirection.x, -GameManager.getInstance().getRand(0.8, 1));
  188. }
  189. else if (this.jingcha.position.y < -(this.node.getComponent(UITransform).contentSize.height / 2 - this.jc_h)) {
  190. this._moveDirection = v2(this._moveDirection.x, GameManager.getInstance().getRand(0.8, 1));
  191. }
  192. }
  193. }