Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 1.33 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ig.module('game.entities.non-weltmeister.weather-controller')
  2.  
  3. .requires('impact.entity')
  4.  
  5. .defines(function() {
  6.  
  7.         EntityWeatherController = ig.Entity.extend({
  8.  
  9.                 // How many raindrops to spawn per second.
  10.                 rainRate: 64,
  11.  
  12.                 // Timer for spawning entities.
  13.                 timer: null,
  14.  
  15.                 // Used to prevent spawning too quickly.
  16.                 lastFrame: -1,
  17.  
  18.                 // Type of weather effect.
  19.                 weather: null,
  20.  
  21.                 init: function(x, y, settings) {
  22.                         this.parent(x, y, settings);
  23.  
  24.                         // Start spawn timer.
  25.                         this.timer = new ig.Timer();
  26.                 },
  27.  
  28.                 update: function() {
  29.  
  30.                         if(this.weather=='rain') rate = this.rainRate;
  31.  
  32.                         var frame = Math.floor(this.timer.delta() * rate);
  33.  
  34.                         // Rain
  35.                         if (this.weather=='rain') {
  36.  
  37.                                 if( frame != this.lastFrame ) {
  38.  
  39.                                         // Make up for missed spawns if too many frames passed.
  40.                                         for(var i=0; i< frame - this.lastFrame; i++) {
  41.  
  42.                                                 // Random distance from left of screen.
  43.                                                 var x = ig.game.screen.x + Math.floor(Math.random() * (ig.system.width + ((1 / 3) * ig.system.height) - 30)) + 30;
  44.  
  45.                                                 // Random distance above top of screen.
  46.                                                 var y = ig.game.screen.y - Math.floor(Math.random() * 16) - 32;
  47.  
  48.                                                 // Spawn a raindrop.
  49.                                                 ig.game.spawnEntity(EntityRaindrop, x, y, {});
  50.  
  51.                                         }
  52.  
  53.                                         this.lastFrame = frame;
  54.                                 }
  55.                         }
  56.  
  57.                         // Call parent.
  58.                         this.parent();
  59.                 }
  60.         });
  61. });