\r
namespace CarFire\r
{\r
+ /*\r
/// <summary>\r
/// A type for the states of an artificually intelligent entity.\r
/// </summary>\r
Fighting,\r
Retreating\r
}\r
-\r
+ */\r
\r
/// <summary>\r
/// An example monster. This can serve as a starting place for\r
{\r
mId = identifier;\r
mMotion = new MovementManager(position);\r
- mRetryInterval = 20 + (position.X * 25789 + position.Y * 259) % 30;\r
+ mRetryInterval = 2 + (position.X * 25789 + position.Y * 259) % 30;\r
\r
// We need to keep the game reference in order to get the grid when we\r
// need to find paths.\r
if (point != null) mWaypoints.Add(point.Value);\r
}\r
}\r
+ mPath = new List<Point>();\r
\r
// Start doing something...\r
StartPacing();\r
}\r
+ public void DefaultAction()\r
+ {\r
\r
+ }\r
+ public void Chasing(Point Chase)\r
+ {\r
+\r
+ }\r
\r
/// <summary>\r
/// Call this to switch the monster AI state to pacing and set up\r
\r
Direction GetDirectionToNextCell()\r
{\r
- if (mPathIndex >= mPath.Count)\r
+ if (mPath != null)\r
{\r
- // We're done with the current path, so find the path to\r
- // the next waypoint... forever.\r
- mWaypointIndex++;\r
- ChartPath();\r
- }\r
+ if (mPathIndex >= mPath.Count)\r
+ {\r
+ // We're done with the current path, so find the path to\r
+ // the next waypoint... forever.\r
+ mWaypointIndex++;\r
+ ChartPath();\r
+ }\r
\r
- // We need to make sure our direction is set to the next cell\r
- // we want to be. If our current coordinates match that, we need\r
- // to change our direction to get to the next cell.\r
- if (mPathIndex < mPath.Count && mPath[mPathIndex] == mMotion.Coordinates)\r
- {\r
- mPathIndex++;\r
- mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);\r
- }\r
+ // We need to make sure our direction is set to the next cell\r
+ // we want to be. If our current coordinates match that, we need\r
+ // to change our direction to get to the next cell.\r
+ else if (mPath[mPathIndex] == mMotion.Coordinates)\r
+ {\r
+ mPathIndex++;\r
+ mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);\r
+ }\r
\r
- return mPathDirection;\r
+ return mPathDirection;\r
+ }\r
+ else return Direction.None;\r
}\r
\r
void ChartPath()\r
{\r
- mPath = new List<Point>(32);\r
+ if (mPathSearch == null)\r
+ {\r
+ Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];\r
+ PathFinder pathFinder = new PathFinder(mGame.Grid);\r
+ Point? nearby = pathFinder.GetNearbyOpenCell(waypoint);\r
+ if (nearby != null) mPathSearch = pathFinder.GetPathAsync(mMotion.Coordinates, nearby.Value);\r
\r
- Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];\r
- PathFinder pathFinder = new PathFinder(mGame.Grid);\r
- List<Point> path = pathFinder.GetPath(mMotion.Coordinates, waypoint);\r
- if (path != null) mPath.AddRange(path);\r
+ mPathIndex = 0;\r
+ mPath = null;\r
+ }\r
+ else if (mPathSearch.IsCompleted)\r
+ {\r
+ mPath = (List<Point>)mPathSearch.Path;\r
+ mPathSearch = null;\r
\r
- mPathIndex = 0;\r
- if (mPathIndex < mPath.Count) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex]);\r
- else mPathDirection = Direction.None;\r
+ if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]);\r
+ }\r
}\r
\r
\r
}\r
else\r
{\r
- if (mGame.CurrentFrameNumber % mRetryInterval == 0)\r
- {\r
- // Something is in our way, so let's chart a new course.\r
- ChartPath();\r
-\r
- direction = GetDirectionToNextCell();\r
- /*if (direction == Direction.None)\r
- {\r
- // If we still can't chart a course, just stand there\r
- // and try to chart again later.\r
- mState = AiState.Standing;\r
- }*/\r
-\r
- mMotion.Update(timeSpan, direction);\r
- }\r
- else mMotion.Update(timeSpan);\r
+ if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath();\r
+ mMotion.Update(timeSpan);\r
}\r
\r
break;\r
List<Point> mWaypoints = new List<Point>(); // List of waypoints that we got from the map.\r
int mWaypointIndex; // Index to the waypoint we're heading for.\r
\r
- List<Point> mPath; // List of cells in the path between the position between where\r
- // we started and the waypoint we're heading for.\r
- int mPathIndex; // Index to the cell we're heading for.\r
- Direction mPathDirection; // The direction between our current position and the place we're going.\r
+ List<Point> mPath; // List of cells in the path between the position between where\r
+ // we started and the waypoint we're heading for.\r
+ int mPathIndex; // Index to the cell we're heading for.\r
+ Direction mPathDirection; // The direction between our current position and the place we're going.\r
int mRetryInterval;\r
+ PathFinder.AsyncTask mPathSearch; // If a path search is in progress, this is the task object.\r
\r
- AiState mState; // What is the monster doing?\r
+ AiState mState; // What is the monster doing?\r
\r
- Texture2D mTexture; // Obvious.\r
+ Texture2D mTexture; // Obvious.\r
\r
#endregion\r
}\r