GameMainPoint.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import { Component, _decorator,Node, Vec2, v2, Vec3, UITransform, Camera, Color, Sprite, math, game } from "cc";
  8. import { GameEvent } from "../../../Common/Base/GameEventConfig";
  9. import GameEventManager from "../../../Common/Base/GameEventManager";
  10. import { GameManager } from "../../../Manager/GameManager";
  11. import GameMainCtrl from "../../Ctrl/GameMainCtrl";
  12. import LevelCtrl from "../../Ctrl/LevelCtrl";
  13. import GameUtils from "../../GameUtils/GameUtils";
  14. import GameMainLine from "./GameMainLine";
  15. const { ccclass, property } = _decorator;
  16. @ccclass
  17. export default class GameMainPoint extends Component {
  18. private _indexID = 0
  19. private _ropeDataList: { rope: GameMainLine, linkPoint: GameMainPoint }[] = [];
  20. private color:number;
  21. private canmove:boolean=true;
  22. resetData(id: number) {
  23. this._indexID = id
  24. //绑定人头的移动事件
  25. this.node.on(Node.EventType.TOUCH_START, this._onTouch, this);
  26. this.node.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
  27. this.node.on(Node.EventType.TOUCH_END, this._onTouchEnd, this);
  28. this.node.on(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  29. this.canmove=true;
  30. }
  31. //给点存线头和线的信息
  32. addRopeData(data: { rope: GameMainLine, linkPoint: GameMainPoint }) {
  33. this._ropeDataList.push(data)
  34. this.setRopeInfo(this.node.getPosition(), data.linkPoint.node.getPosition(), data.rope.node)
  35. }
  36. getLineNum() {
  37. return this._ropeDataList.length
  38. }
  39. private _onTouch(event) {
  40. if(!this.canmove) return;
  41. // this.resetGesturesData()
  42. // GameUtils.getInstance().setVisible(this.gesturesOKBtn, false)
  43. // GameUtils.getInstance().setVisible(this.gesturesRedrawBtn, false)
  44. }
  45. public destroySelf(){
  46. this.canmove=false;
  47. this.node.off(Node.EventType.TOUCH_START, this._onTouch, this);
  48. this.node.off(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
  49. this.node.off(Node.EventType.TOUCH_END, this._onTouchEnd, this);
  50. this.node.off(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  51. this.node.destroy();
  52. }
  53. //移动线头,线跟着移动
  54. private _onTouchMove(event) {
  55. if(!this.canmove) return;
  56. var touches = event.getTouches();
  57. let location=touches[0].getUILocation()
  58. let pos1=new Vec3(location.x,location.y,0)
  59. let movePos=this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(pos1);
  60. // console.log(movePos);
  61. if(Math.abs(movePos.x)>this.node.parent.getComponent(UITransform).width/2){
  62. movePos.x=this.node.parent.getComponent(UITransform).width/2*(movePos.x>0?1:-1);
  63. }
  64. if(Math.abs(movePos.y)>this.node.parent.getComponent(UITransform).height/2){
  65. movePos.y=this.node.parent.getComponent(UITransform).height/2*(movePos.y>0?1:-1);
  66. }
  67. this.node.setPosition(movePos)
  68. for (let index = 0; index < this._ropeDataList.length; index++) {
  69. let ropeData = this._ropeDataList[index]
  70. this.setRopeInfo(this.node.getPosition(), ropeData.linkPoint.node.getPosition(), ropeData.rope.node)
  71. }
  72. }
  73. //结束移动,判断线与线的相交数量来显示线的颜色
  74. private _onTouchEnd() {
  75. if(!this.canmove) return;
  76. this.resetPointLine()
  77. if (GameMainCtrl.getInstance().checkIsGameOver()) {//游戏通关
  78. // let levelData = GameMainCtrl.getInstance().getNowLevelData()
  79. this.canmove=false;
  80. GameManager.getInstance().addScore(100);
  81. //let level=LevelCtrl.getInstance().getPassLevel();
  82. //LevelCtrl.getInstance().setPassLevel(levelData.level+1>=level?levelData.level+1:level);
  83. }else{
  84. GameEventManager.getInstance().dispathcGameEvent(GameEvent.MonkeyMove);
  85. }
  86. }
  87. //重置线的颜色
  88. resetPointLine() {
  89. for (let index = 0; index < this._ropeDataList.length; index++) {
  90. let ropeData = this._ropeDataList[index]
  91. this.setRopeInfo(this.node.getPosition(), ropeData.linkPoint.node.getPosition(), ropeData.rope.node)
  92. //ropeData.rope
  93. // cc.log("相交点数", num)
  94. // ropeData.rope.setColor(num)
  95. }
  96. GameMainCtrl.getInstance().resetLineIntersection();
  97. }
  98. //设置当前绳子的角度,长度和位置
  99. setRopeInfo(p1: Vec3, p2: Vec3, ropeNode: Node) {
  100. let dis = GameUtils.getInstance().distance(p1, p2)
  101. let x = (p1.x + p2.x) / 2
  102. let y = (p1.y + p2.y) / 2
  103. let ang = GameUtils.getInstance().angle(p1, p2)
  104. GameUtils.getInstance().setAngle(ropeNode, ang)
  105. ropeNode.setPosition(new Vec3(x, y))
  106. ropeNode.getComponent(UITransform).width = dis
  107. }
  108. ResetColorNum(){
  109. this.color=0;
  110. }
  111. AddColorNum(num:number){
  112. this.color+=num;
  113. this.setColor();
  114. }
  115. //设置点的颜色
  116. setColor() {
  117. let allColor = [Color.GREEN, Color.YELLOW, Color.RED]
  118. this.color = this.color >= 3 ? 2 : this.color
  119. this.node.children[0].getComponent(Sprite).color = allColor[this.color];
  120. }
  121. }