Revision: 24870
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 13, 2010 11:19 by LeeProbert
Initial Code
package com.game.astar
{
import com.app.data.AppFlags;
import com.robot.comm.DispatchManager;
import com.robot.comm.R_Event;
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
//-------------------------------------------------------------------
public class AStar extends Sprite {
public var map:AStarMap;
protected const cellSize:int = 6;
//----------------------------------------------------------------
protected var _solution_array:Array;
public function set solution_array(v:Array):void { _solution_array = v; };
public function get solution_array():Array { return _solution_array; };
//----------------------------------------------------------------
public function AStar() {
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
//-------------------------------------------------------------------
public function initialize(evt:Event):void
{
//stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
//addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
//addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
map = new AStarMap(15, 15);
/*
map.setCell(2, 4, map.CELL_FILLED);
map.setCell(3, 4, map.CELL_FILLED);
map.setCell(4, 4, map.CELL_FILLED);
map.setCell(5, 4, map.CELL_FILLED);
*/
map.setEndPoints(13,0,1,14);
drawMap();
}
//-------------------------------------------------------------------
public function keyDownHandler(evt:KeyboardEvent):void
{
if(evt.keyCode == 82) {
map.clearMap();
drawMap();
}
//32 = space
if(evt.keyCode == 32) solve();
}
//-------------------------------------------------------------------
public function solve():void
{
if(solution_array) solution_array.length = 0;
solution_array = map.solve();
drawMap();
if(solution_array.length == 0) return;
//trace along path array
graphics.lineStyle(2, 0xffffff);
var endCell:Object = solution_array[0];
//graphics.drawCircle(endCell.x * cellSize + cellSize/2, endCell.y * cellSize + cellSize/2, 30);
graphics.moveTo(endCell.x * cellSize + cellSize/2, endCell.y * cellSize + cellSize/2);
graphics.lineTo(endCell.parentCell.x * cellSize + cellSize/2, endCell.parentCell.y * cellSize + cellSize/2);
for each(var cell:Object in solution_array)
{
//trace(cell.x * cellSize, cell.parentCell.x * cellSize);
graphics.moveTo(cell.x * cellSize + cellSize/2, cell.y * cellSize + cellSize/2);
graphics.lineTo(cell.parentCell.x * cellSize + cellSize/2, cell.parentCell.y * cellSize + cellSize/2);
}
}
//-------------------------------------------------------------------
public function mouseDownHandler(evt:MouseEvent):void
{
var xx:int = evt.localX / cellSize;
var yy:int = evt.localY / cellSize;
if(evt.shiftKey) { //erase cells
map.setCell(xx, yy, map.CELL_FREE);
} else { //fill cells
map.setCell(xx, yy, map.CELL_FILLED);
}
drawMap();
}
//-------------------------------------------------------------------
public function mouseMoveHandler(evt:MouseEvent):void
{
if(evt.buttonDown) {
var xx:int = evt.localX / cellSize;
var yy:int = evt.localY / cellSize;
if(evt.shiftKey) { //erase cells
map.setCell(xx, yy, map.CELL_FREE);
} else { //fill cells
map.setCell(xx, yy, map.CELL_FILLED);
}
drawMap();
}
}
//-------------------------------------------------------------------
public function drawMap():void
{
graphics.clear();
//draw background square
graphics.beginFill(0x000000,0.1);
graphics.drawRect(0,0,map.gridWidth*cellSize,map.gridHeight*cellSize);
//draw cells
for(var xx:int = 0; xx < map.gridWidth; xx++) {
for(var yy:int = 0; yy < map.gridHeight; yy++) {
if(map.getCell(xx,yy).cellType == map.CELL_FILLED) {
fillRect(graphics, xx * cellSize, yy * cellSize, 0xAA0000);
}
if(map.getCell(xx,yy).cellType == map.CELL_ORIGIN) {
fillRect(graphics, xx * cellSize, yy * cellSize, 0x00AA00);
}
if(map.getCell(xx,yy).cellType == map.CELL_DESTINATION) {
fillRect(graphics, xx * cellSize, yy * cellSize, 0x0000AA);
}
}
}
//draw grid
graphics.lineStyle(1, 0xDDDDDD);
var ii:int = 0;
for(ii = cellSize; ii < map.gridWidth * cellSize; ii += cellSize) {
graphics.moveTo(ii, 0);
graphics.lineTo(ii, map.gridHeight * cellSize);
}
for(ii = cellSize; ii < map.gridHeight * cellSize; ii += cellSize) {
graphics.moveTo(0, ii);
graphics.lineTo(map.gridWidth * cellSize, ii);
}
//draw outline
graphics.lineStyle(1, 0xAAAAAA);
graphics.moveTo(0, 0);
graphics.lineTo(map.gridWidth * cellSize, 0);
graphics.lineTo(map.gridWidth * cellSize, map.gridHeight * cellSize);
graphics.lineTo(0, map.gridHeight * cellSize);
graphics.lineTo(0,0);
}
//-------------------------------------------------------------------
private function fillRect(target:Graphics, cellX:int, cellY:int, color:int):void
{
target.lineStyle(1, color);
target.moveTo(cellX + 2, cellY + 2);
target.beginFill(color, 0.5);
target.drawRect(cellX + 2, cellY + 2, cellSize - 4, cellSize - 4);
//target.drawCircle(cellX * cellSize, cellY * cellSize, 10);
target.endFill();
}
}
}
Initial URL
Initial Description
Initial Title
AStar pathfinding class (use with AStarMap)
Initial Tags
textmate
Initial Language
Other