Iavra

Particle Engine - Counters

Jan 28th, 2016 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc v1.01 Extension, that comes with an assortment of counters to be used for the particle engine.
  3.  * @author Iavra
  4.  *
  5.  * Changelog:
  6.  * - 1.01: Renamed the "Steady" counter to "Interval" and added randomization options.
  7.  *
  8.  * @help
  9.  * When setting an emitter's counter, you can specify one of the classes contained in this plugin, like this:
  10.  *
  11.  * emitter.setCounter(new IAVRA.PARTICLE.C.Blast(10));
  12.  *
  13.  * The example creates a Blast counter, which will create a certain number of particles, when the emitter starts and
  14.  * no particles during runtime.
  15.  *
  16.  * The following counters are contained in the IAVRA.PARTICLE.C / IAVRA.PARTICLE.COUNTER namespace:
  17.  *
  18.  * Blast(count)                      Creates a number of particles, when the emitter starts and none during runtime.
  19.  *                                   Can be overloaded, by supplying an array instead, which contains the range used
  20.  *                                   to calculate a random number of particles.
  21.  * Trigger(count)                    Creates a number of particles, whenever its "trigger" function is called. Can be
  22.  *                                   overloaded, by supplying an array instead, which contains the range used to
  23.  *                                   calculate a random number of particles.
  24.  * Interval(count, interval)         Creates the given number of particles in a given interval, in frames. Both
  25.  *                                   parameters can be overloaded by supplying an array, instead, which contains the
  26.  *                                   range used to generate random numbers.
  27.  * Variable(countVar, intervalVar)   Creates a number of particles at a variable rate, both of which are defined by the
  28.  *                                   given variables.
  29.  * Switch(counter, switchId)         Wraps another counter object and surpresses it, if the given switch is set to OFF.
  30.  */
  31.  
  32. (function($, undefined) {
  33.     "use strict";
  34.  
  35.     if(!$.PARTICLE) { throw new Error("This plugin depends on 'Iavra Particle - Core'."); }
  36.  
  37.     /**
  38.      * Extends a given object with all properties from another given object. Mainly used to simplify subclassing.
  39.      * @param {Object} base - Base object to be extended.
  40.      * @param {Object} extend - Object containing the properties to be appended to the given base object.
  41.      * @returns {Object} The base object, after it has been extended.
  42.      */
  43.     var _extend = function(base, extend) {
  44.         for(var key in extend) { base[key] = extend[key]; }
  45.         return base;
  46.     };
  47.  
  48.     /**
  49.      * Returns a random floating point number between the given minimum (inclusive) and maximum (exclusive).
  50.      * @param {number} min - Minimum number to be generated (inclusive).
  51.      * @param {number} max - Maximum number to be generated (exclusive).
  52.      * @returns {number} A random number between the given minimum and maximum numbers.
  53.      */
  54.     var _random = function(min, max) {
  55.         return min + (max - min) * Math.random();
  56.     };
  57.  
  58.     //=============================================================================
  59.     // IAVRA.PARTICLE.COUNTER
  60.     //=============================================================================
  61.  
  62.     $.PARTICLE.COUNTER = $.PARTICLE.C = {
  63.         Blast: function(count) { this._initialize(count); },
  64.         Trigger: function(count) { this._initialize(count); },
  65.         Interval: function(count, interval) { this._initialize(count, interval); },
  66.         Variable: function(countVar, intervalVar) { this._initialize(countVar, intervalVar); },
  67.         Switch: function(counter, switchId) { this._initialize(counter, switchId); }
  68.     };
  69.  
  70.     //=============================================================================
  71.     // IAVRA.PARTICLE.COUNTER.Blast
  72.     //=============================================================================
  73.  
  74.     /**
  75.      * A Blast counter creates a given number of particles, when the emitter starts and won't create new particles
  76.      * afterwards.
  77.      */
  78.     $.PARTICLE.COUNTER.Blast.prototype = _extend(Object.create($.PARTICLE.Counter.prototype), {
  79.  
  80.         /**
  81.          * Initializes the counter. Takes the number of particles to create as a parameter and can be overloaded, by
  82.          * passing an array instead of a number.
  83.          * @param {(number|number[])} count - Number of particles to create. If this is an array, its first and second
  84.          * element are used as the lower and upper bound to calculate a random number.
  85.          */
  86.         _initialize: function(count) {
  87.             if(Array.isArray(count)) {
  88.                 this._min = count[0];
  89.                 this._max = count[1];
  90.             } else {
  91.                 this._min = this._max = count;
  92.             }
  93.         },
  94.  
  95.         /**
  96.          * Gets called, when the emitter is started. Can specify any number of particles to be created at this point.
  97.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  98.          * @returns {number} Either the given count or a random number between the given minimum and maximum.
  99.          */
  100.         start: function(emitter) {
  101.             return (this._min === this.max) ? this._min : _random(this._min, this._max);
  102.         }
  103.  
  104.     });
  105.  
  106.     //=============================================================================
  107.     // IAVRA.PARTICLE.COUNTER.Trigger
  108.     //=============================================================================
  109.  
  110.     /**
  111.      * A Trigger counter creates a given number of particles, whenever its "trigger" function is called.
  112.      */
  113.     $.PARTICLE.COUNTER.Trigger.prototype = _extend(Object.create($.PARTICLE.Counter.prototype), {
  114.  
  115.         /**
  116.          * Initializes the counter. Takes the number of particles to create as a parameter and can be overloaded, by
  117.          * passing an array instead of a number.
  118.          * @param {(number|number[])} count - Number of particles to create. If this is an array, its first and second
  119.          * element are used as the lower and upper bound to calculate a random number.
  120.          */
  121.         _initialize: function(count) {
  122.             if(Array.isArray(count)) {
  123.                 this._min = count[0];
  124.                 this._max = count[1];
  125.             } else {
  126.                 this._min = this._max = count;
  127.             }
  128.             this._trigger = false;
  129.         },
  130.  
  131.         /**
  132.          * Gets called, whenever the emitter updates. Can specify any number of particles to be created at this point.
  133.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  134.          * @returns {number|undefined} If this counter has been triggered, returns either the given count or a random
  135.          * number between the given minimum and maximum. If not, will return undefined, which gets interpreted as 0.
  136.          */
  137.         update: function(emitter) {
  138.             if(this._trigger) {
  139.                 this._trigger = false;
  140.                 return (this._min === this.max) ? this._min : _random(this._min, this._max);
  141.             }
  142.         },
  143.  
  144.         /**
  145.          * Instructs the counter to create particles the next time its "update" function is called.
  146.          */
  147.         trigger: function() {
  148.             this._trigger = true;
  149.         }
  150.  
  151.     });
  152.  
  153.     //=============================================================================
  154.     // IAVRA.PARTICLE.COUNTER.Interval
  155.     //=============================================================================
  156.  
  157.     /**
  158.      * An Interval counter permanently creates a number of particles via the given parameters.
  159.      */
  160.     $.PARTICLE.COUNTER.Interval.prototype = _extend(Object.create($.PARTICLE.Counter.prototype), {
  161.  
  162.         /**
  163.          * Initializes the counter. Takes the number of particles to create and the emission interval as parameters.
  164.          * @param {(number|number[])} count - Number of particles to create during each interval. If this is an array,
  165.          * its first and second element are used as the lower and upper bound to calculate a random number.
  166.          * @param {(number|number[])} interval - How often new particles should be created, in frames. If this is an
  167.          * array, its first and second element are used as the lower and upper bound to calculate a random number.
  168.          */
  169.         _initialize: function(count, interval) {
  170.             if(Array.isArray(count)) {
  171.                 this._minCount = count[0];
  172.                 this._maxCount = count[1];
  173.             } else {
  174.                 this._minCount = this._maxCount = count;
  175.             }
  176.             if(Array.isArray(interval)) {
  177.                 this._minInt = interval[0];
  178.                 this._maxInt = interval[1];
  179.             } else {
  180.                 this._minInt = this._maxInt = interval;
  181.             }
  182.             this._initTimer();
  183.         },
  184.  
  185.         /**
  186.          * Sets the timer to a random number between the given minimum and maximum interval.
  187.          */
  188.         _initTimer: function() {
  189.             this._timer = (this._minInt === this.maxInt) ? this._minInt : _random(this._minInt, this._maxInt);
  190.         },
  191.  
  192.         /**
  193.          * Gets called, whenever the emitter updates. Can specify any number of particles to be created at this point.
  194.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  195.          * @returns {number|undefined} If the internal timer has reached the given interval, returns the given number
  196.          * of particles. If not, will return undefined, which gets interpreted as 0.
  197.          */
  198.         update: function(emitter) {
  199.             if(--this._timer <= 0) {
  200.                 this._initTimer();
  201.                 return (this._minCount === this.maxCount) ? this._minCount : _random(this._minCount, this._maxCount);
  202.             }
  203.         }
  204.  
  205.     });
  206.    
  207.     //=============================================================================
  208.     // IAVRA.PARTICLE.COUNTER.Variable
  209.     //=============================================================================
  210.  
  211.     /**
  212.      * A Variable counter creates particles depending on the values stored in the given variables.
  213.      */
  214.     $.PARTICLE.COUNTER.Variable.prototype = _extend(Object.create($.PARTICLE.Counter.prototype), {
  215.  
  216.         /**
  217.          * Initializes the counter. Takes the variable ids used to determine, how many particles should be created and
  218.          * in which interval.
  219.          * @param {number} countVar - Id of the variable, that contains the number of particles to create.
  220.          * @param {number} intervalVar - Id of the variable containing the emission rate.
  221.          */
  222.         _initialize: function(countVar, intervalVar) {
  223.             this._countVar = countVar;
  224.             this._intervalVar = intervalVar;
  225.             this._timer = 0;
  226.         },
  227.  
  228.         /**
  229.          * Gets called, whenever the emitter updates. Can specify any number of particles to be created at this point.
  230.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  231.          * @returns {number|undefined} If the internal timer has reached the interval stored in the given variable,
  232.          * returns the number of particles defined in the other given variable. If not, will return undefined, which
  233.          * gets interpreted as 0.
  234.          */
  235.         update: function(emitter) {
  236.             if(++this._timer >= $gameVariables.value(this._intervalVar)) {
  237.                 this._timer = 0;
  238.                 return $gameVariables.value(this._countVar);
  239.             }
  240.         }
  241.  
  242.     });
  243.  
  244.     //=============================================================================
  245.     // IAVRA.PARTICLE.COUNTER.Switch
  246.     //=============================================================================
  247.  
  248.     /**
  249.      * A Switch counter serves as a wrapper for another counter object, which will only be called, if the given switch
  250.      * is set to true.
  251.      */
  252.     $.PARTICLE.COUNTER.Switch.prototype = _extend(Object.create($.PARTICLE.Counter.prototype), {
  253.  
  254.         /**
  255.          * Initializes the counter. Takes the counter object to wrap and the switch id used to determine, whether it
  256.          * should emit or not.
  257.          * @param {IAVRA.PARTICLE.Counter} counter - Counter instance to be wrapped.
  258.          * @param {number} switchId - Id of the switch, that controls the wrapped counter's emission rate.
  259.          */
  260.         _initialize: function(counter, switchId) {
  261.             this._counter = counter;
  262.             this._switchId = switchId;
  263.         },
  264.  
  265.         /**
  266.          * Gets called, when the emitter is started. Can specify any number of particles to be created at this point.
  267.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  268.          * @returns {number|undefined} The wrapped counter's result, if the given switch is set to true. If not, will
  269.          * return undefined, which gets interpreted as 0.
  270.          */
  271.         start: function(emitter) {
  272.             if($gameSwitches.value(this._switchId)) { return this._counter.start(emitter); }
  273.             this._counter.start(emitter);
  274.         },
  275.  
  276.         /**
  277.          * Gets called, whenever the emitter updates. Can specify any number of particles to be created at this point.
  278.          * @param {IAVRA.PARTICLE.Emitter} emitter - The enclosing emitter instance.
  279.          * @returns {number|undefined} The wrapped counter's result, if the given switch is set to true. If not, will
  280.          * return undefined, which gets interpreted as 0.
  281.          */
  282.         update: function(emitter) {
  283.             if($gameSwitches.value(this._switchId)) { return this._counter.update(emitter); }
  284.         }
  285.  
  286.     });
  287.  
  288. })(this.IAVRA || (this.IAVRA = {}));
Add Comment
Please, Sign In to add comment