X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;ds=inline;f=CarFire%2FCarFire%2FCarFire%2FSaberMonster.cs;h=8d66efd1a64b12affd3fa48d2e5907a75b332b97;hb=236bc590ff21370c1139a8c01ff35f7b30af743d;hp=f417d52c160a1470935d4249506789033955b705;hpb=08f41ef45f3c41ca6302150bc6d5270c8e7143db;p=chaz%2Fcarfire
diff --git a/CarFire/CarFire/CarFire/SaberMonster.cs b/CarFire/CarFire/CarFire/SaberMonster.cs
index f417d52..8d66efd 100644
--- a/CarFire/CarFire/CarFire/SaberMonster.cs
+++ b/CarFire/CarFire/CarFire/SaberMonster.cs
@@ -8,6 +8,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace CarFire
{
+ /*
///
/// A type for the states of an artificually intelligent entity.
///
@@ -20,7 +21,7 @@ namespace CarFire
Fighting,
Retreating
}
-
+ */
///
/// An example monster. This can serve as a starting place for
@@ -42,6 +43,7 @@ namespace CarFire
{
mId = identifier;
mMotion = new MovementManager(position);
+ mRetryInterval = 2 + (position.X * 25789 + position.Y * 259) % 30;
// We need to keep the game reference in order to get the grid when we
// need to find paths.
@@ -64,14 +66,22 @@ namespace CarFire
foreach (string pathPoint in idlePathPoints)
{
Point? point = Parse.Coordinates(pathPoint);
- if (point != null) mIdlePath.Add(point.Value);
+ if (point != null) mWaypoints.Add(point.Value);
}
}
+ mPath = new List();
// Start doing something...
StartPacing();
}
+ public void DefaultAction()
+ {
+ }
+ public void Chasing(Point Chase)
+ {
+
+ }
///
/// Call this to switch the monster AI state to pacing and set up
@@ -80,66 +90,73 @@ namespace CarFire
///
public void StartPacing()
{
- mState = AiState.Pacing;
+ if (mWaypoints.Count == 0) return;
- if (mIdlePath.Count == 0) return;
+ mState = AiState.Pacing;
// Determine the best (closest) waypoint to start at.
// We may not be on the path, so we have to walk to get on it.
- mIdlePathIndex = 0;
+ mWaypointIndex = 0;
int closest = int.MaxValue;
- for (int i = 0; i < mIdlePath.Count; i++)
+ for (int i = 0; i < mWaypoints.Count; i++)
{
- int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
+ int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);
if (distance < closest)
{
- mIdlePathIndex = i;
+ mWaypointIndex = i;
closest = distance;
}
}
// Find the path to get to the closest waypoint.
- PathFinder pathFinder = new PathFinder(mGame.Grid);
- mPath = new List(32);
- mPath.Add(Coordinates);
- List path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]);
- if (path != null)
- {
- mPath.AddRange(path);
- mPath.Add(mIdlePath[mIdlePathIndex]);
- }
- mPathIndex = 0;
+ ChartPath();
}
Direction GetDirectionToNextCell()
{
- if (mPathIndex >= mPath.Count)
+ if (mPath != null)
{
- // We're done with the current path, so find the path to
- // the next waypoint... forever.
- mIdlePathIndex++;
- PathFinder pathFinder = new PathFinder(mGame.Grid);
- mPath = new List(32);
- mPath.Add(Coordinates);
- List path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]);
- if (path != null)
+ if (mPathIndex >= mPath.Count)
{
- mPath.AddRange(path);
- mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);
+ // We're done with the current path, so find the path to
+ // the next waypoint... forever.
+ mWaypointIndex++;
+ ChartPath();
}
- mPathIndex = 0;
+
+ // We need to make sure our direction is set to the next cell
+ // we want to be. If our current coordinates match that, we need
+ // to change our direction to get to the next cell.
+ else if (mPath[mPathIndex] == mMotion.Coordinates)
+ {
+ mPathIndex++;
+ mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
+ }
+
+ return mPathDirection;
}
+ else return Direction.None;
+ }
- // We need to make sure out direction is set to the next cell
- // we want to be. If our current coordinates match that, we need
- // to change our direction to get to the next cell.
- if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
+ void ChartPath()
+ {
+ if (mPathSearch == null)
{
- mPathIndex++;
- mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
+ Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
+ PathFinder pathFinder = new PathFinder(mGame.Grid);
+ Point? nearby = pathFinder.GetNearbyOpenCell(waypoint);
+ if (nearby != null) mPathSearch = pathFinder.GetPathAsync(mMotion.Coordinates, nearby.Value);
+
+ mPathIndex = 0;
+ mPath = null;
}
+ else if (mPathSearch.IsCompleted)
+ {
+ mPath = (List)mPathSearch.Path;
+ mPathSearch = null;
- return mPathDirection;
+ if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]);
+ }
}
@@ -165,7 +182,7 @@ namespace CarFire
/// The zaphnod.
public void LoadContent(ContentManager contentManager)
{
- mTexture = contentManager.Load("menuItem");
+ mTexture = contentManager.Load("default");
}
///
@@ -176,9 +193,29 @@ namespace CarFire
///
public void Update(TimeSpan timeSpan)
{
- if (mState == AiState.Pacing)
+ switch (mState)
{
- mMotion.Update(timeSpan, GetDirectionToNextCell());
+ case AiState.Pacing:
+
+ Direction direction = GetDirectionToNextCell();
+ Point destination = MovementManager.GetNeighbor(Coordinates, direction);
+
+ if (mGame.IsCellOpen(destination))
+ {
+ mMotion.Update(timeSpan, direction);
+ }
+ else
+ {
+ if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath();
+ mMotion.Update(timeSpan);
+ }
+
+ break;
+
+ case AiState.Standing:
+
+ mMotion.Update(timeSpan);
+ break;
}
}
@@ -241,17 +278,19 @@ namespace CarFire
char mId;
MovementManager mMotion;
- List mIdlePath = new List(); // List of waypoints that we got from the map.
- int mIdlePathIndex; // Index to the waypoint we're heading for.
+ List mWaypoints = new List(); // List of waypoints that we got from the map.
+ int mWaypointIndex; // Index to the waypoint we're heading for.
- List mPath; // List of cells in the path between the position between where
- // we started and the waypoint we're heading for.
- int mPathIndex; // Index to the cell we're heading for.
- Direction mPathDirection; // The direction between our current position and the place we're going.
+ List mPath; // List of cells in the path between the position between where
+ // we started and the waypoint we're heading for.
+ int mPathIndex; // Index to the cell we're heading for.
+ Direction mPathDirection; // The direction between our current position and the place we're going.
+ int mRetryInterval;
+ PathFinder.AsyncTask mPathSearch; // If a path search is in progress, this is the task object.
- AiState mState; // What is the monster doing?
+ AiState mState; // What is the monster doing?
- Texture2D mTexture; // Obvious.
+ Texture2D mTexture; // Obvious.
#endregion
}