ArtistMan.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const ArtistManager = require('../utils/ArtistManager');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. nickLabel: cc.Label,
  6. runMan: cc.Node,
  7. artistSkeleton: sp.Skeleton,
  8. direction: {
  9. get: function () {
  10. if (!this._direction) {
  11. this._direction = 1;
  12. }
  13. return this._direction;
  14. },
  15. set: function(value) {
  16. this._direction = value;
  17. }
  18. },
  19. _accSpeed: 50,
  20. _maxX: 270,
  21. },
  22. init(artistData, direction) {
  23. this.artistData = artistData;
  24. if (!this.isAlready) {
  25. this.direction = direction;
  26. this.node.x = Math.random()*(Math.random()>0.5?1:-1) * this.artistRandom(-150, 150);
  27. this.walkDistance = this.artistRandom(250, 350);
  28. }
  29. this.walkAnimation = [];
  30. this.standAnimation = [];
  31. this.isStand = false;
  32. this.artistSkeleton.clearTracks();
  33. let name = 60000 + parseInt(this.artistData.starId);
  34. this.starName = name;
  35. this.node.active = false;
  36. // 根据明星id来加载不同骨骼
  37. ArtistManager.loadStarSpine(this.artistData.starId)
  38. .then((skeletonData) => {
  39. //判断当前明星的名字是否与骨骼的name一样,因资源为异步加载,可能会出现明星显示不对的情况
  40. if (this.starName == skeletonData._name) {
  41. this.artistSkeleton.skeletonData = skeletonData;
  42. var sd = skeletonData.getRuntimeData(true);
  43. if (sd) {
  44. /**
  45. * walk1为普通行走,如果有walk2则为特殊行走动作
  46. * stand为特殊站立动作
  47. */
  48. this.walkAnimation = sd.animations.filter(item => item.name.indexOf('walk') != -1).map(animation => animation.name) || [];
  49. this.standAnimation = sd.animations.filter(item => item.name.indexOf('stand') != -1).map(animation => animation.name) || [];
  50. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  51. this.isAlready = true;
  52. this.node.active = true;
  53. } else {
  54. this.isAlready = false;
  55. this.node.active = false;
  56. }
  57. }
  58. }).catch((err) => {
  59. console.log(err);
  60. this.isAlready = false;
  61. this.node.active = false;
  62. });
  63. },
  64. onLoad () {
  65. this.isAlready = false;
  66. this.node.y = 20;
  67. this.isStand = false;
  68. this.totalDistance = 0;
  69. },
  70. artistRandom(start, end){
  71. return Math.floor(Math.random() * (end - start) + start);
  72. },
  73. update (dt) {
  74. if (this.isStand) { return; }
  75. // 每一帧移动多少距离, 这是固定的
  76. let distance = this._accSpeed * dt;
  77. // console.log('distance ' + distance);
  78. this.totalDistance += distance;
  79. //骨骼移动到一定距离后做出特定动作
  80. if (Math.floor(this.totalDistance) >= this.walkDistance) {
  81. this.totalDistance = 0;
  82. this.walkDistance = this.artistRandom(250, 350);
  83. if (this.standAnimation.length > 0) { // stand
  84. this.isStand = true;
  85. //特殊明星含有文字动画处理
  86. if (this.direction < 0 && this.standAnimation.length > 1) {
  87. this.runMan.scaleX = 1;
  88. this.artistSkeleton.setAnimation(0, this.standAnimation[1], false);
  89. } else {
  90. this.artistSkeleton.setAnimation(0, this.standAnimation[0], false);
  91. }
  92. this.artistSkeleton.setCompleteListener(() => {
  93. this.isStand = false;
  94. this.artistSkeleton.completeListener = null;
  95. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  96. });
  97. return;
  98. } else { // walk
  99. if (this.walkAnimation.length > 0) {
  100. let walk = Math.floor(Math.random() * this.walkAnimation.length);
  101. if (walk === 0) {
  102. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], true);
  103. } else {
  104. if (walk < this.walkAnimation.length) {
  105. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], false);
  106. this.artistSkeleton.setCompleteListener(() => {
  107. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  108. this.artistSkeleton.completeListener = null;
  109. });
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // 判断方向小于0为从左向右走
  116. if (this.direction < 0) {
  117. this.runMan.scaleX = -1;
  118. if ((this.node.x + distance) > this._maxX) {
  119. this.direction = 1;
  120. if (this.walkAnimation.length > 0) {
  121. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  122. }
  123. } else {
  124. this.node.x = this.node.x + distance;
  125. }
  126. } else {
  127. this.runMan.scaleX = 1;
  128. if ((this.node.x - distance) < -this._maxX) {
  129. this.direction = -1;
  130. if (this.walkAnimation.length > 0) {
  131. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  132. }
  133. } else {
  134. this.node.x = this.node.x - distance;
  135. }
  136. }
  137. },
  138. });