X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FSaberMonster.cs;h=a2b5aae9f327ee0f6762e544ebb582d882b673c1;hb=a716edefa6148bb1847b7029356d610a1886821f;hp=ce5916987a472986505a8c5f2c6284c2f89f14b5;hpb=d167160264cd2c33de81a71039eddbb959c40bb2;p=chaz%2Fcarfire
diff --git a/CarFire/CarFire/CarFire/SaberMonster.cs b/CarFire/CarFire/CarFire/SaberMonster.cs
index ce59169..a2b5aae 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
@@ -28,6 +29,8 @@ namespace CarFire
///
public class SaberMonster : IMonster
{
+ //starting health
+ int health = 100;
///
/// Construct this type of monster. This constructor is called
/// by the map when the game requests entities.
@@ -40,6 +43,7 @@ namespace CarFire
{
mId = identifier;
mMotion = new MovementManager(position);
+ mRetryInterval = 20 + (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.
@@ -62,14 +66,21 @@ 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);
}
}
// 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
@@ -78,35 +89,26 @@ 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()
@@ -115,23 +117,14 @@ namespace CarFire
{
// 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)
- {
- mPath.AddRange(path);
- mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);
- }
- mPathIndex = 0;
+ mWaypointIndex++;
+ ChartPath();
}
- // We need to make sure out direction is set to the next cell
+ // 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.
- if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
+ if (mPathIndex < mPath.Count && mPath[mPathIndex] == mMotion.Coordinates)
{
mPathIndex++;
mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
@@ -140,6 +133,20 @@ namespace CarFire
return mPathDirection;
}
+ void ChartPath()
+ {
+ mPath = new List(32);
+
+ Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
+ PathFinder pathFinder = new PathFinder(mGame.Grid);
+ List path = pathFinder.GetPath(mMotion.Coordinates, waypoint);
+ if (path != null) mPath.AddRange(path);
+
+ mPathIndex = 0;
+ if (mPathIndex < mPath.Count) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex]);
+ else mPathDirection = Direction.None;
+ }
+
#region IMonster Members
@@ -163,7 +170,7 @@ namespace CarFire
/// The zaphnod.
public void LoadContent(ContentManager contentManager)
{
- mTexture = contentManager.Load("menuItem");
+ mTexture = contentManager.Load("default");
}
///
@@ -174,9 +181,43 @@ 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)
+ {
+ // Something is in our way, so let's chart a new course.
+ ChartPath();
+
+ direction = GetDirectionToNextCell();
+ /*if (direction == Direction.None)
+ {
+ // If we still can't chart a course, just stand there
+ // and try to chart again later.
+ mState = AiState.Standing;
+ }*/
+
+ mMotion.Update(timeSpan, direction);
+ }
+ else mMotion.Update(timeSpan);
+ }
+
+ break;
+
+ case AiState.Standing:
+
+ mMotion.Update(timeSpan);
+ break;
}
}
@@ -196,7 +237,8 @@ namespace CarFire
///
public int Health
{
- get { throw new NotImplementedException(); }
+ get { return this.health; }
+
}
///
@@ -205,9 +247,11 @@ namespace CarFire
///
public void causeDamageTo(int amount)
{
- throw new NotImplementedException();
+ this.health -= amount;
}
+ public bool IsCollidable { get { return true; } }
+
///
/// Get the smoothed position.
///
@@ -216,7 +260,15 @@ namespace CarFire
///
/// Get the grid coordinates.
///
- public Point Coordinates { get { return mMotion.Coordinates; } }
+ public Point Coordinates {
+ get { return mMotion.Coordinates; }
+ set { mMotion = new MovementManager(value, mMotion.Speed); }
+ }
+
+ ///
+ /// Get the entity identifier.
+ ///
+ public char Identifier { get { return mId; } }
#endregion
@@ -228,13 +280,14 @@ 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.
+ int mRetryInterval;
AiState mState; // What is the monster doing?