Girl.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. this.Girl = this.Girl || {};
  2. (function(G, cjs){
  3. var lib = {},p;
  4. (lib.Girl = function(_data,_conflicts){
  5. this.initialize();
  6. this._srcData = this.copyData(_data);
  7. this._dressData = this.copyData(_data);
  8. this._dressParts = {};
  9. this._conflicts = _conflicts || {};
  10. if(G.createUI != null) {
  11. G.createUI(this);
  12. }
  13. }).prototype = p = new cjs.Container;
  14. p.copyData = function(sData) {
  15. var o = {};
  16. for(var m in sData) {
  17. o[m] = sData[m];
  18. }
  19. return o;
  20. };
  21. p._srcData = null;
  22. p._conflicts = null;
  23. p._dressData = null;
  24. p._dressParts = null;
  25. p.addDress = function(item, key) {
  26. this.addChild(item);
  27. if(key) {
  28. if(!this._dressParts.hasOwnProperty(key)) {
  29. this._dressParts[key] = [];
  30. }
  31. this._dressParts[key].push(item);
  32. }
  33. };
  34. p.render = function(){
  35. for(var m in this._dressParts) {
  36. this.showPart(m);
  37. }
  38. };
  39. p.reset = function(_data) {
  40. if(_data) {
  41. this._srcData = this.copyData(_data);
  42. }
  43. this._dressData = this.copyData(this._srcData);
  44. this.render();
  45. };
  46. p.autoNext = function(pid) {
  47. var item = this._dressParts[pid][0];
  48. var total = item.spriteSheet.getNumFrames();
  49. var vid = undefined;
  50. if(this._dressData.hasOwnProperty(pid)) {
  51. vid = this._dressData[pid];
  52. }
  53. if(isNaN(vid)) {
  54. vid = 0;
  55. } else {
  56. vid ++;
  57. if(vid >= total) {
  58. vid = 0;
  59. }
  60. }
  61. this.change(pid, vid);
  62. };
  63. p.change = function(pid, vid) {
  64. this._dressData[pid] = vid;
  65. this.showPart(pid);
  66. this.handleConflict(pid);
  67. };
  68. p.handleConflict = function(pKey) {
  69. if(!this._conflicts.hasOwnProperty(pKey)) {
  70. return;
  71. }
  72. var arr = this._conflicts[pKey];
  73. var vid = this._dressData[pKey];
  74. var needErase = !isNaN(vid);
  75. var len = arr.length;
  76. for(var i = 0; i < len; i ++) {
  77. var tmpKey = arr[i];
  78. this._dressData[tmpKey] = needErase?undefined:(this._srcData.hasOwnProperty(tmpKey)?this._srcData[tmpKey]:undefined);
  79. this.showPart(tmpKey);
  80. }
  81. };
  82. p.showPart = function(pKey) {
  83. var arr = this._dressParts[pKey];
  84. var len = arr.length;
  85. var vid = undefined;
  86. if(this._dressData.hasOwnProperty(pKey)) {
  87. vid = this._dressData[pKey];
  88. }
  89. for(var i = 0; i < len; i ++) {
  90. var item = arr[i];
  91. if(isNaN(vid)) {
  92. item.visible = false;
  93. } else {
  94. item.gotoAndStop(vid);
  95. item.visible = true;
  96. }
  97. }
  98. };
  99. p.getData = function(){
  100. return this.copyData(this._dressData);
  101. };
  102. G._instance = null;
  103. G.getInstance = function(){
  104. return G._instance;
  105. };
  106. G.getPerson = function(_data, _conflicts) {
  107. return new lib.Girl(_data,_conflicts);
  108. };
  109. G.createUI = null;
  110. })(this.Girl, createjs);