Test06 - FlappyFlare

From Flare3D Wiki
Revision as of 19:26, 17 February 2014 by Root (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

package physics
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.*;
	import flare.materials.*;
	import flare.materials.filters.*;
	import flare.system.*;
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
 
	[SWF( width = 384, height = 640, frameRate = 60 )]
	/**
	 * @author Ariel Nehmad
	 */
	public class Test06_FlappyFlare extends Sprite 
	{
		[Embed(source = "assets/flappyFlare.zf3d", mimeType = "application/octet-stream")]
		private var Model:Class;
 
		private var scene:Scene3D;
		private var floor:Pivot3D;
		private var bird:Pivot3D;
		private var state:String;
		private var model:Flare3DLoader;
		private var sky:TextureMapFilter;
		private var obstacles:Vector.<Pivot3D>;
		private var trigger:Pivot3D;
		private var text:TextField;
		private var score:int;
 
		public function Test06_FlappyFlare() 
		{
			scene = new Scene3D( this );
			scene.autoResize = true;
			scene.physics.gravity.setTo( 0, -8, 0 );
 
			text = new TextField;
			text.selectable = false;
			text.autoSize = TextFieldAutoSize.CENTER;
			text.defaultTextFormat = new TextFormat( "arial", 100, 0x000000, true );
			text.x = stage.stageWidth * 0.5;
			text.y = 30;
			addChild( text );
 
			resetGame();
		}
 
		public function resetGame():void 
		{
			scene.pause();
			scene.removeEventListener( Scene3D.UPDATE_EVENT, updateEvent );
 
			if ( model ) 
				model.dispose();
 
			model = new Flare3DLoader( new Model );
			model.addEventListener( Event.COMPLETE, completeEvent );
			model.load();
			model.parent = scene;
 
			score = 0;
			state = "wait";
			text.text = score.toString();
		}
 
		private function completeEvent(e:Event):void 
		{
			obstacles = new Vector.<Pivot3D>;
 
			var skyMat:Shader3D = model.getMaterialByName( "sky" ) as Shader3D;
			sky = skyMat.getFilterByClass( TextureMapFilter );
 
			scene.forEach( setupScene );
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
			scene.resume();
		}
 
		private function setupScene( p:Pivot3D ):void 
		{
			// look for obstacles, trigger zone, floor, camera, and the bird.
			if ( p.name == "obstacle" ) {
				p.y = Math.random() * 170 - 40;
				obstacles.push( p );
			} else if ( p.name == "trigger" ) {
				trigger = p;
			} else if ( p.name == "floor" ) {
				floor = p;
			} else if ( p.name == "camera" ) {
				scene.camera = p as Camera3D; 
			} else if ( p.name == "bird" ) {
				bird = p;
				bird.gotoAndPlay( "fly" );
			}
		}
 
		private function updateEvent(e:Event):void 
		{
			if ( state == "wait" ) {
				bird.y = Math.cos( getTimer() / 100 ) * 15;
				if ( Input3D.mouseHit ) 
					state = "fly";
			}
 
			if ( state == "fly" ) {
 
				// moves the trigger zone.
				trigger.z -= 2.5;
 
				// animates the floor.
				floor.z -= 2.5;
				if ( floor.z < 200 ) floor.z += 200;
 
				// animates the cylinders.
				for each ( var p:Pivot3D in obstacles ) {
					p.z -= 2.5;
					if ( p.z < -400 ) {
						p.z += 200 * obstacles.length;
						p.y = Math.random() * 170 - 40;
					}
				}
 
				// when mouse is pressed, apply a new force.
				if ( Input3D.mouseHit ) {
					bird.collider.resetVelocities();
					bird.collider.applyImpulse( 0, 140, 0 );
					bird.x = 0;
				}
 
				// animate the sky.
				sky.offsetX += 0.005;
			}
 
			// update physics.
			if ( state != "wait" )
				scene.physics.step();
 
			// when colliding with something.
			if ( state == "fly" && bird.collider.numContacts ) {
				if ( trigger.collider.numContacts ) {
					trigger.z += 200;
					score++;
					text.text = score.toString();
				} else {
					state = "die";
					bird.gotoAndPlay( "die", 0, Pivot3D.ANIMATION_STOP_MODE );
					bird.collider.applyTorque( 200, 0, -200 );
					setTimeout( resetGame, 3000 );
				}
			}
		}
	}
}
blog comments powered by Disqus