/
Steady.as
173 lines (163 loc) · 4.44 KB
/
Steady.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* FLINT PARTICLE SYSTEM
* .....................
*
* Author: Richard Lord
* Copyright (c) Richard Lord 2008-2011
* http://flintparticles.org
*
*
* Licence Agreement
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.flintparticles.common.counters
{
import org.flintparticles.common.emitters.Emitter;
/**
* The Steady counter causes the emitter to emit particles continuously
* at a steady rate. It can be used to simulate any continuous particle
* stream.
*/
public class Steady implements Counter
{
private var _timeToNext:Number;
private var _rate:Number;
private var _rateInv:Number;
private var _running:Boolean;
/**
* The constructor creates a Steady counter for use by an emitter. To
* add a Steady counter to an emitter use the emitter's counter property.
*
* @param rate The number of particles to emit per second.
*
* @see org.flintparticles.common.emitter.Emitter.counter
*/
public function Steady( rate:Number = 0 )
{
_running = false;
this.rate = rate;
}
/**
* Stops the emitter from emitting particles
*/
public function stop():void
{
_running = false;
}
/**
* Resumes the emitter emitting particles after a stop
*/
public function resume():void
{
_running = true;
}
/**
* The number of particles to emit per second.
*/
public function get rate():Number
{
return _rate;
}
public function set rate( value:Number ):void
{
if( !value || value < 0 )
{
value = 0;
}
if( _rate != value )
{
if( _rate && value )
{
var timePassed:Number = _rateInv - _timeToNext;
_rate = value;
_rateInv = value ? 1 / value : Number.MAX_VALUE;
_timeToNext = Math.max( _rateInv - timePassed, 0 );
}
else
{
_rate = value;
_rateInv = value ? 1 / value : Number.MAX_VALUE;
_timeToNext = _rateInv;
}
}
}
/**
* Initilizes the counter. Returns 0 to indicate that the emitter should
* emit no particles when it starts.
*
* <p>This method is called within the emitter's start method
* and need not be called by the user.</p>
*
* @param emitter The emitter.
* @return 0
*
* @see org.flintparticles.common.counters.Counter#startEmitter()
*/
public function startEmitter( emitter:Emitter ):uint
{
_running = true;
_timeToNext = _rateInv;
return 0;
}
/**
* Uses the time, rateMin and rateMax to calculate how many
* particles the emitter should emit now.
*
* <p>This method is called within the emitter's update loop and need not
* be called by the user.</p>
*
* @param emitter The emitter.
* @param time The time, in seconds, since the previous call to this method.
* @return the number of particles the emitter should create.
*
* @see org.flintparticles.common.counters.Counter#updateEmitter()
*/
public function updateEmitter( emitter:Emitter, time:Number ):uint
{
if( !_running )
{
return 0;
}
var count:uint = 0;
_timeToNext -= time;
while( _timeToNext <= 0 )
{
++count;
_timeToNext += _rateInv;
}
return count;
}
/**
* Indicates if the counter has emitted all its particles. For this counter
* this will always be false.
*/
public function get complete():Boolean
{
return false;
}
/**
* Indicates if the counter is currently emitting particles
*/
public function get running():Boolean
{
return _running;
}
}
}