1 /**
  2  * The Render Engine
  3  * ParticleEmitter component
  4  *
  5  * @fileoverview An extension of the render component which allows the
  6  *    developer to attach a particle emitter to a game object.
  7  *
  8  * @author: Brett Fattori (brettf@renderengine.com)
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.components.render.ParticleEmitter",
 37     "requires":[
 38         "R.components.Render",
 39         "R.particles.Emitter",
 40         "R.math.Point2D"
 41     ]
 42 });
 43 
 44 /**
 45  * @class A {@link R.components.Render render component} that allows the developer
 46  *    to link a particle emitter to a game object.
 47  *
 48  * @param name {String} The name of the component
 49  * @param emitter {R.particles.Emitter} The particle emitter to use with the component
 50  * @param [priority=0.1] {Number} The render priority
 51  * @extends R.components.Render
 52  * @constructor
 53  * @description Creates a component which emits particles.
 54  */
 55 R.components.render.ParticleEmitter = function () {
 56     "use strict";
 57     return R.components.Render.extend(/** @scope R.components.render.ParticleEmitter.prototype */{
 58 
 59         emitter:null,
 60         offset:null,
 61 
 62         /**
 63          * @private
 64          */
 65         constructor:function (name, emitter, priority) {
 66             this.base(name, priority);
 67             this.emitter = emitter;
 68             this.offset = R.math.Point2D.create(0, 0);
 69         },
 70 
 71         /**
 72          * Destroy the particle emitter component.
 73          */
 74         destroy:function () {
 75             this.offset.destroy();
 76             this.base();
 77         },
 78 
 79         /**
 80          * Releases the component back into the object pool. See {@link R.engine.PooledObject#release}
 81          * for more information.
 82          */
 83         release:function () {
 84             this.base();
 85             this.emitter = null;
 86             this.offset = null;
 87         },
 88 
 89         /**
 90          * Set the particle emitter object.
 91          * @param emitter {R.particles.Emitter} The particle emitter
 92          */
 93         setEmitter:function (emitter) {
 94             this.emitter = emitter;
 95         },
 96 
 97         /**
 98          * Get the particle emitter assigned to this component.
 99          * @return {R.particles.Emitter}
100          */
101         getEmitter:function () {
102             return this.emitter;
103         },
104 
105         /**
106          * Set the active state of the particle emitter.
107          * @param state {Boolean} <code>true</code> to set the emitter to generate particles
108          */
109         setActive:function (state) {
110             this.emitter.setActive(state);
111         },
112 
113         /**
114          * Set the offset, from the rendering origin, where the particles are emitted
115          * from.  This will default to the rendering origin.
116          * @param ptOrX {Number|R.math.Point2D} The X offset, or a point
117          * @param [y] {Number} The Y offset if <code>ptOrX</code> is a number
118          */
119         setOffset:function (ptOrX, y) {
120             this.offset.set(ptOrX, y);
121         },
122 
123         /**
124          * Get the offset where the particles will be emitted, from the rendering origin.
125          * @return {R.math.Point2D}
126          */
127         getOffset:function () {
128             return this.offset;
129         },
130 
131         /**
132          * Emit particles to the render context.
133          *
134          * @param renderContext {R.rendercontexts.AbstractRenderContext} The context to render to
135          * @param time {Number} The engine time in milliseconds
136          * @param dt {Number} The delta between the world time and the last time the world was updated
137          *          in milliseconds.
138          */
139         execute:function (renderContext, time, dt) {
140             if (!this.base(renderContext, time, dt)) {
141                 return;
142             }
143 
144             if (this.emitter) {
145                 this.transformOrigin(renderContext, true);
146                 this.emitter.emit(this.getOffset(), time, dt);
147                 this.transformOrigin(renderContext, false);
148             }
149         }
150     }, /** @scope R.components.render.ParticleEmitter.prototype */{
151         /**
152          * Get the class name of this object
153          * @return {String} "R.components.render.ParticleEmitter"
154          */
155         getClassName:function () {
156             return "R.components.render.ParticleEmitter";
157         }
158     });
159 };
160