About Store Forum Documentation Contact



Post Reply 
Connecting objects to waypoints
Author Message
nomax5 Offline
Member

Post: #1
Connecting objects to waypoints
I'm trying to figure out what is done in the editor and what is done by code.

I'm running fiddling with the Esenthel RPG and there is a goblin which when the game starts does a little bit of wandering and if the player gets close it will run up and attack.

Suppose I wanted that Goblin to follow a series of waypoints
I create the waypoints in the editor then do I get the goblin to follow the waypoint by doing something in the editor or do I start coding?
03-16-2013 10:26 AM
Find all posts by this user Quote this message in a reply
gwald Offline
Member

Post: #2
RE: Connecting objects to waypoints
Way points are easy, add the way point on your world.

in draw() draw UID where the UID from the world way point

DrawWaypoint(UID(506788265, 1078651010, 2014472077, 3165799067));
// draw each waypoint

void DrawWaypoint(C UID &id)
{
if(Game.Waypoint *waypoint=Game.World.findWaypoint(id)) // if waypoint exists
{
waypoint.draw( ); // draw waypoint
Vec pos=waypoint.pos (Time.time()*3); // access waypoints position at 'Time.time()*3' position
pos.draw(RED); // draw the position as red dot
}
}

It's pretty straight forward.
Not sure what's on RPG

My Blog
http://www.esenthel.com/community/showthread.php?tid=6043

I hang out at Esenthel IRC Channel
http://webchat.freenode.net/?channels=#Esenthel
(This post was last modified: 03-16-2013 11:20 AM by gwald.)
03-16-2013 11:09 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #3
RE: Connecting objects to waypoints
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.
03-16-2013 11:15 AM
Find all posts by this user Quote this message in a reply
nomax5 Offline
Member

Post: #4
RE: Connecting objects to waypoints
Right you've answered my question perfectly, I add objects to the world then reference them in code.

Thanks gwald and Tottel your answers are fantastic and really helpful.
03-16-2013 11:38 AM
Find all posts by this user Quote this message in a reply
Post Reply