FishManager.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function FishManager() {
  2. base(this,LSprite,[]);
  3. this.init();
  4. }
  5. FishManager.prototype.init = function() {
  6. var self = this;
  7. self.setFishRule =
  8. [{duration:30, period:1, speedInSecond:{min:4,max:7}}
  9. ,{duration:20, period:1.5, speedInSecond:{min:3,max:5}}
  10. ,{duration:-1, period:1.5, speedInSecond:{min:2,max:3}}
  11. ];
  12. self.fishType =
  13. [{
  14. stype:"l"
  15. ,images:[{name:"l_1", width:336, height:84, frameNumber:4}
  16. ,{name:"l_2", width:400, height:80, frameNumber:4}
  17. ,{name:"l_3", width:480, height:82, frameNumber:4}
  18. ,{name:"l_4", width:400, height:80, frameNumber:4}]
  19. },{
  20. stype:"m"
  21. ,images:[{name:"m_1", width:280, height:70, frameNumber:4}
  22. ,{name:"m_2", width:280, height:55, frameNumber:4}
  23. ,{name:"m_3", width:240, height:80, frameNumber:4}]
  24. },{
  25. stype:"s"
  26. ,images:[{name:"s_1", width:240, height:30, frameNumber:4}
  27. ,{name:"s_2", width:280, height:50, frameNumber:4}]
  28. }];
  29. self.setTimeoutQueue = {};
  30. self.fishData = {};
  31. self.setFishData();
  32. self.setStep(0);
  33. self.setFish();
  34. self.frameCount = 0;
  35. self.addEventListener(LEvent.END_CONTACT, self.onremoved);
  36. // self.addEventListener(LEvent.ENTER_FRAME, self.onframe);
  37. };
  38. FishManager.prototype.setFishData = function() {
  39. var self = this;
  40. for(var i in self.fishType) {
  41. for(var j in self.fishType[i].images) {
  42. var image = self.fishType[i].images[j];
  43. self.fishData[image["name"]] = new LBitmapData(dataList[image["name"]], 0, 0, Math.floor(image.width/image.frameNumber), image.height);
  44. }
  45. }
  46. }
  47. FishManager.prototype.setStep = function(newStep) {
  48. var self = this;
  49. self.currentStep = newStep;
  50. if (self.setFishRule[self.currentStep].duration != -1) {
  51. self.setTimeoutQueue["setStep"] = setTimeout(function() {
  52. self.setStep.call(self, self.currentStep+1)
  53. }, self.setFishRule[self.currentStep].duration*1000);
  54. }
  55. };
  56. FishManager.prototype.setFish = function() {
  57. var self = this;
  58. var step = self.currentStep;
  59. var options = {};
  60. var stypeIndex = Util.randomN(self.fishType.length);
  61. options.stype = self.fishType[stypeIndex].stype;
  62. options.image = self.fishType[stypeIndex].images[Util.randomN(self.fishType[stypeIndex].images.length)];
  63. options.motion = {
  64. frequency: Util.randomInRange(0.02, 0.03)
  65. ,phase: Util.randomInRange(400, 900)
  66. ,amplitude: Util.randomInRange(20, 30)
  67. ,direction: Util.randomN(2)
  68. ,speed: GAME_FPS*(LGlobal.width/Util.randomInRange(self.setFishRule[step].speedInSecond.min, self.setFishRule[step].speedInSecond.max))/1000
  69. }
  70. var fish = new Fish(options, self.fishData);
  71. self.addChild(fish);
  72. self.setTimeoutQueue["setStep"] = setTimeout(function() {
  73. self.setFish.call(self);
  74. }, self.setFishRule[step].period*1000);
  75. }
  76. FishManager.prototype.clearSetTimeoutQueue = function() {
  77. for(var i in self.setTimeoutQueue) {
  78. clearTimeout(self.setTimeoutQueue[i]);
  79. }
  80. }
  81. FishManager.prototype.onremoved = function() {
  82. self.clearSetTimeoutQueue();
  83. }
  84. FishManager.prototype.onframe = function() {
  85. self.frameCount += 1;
  86. }
  87. FishManager.prototype.setTimeoutByFrameCount = function(callback, interval) {
  88. }