]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/SaberMonster.cs
git-svn-id: https://bd85.net/svn/cs3505_group@168 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / SaberMonster.cs
index c2932eed05f96802579e167bc08da82dc121b0e6..8d66efd1a64b12affd3fa48d2e5907a75b332b97 100644 (file)
@@ -8,6 +8,7 @@ using Microsoft.Xna.Framework.Graphics;
 \r
 namespace CarFire\r
 {\r
+    /*\r
     /// <summary>\r
     /// A type for the states of an artificually intelligent entity.\r
     /// </summary>\r
@@ -20,7 +21,7 @@ namespace CarFire
         Fighting,\r
         Retreating\r
     }\r
-\r
+    */\r
 \r
     /// <summary>\r
     /// An example monster.  This can serve as a starting place for\r
@@ -28,6 +29,8 @@ namespace CarFire
     /// </summary>\r
     public class SaberMonster : IMonster\r
     {\r
+        //starting health\r
+        int health = 100;\r
         /// <summary>\r
         /// Construct this type of monster.  This constructor is called\r
         /// by the map when the game requests entities.\r
@@ -40,6 +43,7 @@ namespace CarFire
         {\r
             mId = identifier;\r
             mMotion = new MovementManager(position);\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
@@ -62,14 +66,22 @@ 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
+            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
@@ -78,58 +90,73 @@ 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
-            mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]));\r
-            mPath.Add(mIdlePath[mIdlePathIndex]);\r
-            mPathIndex = 0;\r
+            ChartPath();\r
         }\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
-                mIdlePathIndex++;\r
-                PathFinder pathFinder = new PathFinder(mGame.Grid);\r
-                mPath = new List<Point>(32);\r
-                mPath.Add(Coordinates);\r
-                mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]));\r
-                mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);\r
-                mPathIndex = 0;\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
+                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
             }\r
+            else return Direction.None;\r
+        }\r
 \r
-            // We need to make sure out 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
+        void ChartPath()\r
+        {\r
+            if (mPathSearch == null)\r
             {\r
-                mPathIndex++;\r
-                mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);\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
+                mPathIndex = 0;\r
+                mPath = null;\r
             }\r
+            else if (mPathSearch.IsCompleted)\r
+            {\r
+                mPath = (List<Point>)mPathSearch.Path;\r
+                mPathSearch = null;\r
 \r
-            return mPathDirection;\r
+                if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]);\r
+            }\r
         }\r
 \r
 \r
@@ -155,7 +182,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
@@ -166,9 +193,29 @@ 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) ChartPath();\r
+                        mMotion.Update(timeSpan);\r
+                    }\r
+\r
+                    break;\r
+\r
+                case AiState.Standing:\r
+\r
+                    mMotion.Update(timeSpan);\r
+                    break;\r
             }\r
         }\r
 \r
@@ -188,7 +235,8 @@ namespace CarFire
         /// </summary>\r
         public int Health\r
         {\r
-            get { throw new NotImplementedException(); }\r
+            get { return this.health; }\r
+            \r
         }\r
 \r
         /// <summary>\r
@@ -197,9 +245,11 @@ namespace CarFire
         /// <param name="amount"></param>\r
         public void causeDamageTo(int amount)\r
         {\r
-            throw new NotImplementedException();\r
+            this.health -= amount;\r
         }\r
 \r
+        public bool IsCollidable { get { return true; } }\r
+\r
         /// <summary>\r
         /// Get the smoothed position.\r
         /// </summary>\r
@@ -208,7 +258,15 @@ namespace CarFire
         /// <summary>\r
         /// Get the grid coordinates.\r
         /// </summary>\r
-        public Point Coordinates { get { return mMotion.Coordinates; } }\r
+        public Point Coordinates {\r
+            get { return mMotion.Coordinates; }\r
+            set { mMotion = new MovementManager(value, mMotion.Speed); }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get the entity identifier.\r
+        /// </summary>\r
+        public char Identifier { get { return mId; } }\r
 \r
         #endregion\r
 \r
@@ -220,17 +278,19 @@ 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
+        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
This page took 0.042134 seconds and 4 git commands to generate.