[Flash 15+]

January 2016

There is another entity hidden in the center of the level.

Daedalus website and source

package game.screens.dev
{
	import DDLS.ai.DDLSEntityAI;
	import DDLS.ai.DDLSFieldOfView;
	import DDLS.ai.DDLSPathFinder;
	import DDLS.ai.trajectory.DDLSLinearPathSampler;
	import DDLS.data.DDLSMesh;
	import DDLS.data.DDLSObject;
	import DDLS.data.math.DDLSRandGenerator;
	import DDLS.factories.DDLSRectMeshFactory;
	import DDLS.view.DDLSSimpleView;
	import engine.BaseScreen;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import game.Config;
	
	/**
	 * ...
	 * @author YopSolo
	 */
	public class DaedalusFovTest extends BaseScreen
	{
		private var _mesh:DDLSMesh;
		private var _ddlsView:DDLSSimpleView;
		
		private var _entityAI:DDLSEntityAI;
		private var _pathfinder:DDLSPathFinder;
		private var _path:Vector.<Number>;
		private var _pathSampler:DDLSLinearPathSampler;
		private var _entity2AI:DDLSEntityAI;
		private var _ddlsEntities:Vector.<DDLSEntityAI>;
		private var _ddlsFov:DDLSFieldOfView;
		
		public function DaedalusFovTest()
		{
			_nextScreen = DaedalusPath;
			super(new MovieClip);
		}
		
		// =====================================================================
		public function get view():MovieClip
		{
			return _view as MovieClip;
		}
		
		// =====================================================================
		override protected function onViewReady():void
		{
			// build a rectangular 2 polygons mesh of 600x400
			_mesh = DDLSRectMeshFactory.buildRectangle(800, 600);
			
			// create a viewport
			_ddlsView = new DDLSSimpleView();
			_ddlsView.surface.x = (Config.GAME_WIDTH - 800) >> 1;
			_ddlsView.surface.y = (Config.GAME_HEIGHT - 600) >> 1;
			view.addChild(_ddlsView.surface);
			
			// pseudo random generator
			var randGen:DDLSRandGenerator
			randGen = new DDLSRandGenerator();
			randGen.seed = 1325; // put a 4 digits number here
			
			// populate mesh with many square objects
			var object:DDLSObject;
			var shapeCoords:Vector.<Number>;
			for (var i:int = 0; i < 15; i++)
			{
				object = new DDLSObject();
				shapeCoords = new Vector.<Number>();
				shapeCoords.push(-1, -1, 1, -1);
				shapeCoords.push(1, -1, 1, 1);
				shapeCoords.push(1, 1, -1, 1);
				shapeCoords.push(-1, 1, -1, -1);
				object.coordinates = shapeCoords;
				randGen.rangeMin = 10;
				randGen.rangeMax = 40;
				object.scaleX = randGen.next();
				object.scaleY = randGen.next();
				randGen.rangeMin = 0;
				randGen.rangeMax = 1000;
				object.rotation = (randGen.next() / 1000) * Math.PI / 2;
				randGen.rangeMin = 50;
				randGen.rangeMax = 600;
				object.x = randGen.next();
				object.y = randGen.next();
				_mesh.insertObject(object);
			}
			
			// show result mesh on screen
			_ddlsView.drawMesh(_mesh);
			
			// we need an entity
			_entityAI = new DDLSEntityAI();
			_entityAI.radius = 10;
			_entityAI.x = 20;
			_entityAI.y = 20;
			_entityAI.angleFOV = Math.PI / 3; // rad
			_entityAI.radiusFOV = 200; // pixels
			
			// we need another entity
			_entity2AI = new DDLSEntityAI(); 			
			_entity2AI.radius = 10;
			_entity2AI.x = 400;
			_entity2AI.y = 300;						
			//_ddlsView.drawEntity(_entity2AI);
			
			_ddlsEntities = new Vector.<DDLSEntityAI>;
			_ddlsEntities.push(_entityAI, _entity2AI);
			_ddlsView.drawEntity(_entityAI);			
			
			// create the field of view
			_ddlsFov = new DDLSFieldOfView();
			_ddlsFov.mesh = _mesh;
			_ddlsFov.fromEntity = _entityAI;
			
			// now configure the pathfinder
			_pathfinder = new DDLSPathFinder();
			_pathfinder.entity = _entityAI; // set the entity
			_pathfinder.mesh = _mesh; // set the mesh
			
			// we need a vector to store the path
			_path = new Vector.<Number>();
			
			// then configure the path sampler
			_pathSampler = new DDLSLinearPathSampler();
			_pathSampler.entity = _entityAI;
			_pathSampler.samplingDistance = 5;
			_pathSampler.path = _path;
			
			_ddlsView.surface.addEventListener(MouseEvent.CLICK, _onClick);
		}
		
		private function _oef(event:Event):void
		{
			if (_pathSampler.hasNext)
			{
				// move entity
				_pathSampler.next();
				
				_ddlsView.drawEntity(_entityAI);
				// if _entity2AI is visible show both entities
				if (_ddlsFov.isInField(_entity2AI))
				{
					_ddlsView.drawEntities(_ddlsEntities);
				}
			}
			else
			{
				// animation is over
				_view.removeEventListener(Event.ENTER_FRAME, _oef);
			}
		}
		
		private function _onClick(e:MouseEvent):void
		{
			if (e.shiftKey)
			{
				end();
			}
			else
			{
				// find path !
				_pathfinder.findPath(_ddlsView.surface.mouseX, _ddlsView.surface.mouseY, _path);
				
				// show path on screen
				_ddlsView.drawPath(_path);
				
				// reset the path sampler to manage new generated path
				_pathSampler.reset();
				
				// animate !
				_view.addEventListener(Event.ENTER_FRAME, _oef);
			}
		}
		
		// =====================================================================
		override protected function end():void
		{
			_ddlsView.surface.removeEventListener(MouseEvent.CLICK, _onClick);
			if (_view.hasEventListener(Event.ENTER_FRAME))
			{
				_view.removeEventListener(Event.ENTER_FRAME, _oef);
			}
			super.end();
		}
	
	}

}

Vote in HexoSearch