melanke / CascadeCanvas

A framework for canvas that follows the Javascript patterns you already know
6 stars 0 forks source link

pathfinder plugin #22

Open melanke opened 10 years ago

melanke commented 10 years ago

Generate a obstacle map with the obstacle elements

var objRepresentingTheMap = elementListOfObstacles.createObstaclesMap(widthOfTile, heightOfTile); 

default tile W and H is 16x16, the tile size is important to define the size of the elements that will walk the path generate the map the less possible, only when the level changes

find the path with your map, the origin and destination of the path

var objRepresentingThePath = objRepresentingTheMap.pathFind(originX, originY, destinationX, destinationY);

make the element walk that path

elementWhoWalksThePath.walkThePath(objRepresentingThePath, speed);

and we will have a default speed

example:

var obsMap = CC("Obstacles").createObstaclesMap(32, 32); 

var path = obsMap.pathFind(64, 64, 320, 240);

CC("#Element").eg(0).walkThePath(path, 10);

OR, if you will use the path only once

elementWhoWalksThePath.walkAPathTo(objRepresentingTheMap, 320, 240, speed);

example2:

var obsMap = CC("Obstacles").createObstaclesMap(32, 32); 

CC("#Element").eg(0).walkAPathTo(obsMap, 320, 240, 10);

OR, if you need only one map simultaniously

elementListOfObstacles.createObstaclesMap(widthOfTile, heightOfTile);

we will store the last map in a singleton, so you dont have to worry carrying it

example3:

CC("Obstacles").createObstaclesMap(32, 32); 

CC("#Element").eg(0).walkAPathTo(320, 240); //using the default speed too
melanke commented 10 years ago

implementation: it will be a plugin of CascadeCanvas, an adapter of https://github.com/qiao/PathFinding.js

to createObstaclesMap we need to read the list of obstacles we need to know what obstacle is the top-left, and what is the bottom-right to save in x, y, w, h of the map the map will represent only this space (with a margin of 1 tile to each side) the tile size will be the one in the params or 16x16 for each obstacle: we read the x, y, w, h and put a marker in an matrix of tiles

to pathFind we need to read the map if the origin or destiny is out the box of the map we increment the empty map tiles save this x, y, w, h of the path ask qiao/PathFinding to make the path and save it

to walkThePath we need only to make the element to move to the positions of the path and if walkThePath is called again we need to abort the first and make the second one