Skip to content

Commit

Permalink
Run the pathfinding algorithm on every iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
mhluska committed May 24, 2016
1 parent e85a6ca commit e21b6fc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions source/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,28 @@ module.exports = class Snake {
});
}

// TOOD(maros): If this is inefficient, react to a world food drop event
// instead.
_nextPositionAuto() {
if (!this._autoMove) {
return false;
}

if (this._path.empty()) {
// Find new target.
let start = Voxel.findOrCreate(this.position);
let path = Graph.dijkstra(start, node => node.type === 'food');
// Find new target.
let start = Voxel.findOrCreate(this.position);
let path = Graph.dijkstra(start, node => node.type === 'food');

if (!path) {
return false;
}
if (!path) {
return false;
}

path.shift();
path.shift();

if (path.length === 0) {
return false;
}
if (path.length === 0) {
return false;
}

if (this._path.empty() || path.length < this._path.size()) {
this._path = new Queue(path);
}

Expand Down

0 comments on commit e21b6fc

Please sign in to comment.