Spawn.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const BottleType = require('Types').BottleType;
  2. const SizeType = require('Types').SizeType;
  3. const sizeMap = ['S', 'S', 'M', 'M', 'L', 'L'];
  4. const Spawn = cc.Class({
  5. name: 'Spawn',
  6. properties: {
  7. bottleType: {
  8. default: BottleType.MNormal,
  9. type: BottleType
  10. },
  11. total: 0,
  12. },
  13. ctor () {
  14. this.spawned = 0;
  15. this.finished = false;
  16. },
  17. init (obj) {
  18. if(obj) {
  19. this.bottleType = obj.bottleType;
  20. this.total = obj.total || this.total;
  21. }
  22. },
  23. spawn (bottleMng) {
  24. if (this.spawned >= this.total) {
  25. return;
  26. }
  27. this.size = SizeType[sizeMap[this.bottleType]];
  28. let newBottle = bottleMng.requestBottle(this.bottleType);
  29. if (newBottle) {
  30. this.spawned++;
  31. if (this.spawned === this.total) {
  32. this.finished = true;
  33. }
  34. return newBottle;
  35. } else {
  36. console.log('max bottle count reached, will delay spawn');
  37. return null;
  38. }
  39. }
  40. });
  41. module.exports = Spawn;