heartBeat.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const {
  2. ccclass,
  3. property
  4. } = cc._decorator;
  5. export class heartNode {
  6. position: cc.Vec2;
  7. heartBeat: heartBeat;
  8. constructor(pos: cc.Vec2) {
  9. this.position = pos;
  10. }
  11. }
  12. @ccclass
  13. export default class heartBeat extends cc.Component {
  14. @property(cc.Node)
  15. rootNode: cc.Node = null;
  16. @property(cc.Graphics)
  17. Graphic: cc.Graphics = null;
  18. /**
  19. * 存储所有需要绘制的点
  20. *
  21. * @type {Array<heartNode>}
  22. * @memberof heartBeat
  23. */
  24. heartNodeList: Array < heartNode >= null;
  25. /**
  26. * 移动速度
  27. *
  28. * @type {number}
  29. * @memberof heartBeat
  30. */
  31. moveSpeed: number = 1;
  32. /**
  33. * 浮漂,最新创建的点会追逐这个浮漂
  34. *
  35. * @type {number}
  36. * @memberof heartBeat
  37. */
  38. buoy: number = 0;
  39. /**
  40. * 跟随浮漂移动的当前值
  41. *
  42. * @type {number}
  43. * @memberof heartBeat
  44. */
  45. nowValue: number = 0;
  46. addPoint(y: number, qiangdu) {
  47. // console.log(y);
  48. if (this.heartNodeList == null) {
  49. this.heartNodeList = new Array();
  50. }
  51. //控制游标到标定点
  52. // this.buoy = -this.rootNode.height / 2 + (this.rootNode.height * y * 0.01);
  53. this.buoy =(this.rootNode.height/2) * (y * 0.01);
  54. // console.log("--> 游标位置:" + this.buoy);
  55. // console.log("--> 实际数值:" + y);
  56. // console.log("--> 强度 越强越长:" + qiangdu);
  57. // let maiboqiangdu = MathUtil.reMap(qiangdu, 0, 100, 0.15, 0.25)
  58. cc.tween(this)
  59. .to(0.35, {
  60. nowValue: this.buoy
  61. }, {
  62. easing: 'sineInOut'
  63. })
  64. .call(() => {
  65. // this.buoy = -this.rootNode.height / 2;
  66. this.buoy = 0;
  67. })
  68. .to(0.65, {
  69. nowValue: -this.buoy
  70. // nowValue: -this.rootNode.height / 2
  71. }, {
  72. easing: 'sineInOut'
  73. })
  74. .start();
  75. }
  76. //现在数据需要每帧更新
  77. update(dt) {
  78. //清空画布
  79. this.Graphic.clear();
  80. if (this.heartNodeList == null) {
  81. return;
  82. }
  83. //没数据就不用画
  84. if (this.heartNodeList.length <= 0) {
  85. // this.heartNodeList.push(new heartNode(cc.v2(this.rootNode.width / 2, -this.rootNode.height / 2)));
  86. this.heartNodeList.push(new heartNode(cc.v2(this.rootNode.width / 2, 0)));
  87. return;
  88. }
  89. this.DrawCenterLine();
  90. this.DrawWaveLine();
  91. }
  92. /**
  93. * 绘制中间的横线
  94. *
  95. * @memberof heartBeat
  96. */
  97. DrawCenterLine(){
  98. this.Graphic.strokeColor=new cc.Color(255,0,0,255);
  99. this.Graphic.moveTo(-this.rootNode.width / 2,0);
  100. this.Graphic.lineTo(this.rootNode.width / 2,0);
  101. this.Graphic.stroke();
  102. }
  103. /**
  104. * 绘制波浪线
  105. *
  106. * @memberof heartBeat
  107. */
  108. DrawWaveLine(){
  109. this.Graphic.strokeColor=new cc.Color(0,255,0,255);
  110. //先验证有多少个出界的,记录一下
  111. let verificationCount = 0;
  112. for (let i = 0; i < this.heartNodeList.length; i++) {
  113. //计算出位置
  114. if (this.heartNodeList[i].position.x < -this.rootNode.width / 2) {
  115. verificationCount++;
  116. } else {
  117. }
  118. }
  119. if (verificationCount != 0) {
  120. for (let i = 0; i < verificationCount; i++) {
  121. this.heartNodeList.shift();
  122. }
  123. }
  124. //验证完毕,可以绘制有效node
  125. for (let i = 0; i < this.heartNodeList.length; i++) {
  126. if (i == 0) {
  127. this.Graphic.moveTo(this.heartNodeList[i].position.x, this.heartNodeList[i].position.y);
  128. } else {
  129. this.Graphic.lineTo(this.heartNodeList[i].position.x, this.heartNodeList[i].position.y);
  130. }
  131. }
  132. //让所有node往左边移动一点
  133. for (let i = 0; i < this.heartNodeList.length; i++) {
  134. this.heartNodeList[i].position = cc.v2(this.heartNodeList[i].position.x - this.moveSpeed, this.heartNodeList[i].position.y);
  135. }
  136. // 添加一个新的中间点
  137. let lerpNode = new heartNode(cc.v2(this.rootNode.width / 2, this.nowValue))
  138. this.heartNodeList.push(lerpNode);
  139. if (this.heartNodeList.length>=3) {
  140. this.Graphic.stroke();
  141. }
  142. }
  143. }