Skip to content

Commit

Permalink
Added Walkable analyzer code
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgarnett committed Jan 22, 2012
1 parent 142459d commit 872a6da
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
An A* (Astar) library for Scala/Java

This is a direct port of the AS3 code created by Jeroen Beckers at http://www.dauntless.be/astar/
This is a port of the AS3 code created by Jeroen Beckers at http://www.dauntless.be/astar/

This was developed while working at Kixeye http://www.kixeye.com
31 changes: 31 additions & 0 deletions src/com/adverserealms/astar/basic2d/WalkableTile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright (c) 2012 Joshua Garnett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.adverserealms.astar.basic2d

trait WalkableTile {

/**
* Returns if the tile is walkable or not
*/
def getWalkable() : Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright (c) 2012 Joshua Garnett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.adverserealms.astar.basic2d.analyzers

import scala.collection.mutable.ListBuffer

import com.adverserealms.astar.basic2d.WalkableTile
import com.adverserealms.astar.core.Analyzer
import com.adverserealms.astar.core.AstarTile
import com.adverserealms.astar.core.PathRequest

/**
* The WalkableAnalyzer eliminates tiles that aren't walkable. If ignoreEnd is
* true, the end node (PathRequest.isTarget(tile)) doesn't have to be walkable
*/
class WalkableAnalyzer(ignoreEnd:Boolean) extends Analyzer {

def analyzeTile(mainTile:AstarTile, request:PathRequest) : Boolean = {
if(ignoreEnd && request.isTarget(mainTile)) {
true
}
else {
mainTile.asInstanceOf[WalkableTile].getWalkable
}
}

def analyze(mainTile:AstarTile, neighbors:List[AstarTile], request:PathRequest) : List[AstarTile] = {
val walkableNeighbors = new ListBuffer[AstarTile]

for(tile <- neighbors) {
if(tile.asInstanceOf[WalkableTile].getWalkable || ignoreEnd && request.isTarget(mainTile)) {
walkableNeighbors += tile
}
}

walkableNeighbors.toList
}
}
4 changes: 2 additions & 2 deletions src/com/adverserealms/astar/core/Analyzer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ trait Analyzer {
* Eliminates neighbors from the given array and returns the neighbors that were valid.
*
* @param mainTile The tile who's neighbors are being analyzed
* @param neighbours A list consisting of all the neighbors of the mainTile
* @param neighbors A list consisting of all the neighbors of the mainTile
* @param request The PathRequest that is currently being executed
*
* @return A list consisting of all the neighbors that passed this analyzer
*/
def analyze(mainTile:AstarTile, allNeighbors:List[AstarTile], request:PathRequest) : List[AstarTile]
def analyze(mainTile:AstarTile, neighbors:List[AstarTile], request:PathRequest) : List[AstarTile]
}

0 comments on commit 872a6da

Please sign in to comment.