├── .gitignore ├── Readme.markdown ├── docs └── todo.txt ├── samples ├── AnimatedParticleTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── AnimatedParticleTest.as │ │ ├── Smoother.as │ │ └── assets │ │ ├── explosion.png │ │ ├── knightRun.png │ │ ├── numbers.png │ │ └── smurf.png ├── InteractiveRenderTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── CoinClip.as │ │ ├── LotsOfCoins.as │ │ ├── RenderScene.as │ │ ├── Stats.as │ │ └── assets │ │ └── coin.png ├── IsometricTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── IsometricTest.as │ │ ├── Smoother.as │ │ └── assets │ │ ├── brick.png │ │ └── tile.png ├── MovieClipTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── MovieClipTest.as │ │ └── assets │ │ ├── rocket.swf │ │ └── snake.swf ├── ParticleTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── MHPTest.as │ │ ├── Smoother.as │ │ └── assets │ │ ├── air.jpg │ │ ├── circle.png │ │ ├── dot.jpg │ │ ├── flash.png │ │ ├── glow.png │ │ ├── knightRun.png │ │ ├── small_circle.png │ │ ├── smiley.jpg │ │ ├── smiley.png │ │ ├── smileyRect.png │ │ └── sparkle.png ├── SimpleRenderTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── SimpleRenderTest.as │ │ ├── Smoother.as │ │ └── assets │ │ └── smiley.png ├── SpatialSetTests │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── src │ │ ├── Clip.as │ │ ├── CollisionTest.as │ │ ├── PickingTest.as │ │ ├── Projectile.as │ │ ├── Smoother.as │ │ ├── assets │ │ ├── brick.png │ │ ├── smiley.png │ │ └── tile.png │ │ └── spatial │ │ ├── HierarchicalGrid.as │ │ ├── IKineticObject.as │ │ ├── ISpatialListener.as │ │ ├── ISpatialObject.as │ │ ├── ISpatialObjectProxy.as │ │ ├── ISpatialSet.as │ │ └── SimpleSpatialSet.as └── SpriteTest │ ├── .actionScriptProperties │ ├── .project │ ├── .settings │ └── org.eclipse.core.resources.prefs │ └── src │ ├── Knight.as │ ├── MHSpriteTest.as │ ├── Smoother.as │ └── assets │ └── knightRun.png └── src ├── .actionScriptProperties ├── .flexLibProperties ├── .gitignore ├── .project ├── M2D ├── animation │ └── CellAnimation.as ├── core │ ├── GContext.as │ └── IBlitOp.as ├── particles │ ├── DynamicParticleSource.as │ ├── ParticleConfiguration.as │ ├── ParticleData.as │ ├── ParticleInstance.as │ ├── ParticleLibrary.as │ ├── ParticleParameterBuffer.as │ ├── ParticleSource.as │ ├── ParticleSymbol.as │ └── StaticParticleSource.as ├── sprites │ ├── Actor.as │ ├── Asset.as │ └── SymbolLibrary.as ├── time │ ├── Clock.as │ └── IClockListener.as └── worlds │ ├── AssetMgr.as │ ├── BatchTexture.as │ ├── IRenderJob.as │ ├── Instance.as │ ├── RenderMgr.as │ ├── RenderTask.as │ ├── World.as │ └── WorldBase.as ├── com └── adobe │ └── utils │ └── AGALMiniAssembler.as ├── utils └── DebugUtils.as └── zones ├── BitmapDataZone.as ├── DisplayObjectZone.as ├── FastWeightedArray.as ├── PointZone.as ├── RectangleZone.as └── Zone2D.as /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /samples/SpriteTest/bin 3 | /samples/SpriteTest/bin-debug 4 | /M2D/bin 5 | 6 | /flint/bin 7 | /FlintTest/bin-debug 8 | 9 | /samples/ParticleTest/bin 10 | /samples/ParticleTest/bin-debug 11 | 12 | /samples/AnimatedParticleTest/bin 13 | /samples/AnimatedParticleTest/bin-debug 14 | 15 | /samples/MoveClipTest/bin-debug 16 | 17 | bin-debug/ -------------------------------------------------------------------------------- /Readme.markdown: -------------------------------------------------------------------------------- 1 | # M2D: A 2D Library for Molehill 2 | 3 | ## What is M2D 4 | 5 | M2D is a 2D sprite library using Molehill, the new GPU hardware accelerated API for flash. M2D is intended for projects that primarily use bitmaps for rendering, giving up much of the lovely goodness that is the flash display list. 6 | 7 | ## Current Status 8 | 9 | M2D is *not* under current development. For GPU accelerated 2D support in flash, please consider using the starling framework at http://www.starling-framework.org/ 10 | 11 | ## Why M2D? 12 | 13 | M2D was built (is being built) for a few reasons: 14 | 15 | 1. as a starter point for the many great bitmap based flash libraries used by the flash game community today. Libraries like Push Button Engine, Flixel, and many many more serve the community well, and the intention is not to displace them. If anything, it's to help encourage them to integrate Molehill support into their own libraries. 16 | 2. as a library in its own right. Depending on the interest, and contribution, it may grow into something full fledged and broadly useful, or it may remain a proving ground for technologies the other game libraries can adopt as they see fit. 17 | 18 | 19 | ## What does M2D consist of? 20 | 21 | 1. a set of optimized 2D sprite drawing routines. Independent from the rest of the library, the GContext class is a wrapper around molehill that batches up 2D bitmap draws into as few draw calls as possible. 22 | 2. a retained mode 'world' infrastructure for doing display-list like 2D rendering on molehill. 23 | 3. a bare bones set of classes for managing symbols and sprite instances in the world. 24 | 4. a basic particle system for rendering large numbers of particles parametrically on the GPU. 25 | 5. various stubbed out sections for animation, time, etc. 26 | 27 | 28 | ## How optimized is M2D? 29 | Sortof-ish? It's better than the most brute force no-thought way you would use molehill to draw 2D sprites, but there's probably still lots of inneficiencies in there. Probably an extra matrix calculation here and there that could be cached, that sort of thing. If you see problems, please feel free to submit improvements. 30 | 31 | ## how complete is M2D? 32 | Not too complete. It's complete enough to write tests, but at this point, not ready for prime time. There's no way to dispose of sprites and symbols, for example. There's no defense against loss of the GPU context. Again, if there's interest, that stuff will come. 33 | 34 | ## Can I help? 35 | Sure! Send an email, send a patch, roll up your sleeves and get coding. Email me at m2d@quietlyscheming.com 36 | 37 | -------------------------------------------------------------------------------- /docs/todo.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/docs/todo.txt -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | AnimatedParticleTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Thu Apr 07 09:18:31 PDT 2011 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | if(isNaN(value) || value == Infinity) 47 | return; 48 | 49 | sum += value; 50 | values.push(value); 51 | if(values.length > length) 52 | sum -= values.shift(); 53 | var precisionFactor:Number = Math.pow(10,precision); 54 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/src/assets/explosion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/AnimatedParticleTest/src/assets/explosion.png -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/src/assets/knightRun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/AnimatedParticleTest/src/assets/knightRun.png -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/src/assets/numbers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/AnimatedParticleTest/src/assets/numbers.png -------------------------------------------------------------------------------- /samples/AnimatedParticleTest/src/assets/smurf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/AnimatedParticleTest/src/assets/smurf.png -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | InteractiveRenderTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Tue Nov 30 16:33:57 PST 2010 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/src/CoinClip.as: -------------------------------------------------------------------------------- 1 | package { 2 | import M2D.animation.CellAnimation; 3 | import M2D.sprites.Actor; 4 | 5 | public class CoinClip { 6 | public function CoinClip(_actor:Actor) { 7 | actorRef=_actor 8 | super(); 9 | } 10 | 11 | public function set x(_x:Number):void { 12 | actorRef.x=_x 13 | } 14 | 15 | public function set y(_y:Number):void { 16 | actorRef.y=_y 17 | } 18 | 19 | public function get x():Number { 20 | return actorRef.x 21 | } 22 | 23 | public function get y():Number { 24 | return actorRef.y 25 | } 26 | 27 | public function setAlpha(_alpha:Number):void { 28 | actorRef.alpha=_alpha 29 | } 30 | 31 | public function rotate(_rotate:Number):void { 32 | actorRef.rotation+=_rotate 33 | newAngle=actorRef.rotation 34 | } 35 | 36 | public function move(_x:Number, _y:Number):void { 37 | actorRef.x+=_x 38 | actorRef.y+=_y 39 | } 40 | private var radians:Number 41 | private var newAngle:Number 42 | private var angleDiff:Number 43 | 44 | public function manage():void { 45 | // apply velocity 46 | if (Math.abs(velocityx) > 0.1) { 47 | velocityx*=.9 48 | actorRef.x+=velocityx 49 | if (Math.abs(velocityx) < .1) { 50 | velocityx=0 51 | } 52 | } 53 | if (Math.abs(velocityy) > 0.1) { 54 | velocityy*=.9 55 | actorRef.y+=velocityy 56 | if (Math.abs(velocityy) < .1) { 57 | velocityy=0 58 | } 59 | } 60 | // adjust the rotation of the actor 61 | if (Math.abs(velocityx) > 0 || Math.abs(velocityy) > 0) { 62 | radians=Math.atan2(velocityy, velocityx) 63 | newAngle=radians * 180 / Math.PI 64 | } else { 65 | newAngle=0 66 | } 67 | angleDiff=newAngle - actorRef.rotation 68 | if (Math.abs(angleDiff) > 0.1) { 69 | actorRef.rotation+=angleDiff * .1 70 | if (actorRef.rotation > 360) { 71 | actorRef.rotation-=360 72 | } 73 | if (actorRef.rotation < 0) { 74 | actorRef.rotation+=360 75 | } 76 | actorRef.cell++ 77 | if (actorRef.cell > 23) { 78 | actorRef.cell=0 79 | } 80 | } 81 | 82 | if (actorRef.x > LotsOfCoins.stageWidth + LotsOfCoins.edgeBuffer) { 83 | actorRef.x=-LotsOfCoins.edgeBuffer 84 | } 85 | if (actorRef.x < -LotsOfCoins.edgeBuffer) { 86 | actorRef.x+=LotsOfCoins.stageWidth + LotsOfCoins.edgeBuffer 87 | } 88 | if (actorRef.y > LotsOfCoins.stageHeight + LotsOfCoins.edgeBuffer) { 89 | actorRef.y=-LotsOfCoins.edgeBuffer 90 | } 91 | if (actorRef.y < -LotsOfCoins.edgeBuffer) { 92 | actorRef.y=LotsOfCoins.stageHeight + LotsOfCoins.edgeBuffer 93 | } 94 | } 95 | public var velocityx:Number=0; 96 | public var velocityy:Number=0; 97 | public var actorRef:Actor 98 | public var animationRef:CellAnimation 99 | } 100 | } -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/src/LotsOfCoins.as: -------------------------------------------------------------------------------- 1 | /* 2 | * LotsOfCoins - M2D Sample 3 | * Author: Terry Paton 4 | */ 5 | package { 6 | import M2D.animation.CellAnimation; 7 | import M2D.sprites.Actor; 8 | import M2D.sprites.Asset; 9 | import M2D.worlds.BatchTexture; 10 | import flash.display.Sprite; 11 | import flash.events.Event; 12 | import flash.text.TextField; 13 | import flash.text.TextFieldAutoSize; 14 | 15 | [SWF(width="800", height="480", frameRate="40", backgroundColor="0xB0E0E5")] 16 | public class LotsOfCoins extends Sprite 17 | { 18 | private static const totalObjects:int=4000; 19 | public var images:Vector.; 20 | public var tf2:TextField; 21 | protected var scene:RenderScene; 22 | private var instanceCount:int=0; 23 | private var fallingSpeed:Number=5 24 | private var frames:int=0; 25 | private var coinAsset:Asset; 26 | public static var edgeBuffer:Number=10; 27 | public static var stageWidth:Number=800; 28 | public static var stageHeight:Number=480; 29 | private var _mouseX:Number; 30 | private var _mouseY:Number; 31 | private var dx:Number; 32 | private var dy:Number; 33 | private var dist:Number; 34 | 35 | [Embed(source="assets/coin.png")] 36 | public var coinPng:Class; 37 | 38 | public function LotsOfCoins() 39 | { 40 | super(); 41 | scene=new RenderScene(800, 480); 42 | addChild(scene); 43 | createStats(); 44 | 45 | images=new Vector.(); 46 | var n:int=totalObjects; 47 | while (n--) { 48 | addObject() 49 | } 50 | 51 | addEventListener(Event.ENTER_FRAME, loop); 52 | } 53 | 54 | private function addObject():void 55 | { 56 | if (instanceCount < totalObjects) 57 | { 58 | var image:CoinClip = image=scene.addAnimatedSprite(coinPng); 59 | 60 | image.x=Math.random() * stageWidth; 61 | image.y=Math.random() * stageHeight; 62 | image.rotate(Math.random() * 360); 63 | 64 | images.push(image); 65 | instanceCount++; 66 | tf2.text = String(instanceCount) + " Objects"; 67 | var animation:CellAnimation=new CellAnimation(scene._clock, 0, 24); 68 | animation.actor=image.actorRef; 69 | image.actorRef.cell=int(Math.random() * 20) 70 | image.animationRef=animation 71 | } 72 | } 73 | 74 | protected function loop(event:Event):void 75 | { 76 | _mouseX = mouseX; 77 | _mouseY = mouseY; 78 | 79 | var image:CoinClip 80 | var l:uint=images.length; 81 | for (var i:Number=0; i < l; i++) 82 | { 83 | image=images[i]; 84 | 85 | // find how close item is to the mouse 86 | dx=image.x - _mouseX; 87 | dy=image.y - _mouseY; 88 | dist=Math.floor(Math.sqrt(dx * dx + dy * dy)); 89 | 90 | if (dist < 100) 91 | { 92 | // push the item away 93 | image.velocityx=dx * .1 94 | image.velocityy=dy * .1 95 | } 96 | image.manage() 97 | } 98 | } 99 | 100 | private function createStats():void 101 | { 102 | tf2=new TextField(); 103 | tf2.autoSize=TextFieldAutoSize.LEFT; 104 | tf2.width=300; 105 | tf2.x=80 106 | tf2.background=true; 107 | tf2.border=true; 108 | addChild(tf2); 109 | tf2.text=String(instanceCount) + " Objects"; 110 | addChild(new Stats()); 111 | } 112 | } 113 | } -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/src/RenderScene.as: -------------------------------------------------------------------------------- 1 | package { 2 | import M2D.sprites.Actor; 3 | import M2D.sprites.Asset; 4 | import M2D.time.Clock; 5 | import M2D.time.IClockListener; 6 | import M2D.worlds.BatchTexture; 7 | import M2D.worlds.World; 8 | 9 | import flash.display.Bitmap; 10 | import flash.display.BitmapData; 11 | import flash.display.DisplayObject; 12 | import flash.display.Sprite; 13 | import flash.events.Event; 14 | import flash.geom.Rectangle; 15 | import flash.utils.Dictionary; 16 | import flash.utils.getTimer; 17 | 18 | public class RenderScene extends Sprite implements IClockListener 19 | { 20 | public var world:World; 21 | protected var _lastUpdate:Number=getTimer(); 22 | protected var _fps:Number; 23 | protected var _symbols:Vector. 24 | protected var sceneWidth:Number; 25 | protected var sceneHeight:Number; 26 | private var depthTotal:Number=0; 27 | public var onTick:Function=null; 28 | 29 | // asset cache 30 | private var assetsList:Dictionary=new Dictionary(); 31 | 32 | public function RenderScene(sceneWidth:Number, sceneHeight:Number) 33 | { 34 | this.sceneWidth=sceneWidth; 35 | this.sceneHeight=sceneHeight; 36 | addEventListener(Event.ADDED_TO_STAGE, init); 37 | } 38 | 39 | public function init(e:Event=null):void 40 | { 41 | _clock=new Clock(60); 42 | _clock.addListener(this); 43 | world=new World(); 44 | world.backgroundColor=0xB0E0E5; 45 | clock.addListener(world); 46 | world.initContext(stage, this, 0, new Rectangle(0, 0, sceneWidth, sceneHeight)); 47 | _symbols=new Vector.(); 48 | _clock.start(); 49 | } 50 | 51 | public function addAnimatedSprite(img:Class):CoinClip 52 | { 53 | var asset:Asset=getAssetByClass(img); 54 | if (asset == null) 55 | { 56 | asset = world.assetMgr.createAssetFromDisplayObject(new img(), 3, 10); 57 | assetsList[img]=asset; 58 | } 59 | var symbol:Actor=asset.createActor(); 60 | symbol.depth = 1; 61 | _symbols.push(symbol); 62 | return new CoinClip(symbol) 63 | } 64 | 65 | protected function getAssetByClass(cls:Class):Asset 66 | { 67 | return assetsList[cls]; 68 | } 69 | 70 | protected function getAssetByDisplayObject(d:DisplayObject):Asset 71 | { 72 | return assetsList[d]; 73 | } 74 | 75 | public function tick():void 76 | { 77 | if (onTick != null) { 78 | onTick.call(this); 79 | } 80 | // FPS 81 | var deltaT:Number=getTimer() - _lastUpdate; 82 | _lastUpdate=getTimer(); 83 | _fps=Math.round(100 / deltaT) * 10; 84 | } 85 | 86 | // GETTERS/SETTERS 87 | public function get fps():Number 88 | { 89 | return _fps; 90 | } 91 | public var _clock:Clock; 92 | 93 | public function get clock():Clock 94 | { 95 | return _clock; 96 | } 97 | 98 | public function set clock(value:Clock):void 99 | { 100 | _clock=value; 101 | } 102 | 103 | public function get symbols():Vector. 104 | { 105 | return _symbols; 106 | } 107 | } 108 | } -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/src/Stats.as: -------------------------------------------------------------------------------- 1 | /** 2 | * Hi-ReS! Stats 3 | * 4 | * Released under MIT license: 5 | * http://www.opensource.org/licenses/mit-license.php 6 | * 7 | * How to use: 8 | * 9 | * addChild( new Stats() ); 10 | * 11 | * or 12 | * 13 | * addChild( new Stats( { bg: 0xffffff } ); 14 | * 15 | * version log: 16 | * 17 | * 09.03.28 2.1 Mr.doob + Theme support. 18 | * 09.02.21 2.0 Mr.doob + Removed Player version, until I know if it's really needed. 19 | * + Added MAX value (shows Max memory used, useful to spot memory leaks) 20 | * + Reworked text system / no memory leak (original reason unknown) 21 | * + Simplified 22 | * 09.02.07 1.5 Mr.doob + onRemovedFromStage() (thx huihuicn.xu) 23 | * 08.12.14 1.4 Mr.doob + Code optimisations and version info on MOUSE_OVER 24 | * 08.07.12 1.3 Mr.doob + Some speed and code optimisations 25 | * 08.02.15 1.2 Mr.doob + Class renamed to Stats (previously FPS) 26 | * 08.01.05 1.2 Mr.doob + Click changes the fps of flash (half up increases, half down decreases) 27 | * 08.01.04 1.1 Mr.doob + Shameless ripoff of Alternativa's FPS look :P 28 | * Theo + Log shape for MEM 29 | * + More room for MS 30 | * 07.12.13 1.0 Mr.doob + First version 31 | **/ 32 | package { 33 | import flash.display.Bitmap; 34 | import flash.display.BitmapData; 35 | import flash.display.Sprite; 36 | import flash.events.Event; 37 | import flash.events.MouseEvent; 38 | import flash.geom.Rectangle; 39 | import flash.system.System; 40 | import flash.text.StyleSheet; 41 | import flash.text.TextField; 42 | import flash.utils.getTimer; 43 | /** 44 | * Hi-ReS! Stats FPS, MS and MEM, all in one. 45 | */ 46 | public class Stats extends Sprite { 47 | private var _xml:XML; 48 | private var _text:TextField; 49 | private var _style:StyleSheet; 50 | private var _timer:uint; 51 | private var _fps:uint; 52 | private var _ms:uint; 53 | private var _ms_prev:uint; 54 | private var _mem:Number; 55 | private var _mem_max:Number; 56 | private var _graph:BitmapData; 57 | private var _rectangle:Rectangle; 58 | private var _fps_graph:uint; 59 | private var _mem_graph:uint; 60 | private var _mem_max_graph:uint; 61 | private var _theme:Object = {bg: 0x000033, fps: 0xffff00, ms: 0x00ff00, mem: 0x00ffff, memmax: 0xff0070}; 62 | 63 | /** 64 | * Hi-ReS! Stats FPS, MS and MEM, all in one. 65 | * 66 | * @param theme Example: { bg: 0x202020, fps: 0xC0C0C0, ms: 0x505050, mem: 0x707070, memmax: 0xA0A0A0 } 67 | */ 68 | public function Stats(theme:Object = null):void { 69 | if (theme) { 70 | if (theme.bg != null) 71 | _theme.bg = theme.bg; 72 | if (theme.fps != null) 73 | _theme.fps = theme.fps; 74 | if (theme.ms != null) 75 | _theme.ms = theme.ms; 76 | if (theme.mem != null) 77 | _theme.mem = theme.mem; 78 | if (theme.memmax != null) 79 | _theme.memmax = theme.memmax; 80 | } 81 | addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); 82 | addEventListener(Event.REMOVED_FROM_STAGE, kill, false, 0, true); 83 | } 84 | 85 | private function kill(e:Event):void { 86 | removeEventListener(Event.ADDED_TO_STAGE, init); 87 | removeEventListener(Event.REMOVED_FROM_STAGE, kill); 88 | removeEventListener(MouseEvent.CLICK, onClick); 89 | removeEventListener(Event.ENTER_FRAME, update); 90 | } 91 | 92 | private function init(e:Event):void { 93 | removeEventListener(Event.ADDED_TO_STAGE, init); 94 | graphics.beginFill(_theme.bg); 95 | graphics.drawRect(0, 0, 70, 50); 96 | graphics.endFill(); 97 | _mem_max = 0; 98 | _xml = FPS:MS:MEM:MAX:; 99 | _style = new StyleSheet(); 100 | _style.setStyle("xml", {fontSize: '9px', fontFamily: '_sans', leading: '-2px'}); 101 | 102 | _style.setStyle("fps", {color: hex2css(_theme.fps)}); 103 | _style.setStyle("ms", {color: hex2css(_theme.ms)}); 104 | _style.setStyle("mem", {color: hex2css(_theme.mem)}); 105 | _style.setStyle("memMax", {color: hex2css(_theme.memmax)}); 106 | _text = new TextField(); 107 | _text.width = 70; 108 | _text.height = 50; 109 | _text.styleSheet = _style; 110 | _text.condenseWhite = true; 111 | _text.selectable = false; 112 | _text.mouseEnabled = false; 113 | addChild(_text); 114 | var bitmap:Bitmap = new Bitmap(_graph = new BitmapData(70, 50, false, _theme.bg)); 115 | bitmap.y = 50; 116 | addChild(bitmap); 117 | _rectangle = new Rectangle(0, 0, 1, _graph.height); 118 | addEventListener(MouseEvent.CLICK, onClick); 119 | addEventListener(Event.ENTER_FRAME, update); 120 | } 121 | 122 | private function update(e:Event):void { 123 | _timer = getTimer(); 124 | if (_timer - 1000 > _ms_prev) { 125 | _ms_prev = _timer; 126 | _mem = Number((System.totalMemory * 0.000000954).toFixed(3)); 127 | _mem_max = _mem_max > _mem ? _mem_max : _mem; 128 | _fps_graph = Math.min(50, (_fps / stage.frameRate) * 50); 129 | _mem_graph = Math.min(50, Math.sqrt(Math.sqrt(_mem * 5000))) - 2; 130 | _mem_max_graph = Math.min(50, Math.sqrt(Math.sqrt(_mem_max * 5000))) - 2; 131 | _graph.scroll(1, 0); 132 | _graph.fillRect(_rectangle, _theme.bg); 133 | _graph.setPixel(0, _graph.height - _fps_graph, _theme.fps); 134 | _graph.setPixel(0, _graph.height - ((_timer - _ms) >> 1), _theme.ms); 135 | _graph.setPixel(0, _graph.height - _mem_graph, _theme.mem); 136 | _graph.setPixel(0, _graph.height - _mem_max_graph, _theme.memmax); 137 | _xml.fps = "FPS: " + _fps + " / " + stage.frameRate; 138 | _xml.mem = "MEM: " + _mem; 139 | _xml.memMax = "MAX: " + _mem_max; 140 | _fps = 0; 141 | } 142 | _fps++; 143 | _xml.ms = "MS: " + (_timer - _ms); 144 | _ms = _timer; 145 | _text.htmlText = _xml; 146 | } 147 | 148 | private function onClick(e:MouseEvent):void { 149 | mouseY / height > .5 ? stage.frameRate-- : stage.frameRate++; 150 | _xml.fps = "FPS: " + _fps + " / " + stage.frameRate; 151 | _text.htmlText = _xml; 152 | } 153 | 154 | // .. Utils 155 | private function hex2css(color:int):String { 156 | return "#" + color.toString(16); 157 | } 158 | } 159 | } -------------------------------------------------------------------------------- /samples/InteractiveRenderTest/src/assets/coin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/InteractiveRenderTest/src/assets/coin.png -------------------------------------------------------------------------------- /samples/IsometricTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/IsometricTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | IsometricTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/IsometricTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Tue Nov 30 16:33:57 PST 2010 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/IsometricTest/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | sum += value; 47 | values.push(value); 48 | if(values.length > length) 49 | sum -= values.shift(); 50 | var precisionFactor:Number = Math.pow(10,precision); 51 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /samples/IsometricTest/src/assets/brick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/IsometricTest/src/assets/brick.png -------------------------------------------------------------------------------- /samples/IsometricTest/src/assets/tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/IsometricTest/src/assets/tile.png -------------------------------------------------------------------------------- /samples/MovieClipTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/MovieClipTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | MovieClipTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/MovieClipTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Wed Apr 06 13:48:02 EDT 2011 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/MovieClipTest/src/MovieClipTest.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 32 | { 33 | import M2D.animation.CellAnimation; 34 | import M2D.sprites.Actor; 35 | import M2D.sprites.Asset; 36 | import M2D.time.Clock; 37 | import M2D.worlds.World; 38 | 39 | import flash.display.Bitmap; 40 | import flash.display.Sprite; 41 | import flash.display.StageAlign; 42 | import flash.display.StageScaleMode; 43 | import flash.geom.Rectangle; 44 | 45 | [SWF(width="650", height="400", frameRate="60", backgroundColor="0xB0E0E5")] 46 | public class MovieClipTest extends Sprite 47 | { 48 | private var world:World; 49 | private var clock:Clock; 50 | 51 | [Embed(source="assets/snake.swf", symbol="Snake")] 52 | public var snake:Class; 53 | 54 | [Embed(source="assets/rocket.swf", symbol="Rocketship")] 55 | public var rocket:Class; 56 | 57 | public function MovieClipTest() 58 | { 59 | stage.scaleMode = StageScaleMode.NO_SCALE; 60 | stage.align = StageAlign.TOP_LEFT; 61 | 62 | world = new World(); 63 | world.backgroundColor = 0xB0E0E5; 64 | world.initContext(stage, this, 0, new Rectangle(0, 0, stage.stageWidth, stage.stageHeight)); 65 | 66 | clock = new Clock(60); 67 | clock.start(); 68 | clock.addListener(world); 69 | 70 | createSnake(); 71 | createRocket(); 72 | } 73 | 74 | private function createSnake():void 75 | { 76 | // Create sprite sheet asset from embedded MovieClip/SpriteAsset instance. 77 | var spriteSheet:Asset = world.assetMgr.createAssetFromAnimatedDisplayObject(new snake(), 1); 78 | 79 | // Spawn an actor and position it. 80 | var actor:Actor = spriteSheet.createActor(); 81 | actor.x = 150; 82 | actor.y = 250; 83 | 84 | // Create a cell animation to increment our animation frames. 85 | // Set our animation speed to 15 fps. 86 | var animator:CellAnimation = new CellAnimation(clock, 0, spriteSheet.frameCount, actor); 87 | animator.mpf = 1000.0 / 15.0; 88 | animator.start(); 89 | 90 | // Add generated sprite sheet to display list for demonstration purposes. 91 | var sheet:Bitmap = new Bitmap(spriteSheet.texture.data); 92 | sheet.x = 0; 93 | sheet.y = 50; 94 | sheet.scaleX = .5; 95 | sheet.scaleY = .5; 96 | addChild(sheet); 97 | } 98 | 99 | private function createRocket():void 100 | { 101 | // Create sprite sheet asset from embedded MovieClip/SpriteAsset instance. In this 102 | // case we generate a sprite sheet at 2x scale. 103 | var spriteSheet:Asset = world.assetMgr.createAssetFromAnimatedDisplayObject(new rocket(), 1, 2, 2); 104 | 105 | // Spawn an actor and position it. 106 | var actor:Actor = spriteSheet.createActor(); 107 | actor.x = 390; 108 | actor.y = 150; 109 | 110 | // Create a cell animation to increment our animation frames. 111 | // Set our animation speed to 40 fps. 112 | var animator:CellAnimation = new CellAnimation(clock, 0, spriteSheet.frameCount, actor); 113 | animator.mpf = 1000.0 / 40.0; 114 | animator.start(); 115 | 116 | // Add generated sprite sheet to display list for demonstration purposes. 117 | var sheet:Bitmap = new Bitmap(spriteSheet.texture.data); 118 | sheet.x = 500; 119 | sheet.scaleX = .5; 120 | sheet.scaleY = .5; 121 | addChild(sheet); 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /samples/MovieClipTest/src/assets/rocket.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/MovieClipTest/src/assets/rocket.swf -------------------------------------------------------------------------------- /samples/MovieClipTest/src/assets/snake.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/MovieClipTest/src/assets/snake.swf -------------------------------------------------------------------------------- /samples/ParticleTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/ParticleTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ParticleTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/ParticleTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Fri Feb 25 16:48:56 PST 2011 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/ParticleTest/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | if(isNaN(value) || value == Infinity) 47 | return; 48 | 49 | sum += value; 50 | values.push(value); 51 | if(values.length > length) 52 | sum -= values.shift(); 53 | var precisionFactor:Number = Math.pow(10,precision); 54 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/air.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/air.jpg -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/circle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/circle.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/dot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/dot.jpg -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/flash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/flash.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/glow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/glow.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/knightRun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/knightRun.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/small_circle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/small_circle.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/smiley.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/smiley.jpg -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/smiley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/smiley.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/smileyRect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/smileyRect.png -------------------------------------------------------------------------------- /samples/ParticleTest/src/assets/sparkle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/ParticleTest/src/assets/sparkle.png -------------------------------------------------------------------------------- /samples/SimpleRenderTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/SimpleRenderTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SimpleRenderTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/SimpleRenderTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Tue Nov 30 16:33:57 PST 2010 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/SimpleRenderTest/src/SimpleRenderTest.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | 32 | import M2D.sprites.Actor; 33 | import M2D.sprites.Asset; 34 | import M2D.time.Clock; 35 | import M2D.time.IClockListener; 36 | import M2D.worlds.World; 37 | 38 | import flash.display.Sprite; 39 | import flash.display.StageAlign; 40 | import flash.display.StageScaleMode; 41 | import flash.geom.Rectangle; 42 | import flash.text.TextField; 43 | import flash.text.TextFieldAutoSize; 44 | import flash.utils.getTimer; 45 | 46 | [SWF(width="1024", height="768", frameRate="60", backgroundColor="0xB0E0E5")] 47 | public class SimpleRenderTest extends Sprite implements IClockListener 48 | { 49 | public static const ACTOR_COUNT:int = 6000; 50 | public static var world:World; 51 | public static var viewWidth:Number = 1024; 52 | public static var viewHeight:Number = 768; 53 | private var actors:Vector. = new Vector.(); 54 | 55 | [Embed(source="assets/smiley.png")] 56 | public static var smiley:Class; 57 | 58 | public function SimpleRenderTest() 59 | { 60 | stage.scaleMode = StageScaleMode.NO_SCALE; 61 | stage.align = StageAlign.TOP_LEFT; 62 | 63 | world = new World(); 64 | world.backgroundColor = 0xB0E0E5; 65 | world.initContext(stage,this,0,new Rectangle(0,0,viewWidth,viewHeight)); 66 | 67 | _clock = new Clock(60); 68 | _clock.addListener(this); 69 | clock.addListener(world); 70 | 71 | createActors(); 72 | createStats(); 73 | 74 | _clock.start(); 75 | } 76 | 77 | private function createActors():void 78 | { 79 | var texture:Asset = world.assetMgr.createAssetFromDisplayObject(new smiley()); 80 | for(var i:int = 0; i < ACTOR_COUNT; i++) 81 | { 82 | var actor:Actor = texture.createActor(); 83 | actor.x = Math.random() * viewWidth; 84 | actor.y = Math.random() * viewHeight; 85 | actor.scaleX = (Math.random() * .25) + .25; 86 | actor.scaleY = actor.scaleX; 87 | actor.rotation = (Math.random() * 180) - 180; 88 | actor.alpha = (Math.random() * .2) + .80; 89 | actors.push(actor); 90 | } 91 | } 92 | 93 | private var _clock:Clock; 94 | 95 | public function get clock():Clock 96 | { 97 | return _clock; 98 | } 99 | 100 | public function set clock(value:Clock):void 101 | { 102 | _clock = value; 103 | } 104 | 105 | public function tick():void 106 | { 107 | var length:int = actors.length; 108 | for(var i:int = 0; i < length; i++) 109 | { 110 | var rotation:int = actors[i].rotation; 111 | rotation += i % 3; 112 | if (rotation > 360) 113 | rotation-=360 114 | if (rotation < 0) 115 | rotation+=360 116 | actors[i].rotation = rotation; 117 | } 118 | updateStats(); 119 | } 120 | 121 | // Stats Housekeeping 122 | 123 | public static const smoothWindow:int = 100; 124 | private var tf:TextField; 125 | private var previousTime:Number = 0; 126 | 127 | private var mpf:Smoother = new Smoother(smoothWindow); 128 | private var fps:Smoother = new Smoother(smoothWindow); 129 | private var asProcessing:Smoother = new Smoother(smoothWindow); 130 | private var lastUpdate:Number = getTimer(); 131 | 132 | private function createStats():void 133 | { 134 | tf = new TextField(); 135 | tf.autoSize = TextFieldAutoSize.LEFT; 136 | tf.background = true; 137 | tf.border = true; 138 | addChild(tf); 139 | } 140 | 141 | private function updateStats():void 142 | { 143 | var t:Number = clock.currentTime; 144 | var delta:Number = t - previousTime; 145 | fps.sample((1/delta) * 1000); 146 | mpf.sample(delta); 147 | previousTime = t; 148 | reportTime(clock.processingTime); 149 | } 150 | 151 | private function reportTime(asProcessingTime:Number):void 152 | { 153 | asProcessing.sample(asProcessingTime); 154 | 155 | var t:Number = getTimer(); 156 | if( t - lastUpdate > 1000) 157 | lastUpdate = t; 158 | else 159 | return; 160 | tf.text = 161 | "number of actors: " + ACTOR_COUNT + 162 | "\nfps: " + fps.average + 163 | "\nmilli/frame: " + mpf.average + 164 | "\ntotal AS processing: " + asProcessing.average + 165 | ""; 166 | } 167 | } 168 | } -------------------------------------------------------------------------------- /samples/SimpleRenderTest/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | sum += value; 47 | values.push(value); 48 | if(values.length > length) 49 | sum -= values.shift(); 50 | var precisionFactor:Number = Math.pow(10,precision); 51 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /samples/SimpleRenderTest/src/assets/smiley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/SimpleRenderTest/src/assets/smiley.png -------------------------------------------------------------------------------- /samples/SpatialSetTests/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/SpatialSetTests/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpatialSetTests 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/SpatialSetTests/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Tue Nov 30 16:33:57 PST 2010 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/CollisionTest.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | 32 | import M2D.time.Clock; 33 | import M2D.time.IClockListener; 34 | import M2D.worlds.World; 35 | 36 | import flash.display.Sprite; 37 | import flash.display.StageAlign; 38 | import flash.display.StageScaleMode; 39 | import flash.geom.Rectangle; 40 | import flash.text.TextField; 41 | import flash.text.TextFieldAutoSize; 42 | import flash.utils.getTimer; 43 | 44 | import spatial.HierarchicalGrid; 45 | import spatial.ISpatialObject; 46 | import spatial.ISpatialSet; 47 | 48 | /** 49 | * Simple example exercising the HierarchicalGrid spatial manager's queryCollisions 50 | * method. Swap the HierarchicalGrid with a SimpleSpatialSet to contrast with a brute 51 | * force approach. 52 | */ 53 | [SWF(width="1200", height="1050", frameRate="60", backgroundColor="0x00AA00")] 54 | public class CollisionTest extends Sprite implements IClockListener 55 | { 56 | public static const ACTOR_COUNT:int = 750; 57 | public static var world:World; 58 | public static var viewWidth:Number = 1200; 59 | public static var viewHeight:Number = 1050; 60 | public static var queryTime:Number = 0; 61 | private var actors:Vector. = new Vector.(); 62 | 63 | private var spatialManager:ISpatialSet = new HierarchicalGrid(viewWidth, viewHeight); 64 | //private var spatialManager:ISpatialSet = new SimpleSpatialSet(viewWidth, viewHeight); 65 | 66 | public function CollisionTest() 67 | { 68 | stage.scaleMode = StageScaleMode.NO_SCALE; 69 | stage.align = StageAlign.TOP_LEFT; 70 | 71 | world = new World(); 72 | world.backgroundColor = 0x00CC00; 73 | world.initContext(stage,this,0,new Rectangle(0,0,viewWidth,viewHeight)); 74 | 75 | _clock = new Clock(60); 76 | _clock.addListener(this); 77 | clock.addListener(world); 78 | 79 | createActors(); 80 | createStats(); 81 | 82 | _clock.start(); 83 | } 84 | 85 | private function createActors():void 86 | { 87 | for(var i:int = 0; i < ACTOR_COUNT; i++) 88 | { 89 | var instance:Projectile = new Projectile(clock); 90 | instance.depth = i * .5; 91 | actors.push(instance); 92 | spatialManager.add(instance); 93 | } 94 | } 95 | 96 | private var _clock:Clock; 97 | 98 | public function get clock():Clock 99 | { 100 | return _clock; 101 | } 102 | 103 | public function set clock(value:Clock):void 104 | { 105 | _clock = value; 106 | } 107 | 108 | public function tick():void 109 | { 110 | 111 | var length:int = actors.length; 112 | 113 | // Update positions 114 | for(var i:int = 0; i < length; i++) 115 | { 116 | actors[i].alpha = 1; 117 | actors[i].update(); 118 | } 119 | 120 | var start:Number = getTimer(); 121 | spatialManager.queryAllCollisions(collisionDetected); 122 | queryTime = getTimer() - start; 123 | updateStats(); 124 | } 125 | 126 | private function collisionDetected(obj1:ISpatialObject, obj2:ISpatialObject):void 127 | { 128 | Projectile(obj1).alpha = .5; 129 | Projectile(obj2).alpha = .5; 130 | } 131 | // Stats Housekeeping 132 | 133 | public static const smoothWindow:int = 100; 134 | private var tf:TextField; 135 | private var previousTime:Number = 0; 136 | 137 | private var mpf:Smoother = new Smoother(smoothWindow); 138 | private var fps:Smoother = new Smoother(smoothWindow); 139 | private var asProcessing:Smoother = new Smoother(smoothWindow); 140 | private var lastUpdate:Number = getTimer(); 141 | 142 | private function createStats():void 143 | { 144 | tf = new TextField(); 145 | tf.autoSize = TextFieldAutoSize.LEFT; 146 | tf.background = true; 147 | tf.border = true; 148 | addChild(tf); 149 | } 150 | 151 | private function updateStats():void 152 | { 153 | var t:Number = clock.currentTime; 154 | var delta:Number = t - previousTime; 155 | fps.sample((1/delta) * 1000); 156 | mpf.sample(delta); 157 | previousTime = t; 158 | reportTime(clock.processingTime); 159 | } 160 | 161 | private function reportTime(asProcessingTime:Number):void 162 | { 163 | asProcessing.sample(asProcessingTime); 164 | 165 | var t:Number = getTimer(); 166 | if( t - lastUpdate > 1000) 167 | lastUpdate = t; 168 | else 169 | return; 170 | tf.text = 171 | "number of actors: " + ACTOR_COUNT + 172 | "\nfps: " + fps.average + 173 | "\nmilli/frame: " + mpf.average + 174 | "\ntotal AS processing: " + asProcessing.average + 175 | "\ncollision query: " + queryTime + 176 | ""; 177 | } 178 | 179 | } 180 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/Projectile.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 32 | { 33 | import M2D.animation.CellAnimation; 34 | import M2D.sprites.Asset; 35 | import M2D.time.Clock; 36 | 37 | public class Projectile extends Clip 38 | { 39 | public var speed:Number = 1; 40 | private var direction:int = 0; 41 | private var wrapBorder:int = 75; 42 | private var anim:CellAnimation; 43 | private static var _spriteSheet:Asset; 44 | 45 | [Embed(source="assets/smiley.png")] 46 | public static var smiley:Class; 47 | 48 | public function Projectile(clock:Clock) 49 | { 50 | actor = spriteSheet.createActor(); 51 | actor.x = Math.random() * CollisionTest.viewWidth; 52 | actor.y = Math.random() * CollisionTest.viewHeight; 53 | 54 | var rnd:Number = ((Math.random() * 4 ) % 3) * .25; 55 | actor.scaleX = rnd; 56 | actor.scaleY = rnd; 57 | actor.rotation = Math.random() * 360; 58 | 59 | speed = 3.0; 60 | direction = Math.floor(Math.random()*7); 61 | 62 | super(actor); 63 | } 64 | 65 | private static function get spriteSheet():Asset 66 | { 67 | if (!_spriteSheet) 68 | { 69 | _spriteSheet = CollisionTest.world.assetMgr.createAssetFromDisplayObject(new smiley()); 70 | _spriteSheet.hasAlphaChannel = true; 71 | } 72 | return _spriteSheet; 73 | } 74 | 75 | public function update():void 76 | { 77 | var deg:Number = ((direction + 1) * 45 + 45) % 360; 78 | var rad:Number = Math.PI * deg / 180.0; 79 | var x:Number = actor.x + speed * Math.cos(rad); 80 | var y:Number = actor.y + speed * Math.sin(rad); 81 | 82 | actor.x = actor.x + speed * Math.cos(rad); 83 | actor.y = actor.y + speed * Math.sin(rad); 84 | 85 | x = (x > CollisionTest.viewWidth + wrapBorder) ? -wrapBorder : x; 86 | x = (x < -wrapBorder) ? CollisionTest.viewWidth + wrapBorder : x; 87 | y = (y > CollisionTest.viewHeight + wrapBorder) ? -wrapBorder : y; 88 | y = (y < -wrapBorder) ? CollisionTest.viewHeight + wrapBorder : y; 89 | 90 | actor.x = x; 91 | actor.y = y; 92 | 93 | notifyPositionChanged(); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | sum += value; 47 | values.push(value); 48 | if(values.length > length) 49 | sum -= values.shift(); 50 | var precisionFactor:Number = Math.pow(10,precision); 51 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/assets/brick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/SpatialSetTests/src/assets/brick.png -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/assets/smiley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/SpatialSetTests/src/assets/smiley.png -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/assets/tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/SpatialSetTests/src/assets/tile.png -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/IKineticObject.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | package spatial 31 | { 32 | /** 33 | * Marker interface used to allow spatial object managers 34 | * to be notified of changes to an objects size, position, 35 | * or depth. 36 | */ 37 | public interface IKineticObject extends ISpatialObject 38 | { 39 | function addSpatialListener(value:ISpatialListener):void 40 | function removeSpatialListener(value:ISpatialListener):void 41 | } 42 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/ISpatialListener.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 spatial 32 | { 33 | /** 34 | * Interface used for spatial update notifications, usually implemented 35 | * by a spatial object manager. 36 | */ 37 | public interface ISpatialListener 38 | { 39 | function positionChanged(proxy:ISpatialObjectProxy):void; 40 | function boundsChanged(proxy:ISpatialObjectProxy):void; 41 | function depthChanged(proxy:ISpatialObjectProxy):void; 42 | } 43 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/ISpatialObject.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | package spatial 31 | { 32 | import flash.geom.Point; 33 | import flash.geom.Rectangle; 34 | 35 | /** 36 | * Interface used to represent a spatial object instance. 37 | */ 38 | public interface ISpatialObject 39 | { 40 | function get boundingRect():Rectangle; 41 | function get boundingRadius():Number; 42 | 43 | function set x(value:Number):void; 44 | function get x():Number; 45 | 46 | function set y(value:Number):void; 47 | function get y():Number; 48 | 49 | function set height(value:Number):void 50 | function get height():Number; 51 | 52 | function set width(value:Number):void 53 | function get width():Number; 54 | 55 | function set depth(value:Number):void; 56 | function get depth():Number; 57 | 58 | function set registration(value:Point):void; 59 | function get registration():Point; 60 | 61 | function set rotation(value:Number):void; 62 | function get rotation():Number; 63 | 64 | function set scale(value:Point):void; 65 | function get scale():Point; 66 | 67 | function set proxy(value:ISpatialObjectProxy):void; 68 | function get proxy():ISpatialObjectProxy; 69 | 70 | function intersectsPoint(x:Number, y:Number, sampleTexture:Boolean=false):Boolean; 71 | function intersectsRect(x:Number, y:Number, width:Number, height:Number):Boolean; 72 | } 73 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/ISpatialObjectProxy.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | package spatial 31 | { 32 | /** 33 | * Interface used by spatial object managers to make it easy 34 | * to correlate spatial object instances with data structure 35 | * specific elements (quad tree nodes, list nodes, etc.). 36 | */ 37 | public interface ISpatialObjectProxy 38 | { 39 | function set object(value:ISpatialObject):void 40 | function get object():ISpatialObject; 41 | } 42 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/ISpatialSet.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 spatial 32 | { 33 | import flash.geom.Point; 34 | import flash.geom.Rectangle; 35 | 36 | /** 37 | * Interface utilized by spatial object managers (quad trees, 38 | * hierarchical grids, etc.). 39 | */ 40 | public interface ISpatialSet 41 | { 42 | function get length():uint; 43 | function get worldBounds():Rectangle; 44 | 45 | function add(item:ISpatialObject):void 46 | function remove(item:ISpatialObject):void 47 | 48 | function queryObjectAtPoint(point:Point, sampleTexture:Boolean=false):ISpatialObject; 49 | function queryObjectsAtPoint(point:Point, out:Vector., sampleTexture:Boolean=false):int; 50 | function queryObjectsInRect(region:Rectangle, out:Vector.):int; 51 | function queryCollisions(object:ISpatialObject, notification:Function):void; 52 | function queryAllCollisions(notification:Function):void 53 | 54 | function dispose():void 55 | } 56 | } -------------------------------------------------------------------------------- /samples/SpatialSetTests/src/spatial/SimpleSpatialSet.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Corey Lucier 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 spatial 32 | { 33 | import flash.geom.Point; 34 | import flash.geom.Rectangle; 35 | import flash.utils.Dictionary; 36 | 37 | /** 38 | * Very basic spatial object manager that simply keeps 39 | * a list of all managed objects sorted by depth. With the exception 40 | * of hit testing (which short circuits after an item is found at 41 | * the largest depth), all query methods are brute force. 42 | */ 43 | public class SimpleSpatialSet implements ISpatialSet 44 | { 45 | protected var objects:Vector.; 46 | protected var _worldBounds:Rectangle; 47 | protected var _sortRequired:Boolean = true; 48 | 49 | public function SimpleSpatialSet(worldWidth:uint, worldHeight:uint) 50 | { 51 | objects = new Vector.(); 52 | _worldBounds = new Rectangle(0,0,worldWidth, worldHeight); 53 | } 54 | 55 | public function get length():uint 56 | { 57 | return objects.length; 58 | } 59 | 60 | public function get worldBounds():Rectangle 61 | { 62 | return _worldBounds; 63 | } 64 | 65 | public function add(item:ISpatialObject):void 66 | { 67 | objects.push(item); 68 | _sortRequired = true; 69 | } 70 | 71 | public function remove(item:ISpatialObject):void 72 | { 73 | var index:int = objects.indexOf(item); 74 | if (index >= 0) 75 | objects.splice(index, 1); 76 | } 77 | 78 | public function queryObjectAtPoint(point:Point, sampleTexture:Boolean = false):ISpatialObject 79 | { 80 | depthSort(); 81 | for (var i:int = 0; i < length; i++) 82 | { 83 | var object:ISpatialObject = objects[i]; 84 | if (object.intersectsPoint(point.x, point.y, sampleTexture)) 85 | return object; 86 | } 87 | return null; 88 | } 89 | 90 | public function queryObjectsAtPoint(point:Point, out:Vector., sampleTexture:Boolean = false):int 91 | { 92 | var total:int = 0; 93 | depthSort(); 94 | for (var i:int = 0; i < length; i++) 95 | { 96 | var object:ISpatialObject = objects[i]; 97 | if (object.intersectsPoint(point.x, point.y)) 98 | { 99 | out.push(object); 100 | total++; 101 | } 102 | } 103 | return total; 104 | } 105 | 106 | public function queryObjectsInRect(region:Rectangle, out:Vector.):int 107 | { 108 | var total:int = 0; 109 | for (var i:int = 0; i < length; i++) 110 | { 111 | var object:ISpatialObject = objects[i]; 112 | if (object.intersectsRect(region.x, region.y, region.width, region.height)) 113 | { 114 | out.push(object); 115 | total++; 116 | } 117 | } 118 | return total; 119 | } 120 | 121 | public function dispose():void 122 | { 123 | objects.length = 0; 124 | } 125 | 126 | protected function depthSort():void 127 | { 128 | if (_sortRequired) 129 | { 130 | objects = objects.sort(compare); 131 | _sortRequired = false; 132 | } 133 | } 134 | 135 | protected function compare(x:ISpatialObject, y:ISpatialObject):Number 136 | { 137 | if (x.depth < y.depth) 138 | return 1; 139 | else if (x.depth > y.depth) 140 | return -1; 141 | else 142 | return 0; 143 | } 144 | 145 | public function queryAllCollisions(notification:Function):void 146 | { 147 | var completed:Dictionary = new Dictionary(); 148 | 149 | for (var i:int = 0; i < length; i++) 150 | { 151 | var current:ISpatialObject = objects[i]; 152 | checkCollisions(current, notification, completed); 153 | completed[current] = true; 154 | } 155 | } 156 | 157 | public function queryCollisions(object:ISpatialObject, notification:Function):void 158 | { 159 | checkCollisions(object, notification, new Dictionary()); 160 | } 161 | 162 | private function checkCollisions(current:ISpatialObject, notification:Function, completed:Dictionary):void 163 | { 164 | for (var j:int = 0; j < length; j++) 165 | { 166 | var candidate:ISpatialObject = objects[j]; 167 | if (current != candidate && !completed[candidate]) 168 | { 169 | var dx:Number = current.x - candidate.x; 170 | var dy:Number = current.y - candidate.y; 171 | var dist:Number = Math.sqrt(dx * dx + dy * dy); 172 | 173 | if (dist <= current.boundingRadius + candidate.boundingRadius) 174 | notification(current, candidate); 175 | } 176 | } 177 | } 178 | 179 | } 180 | } -------------------------------------------------------------------------------- /samples/SpriteTest/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /samples/SpriteTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpriteTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /samples/SpriteTest/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Tue Nov 30 16:33:57 PST 2010 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /samples/SpriteTest/src/Knight.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 32 | { 33 | import M2D.animation.CellAnimation; 34 | import M2D.sprites.Actor; 35 | import M2D.sprites.Asset; 36 | import M2D.time.Clock; 37 | 38 | public class Knight 39 | { 40 | public var actor:Actor; 41 | public var speed:Number = 1; 42 | private var direction:int = 0; 43 | private var wrapBorder:int = 75; 44 | private var anim:CellAnimation; 45 | private static var _spriteSheet:Asset; 46 | 47 | [Embed(source="assets/knightRun.png")] 48 | public static var knight:Class; 49 | 50 | public function Knight(clock:Clock) 51 | { 52 | actor = spriteSheet.createActor(); 53 | actor.x = Math.random() * MHSpriteTest.viewWidth; 54 | actor.y = Math.random() * MHSpriteTest.viewHeight; 55 | speed = (Math.random() * 3) + 1.5; 56 | direction = Math.floor(Math.random()*7); 57 | anim = new CellAnimation(clock, 0, 10); 58 | anim.actor = actor; 59 | anim.mpf = 1000.0 / 20.0; 60 | anim.base = direction * 10; 61 | anim.start(); 62 | super(); 63 | } 64 | 65 | private static function get spriteSheet():Asset 66 | { 67 | if (!_spriteSheet) 68 | _spriteSheet = MHSpriteTest.world.assetMgr.createAssetFromDisplayObject(new knight(), 8, 10); 69 | return _spriteSheet; 70 | } 71 | 72 | public function update():void 73 | { 74 | var deg:Number = ((direction + 1) * 45 + 45) % 360; 75 | var rad:Number = Math.PI * deg / 180.0; 76 | var x:Number = actor.x + speed * Math.cos(rad); 77 | var y:Number = actor.y + speed * Math.sin(rad); 78 | 79 | actor.x = actor.x + speed * Math.cos(rad); 80 | actor.y = actor.y + speed * Math.sin(rad); 81 | 82 | x = (x > MHSpriteTest.viewWidth + wrapBorder) ? -wrapBorder : x; 83 | x = (x < -wrapBorder) ? MHSpriteTest.viewWidth + wrapBorder : x; 84 | y = (y > MHSpriteTest.viewHeight + wrapBorder) ? -wrapBorder : y; 85 | y = (y < -wrapBorder) ? MHSpriteTest.viewHeight + wrapBorder : y; 86 | 87 | actor.x = x; 88 | actor.y = y; 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /samples/SpriteTest/src/MHSpriteTest.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | 32 | import M2D.time.Clock; 33 | import M2D.time.IClockListener; 34 | import M2D.worlds.World; 35 | 36 | import flash.display.Sprite; 37 | import flash.display.StageAlign; 38 | import flash.display.StageScaleMode; 39 | import flash.geom.Rectangle; 40 | import flash.text.TextField; 41 | import flash.text.TextFieldAutoSize; 42 | import flash.utils.getTimer; 43 | 44 | [SWF(width="1200", height="1050", frameRate="60", backgroundColor="0x00AA00")] 45 | public class MHSpriteTest extends Sprite implements IClockListener 46 | { 47 | public static const ACTOR_COUNT:int = 2000; 48 | public static var world:World; 49 | public static var viewWidth:Number = 1200; 50 | public static var viewHeight:Number = 1050; 51 | private var actors:Vector. = new Vector.(); 52 | 53 | public function MHSpriteTest() 54 | { 55 | stage.scaleMode = StageScaleMode.NO_SCALE; 56 | stage.align = StageAlign.TOP_LEFT; 57 | 58 | world = new World(); 59 | world.backgroundColor = 0x00CC00; 60 | world.initContext(stage,this,0,new Rectangle(0,0,viewWidth,viewHeight)); 61 | 62 | _clock = new Clock(60); 63 | _clock.addListener(this); 64 | clock.addListener(world); 65 | 66 | createActors(); 67 | createStats(); 68 | 69 | _clock.start(); 70 | } 71 | 72 | private function createActors():void 73 | { 74 | for(var i:int = 0; i < ACTOR_COUNT; i++) 75 | { 76 | var instance:Knight = new Knight(clock); 77 | actors.push(instance); 78 | } 79 | } 80 | 81 | private var _clock:Clock; 82 | 83 | public function get clock():Clock 84 | { 85 | return _clock; 86 | } 87 | 88 | public function set clock(value:Clock):void 89 | { 90 | _clock = value; 91 | } 92 | 93 | public function tick():void 94 | { 95 | var length:int = actors.length; 96 | for(var i:int = 0; i < length; i++) 97 | actors[i].update(); 98 | updateStats(); 99 | } 100 | 101 | // Stats Housekeeping 102 | 103 | public static const smoothWindow:int = 100; 104 | private var tf:TextField; 105 | private var previousTime:Number = 0; 106 | 107 | private var mpf:Smoother = new Smoother(smoothWindow); 108 | private var fps:Smoother = new Smoother(smoothWindow); 109 | private var asProcessing:Smoother = new Smoother(smoothWindow); 110 | private var lastUpdate:Number = getTimer(); 111 | 112 | private function createStats():void 113 | { 114 | tf = new TextField(); 115 | tf.autoSize = TextFieldAutoSize.LEFT; 116 | tf.background = true; 117 | tf.border = true; 118 | addChild(tf); 119 | } 120 | 121 | private function updateStats():void 122 | { 123 | var t:Number = clock.currentTime; 124 | var delta:Number = t - previousTime; 125 | fps.sample((1/delta) * 1000); 126 | mpf.sample(delta); 127 | previousTime = t; 128 | reportTime(clock.processingTime); 129 | } 130 | 131 | private function reportTime(asProcessingTime:Number):void 132 | { 133 | asProcessing.sample(asProcessingTime); 134 | 135 | var t:Number = getTimer(); 136 | if( t - lastUpdate > 1000) 137 | lastUpdate = t; 138 | else 139 | return; 140 | tf.text = 141 | "number of actors: " + ACTOR_COUNT + 142 | "\nfps: " + fps.average + 143 | "\nmilli/frame: " + mpf.average + 144 | "\ntotal AS processing: " + asProcessing.average + 145 | ""; 146 | } 147 | } 148 | } -------------------------------------------------------------------------------- /samples/SpriteTest/src/Smoother.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package 30 | { 31 | public class Smoother 32 | { 33 | public var values:Vector.; 34 | public var length:int; 35 | public var average:Number = 0; 36 | public var sum:Number = 0; 37 | public var precision:Number = 2; 38 | 39 | public function Smoother(len:int) 40 | { 41 | values = new Vector.(); 42 | length = len; 43 | } 44 | public function sample(value:Number):void 45 | { 46 | sum += value; 47 | values.push(value); 48 | if(values.length > length) 49 | sum -= values.shift(); 50 | var precisionFactor:Number = Math.pow(10,precision); 51 | average = Math.floor(precisionFactor * sum/values.length)/precisionFactor; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /samples/SpriteTest/src/assets/knightRun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/egreenfield/M2D/43891cffb6221fc988a1ee6e584a78a3de7fb317/samples/SpriteTest/src/assets/knightRun.png -------------------------------------------------------------------------------- /src/.actionScriptProperties: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/.flexLibProperties: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /samples/SpriteTest/bin 3 | /samples/SpriteTest/bin-debug 4 | /M2D/bin 5 | 6 | /flint/bin 7 | /FlintTest/bin-debug 8 | 9 | /samples/ParticleTest/bin 10 | /samples/ParticleTest/bin-debug 11 | -------------------------------------------------------------------------------- /src/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | M2D 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.flexlibnature 16 | com.adobe.flexbuilder.project.actionscriptnature 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/M2D/animation/CellAnimation.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | package M2D.animation 31 | { 32 | import M2D.sprites.Actor; 33 | import M2D.time.Clock; 34 | import M2D.time.IClockListener; 35 | 36 | import flash.display.FrameLabel; 37 | 38 | public class CellAnimation implements IClockListener 39 | { 40 | private var _clock:Clock; 41 | private var _actor:Actor; 42 | private var _base:int; 43 | private var _length:int; 44 | private var _mpf:Number = 1000/30; 45 | private var _currentFrame:int = 0; 46 | private var _lastFrameTime:Number; 47 | 48 | public function CellAnimation(clock:Clock,base:int,length:int,actor:Actor = null) 49 | { 50 | _clock = clock; 51 | _actor = actor; 52 | _base = base; 53 | _length = length; 54 | _currentFrame = _base; 55 | } 56 | 57 | public function get currentFrame():int 58 | { 59 | return _currentFrame; 60 | } 61 | 62 | public function set currentFrame(value:int):void 63 | { 64 | _currentFrame = value; 65 | } 66 | 67 | public function get length():int 68 | { 69 | return _length; 70 | } 71 | 72 | public function set length(value:int):void 73 | { 74 | _length = value; 75 | } 76 | 77 | public function get base():int 78 | { 79 | return _base; 80 | } 81 | 82 | public function set base(value:int):void 83 | { 84 | _base = value; 85 | if (_currentFrame < value) 86 | _currentFrame = value; 87 | } 88 | 89 | public function clone():CellAnimation 90 | { 91 | var c:CellAnimation = new CellAnimation(_clock,_base,_length,_actor); 92 | c._mpf = _mpf; 93 | c._currentFrame = _currentFrame; 94 | return c; 95 | } 96 | 97 | public function get actor():Actor 98 | { 99 | return _actor; 100 | } 101 | 102 | public function set actor(value:Actor):void 103 | { 104 | _actor = value; 105 | } 106 | 107 | public function get clock():Clock 108 | { 109 | return _clock; 110 | } 111 | 112 | public function set clock(value:Clock):void 113 | { 114 | _clock = value; 115 | } 116 | 117 | public function get mpf():Number 118 | { 119 | return _mpf; 120 | } 121 | 122 | public function set mpf(value:Number):void 123 | { 124 | _mpf = value; 125 | } 126 | 127 | public function start():void 128 | { 129 | _lastFrameTime = _clock.currentTime; 130 | _actor.cell = _currentFrame; 131 | _clock.addListener(this); 132 | } 133 | 134 | public function tick():void 135 | { 136 | var tDelta:Number = clock.currentTime - _lastFrameTime; 137 | var elapsedFrames:Number = Math.floor(tDelta / _mpf); 138 | if (elapsedFrames > 0) 139 | { 140 | _currentFrame = _base + (((_currentFrame - _base) + elapsedFrames) % _length); 141 | _actor.cell = _currentFrame; 142 | _lastFrameTime = _clock.currentTime; 143 | } 144 | } 145 | } 146 | } -------------------------------------------------------------------------------- /src/M2D/core/IBlitOp.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.core 30 | { 31 | import flash.geom.Matrix3D; 32 | import flash.geom.Rectangle; 33 | 34 | public interface IBlitOp 35 | { 36 | function getBlitXForm():Vector.; 37 | } 38 | } -------------------------------------------------------------------------------- /src/M2D/particles/DynamicParticleSource.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.particles 30 | { 31 | import flash.display3D.Context3D; 32 | import flash.geom.Matrix3D; 33 | 34 | import mx.utils.NameUtil; 35 | 36 | public class DynamicParticleSource implements ParticleSource 37 | { 38 | public var symbol:ParticleSymbol; 39 | public var instance:ParticleInstance; 40 | 41 | public var params:Vector.; 42 | public static var period:Number = 3; 43 | public var totalBufferSize:Number; 44 | public var numParamBuffers:int; 45 | 46 | private var indexOfHead:Number = 0; 47 | private var indexOfTail:Number = 0; 48 | private var positionOfTail:Number = 0; 49 | private var buffersDirty:Boolean = true; 50 | 51 | public function get maxParticlesPerDrawCall():Number 52 | { 53 | return Math.min(ParticleSymbol.MAX_BUFFER_SIZE,symbol.possibleNumLivingParticles*period); 54 | } 55 | 56 | private function get singleBufferSize():Number 57 | { 58 | return Math.min(ParticleSymbol.MAX_BUFFER_SIZE,symbol.possibleNumLivingParticles*period); 59 | } 60 | 61 | public function DynamicParticleSource(symbol:ParticleSymbol,instance:ParticleInstance) 62 | { 63 | this.symbol = symbol; 64 | this.instance = instance; 65 | } 66 | 67 | public function updateNumbers():void 68 | { 69 | numParamBuffers = Math.ceil((symbol.possibleNumLivingParticles*period)/singleBufferSize); 70 | totalBufferSize = numParamBuffers * singleBufferSize; 71 | } 72 | 73 | private function indexToPosition(value:Number):Number 74 | { 75 | return (value % totalBufferSize); 76 | } 77 | 78 | private function indexToBufferPosition(value:Number):Number 79 | { 80 | return value % singleBufferSize; 81 | } 82 | 83 | private function indexToBuffer(value:Number):Number 84 | { 85 | return Math.floor(value / singleBufferSize) % numParamBuffers; 86 | } 87 | 88 | public function getBufferRange(range:*,baseIndex:Number,firstIndex:Number,requestedLength:Number):void 89 | { 90 | if(firstIndex < indexOfTail) 91 | throw new Error("Index out of range"); 92 | if((firstIndex + requestedLength) > (baseIndex + totalBufferSize)) 93 | throw new Error("request too big"); 94 | if(firstIndex > indexOfHead) 95 | { 96 | fillRange(firstIndex,requestedLength); 97 | } 98 | else if (firstIndex + requestedLength > indexOfHead) 99 | { 100 | fillRange(indexOfHead,requestedLength - (indexOfHead - firstIndex)); 101 | } 102 | indexOfTail = baseIndex; 103 | positionOfTail = indexToPosition(indexOfTail); 104 | 105 | // at this point, we can guarantee that [head,tail] is fully filled and contains the range we care about. 106 | // now we find the requested index and cap to the buffer it exists in. 107 | 108 | var bufferIndex:Number = indexToBuffer(firstIndex); 109 | var positionInBuffer:Number = indexToBufferPosition(firstIndex); 110 | var fillLength:Number = Math.min(requestedLength,singleBufferSize - positionInBuffer); 111 | range.firstPosition = positionInBuffer; 112 | range.length = fillLength; 113 | range.timeOffsetOfBuffer = 0; 114 | range.params = params[bufferIndex]; 115 | } 116 | 117 | public static function getMaxParticlesPerDrawCall(possibleNumLivingParticles:Number):Number 118 | { 119 | return Math.min(ParticleSymbol.MAX_BUFFER_SIZE,possibleNumLivingParticles*period); 120 | } 121 | 122 | private function fillRange(firstIndex:Number,requestedLength:Number):void 123 | { 124 | var tailBuffer:Number = indexToBuffer(indexOfTail); 125 | var positionOfTailInBuffer:Number = indexToBufferPosition(indexOfTail); 126 | var m:Matrix3D = (symbol.generateInWorldSpace)? instance.getBlitXForm():null; 127 | 128 | while(requestedLength > 0) 129 | { 130 | var bufferIndex:Number = indexToBuffer(firstIndex); 131 | var buffer:ParticleParameterBuffer = params[bufferIndex]; 132 | var positionInBuffer:Number = indexToBufferPosition(firstIndex); 133 | // we fill the entire rest of the buffer, even if it means going past the requested length 134 | var fillLength:Number = Math.min(singleBufferSize - positionInBuffer,requestedLength); 135 | // make sure we on't overwrite the tail. This should only happen because we are overfilling the request. 136 | if(tailBuffer == bufferIndex && positionOfTailInBuffer > positionInBuffer) 137 | fillLength = Math.min(fillLength,positionOfTailInBuffer - positionInBuffer); 138 | symbol.fillBuffer(buffer,positionInBuffer,firstIndex,fillLength,m); 139 | indexOfHead = firstIndex + fillLength; 140 | requestedLength -= fillLength; 141 | firstIndex += fillLength; 142 | buffer.commit(); 143 | } 144 | } 145 | 146 | public function initData():void 147 | { 148 | if(params != null) 149 | return; 150 | 151 | updateNumbers(); 152 | 153 | params = new Vector.(); 154 | for(var i:int = 0;i,start:uint):uint 72 | { 73 | var pi:ParticleInstance = renderData[start].data as ParticleInstance; 74 | pi.render(); 75 | return start+1; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/M2D/particles/ParticleParameterBuffer.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.particles 30 | { 31 | import flash.display3D.Context3D; 32 | import flash.display3D.VertexBuffer3D; 33 | 34 | public class ParticleParameterBuffer 35 | { 36 | public var firstIndexInParams:Number = 0; 37 | public var numParticles:Number; 38 | 39 | public function ParticleParameterBuffer(numParticles:int) 40 | { 41 | this.numParticles = numParticles; 42 | paramVector = new Vector.(numParticles * ParticleSymbol.NUM_PARAM_FLOATS_PER_PARTICLE); 43 | } 44 | 45 | public var paramVector:Vector.; 46 | public var paramBuffer:VertexBuffer3D; 47 | private var _buffersDirty:Boolean = true; 48 | 49 | public function initBuffers(ctx:Context3D):void 50 | { 51 | if(_buffersDirty == false) 52 | return; 53 | if(paramBuffer != null) 54 | { 55 | paramBuffer.dispose(); 56 | paramBuffer = null; 57 | } 58 | paramBuffer = ctx.createVertexBuffer( paramVector.length /ParticleSymbol.NUM_PARAMS_PER_PARTICLE, ParticleSymbol.NUM_PARAMS_PER_PARTICLE ); // 3 vertices, 5 floats per vertex 59 | paramBuffer.uploadFromVector(paramVector,0,paramVector.length/ParticleSymbol.NUM_PARAMS_PER_PARTICLE); 60 | 61 | _buffersDirty = false; 62 | } 63 | public function commit():void 64 | { 65 | paramBuffer.uploadFromVector(paramVector,0,paramVector.length/ParticleSymbol.NUM_PARAMS_PER_PARTICLE); 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /src/M2D/particles/ParticleSource.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.particles 30 | { 31 | public interface ParticleSource 32 | { 33 | function getBufferRange(range:*,baseIndex:Number,firstIndex:Number,requestedLength:Number):void; 34 | function initData():void; 35 | function initBuffers():void; 36 | function updateNumbers():void; 37 | function get maxParticlesPerDrawCall():Number; 38 | } 39 | } -------------------------------------------------------------------------------- /src/M2D/particles/StaticParticleSource.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.particles 30 | { 31 | import flash.display3D.Context3D; 32 | 33 | public class StaticParticleSource implements ParticleSource 34 | { 35 | public var symbol:ParticleSymbol; 36 | public var params:Vector.; 37 | public var period:Number = 3; 38 | private var buffersDirty:Boolean = true; 39 | 40 | public function get maxParticlesPerDrawCall():Number 41 | { 42 | return Math.min(ParticleSymbol.MAX_BUFFER_SIZE,numParticlesBeforeRepeat); 43 | } 44 | 45 | public function get numParticlesBeforeRepeat():Number 46 | { 47 | return symbol.possibleNumLivingParticles*period; 48 | } 49 | public function StaticParticleSource(symbol:ParticleSymbol) 50 | { 51 | this.symbol = symbol; 52 | } 53 | 54 | public function updateNumbers():void 55 | { 56 | 57 | } 58 | public function get numParamBuffers():Number 59 | { 60 | return Math.ceil(numParticlesBeforeRepeat/maxParticlesPerDrawCall); 61 | } 62 | 63 | 64 | public function getBufferRange(range:*,baseIndex:Number, firstIndex:Number,requestedLength:Number):void 65 | { 66 | 67 | var parameterRefillLimit:Number = numParticlesBeforeRepeat; 68 | var positionOfFirstIndex:Number = firstIndex % parameterRefillLimit; 69 | var timeLengthOfRepeat:Number = (parameterRefillLimit)*symbol.birthDelay; 70 | var timeLengthOfBuffer:Number = (maxParticlesPerDrawCall)*symbol.birthDelay; 71 | var timeOffset:Number = Math.floor((firstIndex*symbol.birthDelay)/timeLengthOfRepeat) * timeLengthOfRepeat; 72 | 73 | var paramBufferIndex:int = 0; 74 | var bufferStart:Number = 0; 75 | while((paramBufferIndex+1)*maxParticlesPerDrawCall <= positionOfFirstIndex) 76 | { 77 | paramBufferIndex++; 78 | timeOffset += timeLengthOfBuffer; 79 | bufferStart += maxParticlesPerDrawCall; 80 | } 81 | range.params = params[paramBufferIndex]; 82 | range.firstPosition = positionOfFirstIndex - bufferStart; 83 | range.length = Math.min(requestedLength,range.params.numParticles - range.firstPosition); 84 | range.timeOffsetOfBuffer = timeOffset; 85 | } 86 | public function initData():void 87 | { 88 | if(params != null) 89 | return; 90 | 91 | params = new Vector.(); 92 | for(var i:int = 0;i,start:uint):uint 94 | { 95 | var newResult:uint = world.gContext.blit2D(renderData,start,renderData.length); 96 | return newResult; 97 | } 98 | } 99 | } 100 | import M2D.core.IBlitOp; 101 | import M2D.sprites.Actor; 102 | 103 | class ActorList 104 | { 105 | public var activeActorsDirty:Boolean = true; 106 | public var blitOps:Vector. = new Vector.(); 107 | } -------------------------------------------------------------------------------- /src/M2D/time/Clock.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.time 30 | { 31 | import flash.events.Event; 32 | import flash.events.EventDispatcher; 33 | import flash.events.TimerEvent; 34 | import flash.utils.Timer; 35 | import flash.utils.getTimer; 36 | 37 | public class Clock 38 | { 39 | protected var timer:Timer; 40 | protected var _frameRate:int = 60; 41 | public var maxDeltaMilliseconds:Number = 0; 42 | private var startTime:Number; 43 | public var currentTime:int; 44 | public var processingTime:Number = 0; 45 | public var paused:Boolean = false; 46 | public var lastUpdateTime:Number; 47 | 48 | public function Clock(frameRate:int = 60) 49 | { 50 | _frameRate = frameRate; 51 | } 52 | 53 | public function start():void 54 | { 55 | timer = new Timer(1000/_frameRate); 56 | timer.addEventListener(TimerEvent.TIMER,timerHandler); 57 | startTime = getTimer(); 58 | timer.start(); 59 | } 60 | protected function timerHandler(e:TimerEvent):void 61 | { 62 | if(paused == false) 63 | { 64 | var startT:Number = getTimer(); 65 | if(maxDeltaMilliseconds > 0 && startT - lastUpdateTime > maxDeltaMilliseconds) 66 | { 67 | startT += (startT - (lastUpdateTime-maxDeltaMilliseconds)); 68 | } 69 | currentTime = startT-startTime; 70 | } 71 | for(var i:int =0;i = new Vector.(); 79 | 80 | public function pause():void 81 | { 82 | paused = true; 83 | } 84 | public function unpause():void 85 | { 86 | var startT:Number = getTimer(); 87 | startTime = startT-currentTime; 88 | paused = false; 89 | } 90 | public function addListener(l:IClockListener):void 91 | { 92 | listeners.push(l); 93 | } 94 | 95 | } 96 | } -------------------------------------------------------------------------------- /src/M2D/time/IClockListener.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.time 30 | { 31 | public interface IClockListener 32 | { 33 | function tick():void; 34 | function set clock(value:Clock):void; 35 | function get clock():Clock; 36 | } 37 | } -------------------------------------------------------------------------------- /src/M2D/worlds/AssetMgr.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 M2D.worlds 32 | { 33 | import M2D.sprites.Asset; 34 | 35 | import flash.display.DisplayObject; 36 | import flash.display.Sprite; 37 | import flash.geom.Rectangle; 38 | 39 | public class AssetMgr 40 | { 41 | public var world:World; 42 | private var nextTextureID:uint = 0; 43 | 44 | public function AssetMgr(world:World) 45 | { 46 | this.world = world; 47 | } 48 | 49 | public function createTextureFromDisplayObject(d:DisplayObject):BatchTexture 50 | { 51 | var tx:BatchTexture = BatchTexture.createFromDisplayObject(d); 52 | tx.textureID = nextTextureID++; 53 | tx.assetMgr = this; 54 | return tx; 55 | } 56 | 57 | public function createAssetFromDisplayObject(d:DisplayObject, cellRowCount:uint = 1, cellColumnCount:uint = 1, srcRect:Rectangle = null):Asset 58 | { 59 | var tx:BatchTexture = createTextureFromDisplayObject(d); 60 | var asset:Asset = world.library.createAsset(tx); 61 | asset.cellColumnCount = cellColumnCount; 62 | asset.cellRowCount = cellRowCount; 63 | return asset; 64 | } 65 | 66 | public function createTextureFromAnimatedDisplayObject(d:Sprite, padding:uint=1, scaleX:Number=1, scaleY:Number=1):BatchTexture 67 | { 68 | var tx:BatchTexture = BatchTexture.createFromAnimatedDisplayObject(d, padding, scaleX, scaleY); 69 | tx.textureID = nextTextureID++; 70 | tx.assetMgr = this; 71 | return tx; 72 | } 73 | 74 | public function createAssetFromAnimatedDisplayObject(d:Sprite, padding:uint=1, scaleX:Number=1, scaleY:Number=1):Asset 75 | { 76 | var tx:BatchTexture = createTextureFromAnimatedDisplayObject(d, padding, scaleX, scaleY); 77 | var asset:Asset = world.library.createAsset(tx); 78 | asset.cellColumnCount = tx.generatedColCount; 79 | asset.cellRowCount = tx.generatedRowCount; 80 | asset.frameCount = tx.generatedFrameCount; 81 | return asset; 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /src/M2D/worlds/IRenderJob.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.worlds 30 | { 31 | 32 | public interface IRenderJob 33 | { 34 | function set world(value:WorldBase):void; 35 | function get world():WorldBase; 36 | function render(renderData:Vector.,start:uint):uint; 37 | function set renderID(value:uint):void; 38 | function get renderID():uint; 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /src/M2D/worlds/Instance.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.worlds 30 | { 31 | import flash.geom.Matrix3D; 32 | import flash.geom.Rectangle; 33 | import flash.geom.Vector3D; 34 | 35 | public class Instance 36 | { 37 | public function Instance() 38 | { 39 | task = new RenderTask(); 40 | task.data = this; 41 | } 42 | 43 | protected var _x:Number = 0; 44 | protected var _y:Number = 0; 45 | public var scaleX:Number = 1; 46 | public var scaleY:Number = 1; 47 | private var _depth:Number = 0; 48 | public var _rotation:Number = 0; 49 | 50 | public var task:RenderTask; 51 | 52 | public function set depth(v:Number):void {_depth= v;updateKey();} 53 | public function get depth():Number { return _depth;} 54 | 55 | public var regX:Number; 56 | public var regY:Number; 57 | protected var _active:Boolean = false; 58 | protected var blitXForm:Matrix3D = new Matrix3D(); 59 | 60 | private var sourceRC:Rectangle = new Rectangle(); 61 | 62 | protected var blitXFormDirty:Boolean = true; 63 | 64 | 65 | public function get x():Number { return _x;} 66 | public function get y():Number { return _y;} 67 | 68 | public function set rotation(value:Number):void 69 | { 70 | _rotation = value; 71 | blitXFormDirty = true; 72 | } 73 | public function get rotation():Number { return _rotation; } 74 | 75 | 76 | public function set active(value:Boolean):void 77 | { 78 | if(value == _active) 79 | return; 80 | 81 | _active = value; 82 | } 83 | 84 | public function get active():Boolean 85 | { 86 | return _active; 87 | } 88 | 89 | public function move(x:Number,y:Number):void 90 | { 91 | if(blitXFormDirty == false) 92 | { 93 | blitXForm.appendTranslation(x- _x,y - _y,0); 94 | } 95 | _x = x; 96 | _y = y; 97 | } 98 | public function set x(value:Number):void 99 | { 100 | _x = value; 101 | blitXFormDirty = true; 102 | } 103 | 104 | public function set y(value:Number):void 105 | { 106 | _y = value; 107 | blitXFormDirty = true; 108 | } 109 | 110 | public function get width():Number 111 | { 112 | return 0; 113 | } 114 | public function get height():Number 115 | { 116 | return 0; 117 | } 118 | public function getBlitXForm():Matrix3D 119 | { 120 | if(blitXFormDirty) 121 | { 122 | blitXForm.identity(); 123 | blitXForm.appendTranslation((isNaN(regX))? -width/2:regX,(isNaN(regY))? -height/2:regY,0); 124 | blitXForm.appendScale(scaleX,scaleY,1); 125 | if(rotation != 0) 126 | blitXForm.appendRotation(_rotation,Vector3D.Z_AXIS); 127 | blitXForm.appendTranslation(_x,_y,-depth/30000); 128 | blitXFormDirty = false; 129 | } 130 | return blitXForm; 131 | } 132 | protected function updateKey():void 133 | { 134 | } 135 | } 136 | } -------------------------------------------------------------------------------- /src/M2D/worlds/RenderMgr.as: -------------------------------------------------------------------------------- 1 | package M2D.worlds 2 | { 3 | 4 | import flash.display.DisplayObject; 5 | import flash.display.DisplayObjectContainer; 6 | import flash.display.Sprite; 7 | import flash.display.Stage; 8 | import flash.events.Event; 9 | 10 | public class RenderMgr 11 | { 12 | private var stage:Stage; 13 | private var root:DisplayObject; 14 | 15 | public var autoRender:Boolean = true; 16 | public var world:WorldBase; 17 | public var renderEveryFrame:Boolean = false; 18 | 19 | public function RenderMgr(world:WorldBase) 20 | { 21 | this.world = world; 22 | } 23 | 24 | public function init(root:DisplayObjectContainer):void 25 | { 26 | this.root = root; 27 | root.addEventListener(Event.ENTER_FRAME,enterFrameHandler,false,0,true); 28 | if(root.stage != null) 29 | { 30 | stage = root.stage; 31 | if(autoRender) 32 | stage.addEventListener(Event.RENDER,renderHandler,false,0,true); 33 | } 34 | else 35 | root.addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler,false,0,true); 36 | } 37 | 38 | 39 | private function enterFrameHandler(e:Event):void 40 | { 41 | if(renderEveryFrame) 42 | stage.invalidate(); 43 | } 44 | 45 | private function addedToStageHandler(e:Event):void 46 | { 47 | stage = e.currentTarget.stage as Stage; 48 | stage.addEventListener(Event.RENDER,renderHandler,false,0,true); 49 | } 50 | 51 | private function renderHandler(e:Event):void 52 | { 53 | if(renderEveryFrame) 54 | world.render(); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /src/M2D/worlds/RenderTask.as: -------------------------------------------------------------------------------- 1 | package M2D.worlds 2 | { 3 | public class RenderTask 4 | { 5 | public function RenderTask() 6 | { 7 | } 8 | public var key:Number; 9 | public var highKey:uint; 10 | public var lowKey:uint; 11 | 12 | public var job:IRenderJob; 13 | public var data:Object; 14 | 15 | // transparent: bit 30 16 | public static const TRANSPARENT_MASK:Number = (1 << (31)); 17 | public static const RENDER_MASK:Number = (0x7 << (28)); // 3 bits 18 | public static const MATERIAL_MASK:Number = (0x3FF << (18)); // 10 bits 19 | 20 | public static const DEPTH_MASK:Number = (0xFFFF << (16)); // 16 bits 21 | 22 | public static function makeAlphaCode(alpha:Number):Number {return (alpha < 1)? (1 << 31):0} 23 | public static function makeRenderCode(code:Number):Number {return code << 28} 24 | public static function makeMaterialCode(code:Number):Number {return code << 18} 25 | public static function makeDepthCode(code:Number,isTransparent:Boolean = false):Number {return (isTransparent? code:(0xFFFF-code)) << 16} 26 | 27 | public static const TRANSPARENT:Number = TRANSPARENT_MASK; 28 | public static const OPAQUE:Number = 0; 29 | public function setKey(high:uint,low:uint):void 30 | { 31 | highKey = high; 32 | lowKey = low; 33 | key = makeKey(high,low); 34 | } 35 | 36 | public static function makeKey(high:uint,low:uint):Number 37 | { 38 | var result:Number = high * 0xFFFFFFFF + low; 39 | return result; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /src/M2D/worlds/World.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 | */package M2D.worlds 30 | { 31 | import M2D.particles.ParticleLibrary; 32 | import M2D.sprites.SymbolLibrary; 33 | 34 | import flash.display.DisplayObjectContainer; 35 | import flash.display.Stage; 36 | import flash.geom.Rectangle; 37 | 38 | public class World extends WorldBase 39 | { 40 | public function World() 41 | { 42 | assetMgr = new AssetMgr(this); 43 | renderMgr = new RenderMgr(this); 44 | addJob(library); 45 | addJob(particleLibrary); 46 | } 47 | 48 | override public function initContext(stage:Stage,container:DisplayObjectContainer,slot:int,bounds:Rectangle):void 49 | { 50 | // renderMgr.init(container); 51 | super.initContext(stage,container,slot,bounds); 52 | } 53 | // ====================================================================== 54 | // Constants 55 | // ---------------------------------------------------------------------- 56 | 57 | public var assetMgr:AssetMgr; 58 | public var particleLibrary:ParticleLibrary = new ParticleLibrary(); 59 | public var library:SymbolLibrary = new SymbolLibrary(); 60 | public var renderMgr:RenderMgr; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/M2D/worlds/WorldBase.as: -------------------------------------------------------------------------------- 1 | /* 2 | * M2D 3 | * ..................... 4 | * 5 | * Author: Ely Greenfield 6 | * Copyright (c) Adobe Systems 2011 7 | * https://github.com/egreenfield/M2D 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 M2D.worlds 32 | { 33 | import M2D.core.GContext; 34 | import M2D.time.Clock; 35 | import M2D.time.IClockListener; 36 | 37 | import flash.display.DisplayObjectContainer; 38 | import flash.display.Stage; 39 | import flash.display.Stage3D; 40 | import flash.display3D.Context3D; 41 | import flash.display3D.Context3DBlendFactor; 42 | import flash.display3D.Context3DCompareMode; 43 | import flash.display3D.Context3DRenderMode; 44 | import flash.events.Event; 45 | import flash.geom.Matrix3D; 46 | import flash.geom.Rectangle; 47 | 48 | public class WorldBase implements IClockListener 49 | { 50 | protected var bounds:Rectangle; 51 | protected var slot:int; 52 | protected var stage:Stage; 53 | public var backgroundColor:uint = 0xFFAAAA; 54 | public var readyCallback:Function; 55 | private var sorted:Boolean = false; 56 | 57 | public var renderTasks:Array = []; 58 | public var sortedRenderTasks:Vector.; 59 | private var _clock:Clock; 60 | 61 | private var renderMode:String = Context3DRenderMode.AUTO; 62 | public var context3D:Context3D; 63 | public var gContext:GContext = new GContext(); 64 | 65 | public var cameraMatrix:Matrix3D; 66 | 67 | private var stage3D:Stage3D; 68 | public var antiAliasDepth:int = 2; 69 | private var cameraDirty:Boolean = true; 70 | 71 | private var jobs:Vector. = new Vector.(); 72 | 73 | public function WorldBase() 74 | { 75 | } 76 | 77 | public function initContext(stage:Stage,container:DisplayObjectContainer,slot:int,bounds:Rectangle):void 78 | { 79 | this.bounds = bounds.clone(); 80 | this.slot = slot; 81 | this.stage = stage; 82 | acquireContext(); 83 | } 84 | 85 | private function acquireContext():void 86 | { 87 | stage3D = stage.stage3Ds[slot]; 88 | 89 | stage3D.viewPort = bounds; 90 | context3D = stage3D.context3D; 91 | if(context3D == null) 92 | { 93 | stage3D.addEventListener ( Event.CONTEXT3D_CREATE, stageNotificationHandler,false,0,true); 94 | stage3D.requestContext3D ( renderMode ); 95 | } 96 | else 97 | { 98 | initContext3D(); 99 | } 100 | } 101 | private function stageNotificationHandler(e:Event):void 102 | { 103 | context3D = stage3D.context3D; 104 | initContext3D(); 105 | } 106 | 107 | private function initContext3D():void 108 | { 109 | // Keep enabled for now since the public incubator release has 110 | // issues with rendering when set to false. 111 | context3D.enableErrorChecking = true; 112 | 113 | context3D.configureBackBuffer( bounds.width, bounds.height, antiAliasDepth, true); // fixed size 114 | context3D.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA); 115 | context3D.setDepthTest(true,Context3DCompareMode.GREATER_EQUAL); 116 | gContext.init(context3D); 117 | if(readyCallback != null) 118 | readyCallback(this); 119 | } 120 | 121 | private function buildCameraMatrix():void 122 | { 123 | if(cameraDirty == false) 124 | return; 125 | cameraMatrix = new Matrix3D(); 126 | cameraMatrix.appendScale(2/bounds.width,-2/bounds.height,1/3000); 127 | cameraMatrix.appendTranslation(-1,1,0); 128 | 129 | gContext.cameraMatrix = cameraMatrix; 130 | cameraDirty = false; 131 | } 132 | 133 | public function addRenderData(r:RenderTask):void 134 | { 135 | renderTasks.push(r); 136 | } 137 | 138 | public function render():void 139 | { 140 | 141 | if(context3D == null) 142 | { 143 | return; 144 | } 145 | 146 | context3D.clear(((backgroundColor & 0xFF0000) >> 16 )/256, 147 | ((backgroundColor & 0x00FF00) >> 8 )/256, 148 | ((backgroundColor & 0x0000FF) )/256,0,0); 149 | 150 | if(cameraDirty) 151 | { 152 | buildCameraMatrix(); 153 | } 154 | 155 | var nextGroup:int = 0; 156 | 157 | 158 | sortTasks(); 159 | 160 | var len:uint = sortedRenderTasks.length; 161 | 162 | var renderIndex:uint = 0; 163 | 164 | while(renderIndex(renderTasks); 181 | sorted = true; 182 | } 183 | } 184 | public function addJob(job:IRenderJob):void 185 | { 186 | jobs.push(job); 187 | job.world = this; 188 | } 189 | 190 | public function get clock():Clock 191 | { 192 | return _clock; 193 | } 194 | 195 | public function set clock(value:Clock):void 196 | { 197 | _clock = value; 198 | } 199 | 200 | 201 | public function tick():void 202 | { 203 | render(); 204 | } 205 | 206 | } 207 | } -------------------------------------------------------------------------------- /src/utils/DebugUtils.as: -------------------------------------------------------------------------------- 1 | package utils 2 | { 3 | public class DebugUtils 4 | { 5 | public function DebugUtils() 6 | { 7 | } 8 | private static var c:Array = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', 9 | '9', 'A', 'B', 'C', 'D', 'E', 'F' ]; 10 | 11 | public static function d2h( d:Number ) : String 12 | { 13 | var result:String = ""; 14 | while(d > 0) 15 | { 16 | var v:int = d % 16; 17 | result = c[v] + result; 18 | d -= v; 19 | d = d >> 4; 20 | } 21 | return "0x" + result; 22 | } 23 | public static function d2b( d:Number ) : String 24 | { 25 | var result:String = ""; 26 | while(d > 0) 27 | { 28 | var v:int = d % 2; 29 | result = c[v] + result; 30 | d -= v; 31 | d = d >> 1; 32 | } 33 | return "." + result; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/zones/BitmapDataZone.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.display.BitmapData; 35 | import flash.geom.Point; 36 | import flash.geom.Vector3D; 37 | 38 | import zones.FastWeightedArray; 39 | import zones.Zone2D; 40 | 41 | /** 42 | * The BitmapData zone defines a shaped zone based on a BitmapData object. 43 | * The zone contains all pixels in the bitmap that are not transparent - 44 | * i.e. they have an alpha value greater than zero. 45 | */ 46 | 47 | public class BitmapDataZone implements Zone2D 48 | { 49 | private var _bitmapData : BitmapData; 50 | private var _offsetX : Number; 51 | private var _offsetY : Number; 52 | private var _scaleX : Number; 53 | private var _scaleY : Number; 54 | private var _validPoints : FastWeightedArray; 55 | 56 | /** 57 | * The constructor creates a BitmapDataZone object. 58 | * 59 | * @param bitmapData The bitmapData object that defines the zone. 60 | * @param xOffset A horizontal offset to apply to the pixels in the BitmapData object 61 | * to reposition the zone 62 | * @param yOffset A vertical offset to apply to the pixels in the BitmapData object 63 | * to reposition the zone 64 | * @param scaleX A scale factor to stretch the bitmap horizontally 65 | * @param scaleY A scale factor to stretch the bitmap vertically 66 | */ 67 | public function BitmapDataZone( bitmapData : BitmapData = null, offsetX : Number = 0, offsetY : Number = 0, scaleX:Number = 1, scaleY:Number = 1 ) 68 | { 69 | _bitmapData = bitmapData; 70 | _offsetX = offsetX; 71 | _offsetY = offsetY; 72 | _scaleX = scaleX; 73 | _scaleY = scaleY; 74 | invalidate(); 75 | } 76 | 77 | /** 78 | * The bitmapData object that defines the zone. 79 | */ 80 | public function get bitmapData() : BitmapData 81 | { 82 | return _bitmapData; 83 | } 84 | public function set bitmapData( value : BitmapData ) : void 85 | { 86 | _bitmapData = value; 87 | invalidate(); 88 | } 89 | 90 | /** 91 | * A horizontal offset to apply to the pixels in the BitmapData object 92 | * to reposition the zone 93 | */ 94 | public function get offsetX() : Number 95 | { 96 | return _offsetX; 97 | } 98 | public function set offsetX( value : Number ) : void 99 | { 100 | _offsetX = value; 101 | } 102 | 103 | /** 104 | * A vertical offset to apply to the pixels in the BitmapData object 105 | * to reposition the zone 106 | */ 107 | public function get offsetY() : Number 108 | { 109 | return _offsetY; 110 | } 111 | public function set offsetY( value : Number ) : void 112 | { 113 | _offsetY = value; 114 | } 115 | 116 | /** 117 | * A scale factor to stretch the bitmap horizontally 118 | */ 119 | public function get scaleX() : Number 120 | { 121 | return _scaleX; 122 | } 123 | public function set scaleX( value : Number ) : void 124 | { 125 | _scaleX = value; 126 | } 127 | 128 | /** 129 | * A scale factor to stretch the bitmap vertically 130 | */ 131 | public function get scaleY() : Number 132 | { 133 | return _scaleY; 134 | } 135 | public function set scaleY( value : Number ) : void 136 | { 137 | _scaleY = value; 138 | } 139 | 140 | /** 141 | * This method forces the zone to revaluate itself. It should be called whenever the 142 | * contents of the BitmapData object change. However, it is an intensive method and 143 | * calling it frequently will likely slow your code down. 144 | */ 145 | public function invalidate():void 146 | { 147 | if( ! _bitmapData ) 148 | { 149 | return; 150 | } 151 | _validPoints = new FastWeightedArray(); 152 | for( var x : int = 0; x < _bitmapData.width ; ++x ) 153 | { 154 | for( var y : int = 0; y < _bitmapData.height ; ++y ) 155 | { 156 | var pixel : uint = _bitmapData.getPixel32( x, y ); 157 | var ratio : Number = ( pixel >> 24 & 0xFF ) / 0xFF; 158 | if ( ratio != 0 ) 159 | { 160 | _validPoints.add( new Point( x, y ), ratio ); 161 | } 162 | } 163 | } 164 | } 165 | 166 | /** 167 | * The contains method determines whether a point is inside the zone. 168 | * 169 | * @param point The location to test for. 170 | * @return true if point is inside the zone, false if it is outside. 171 | */ 172 | public function contains( x : Number, y : Number ) : Boolean 173 | { 174 | if( x >= _offsetX && x <= _offsetX + _bitmapData.width * scaleX 175 | && y >= _offsetY && y <= _offsetY + _bitmapData.height * scaleY ) 176 | { 177 | var pixel : uint = _bitmapData.getPixel32( Math.round( ( x - _offsetX ) / _scaleX ), Math.round( ( y - _offsetY ) / _scaleY ) ); 178 | return ( pixel >> 24 & 0xFF ) != 0; 179 | } 180 | return false; 181 | } 182 | 183 | /** 184 | * The getLocation method returns a random point inside the zone. 185 | * 186 | * @return a random point inside the zone. 187 | */ 188 | public function getLocation(p : Vector3D):void 189 | { 190 | var pa:Point = _validPoints.getRandomValue(); 191 | p.x = pa.x * _scaleX + _offsetX; 192 | p.y = pa.y * _scaleY + _offsetY; 193 | } 194 | 195 | /** 196 | * The getArea method returns the size of the zone. 197 | * It's used by the MultiZone class to manage the balancing between the 198 | * different zones. 199 | * 200 | * @return the size of the zone. 201 | */ 202 | public function getArea() : Number 203 | { 204 | return _validPoints.totalRatios * _scaleX * _scaleY; 205 | } 206 | 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/zones/DisplayObjectZone.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.display.DisplayObject; 34 | import flash.geom.Point; 35 | import flash.geom.Rectangle; 36 | import flash.geom.Vector3D; 37 | 38 | import zones.Zone2D; 39 | 40 | /** 41 | * The DisplayObjectZone zone defines a shaped zone based on a DisplayObject. 42 | * The zone contains the shape of the DisplayObject. The DisplayObject must be 43 | * on the stage for it to be used, since it's position on stage determines the 44 | * position of the zone. 45 | */ 46 | 47 | public class DisplayObjectZone implements Zone2D 48 | { 49 | private var _displayObject : DisplayObject; 50 | private var _renderer : DisplayObject; 51 | private var _area : Number; 52 | 53 | 54 | /** 55 | * The constructor creates a DisplayObjectZone object. 56 | * 57 | * @param displayObject The DisplayObject that defines the zone. 58 | * @param emitter The renderer that you plan to use the zone with. The 59 | * coordinates of the DisplayObject are translated to the local coordinate 60 | * space of the renderer. 61 | */ 62 | public function DisplayObjectZone( displayObject : DisplayObject = null, renderer : DisplayObject = null ) 63 | { 64 | _displayObject = displayObject; 65 | _renderer = renderer; 66 | calculateArea(); 67 | } 68 | 69 | private function calculateArea():void 70 | { 71 | if( ! _displayObject ) 72 | { 73 | return; 74 | } 75 | 76 | var bounds:Rectangle = _displayObject.getBounds( _displayObject.stage ); 77 | 78 | _area = 0; 79 | var right:Number = bounds.right; 80 | var bottom:Number = bounds.bottom; 81 | for( var x : int = bounds.left; x <= right ; ++x ) 82 | { 83 | for( var y : int = bounds.top; y <= bottom ; ++y ) 84 | { 85 | if ( _displayObject.hitTestPoint( x, y, true ) ) 86 | { 87 | ++_area; 88 | } 89 | } 90 | } 91 | } 92 | 93 | /** 94 | * The DisplayObject that defines the zone. 95 | */ 96 | public function get displayObject() : DisplayObject 97 | { 98 | return _displayObject; 99 | } 100 | public function set displayObject( value : DisplayObject ) : void 101 | { 102 | _displayObject = value; 103 | calculateArea(); 104 | } 105 | 106 | /** 107 | * The emitter that you plan to use the zone with. The 108 | * coordinates of the DisplayObject are translated to the local coordinate 109 | * space of the emitter. 110 | */ 111 | public function get renderer() : DisplayObject 112 | { 113 | return _renderer; 114 | } 115 | public function set renderer( value : DisplayObject ) : void 116 | { 117 | _renderer = value; 118 | } 119 | 120 | /** 121 | * The contains method determines whether a point is inside the zone. 122 | * 123 | * @param point The location to test for. 124 | * @return true if point is inside the zone, false if it is outside. 125 | */ 126 | public function contains( x : Number, y : Number ) : Boolean 127 | { 128 | return _displayObject.hitTestPoint( x, y, true ); 129 | } 130 | 131 | /** 132 | * The getLocation method returns a random point inside the zone. 133 | * 134 | * @return a random point inside the zone. 135 | */ 136 | public function getLocation(p: Vector3D):void 137 | { 138 | var bounds:Rectangle = _displayObject.getBounds( _displayObject ); 139 | do 140 | { 141 | var x : Number = bounds.left + Math.random( ) * bounds.width; 142 | var y : Number = bounds.top + Math.random( ) * bounds.height; 143 | } 144 | while( !_displayObject.hitTestPoint( x, y, true ) ); 145 | p.x = x; 146 | p.y = y; 147 | } 148 | 149 | /** 150 | * The getArea method returns the size of the zone. 151 | * It's used by the MultiZone class to manage the balancing between the 152 | * different zones. 153 | * 154 | * @return the size of the zone. 155 | */ 156 | public function getArea() : Number 157 | { 158 | return _area; 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/zones/FastWeightedArray.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 | * A FastWeightedArray performes the same purpose as a WeightedArray 35 | * but this array is optimized to select random items in a large array 36 | * much more rapidly. In improving the speed if the array, it is necessary 37 | * to loose the functionality to remove items from the FastWeightedArray, so 38 | * the original WeightedArray also still available. 39 | * 40 | *

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 | } --------------------------------------------------------------------------------