1 Star 0 Fork 1

tbs005/anima

forked from teleis/anima 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AstarPathStep 5.52 KB
一键复制 编辑 原始数据 按行查看 历史
qiuhd_2015 提交于 2017-09-20 19:21 . Update AstarPathStep
package {
import laya.display.Stage;
import laya.display.Sprite;
import laya.events.Event;
import laya.map.TiledMap;
import laya.maths.Rectangle;
import laya.utils.Browser;
import laya.utils.Handler;
import laya.utils.Stat;
import laya.webgl.WebGL;
import laya.map.MapLayer;
import laya.map.GridSprite;
import laya.maths.Point;
import PathFinding.core.Grid;
import PathFinding.core.Node;
import PathFinding.finders.AStarFinder;
import PathFinding.finders.JPFAlwaysMoveDiagonally;
import PathFinding.core.Heuristic;
public class LayaSample {
private var tiledMap:TiledMap;
private var tileWidth:int;
private var tileHeight:int;
public function LayaSample() {
// 不支持WebGL时自动切换至Canvas
Laya.init(Browser.width, Browser.height, WebGL);
Laya.stage.alignV = Stage.ALIGN_MIDDLE;
Laya.stage.alignH = Stage.ALIGN_CENTER;
Laya.stage.scaleMode = Stage.SCALE_SHOWALL;
Stat.show();
createMap();
}
//创建地图
private function createMap():void
{
//创建地图对象
tiledMap = new TiledMap();
//创建地图,适当的时候调用destroy销毁地图
tiledMap.createMap("res/map/map1.json", new Rectangle(0, 0, Browser.width, Browser.height), new Handler(this, completeHandler));
}
/**
* 地图加载完成的回调
*/
private function completeHandler(e:*=null):void
{
var blockLayer:MapLayer = this.tiledMap.getLayerByName("Block_Layer");
var mapData:Array = blockLayer._mapData;
console.log("Map length :" + mapData.length);
var objLayer:MapLayer = this.tiledMap.getLayerByName("Objects");
var startObj:GridSprite = this.tiledMap.getLayerObject("Objects", "StartPoint");
var startPoint:Point = new Laya.Point(startObj.x,startObj.y);
this.tileWidth = this.tiledMap.tileWidth;
this.tileHeight = this.tiledMap.tileHeight;
//start point
startPoint = tileCoordForPosition(startPoint);
console.log("StartPoint x:" + startPoint.x + ",y :" + startPoint.y);
var startScreenPos:Point = new Laya.Point();
blockLayer.getScreenPositionByTilePos(startPoint.x,startPoint.y,startScreenPos);
//console.log("StartPoint2 ==== >> X :" + startPoint.x + ",Y:" + startPoint.y);
var startSprite:Sprite = new Sprite();
startSprite.graphics.drawCircle(startScreenPos.x,startScreenPos.y,6,"#00ff00");
Laya.stage.addChild(startSprite);
console.log(tileHeight + ",h:" + tileHeight);
//target point
var targetObj:GridSprite = this.tiledMap.getLayerObject("Objects", "205");
var targetSprite:Sprite = new Sprite();
targetSprite.graphics.drawCircle(targetObj.x,targetObj.y,6,"#00ff00");
Laya.stage.addChild(targetSprite);
var targetPoint:Point = new Laya.Point(targetObj.x,targetObj.y);
targetPoint = tileCoordForPosition(targetPoint);
console.log("TargetPoint x:" + targetPoint.x + ",y:" + targetPoint.y);
var targetScreenPos:Point = new Laya.Point();
blockLayer.getScreenPositionByTilePos(targetPoint.x,targetPoint.y,targetScreenPos);
var targetSprite1:Sprite = new Sprite();
targetSprite1.graphics.drawCircle(targetScreenPos.x,targetScreenPos.y,6,"#00ff00");
Laya.stage.addChild(targetSprite1);
var map_x:int = tiledMap.numColumnsTile;
var map_y:int = tiledMap.numRowsTile;
console.log("map_x :" + map_x + ",map_y :" + map_y);
var mapGrid:Grid = new Grid(map_x,map_y);
var i:int, j:int,n:int;
for (i = 0; i < map_y; ++i)
{
for (j = 0; j < map_x; ++j)
{
var wallk:int = mapData[n];
var walkable:Boolean = wallk != 1 ? true : false;
mapGrid.setWalkableAt(j,i,walkable);
var node:Node = mapGrid.getNodeAt(j,i);
console.log("Node x:" + node.x + ",y:" + node.y + ",wallkable:" + walkable);
++n;
}
}
var finder:JPFAlwaysMoveDiagonally = new JPFAlwaysMoveDiagonally({allowDiagonal: true,dontCrossCorners: true,heuristic:Heuristic.manhattan});
var paths:Array = finder.findPath(startPoint.x,startPoint.y,targetPoint.x,targetPoint.y,mapGrid);
var pathPath:int = paths.length;
var pathPoint:Sprite;
console.log("Path length :" + paths.length);
for (var index:int = 0;index < pathPath;index ++) {
var point:Point = new Point(paths[index][0],paths[index][1]);
console.log("============= :" + paths[index][0] + ",y :" + paths[index][1]);
pathPoint = new Laya.Sprite();
var nodeScreenPos:Point = new Laya.Point();
blockLayer.getScreenPositionByTilePos(point.x,point.y,nodeScreenPos);
pathPoint.graphics.drawCircle(nodeScreenPos.x,nodeScreenPos.y,6,"#ff0000");
Laya.stage.addChild(pathPoint);
//console.log("X:" + point.x + ",y :" + point.y + ",index :" + index + ",ScreenPosX:" + nodeScreenPos.x + ",ScreenPosY :" + nodeScreenPos.y);
}
}
public function tileCoordForPosition(point:Point):Point
{
// Size mapSize = tiledMap->getMapSize();
// Size tileSize = tiledMap->getTileSize();
// int x = position.x / tileSize.width;
// int y = (mapSize.height*tileSize.height - position.y) / tileSize.height;
// return Vec2(x, y);
var x = Math.floor(point.x / this.tileWidth);
var y = Math.floor(point.y / this.tileHeight);
//var y = Math.floor((this.MAP_Y * this.TILE_HEIGHT - point.y) / this.TILE_HEIGHT);
return new Laya.Point(x,y);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/tbs005/anima.git
git@gitee.com:tbs005/anima.git
tbs005
anima
anima
master

搜索帮助