我是靠谱客的博主 开朗豆芽,最近开发中收集的这篇文章主要介绍cocos creator 数组_cocos creator二维数组A*寻路,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

寻路的步类

var AStarStep = cc.Class({

properties: () => ({

g: {

default: 0,

type: cc.Integer

},

h: {

default: 0,

type: cc.Integer

},

f: {

get: function() {

return this.g + this.h;

}

},

position: {

default: new cc.Vec2()

},

last: {

default: null,

type: AStarStep,

serializable: false

}

}),

ctor: function () {

if (arguments.length > 0 && arguments[0] instanceof cc.Vec2) {

this.position = arguments[0];

}

},

equalTo(other) {

if (other instanceof AStarStep) {

return this.position.equals(other.position);

}

return false;

},

toString: function () {

return '(' + 'position: ' + this.position + ' g: ' + this.g + ' h: ' + this.h + ' f: ' + this.f + ')';

}

});

module.exports = AStarStep;

寻路类

var AStarStep = require('AStarStep');

cc.Class({

extends: cc.Component,

properties: {

speed : 0.2,

},

// use this for initialization

init: function (player,moveType,location,speed) {

this.speed = speed?speed:0.2;

this.player = player;//玩家

this.moveType = moveType;//是否打开八方向寻路

this.paths = [];

let targetTilePosition = location;

if (targetTilePosition.x < 0 || targetTilePosition.x >= cc.data.w ||

targetTilePosition.y < 0 || targetTilePosition.y >= cc.data.h ||

cc.data.map[targetTilePosition.x][targetTilePosition.y] >= 3) {

cc.log("超出边界");

return true;

}

let playerPosition = this.player.getPosition();

// cc.log('player position: ' + playerPosition);

let playerTilePosition = cc.data.mapPosistion(playerPosition);

// cc.log('player tile position: ' + playerTilePosition);

let sequence = [];

this._move(playerTilePosition, targetTilePosition, sequence);

if (sequence.length < 1) {

cc.log("超出边界");

return true;

}

this.player.runAction(cc.sequence(sequence));

return false;

},

isRoad: function () {

this.moveType = true;//是否打开八方向寻路

this.paths = [];

let targetTilePosition = [];

for (let i = 0; i < cc.data.road.length; i++) {

let road = cc.p(cc.data.road[i].x,cc.data.road[i].y);

targetTilePosition.push(road);

if (targetTilePosition[i].x < 0 || targetTilePosition[i].x >= cc.data.w ||

targetTilePosition[i].y < 0 || targetTilePosition[i].y >= cc.data.h ||

cc.data.map[targetTilePosition[i].x][targetTilePosition[i].y] >= 3) {

cc.log("超出边界");

return true;

}

}

let playerTilePosition = cc.p(cc.data.monsterStar.x,cc.data.monsterStar.y);

let sequence = [];

if (this._move(playerTilePosition, targetTilePosition[0], sequence)) {

return true;

}

for (let j = 1; j < targetTilePosition.length; j++) {

if (this._move(targetTilePosition[j-1], targetTilePosition[j], sequence)) {

return true;

}

}

return false;

},

_move: function(start, finish,sequence) {

if (this.player) {

this.player.stopAllActions();

}

this.paths = this.moveToward(start, finish);

if (this.paths.length < 1) {

cc.log('cannot find path');

return true;

}

cc.log(this.paths)

for (let i = 0; i < this.paths.length; ++i) {

let actionPosition = this.paths[i];

actionPosition.x = actionPosition.x * cc.data.interval + cc.data.interval / 2;

actionPosition.y = actionPosition.y * cc.data.interval + cc.data.interval / 2;

sequence.push(cc.moveTo(this.speed, actionPosition));

}

return false;

},

//判断坐标位于数组中的位置

_indexOfStepArray: function(value, stepArray) {

for (let i = 0; i < stepArray.length; ++i) {

if (value.equals(stepArray[i].position)) {

return i;

}

}

return -1;

},

//向数组中插入元素

_insertToOpen: function(step) {

let stepF = step.f;

let length = this._open.length;

let i = 0;

for (; i < length; ++i) {

if (stepF <= this._open[i].f) {

break;

}

}

this._open.splice(i, 0, step);

},

moveToward: function(start, finish) {

this._closed = [];

this._open = [];

let paths = [];

this._open.push(new AStarStep(start));

let pathFound = false;

do {

let currentStep = this._open.shift();

this._closed.push(currentStep);

if (currentStep.position.equals(finish)) {

pathFound = true;

let tmpStep = currentStep;

do {

paths.unshift(tmpStep.position);

tmpStep = tmpStep.last;

} while (tmpStep !== null);

this._open = [];

this._closed = [];

break;

}

let borderPositions = this._borderMovablePoints(currentStep.position);

for (let i = 0; i < borderPositions.length; ++i) {

let borderPosition = borderPositions[i];

if (this._indexOfStepArray(borderPosition, this._closed) != -1) {

borderPositions.splice(i, 1);

i--;

continue;

}

let step = new AStarStep(borderPosition);

let moveCost = this._costToMove(borderPosition, finish);

let index = this._indexOfStepArray(borderPosition, this._open);

if (index == -1) {

step.last = currentStep;

step.g = currentStep.g + moveCost;

let distancePoint = borderPosition.sub(finish);

step.h = Math.abs(distancePoint.x) + Math.abs(distancePoint.y);

this._insertToOpen(step);

} else {

step = this._open[index];

if (currentStep.g + moveCost < step.g) {

step.g = currentStep.g + moveCost;

this._open.splice(index, 1);

this._insertToOpen(step);

}

}

}

} while (this._open.length > 0);

return paths;

},

_costToMove(positionLeft, positionRight) {

if (this.moveType) {

return (positionLeft.x != positionRight.x) && (positionLeft.y != positionRight.y) ? 14 : 10;

} else {

return 1;

}

},

_borderMovablePoints: function(position) {

var results = [];

let hasTop = false;

let hasBottom = false;

let hasLeft = false;

let hasRight = false;

// top

let top = cc.p(position.x, position.y - 1);

if (cc.data.map[top.x][top.y] < 3) {

results.push(top);

hasTop = true;

}

// bottom

let bottom = cc.p(position.x, position.y + 1);

if (cc.data.map[bottom.x][bottom.y] < 3) {

results.push(bottom);

hasBottom = true;

}

// left

let left = cc.p(position.x - 1, position.y);

if (cc.data.map[left.x][left.y] < 3) {

results.push(left);

hasLeft = true;

}

// right

let right = cc.p(position.x + 1, position.y);

if (cc.data.map[right.x][right.y] < 3) {

results.push(right);

hasRight = true;

}

if (this.moveType) {

// Top Left

let topLeft = cc.p(position.x - 1, position.y - 1);

if (hasTop && hasLeft) {

if (cc.data.map[topLeft.x][topLeft.y] < 3) {

results.push(topLeft);

}

}

// Top Right

let topRight = cc.p(position.x + 1, position.y - 1);

if (hasTop && hasRight) {

if (cc.data.map[topRight.x][topRight.y] < 3) {

results.push(topRight);

}

}

// Bottom Left

let bottomLeft = cc.p(position.x - 1, position.y + 1);

if (hasBottom && hasLeft) {

if (cc.data.map[bottomLeft.x][bottomLeft.y] < 3) {

results.push(bottomLeft);

}

}

// Bottom Right

let bottomRight = cc.p(position.x + 1, position.y + 1);

if (hasBottom && hasRight) {

if (cc.data.map[bottomRight.x][bottomRight.y] < 3) {

results.push(bottomRight);

}

}

}

return results;

}

// called every frame, uncomment this function to activate update callback

// update: function (dt) {

// },

});

最后

以上就是开朗豆芽为你收集整理的cocos creator 数组_cocos creator二维数组A*寻路的全部内容,希望文章能够帮你解决cocos creator 数组_cocos creator二维数组A*寻路所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部