In the world editor, I placed some waypoints and named them "EnemyPath".
Then in my code, I retrieve the waypoints from the world and spawn the enemy CHR on the first waypoint, like so:
Code:
Bool Enemy.Init()
{
// Spawn on the first waypoint
Vec pos;
if(Game.Waypoint *waypoint = Game.World.findWaypoint(UID(1699057006, 1192779511, 921400724, 2956126592))) // if waypoint exists
{
pos = waypoint->points(0).pos;
}
Game.ObjParamsPtr temp=UID(3695286120, 1234939607, 3884290705, 2119083346); // get enemy object parameters
// create new object at position and give objects default scaling.
// This object is automatically added to the Enemies container and is updated and drawn by the world manager.
Game.World.objCreate(*temp, Matrix(temp->scale(), pos));
return true;
}
Now, the enemy has to run along the waypoint, which I do like so:
Code:
Bool Enemy.update()
{
....
if (m_IsActivated) MoveToWaypoint();
....
}
The MoveToWapoint() simply checks what the next waypoint is that the CHR is running to, if you're not at the waypoint yet, just keep running to the waypoint. If the CHR is at the waypoint, increment the goalwaypoint.
Code:
void Enemy.MoveToWaypoint()
{
if(Game.Waypoint *waypoint = Game.World.findWaypoint(UID(1699057006, 1192779511, 921400724, 2956126592))) // if waypoint exists
{
Vec pos = waypoint->points(m_CurrentGoal).pos; // Get the current waypoint goal
actionMoveTo(pos);
Flt distanceFromWaypoint = Vec(this->pos() - pos).length(); // Check the distance between the Enemy and the waypoint
if (!m_IsAtWaypoint && distanceFromWaypoint < m_MinDistFromGoal)
{
++m_CurrentGoal;
m_IsAtWaypoint = true;
}
else if (distanceFromWaypoint > m_MinDistFromGoal) m_IsAtWaypoint = false;
}
}
Do note that I'm not using a looping system. Once the enemies reach the end, I simply remove them. If you want them to loop, you'd reset the goalwaypoint back to 0 once you reach the last node.