Enemy following nodes in a tile map (game programming)
<p>I was looking for a method to make an enemy sprite move along a set of predefined nodes in a tile map. Map is 20(w) x 15(h). I am generating the node values </p>

<pre><code>int tx, ty;
tx = x/32;
ty = y/32;
</code></pre>

<p>where x and y are actual co-ords. I've a std::list which stores the lists of nodes to follow. The nodes are such that between any two of them, only one co-ordinate changes. 
I'm using the following method to navigate, but it doesn't work correct, please suggest some alternatives:</p>

<pre><code>index1 = ty*MAP_WIDTH + tx;
 if (!moving &amp;&amp; nextindex == 0 &amp;&amp; index1 != path.front())
 {
     nextindex = path.front();
     py = nextindex / 20;
     px = nextindex - (py*20);
     py *= 32;
     px *= 32;
     moving = true;
 }

 if (moving)
 {
     if (x &gt; px)
     {
         x -= 4;
         dir = left;
     }
     else if (x &lt; px)
     {
         x += 4;
         dir = right;
     }
     else if (y &gt; py)
     {
         y -= 4;
         dir = up;
     }
     else if (y &lt; py)
     {
         y += 4;
         dir = down;
     }
     else if ( index1 == nextindex )
     {
         logger &lt;&lt; "node reached\n";
         dir = null;
         moving = false;
         path.pop_back();
     }
 }
</code></pre>

<p>Please help, its kind of, urgent.</p>
