WorkingBuilding.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. var reGameStates = require('GameStates');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. stateArray: {
  6. default: [],
  7. type: cc.SpriteFrame,
  8. serializable: true,
  9. },
  10. workArray: {
  11. default: [],
  12. type: cc.SpriteFrame,
  13. serializable: true,
  14. },
  15. changeSprite: {
  16. default: null,
  17. type: cc.Sprite,
  18. serializable: true,
  19. },
  20. //工作状态
  21. workingStatus: {
  22. default: null,
  23. type: cc.Node,
  24. serializable: true,
  25. },
  26. workPlace: {
  27. default: reGameStates.BuildType.Farmland,
  28. type: cc.Enum(reGameStates.BuildType),
  29. serializable: true,
  30. },
  31. },
  32. start() {
  33. if (this.workPlace == reGameStates.BuildType.TimberYard) {
  34. // this.onChangeSpriteFromGrow();
  35. }
  36. },
  37. onChangeFromAIWorker(_workPlace, _consume) {
  38. // cc.log('this.workPlace',_workPlace,_consume);
  39. if (_workPlace == reGameStates.BuildType.Farmland) {
  40. this.onChangeSpriteFromWork(_consume);
  41. } else if (_workPlace == reGameStates.BuildType.TimberYard) {
  42. this.onChangeSpriteFromTimberYardWork(_consume);
  43. } else if (_workPlace == reGameStates.BuildType.MiningPit) {
  44. this.onChangeSpriteFromWork(_consume);
  45. }
  46. },
  47. // 工作时候
  48. onChangeSpriteFromWork(consume) {
  49. if (consume <= 0 || consume >= 50) {
  50. this.workingStatus.active = false;
  51. } else if (!this.workingStatus.active) {
  52. this.workingStatus.active = true
  53. }
  54. let index = 50 - consume;
  55. /*
  56. //这部分是旧的代码
  57. if (index < 20 && index > 10)
  58. // this.changeSprite.spriteFrame = this.workArray[0];
  59. this.changeSprite.spriteFrame = this.workArray[0];
  60. else if (index >= 20 && index <= 35)
  61. this.changeSprite.spriteFrame = this.workArray[1];
  62. else if (index > 35)
  63. this.changeSprite.spriteFrame = this.workArray[2];
  64. */
  65. if (index < 20 && index > 10) {
  66. this.changeSprite.spriteFrame = this.workArray[0];
  67. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  68. // this.changeSprite.node.width = this.workArray[0].getRect().width;
  69. // this.changeSprite.node.height = this.workArray[0].getRect().height;
  70. }
  71. else if (index >= 20 && index <= 35) {
  72. this.changeSprite.spriteFrame = this.workArray[1];
  73. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  74. // this.changeSprite.node.width = this.workArray[1].getRect().width;
  75. // this.changeSprite.node.height = this.workArray[1].getRect().height;
  76. }
  77. else if (index > 35) {
  78. this.changeSprite.spriteFrame = this.workArray[2];
  79. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  80. // this.changeSprite.node.width = this.workArray[2].getRect().width;
  81. // this.changeSprite.node.height = this.workArray[2].getRect().height;
  82. }
  83. },
  84. onChangeSpriteFromTimberYardWork(consume) {
  85. if (consume <= 0 || consume >= 50) {
  86. this.workingStatus.active = false;
  87. } else if (!this.workingStatus.active) {
  88. this.workingStatus.active = true
  89. }
  90. let index = 50 - consume;
  91. /*
  92. //这部分是旧的代码
  93. if (index < 25 && index > 10)
  94. this.changeSprite.spriteFrame = this.workArray[0];
  95. else if (index >= 25 && index <= 40)
  96. this.changeSprite.spriteFrame = this.workArray[1];
  97. else if (index > 40)
  98. this.changeSprite.spriteFrame = this.workArray[2];
  99. */
  100. if (index < 25 && index > 10) {
  101. this.changeSprite.spriteFrame = this.workArray[0];
  102. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  103. // this.changeSprite.node.width = this.workArray[0].getRect().width;
  104. // this.changeSprite.node.height = this.workArray[0].getRect().height;
  105. // cc.log(this.workArray[0].getRect().width);
  106. }
  107. else if (index >= 25 && index <= 40) {
  108. this.changeSprite.spriteFrame = this.workArray[1];
  109. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  110. // this.changeSprite.node.width = this.workArray[1].getRect().width;
  111. // this.changeSprite.node.height = this.workArray[1].getRect().height;
  112. }
  113. else if (index > 40) {
  114. this.changeSprite.spriteFrame = this.workArray[2];
  115. // this.changeSprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  116. // this.changeSprite.node.width = this.workArray[2].getRect().width;
  117. // this.changeSprite.node.height = this.workArray[2].getRect().height;
  118. }
  119. },
  120. // 运输时候调用
  121. onChangeSpriteFromGrow() {
  122. this.count = 0;
  123. this.callback = function () {
  124. if (this.count === this.stateArray.length - 1) {
  125. // 在第六次执行回调时取消这个计时器
  126. this.unschedule(this.callback);
  127. }
  128. this.onTreeGrowth(this.count);
  129. this.count++;
  130. }
  131. this.schedule(this.callback, 0.5);
  132. },
  133. /**
  134. * 神龙田地的直接生长
  135. */
  136. onHolyFarmlandSeedFromGrow() {
  137. this.count = 0;
  138. this.callback = function () {
  139. if (this.count === this.stateArray.length - 1) {
  140. // 在第六次执行回调时取消这个计时器
  141. this.unschedule(this.callback);
  142. }
  143. this.onTreeGrowth(this.count);
  144. this.count++;
  145. }
  146. this.schedule(this.callback, 0.5);
  147. },
  148. // 生长
  149. onTreeGrowth(count) {
  150. this.changeSprite.spriteFrame = this.stateArray[count];
  151. },
  152. /**
  153. * 神龙田地的直接 生长图片设置
  154. */
  155. onSetGrow(growStage, harvestSpriteFrame) {
  156. // console.log(growStage,harvestSpriteFrame);
  157. if (growStage === 0) {
  158. this.changeSprite.spriteFrame = null;
  159. } else if (growStage === 1 || growStage === 2) {
  160. this.changeSprite.spriteFrame = this.stateArray[growStage - 1];
  161. } else {
  162. this.changeSprite.spriteFrame = harvestSpriteFrame;
  163. }
  164. },
  165. onResetSprite(_workPlace) {
  166. if (_workPlace == reGameStates.BuildType.Farmland) {
  167. // this.changeSprite.spriteFrame = this.stateArray[0];
  168. this.changeSprite.spriteFrame = this.stateArray[0];
  169. this.onChangeSpriteFromGrow();
  170. } else if (_workPlace == reGameStates.BuildType.TimberYard) {
  171. //运输时候木材清空
  172. this.changeSprite.spriteFrame = this.workArray[3];
  173. this.onChangeSpriteFromGrow();
  174. } else if (_workPlace == reGameStates.BuildType.MiningPit) {
  175. this.changeSprite.spriteFrame = this.stateArray[0];
  176. }
  177. },
  178. //设置当前的完成状态
  179. //读取数据时候设置
  180. //todo....
  181. onSetWorkStateFormReadingData(_Index) {
  182. this.changeSprite.spriteFrame = this.workArray[_Index];
  183. },
  184. });