Created
August 9, 2014 15:05
-
-
Save azcdev/85b118b2facb2a04d37f to your computer and use it in GitHub Desktop.
Pathfinding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
import flash.display.MovieClip; | |
public class pathfinding extends Sprite | |
{ | |
private var ANCHO:int = stage.stageWidth; | |
private var ALTO:int = stage.stageHeight; | |
private var anchoCelda:int = 10; | |
private var altoCelda:int = 10; | |
private var cuadricula:Cuadricula; | |
private var agente:Sprite; | |
private var indice:int; | |
private var ruta:Array; | |
public function pathfinding() | |
{ | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
dibujaAgente(); | |
hazCuadricula(); | |
stage.addEventListener(MouseEvent.CLICK, onCuadriculaClick); | |
} | |
private function dibujaAgente():void | |
{ | |
agente = new Sprite(); | |
agente.graphics.beginFill(0xff0000); | |
agente.graphics.drawCircle(0, 0, 5); | |
agente.graphics.endFill(); | |
agente.x = Math.random() * ANCHO; | |
agente.y = Math.random() * ALTO; | |
addChild(agente); | |
} | |
private function hazCuadricula():void | |
{ | |
cuadricula = new Cuadricula(20,20); | |
for (var i:int = 0; i< 150; i++) | |
{ | |
var fila:int = Math.floor(Math.random() * 20); | |
var columna:int = Math.floor(Math.random() * 20); | |
cuadricula.fijarNodoPermitido(fila,columna,false); | |
} | |
dibujaCuadricula(); | |
} | |
private function dibujaCuadricula():void | |
{ | |
graphics.clear(); | |
for (var i:int = 0; i < cuadricula.Columnas; i++) | |
{ | |
for (var j:int = 0; j < cuadricula.Filas; j++) | |
{ | |
var nodo:Nodo = cuadricula.dameNodo(i,j); | |
graphics.lineStyle(0); | |
graphics.beginFill(pintarNodo(nodo)); | |
graphics.drawRect(i * anchoCelda, j * altoCelda, anchoCelda, altoCelda); | |
} | |
} | |
} | |
private function pintarNodo(nodo:Nodo):uint | |
{ | |
if (! nodo.permitido) | |
{ | |
return 0x00ccff; | |
} | |
if (nodo == cuadricula.nodoInicial) | |
{ | |
return 0x00ff00; | |
} | |
if (nodo == cuadricula.nodoFinal) | |
{ | |
return 0xccff00; | |
} | |
return 0x669900; | |
} | |
private function onCuadriculaClick(event:MouseEvent):void | |
{ | |
var xpos:int = Math.floor(mouseX / anchoCelda); | |
var ypos:int = Math.floor(mouseY / altoCelda); | |
cuadricula.fijarNodoFinal(xpos, ypos); | |
xpos = Math.floor(agente.x / anchoCelda); | |
ypos = Math.floor(agente.y / altoCelda); | |
cuadricula.fijarNodoInicial(xpos, ypos); | |
dibujaCuadricula(); | |
buscaRuta(); | |
} | |
private function buscaRuta():void | |
{ | |
var astar:Astar = new Astar(); | |
if (astar.buscaRuta(cuadricula)) | |
{ | |
ruta = astar.ruta; | |
indice = 0; | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
} | |
private function onEnterFrame(event:Event):void | |
{ | |
var posicionFinalX:Number = ruta[indice].x * anchoCelda + altoCelda / 2; | |
var posicionFinalY:Number = ruta[indice].y * altoCelda + anchoCelda / 2; | |
var dx:Number = posicionFinalX - agente.x; | |
var dy:Number = posicionFinalY - agente.y; | |
var dist:Number = Math.sqrt(dx * dx + dy * dy); | |
if (dist < 1) | |
{ | |
indice++; | |
if (indice >= ruta.length) | |
{ | |
removeEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
} | |
else | |
{ | |
agente.x += dx * .5; | |
agente.y += dy * .5; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment