]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/SaberMonster.cs
colosseum is now crowded with monsters; made the SaberMonster moving code more robust...
[chaz/carfire] / CarFire / CarFire / CarFire / SaberMonster.cs
index f417d52c160a1470935d4249506789033955b705..69b42ca1a44f11d578af78fb4a92fcb5b52b5a9d 100644 (file)
@@ -42,6 +42,7 @@ namespace CarFire
         {\r
             mId = identifier;\r
             mMotion = new MovementManager(position);\r
+            mRetryInterval = 20 + (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
@@ -64,7 +65,7 @@ namespace CarFire
                 foreach (string pathPoint in idlePathPoints)\r
                 {\r
                     Point? point = Parse.Coordinates(pathPoint);\r
-                    if (point != null) mIdlePath.Add(point.Value);\r
+                    if (point != null) mWaypoints.Add(point.Value);\r
                 }\r
             }\r
 \r
@@ -80,35 +81,26 @@ namespace CarFire
         /// </summary>\r
         public void StartPacing()\r
         {\r
-            mState = AiState.Pacing;\r
+            if (mWaypoints.Count == 0) return;\r
 \r
-            if (mIdlePath.Count == 0) return;\r
+            mState = AiState.Pacing;\r
 \r
             // Determine the best (closest) waypoint to start at.\r
             // We may not be on the path, so we have to walk to get on it.\r
-            mIdlePathIndex = 0;\r
+            mWaypointIndex = 0;\r
             int closest = int.MaxValue;\r
-            for (int i = 0; i < mIdlePath.Count; i++)\r
+            for (int i = 0; i < mWaypoints.Count; i++)\r
             {\r
-                int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);\r
+                int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);\r
                 if (distance < closest)\r
                 {\r
-                    mIdlePathIndex = i;\r
+                    mWaypointIndex = i;\r
                     closest = distance;\r
                 }\r
             }\r
 \r
             // Find the path to get to the closest waypoint.\r
-            PathFinder pathFinder = new PathFinder(mGame.Grid);\r
-            mPath = new List<Point>(32);\r
-            mPath.Add(Coordinates);\r
-            List<Point> path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]);\r
-            if (path != null)\r
-            {\r
-                mPath.AddRange(path);\r
-                mPath.Add(mIdlePath[mIdlePathIndex]);\r
-            }\r
-            mPathIndex = 0;\r
+            ChartPath();\r
         }\r
 \r
         Direction GetDirectionToNextCell()\r
@@ -117,23 +109,14 @@ namespace CarFire
             {\r
                 // We're done with the current path, so find the path to\r
                 // the next waypoint... forever.\r
-                mIdlePathIndex++;\r
-                PathFinder pathFinder = new PathFinder(mGame.Grid);\r
-                mPath = new List<Point>(32);\r
-                mPath.Add(Coordinates);\r
-                List<Point> path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]);\r
-                if (path != null)\r
-                {\r
-                    mPath.AddRange(path);\r
-                    mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);\r
-                }\r
-                mPathIndex = 0;\r
+                mWaypointIndex++;\r
+                ChartPath();\r
             }\r
 \r
-            // We need to make sure out direction is set to the next cell\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 (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)\r
+            if (mPathIndex < mPath.Count && mPath[mPathIndex] == mMotion.Coordinates)\r
             {\r
                 mPathIndex++;\r
                 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);\r
@@ -142,6 +125,20 @@ namespace CarFire
             return mPathDirection;\r
         }\r
 \r
+        void ChartPath()\r
+        {\r
+            mPath = new List<Point>(32);\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
+\r
+            mPathIndex = 0;\r
+            if (mPathIndex < mPath.Count) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex]);\r
+            else mPathDirection = Direction.None;\r
+        }\r
+\r
 \r
         #region IMonster Members\r
 \r
@@ -165,7 +162,7 @@ namespace CarFire
         /// <param name="contentManager">The zaphnod.</param>\r
         public void LoadContent(ContentManager contentManager)\r
         {\r
-            mTexture = contentManager.Load<Texture2D>("menuItem");\r
+            mTexture = contentManager.Load<Texture2D>("default");\r
         }\r
 \r
         /// <summary>\r
@@ -176,9 +173,43 @@ namespace CarFire
         /// <param name="timeSpan"></param>\r
         public void Update(TimeSpan timeSpan)\r
         {\r
-            if (mState == AiState.Pacing)\r
+            switch (mState)\r
             {\r
-                mMotion.Update(timeSpan, GetDirectionToNextCell());\r
+                case AiState.Pacing:\r
+\r
+                    Direction direction = GetDirectionToNextCell();\r
+                    Point destination = MovementManager.GetNeighbor(Coordinates, direction);\r
+\r
+                    if (mGame.IsCellOpen(destination))\r
+                    {\r
+                        mMotion.Update(timeSpan, direction);\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
+                    }\r
+\r
+                    break;\r
+\r
+                case AiState.Standing:\r
+\r
+                    mMotion.Update(timeSpan);\r
+                    break;\r
             }\r
         }\r
 \r
@@ -241,13 +272,14 @@ namespace CarFire
         char mId;\r
         MovementManager mMotion;\r
 \r
-        List<Point> mIdlePath = new List<Point>();      // List of waypoints that we got from the map.\r
-        int mIdlePathIndex;                             // Index to the waypoint we're heading for.\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
+        int mRetryInterval;\r
 \r
         AiState mState;                 // What is the monster doing?\r
 \r
This page took 0.022803 seconds and 4 git commands to generate.