The FastWeightedArray is a collection of values that are weighted. When 41 | * a random value is required from the collection, the value returned 42 | * is randomly selkected based on the weightings.
43 | * 44 | *Due to the nature of a FastWeightedArray, there are no facilities 45 | * to push, unshift or splice items into the array. All items are 46 | * added to the FastWeightedArray using the add method.
47 | */ 48 | public class FastWeightedArray 49 | { 50 | private var _values:Array; 51 | private var _totalRatios:Number; 52 | 53 | /** 54 | * Then constructor function is used to create a FastWeightedArray 55 | */ 56 | public function FastWeightedArray() 57 | { 58 | _values = new Array(); 59 | _totalRatios = 0; 60 | } 61 | 62 | /** 63 | * Adds a value to the FastWeightedArray. 64 | * 65 | * @param value the value to add 66 | * @param weight the weighting to place on the item 67 | * @return the length of the FastWeightedArray 68 | */ 69 | public function add( value:*, ratio:Number ):uint 70 | { 71 | _totalRatios += ratio; 72 | _values.push( new Pair( _totalRatios, value ) ); 73 | return _values.length; 74 | } 75 | 76 | /** 77 | * Empties the FastWeightedArray. After calling this method the FastWeightedArray 78 | * contains no items. 79 | */ 80 | public function clear():void 81 | { 82 | _values.length = 0; 83 | _totalRatios = 0; 84 | } 85 | 86 | /** 87 | * The number of items in the FastWeightedArray 88 | */ 89 | public function get length():uint 90 | { 91 | return _values.length; 92 | } 93 | 94 | /** 95 | * The sum of the weights of all the values. 96 | */ 97 | public function get totalRatios():Number 98 | { 99 | return _totalRatios; 100 | } 101 | 102 | /** 103 | * Returns a random value from the FastWeightedArray. The weighting of the values is 104 | * used when selcting the random value, so items with a higher weighting are 105 | * more likely to be seleted. 106 | * 107 | * @return A randomly selected item from the array. 108 | */ 109 | public function getRandomValue():* 110 | { 111 | var position:Number = Math.random() * _totalRatios; 112 | var low:uint = 0; 113 | var mid:uint; 114 | var high:uint = _values.length; 115 | while( low < high ) 116 | { 117 | mid = Math.floor( ( low + high ) * 0.5 ); 118 | if( Pair( _values[ mid ] ).topWeight < position ) 119 | { 120 | low = mid + 1; 121 | } 122 | else 123 | { 124 | high = mid; 125 | } 126 | } 127 | return Pair( _values[low] ).value; 128 | } 129 | } 130 | } 131 | 132 | class Pair 133 | { 134 | internal var topWeight:Number; 135 | internal var value:*; 136 | 137 | public function Pair( topWeight:Number, value:* ) 138 | { 139 | this.topWeight = topWeight; 140 | this.value = value; 141 | } 142 | } -------------------------------------------------------------------------------- /src/zones/PointZone.as: -------------------------------------------------------------------------------- 1 | /* 2 | * FLINT PARTICLE SYSTEM 3 | * ..................... 4 | * 5 | * Author: Richard Lord 6 | * Copyright (c) Richard Lord 2008-2010 7 | * http://flintparticles.org 8 | * 9 | * 10 | * Licence Agreement 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | 31 | package zones 32 | { 33 | 34 | import flash.geom.Point; 35 | import flash.geom.Vector3D; 36 | 37 | import zones.Zone2D; 38 | 39 | /** 40 | * The PointZone zone defines a zone that contains a single point. 41 | */ 42 | 43 | public class PointZone implements Zone2D 44 | { 45 | private var _point:Point; 46 | 47 | /** 48 | * The constructor defines a PointZone zone. 49 | * 50 | * @param point The point that is the zone. 51 | */ 52 | public function PointZone( point:Point = null ) 53 | { 54 | if( point == null ) 55 | { 56 | _point = new Point( 0, 0 ); 57 | } 58 | else 59 | { 60 | _point = point; 61 | } 62 | } 63 | 64 | /** 65 | * The point that is the zone. 66 | */ 67 | public function get point() : Point 68 | { 69 | return _point; 70 | } 71 | 72 | public function set point( value : Point ) : void 73 | { 74 | _point = value; 75 | } 76 | 77 | /** 78 | * The x coordinate of the point that is the zone. 79 | */ 80 | public function get x() : Number 81 | { 82 | return _point.x; 83 | } 84 | 85 | public function set x( value : Number ) : void 86 | { 87 | _point.x = value; 88 | } 89 | 90 | /** 91 | * The y coordinate of the point that is the zone. 92 | */ 93 | public function get y() : Number 94 | { 95 | return _point.y; 96 | } 97 | 98 | public function set y( value : Number ) : void 99 | { 100 | _point.y = value; 101 | } 102 | 103 | /** 104 | * The contains method determines whether a point is inside the zone. 105 | * This method is used by the initializers and actions that 106 | * use the zone. Usually, it need not be called directly by the user. 107 | * 108 | * @param x The x coordinate of the location to test for. 109 | * @param y The y coordinate of the location to test for. 110 | * @return true if point is inside the zone, false if it is outside. 111 | */ 112 | public function contains( x:Number, y:Number ):Boolean 113 | { 114 | return _point.x == x && _point.y == y; 115 | } 116 | 117 | /** 118 | * The getLocation method returns a random point inside the zone. 119 | * This method is used by the initializers and actions that 120 | * use the zone. Usually, it need not be called directly by the user. 121 | * 122 | * @return a random point inside the zone. 123 | */ 124 | public function getLocation(p:Vector3D):void 125 | { 126 | p.x= _point.x;p.y = _point.y; 127 | } 128 | 129 | /** 130 | * The getArea method returns the size of the zone. 131 | * This method is used by the MultiZone class. Usually, 132 | * it need not be called directly by the user. 133 | * 134 | * @return a random point inside the zone. 135 | */ 136 | public function getArea():Number 137 | { 138 | // treat as one pixel square 139 | return 1; 140 | } 141 | 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/zones/RectangleZone.as: -------------------------------------------------------------------------------- 1 | /* 2 | * FLINT PARTICLE SYSTEM 3 | * ..................... 4 | * 5 | * Author: Richard Lord 6 | * Copyright (c) Richard Lord 2008-2010 7 | * http://flintparticles.org 8 | * 9 | * 10 | * Licence Agreement 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | 31 | package zones 32 | { 33 | import flash.geom.Point; 34 | import flash.geom.Vector3D; 35 | 36 | /** 37 | * The RectangleZone zone defines a rectangular shaped zone. 38 | */ 39 | 40 | public class RectangleZone implements Zone2D 41 | { 42 | private var _left : Number; 43 | private var _top : Number; 44 | private var _right : Number; 45 | private var _bottom : Number; 46 | private var _width : Number; 47 | private var _height : Number; 48 | 49 | /** 50 | * The constructor creates a RectangleZone zone. 51 | * 52 | * @param left The left coordinate of the rectangle defining the region of the zone. 53 | * @param top The top coordinate of the rectangle defining the region of the zone. 54 | * @param right The right coordinate of the rectangle defining the region of the zone. 55 | * @param bottom The bottom coordinate of the rectangle defining the region of the zone. 56 | */ 57 | public function RectangleZone( left:Number = 0, top:Number = 0, right:Number = 0, bottom:Number = 0 ) 58 | { 59 | _left = left; 60 | _top = top; 61 | _right = right; 62 | _bottom = bottom; 63 | _width = right - left; 64 | _height = bottom - top; 65 | } 66 | 67 | /** 68 | * The left coordinate of the rectangle defining the region of the zone. 69 | */ 70 | public function get left() : Number 71 | { 72 | return _left; 73 | } 74 | 75 | public function set left( value : Number ) : void 76 | { 77 | _left = value; 78 | if( !isNaN( _right ) && !isNaN( _left ) ) 79 | { 80 | _width = right - left; 81 | } 82 | } 83 | 84 | /** 85 | * The right coordinate of the rectangle defining the region of the zone. 86 | */ 87 | public function get right() : Number 88 | { 89 | return _right; 90 | } 91 | 92 | public function set right( value : Number ) : void 93 | { 94 | _right = value; 95 | if( !isNaN( _right ) && !isNaN( _left ) ) 96 | { 97 | _width = right - left; 98 | } 99 | } 100 | 101 | /** 102 | * The top coordinate of the rectangle defining the region of the zone. 103 | */ 104 | public function get top() : Number 105 | { 106 | return _top; 107 | } 108 | 109 | public function set top( value : Number ) : void 110 | { 111 | _top = value; 112 | if( !isNaN( _top ) && !isNaN( _bottom ) ) 113 | { 114 | _height = bottom - top; 115 | } 116 | } 117 | 118 | /** 119 | * The bottom coordinate of the rectangle defining the region of the zone. 120 | */ 121 | public function get bottom() : Number 122 | { 123 | return _bottom; 124 | } 125 | 126 | public function set bottom( value : Number ) : void 127 | { 128 | _bottom = value; 129 | if( !isNaN( _top ) && !isNaN( _bottom ) ) 130 | { 131 | _height = bottom - top; 132 | } 133 | } 134 | 135 | /** 136 | * The contains method determines whether a point is inside the zone. 137 | * This method is used by the initializers and actions that 138 | * use the zone. Usually, it need not be called directly by the user. 139 | * 140 | * @param x The x coordinate of the location to test for. 141 | * @param y The y coordinate of the location to test for. 142 | * @return true if point is inside the zone, false if it is outside. 143 | */ 144 | public function contains( x:Number, y:Number ):Boolean 145 | { 146 | return x >= _left && x <= _right && y >= _top && y <= _bottom; 147 | } 148 | 149 | /** 150 | * The getLocation method returns a random point inside the zone. 151 | * This method is used by the initializers and actions that 152 | * use the zone. Usually, it need not be called directly by the user. 153 | * 154 | * @return a random point inside the zone. 155 | */ 156 | public function getLocation(p:Vector3D):void 157 | { 158 | p.x = _left + Math.random() * _width; 159 | p.y = _top + Math.random() * _height; 160 | } 161 | 162 | /** 163 | * The getArea method returns the size of the zone. 164 | * This method is used by the MultiZone class. Usually, 165 | * it need not be called directly by the user. 166 | * 167 | * @return a random point inside the zone. 168 | */ 169 | public function getArea():Number 170 | { 171 | return _width * _height; 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/zones/Zone2D.as: -------------------------------------------------------------------------------- 1 | /* 2 | * FLINT PARTICLE SYSTEM 3 | * ..................... 4 | * 5 | * Author: Richard Lord 6 | * Copyright (c) Richard Lord 2008-2010 7 | * http://flintparticles.org 8 | * 9 | * 10 | * Licence Agreement 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | 31 | package zones 32 | { 33 | import flash.geom.Point; 34 | import flash.geom.Vector3D; 35 | 36 | /** 37 | * The Zones interface must be implemented by all zones. 38 | * 39 | *A zone is a class that defined a region in 2d space. The two required methods 40 | * make it easy to get a random point within the zone and to find whether a specific 41 | * point is within the zone. Zones are used to define the start location for particles 42 | * (in the Position initializer), to define the start velocity for particles (in the 43 | * Velocity initializer), and to define zones within which the particles die.
44 | */ 45 | public interface Zone2D 46 | { 47 | /** 48 | * Determines whether a point is inside the zone. 49 | * This method is used by the initializers and actions that 50 | * use the zone. Usually, it need not be called directly by the user. 51 | * 52 | * @param x The x coordinate of the location to test for. 53 | * @param y The y coordinate of the location to test for. 54 | * @return true if point is inside the zone, false if it is outside. 55 | */ 56 | function contains( x:Number, y:Number ):Boolean; 57 | 58 | /** 59 | * Returns a random point inside the zone. 60 | * This method is used by the initializers and actions that 61 | * use the zone. Usually, it need not be called directly by the user. 62 | * 63 | * @return a random point inside the zone. 64 | */ 65 | function getLocation(p:Vector3D):void; 66 | 67 | /** 68 | * Returns the size of the zone. 69 | * This method is used by the MultiZone class to manage the balancing between the 70 | * different zones. 71 | * 72 | * @return the size of the zone. 73 | */ 74 | function getArea():Number; 75 | } 76 | } --------------------------------------------------------------------------------