Skip to content

Instantly share code, notes, and snippets.

@thetooth
Created June 3, 2014 04:31
Show Gist options
  • Select an option

  • Save thetooth/2efe61ab9e16afee62dc to your computer and use it in GitHub Desktop.

Select an option

Save thetooth/2efe61ab9e16afee62dc to your computer and use it in GitHub Desktop.
void Path::Follow(AI &ai){
float nx, ny;
if (!ai.casual && index < nodeList.size() && index < 4){
auto node = (int)nodeList[index];
ny = node / solver->mapWidth;
nx = node - ny * solver->mapWidth;
nx *= PATH_DIVISOR;
ny *= PATH_DIVISOR;
}else{
ai.casual = true;
nx = target.x;
ny = target.y;
}
float dx = nx - ai.pos.x;
float dy = ny - ai.pos.y;
auto attack = atan2(dy, dx);
ai.vel.x = cos(attack) * ai.speed;
ai.vel.y = sin(attack) * ai.speed;
if (std::abs(dx) < 1.0 && std::abs(dy) < 1.0){
ai.vel.x = 0.0; ai.vel.y = 0.0;
if (!ai.casual){
ai.pos.x = nx; ai.pos.y = ny;
index++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment