ArtistMan.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) {
  23. this.artistData = artistData;
  24. this.nickLabel.string = artistData.nick;
  25. this.walkAnimation = [];
  26. this.standAnimation = [];
  27. // this.artistData.jobId 固定为2吧, 暂时只有舞者的骨骼动画
  28. ArtistManager.loadArtist(false, this.artistData.gender, this.artistData.jobId)
  29. .then((skeletonData) => {
  30. this.artistSkeleton.skeletonData = skeletonData;
  31. var sd = skeletonData.getRuntimeData(true);
  32. if (sd) {
  33. /**
  34. * item: Animation {name: "stand1", timelines: Array(113), duration: 2}
  35. */
  36. this.walkAnimation = sd.animations.filter(item => item.name.indexOf('walk') != -1).map(animation => animation.name) || [];
  37. this.standAnimation = sd.animations.filter(item => item.name.indexOf('stand') != -1).map(animation => animation.name) || [];
  38. }
  39. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  40. }).catch((err) => {
  41. console.log(err);
  42. });
  43. },
  44. onLoad () {
  45. this.node.y = 20;
  46. this.isStand = false;
  47. this.totalDistance = 0;
  48. },
  49. update (dt) {
  50. if (this.isStand) { return; }
  51. // 每一帧移动多少距离, 这是固定的
  52. let distance = this._accSpeed * dt;
  53. this.totalDistance += distance;
  54. if (Math.floor(this.totalDistance) == 340) {
  55. this.totalDistance = 0;
  56. let random = Math.floor(Math.random() * 2);
  57. if (random === 0) { // walk
  58. let walk = Math.floor(Math.random() * this.walkAnimation.length);
  59. if (walk === 0) {
  60. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], true);
  61. } else {
  62. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], false);
  63. this.artistSkeleton.setCompleteListener(() => {
  64. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  65. this.artistSkeleton.completeListener = null;
  66. });
  67. }
  68. } else { // stand
  69. let stand = Math.floor(Math.random() * this.standAnimation.length);
  70. this.artistSkeleton.setAnimation(0, this.standAnimation[stand], false);
  71. this.artistSkeleton.setCompleteListener(() => {
  72. this.isStand = false;
  73. this.artistSkeleton.completeListener = null;
  74. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  75. });
  76. this.isStand = true;
  77. return;
  78. }
  79. }
  80. // 判断方向
  81. if (this.direction < 0) {
  82. this.runMan.scaleX = -1;
  83. if ((this.node.x + distance) > this._maxX) {
  84. this.direction = 1;
  85. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  86. } else {
  87. this.node.x = this.node.x + distance;
  88. }
  89. } else {
  90. this.runMan.scaleX = 1;
  91. if ((this.node.x - distance) < -this._maxX) {
  92. this.direction = -1;
  93. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  94. } else {
  95. this.node.x = this.node.x - distance;
  96. }
  97. }
  98. },
  99. });