Skip to content

Commit

Permalink
pathfinding demo for issue DarthBurrit0#2
Browse files Browse the repository at this point in the history
  • Loading branch information
emkay committed May 12, 2013
1 parent 878c3c6 commit 559b0b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
39 changes: 35 additions & 4 deletions lib/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var domready = require('domready')
, $qs = document.querySelector.bind(document)
, iio = require('./vendor/iio-engine')
, user = require('./user-input')
, PF = require('pathfinding')

function resizeCanvas(c) {
c.width = window.innerWidth
Expand All @@ -24,8 +25,27 @@ domready(function(){

var hero
, sprites
, grid
, gridPf

// we should make 1 grid that io and PF can both use
gridPf = new PF.Grid(10, 10)
grid = io.addObj(new iio.ioGrid(0, 0, 10, 10, 64, 64)
.setStrokeStyle('black'))

// for testing pathfinding
var finder = new PF.AStarFinder({
allowDiagonal: true,
dontCrossCorners: true
})

gridPf.setWalkableAt(1, 1, false) // set the blocker in PF grid.
blocker = io.addObj(new iio.ioRect(new iio.ioVec(96,96), 64)
.setFillStyle('black')) // test blocker

sprites = new iio.ioSpriteMap('/images/sprites/hero.png', 64, 64, function(){
var blockerCell;

hero = new iio.ioRect(io.canvas.center, 64)
.createWithAnim(sprites.getSprite(0, 0),'still')
.enableKinematics()
Expand All @@ -35,6 +55,8 @@ domready(function(){
hero.addAnim(sprites.getSprite(4, 5), 'walk-up')
hero.addAnim(sprites.getSprite(0, 1), 'walk-down')

blockerCell = grid.getCellAt(blocker.right(), blocker.bottom())
console.log(blockerCell)
io.addObj(hero)
})

Expand Down Expand Up @@ -77,6 +99,19 @@ domready(function(){
}
})

function onTap(x, y){
console.log('tap', x, y)
var v = new iio.ioVec(x, y)
, toCell = grid.getCellAt(v)
, heroV = hero.pos
, fromCell = grid.getCellAt(heroV)

// grid is modified after you findPath so we clone it
var gridBackup = gridPf.clone()
var path = finder.findPath(fromCell.x, fromCell.y, toCell.x, toCell.y, gridBackup);
console.log('path: ', path)
}

// var interactive = true
// var stage = new PIXI.Stage(0x000000, interactive)
// var renderer = PIXI.autoDetectRenderer(620, 400)
Expand All @@ -97,10 +132,6 @@ domready(function(){
// })
})

function onTap(x, y){
console.log('tap', x, y)
}

function onHold(){
console.log('hold')
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"bunyan": "~0.21.1",
"supertest": "~0.6.0",
"rework": "~0.14.0",
"mousetrap": "0.0.1"
"mousetrap": "0.0.1",
"pathfinding": "~0.4.1"
},
"devDependencies": {
"mocha": "~1.4.0"
Expand Down

0 comments on commit 559b0b8

Please sign in to comment.