Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created May 17, 2012 17:04
Show Gist options
  • Save TinkerWorX/2720229 to your computer and use it in GitHub Desktop.
Save TinkerWorX/2720229 to your computer and use it in GitHub Desktop.
function update() {
var npc_speed = 3; //seconds it takes to go from one tile to the next
var npc_pos = 0; //position in array
var secs2 = 0; //seconds past since "starting walk"
var pos = 0; //position in the pdata[i][1]
for (var i in pdata) { //pdata array containing path tile sequence
var path_data = pdata[i];
var path = path_data[1];
var lastWalk = path_data[2];
secs = (login - lastWalk); //llogin & pdata[i][2] is time in seconds since epoch
path_length = path.length; //the array of tiles
if(secs > 100) { //only works after 100 seconds past from previous walk
secs2 = secs - 100; //seconds past since the 100 seconds past
npc_pos = Math.floor(secs2 / npc_speed); //seconds past divided by speed = position in the array (rounded down)
if(npc_pos == 0 || npc_pos == path_length) {
//delete from array list;
}
if(npc_pos >= path_length) { //once npc reaches end of path, start walking back to the start so reverse the position number downwards
npc_pos = path_length - (npc_pos - path_length);
}
pos = path[npc_pos]; //with npc_pos get the X:Y stored in pdata[i][0] [array position] = returns [4,6] as example
if(pos != null && npc_pos > 0 && npc_pos < path_length) { // if npc is visible on map
npc_array[0] = pos[0]; //tile X
npc_array[1] = pos[1]; //tile Y
}
if(secs2 >= ( (path_length*2) * npc_speed)) { //once npc is home
lastWalk = Math.floor(new Date().getTime() / 1000); // update timer to repeat the loop seconds since epoch
npc_pos = 0; //reset npc back to start position ready for next walk
}
}
login = Math.floor(new Date().getTime() / 1000); //seconds since epoch
//debug('result','Seconds: '+secs+'<br/>'+'Seconds Two: '+secs2+'<br/>Pos: '+pos[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment