X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FSaberMonster.cs;fp=CarFire%2FCarFire%2FCarFire%2FSaberMonster.cs;h=69b42ca1a44f11d578af78fb4a92fcb5b52b5a9d;hp=f417d52c160a1470935d4249506789033955b705;hb=122c062297acac44673e947b666c1d72cd23fb1b;hpb=198cb6056e93fecd69e65351ca8d0b34a077523f diff --git a/CarFire/CarFire/CarFire/SaberMonster.cs b/CarFire/CarFire/CarFire/SaberMonster.cs index f417d52..69b42ca 100644 --- a/CarFire/CarFire/CarFire/SaberMonster.cs +++ b/CarFire/CarFire/CarFire/SaberMonster.cs @@ -42,6 +42,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. @@ -64,7 +65,7 @@ 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); } } @@ -80,35 +81,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() @@ -117,23 +109,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]); @@ -142,6 +125,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 @@ -165,7 +162,7 @@ namespace CarFire /// The zaphnod. public void LoadContent(ContentManager contentManager) { - mTexture = contentManager.Load("menuItem"); + mTexture = contentManager.Load("default"); } /// @@ -176,9 +173,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; } } @@ -241,13 +272,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?