2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
13 /// A type for the states of an artificually intelligent entity.
27 /// An example monster. This can serve as a starting place for
28 /// creating other monsters. This one just follows a path.
30 public class SaberMonster : IMonster
35 /// Construct this type of monster. This constructor is called
36 /// by the map when the game requests entities.
38 /// <param name="identifier">The single character ID.</param>
39 /// <param name="position">The initial position on the map.</param>
40 /// <param name="info">More parameters.</param>
41 /// <param name="game">The game object reference.</param>
42 public SaberMonster(char identifier, Point position, Dictionary<string, string> info, Game game)
45 mMotion = new MovementManager(position);
46 mRetryInterval = 2 + (position.X * 25789 + position.Y * 259) % 30;
48 // We need to keep the game reference in order to get the grid when we
49 // need to find paths.
52 // Get the speed of the monster. If not set in the map, it defaults to
53 // whatever the default of MovementManager is... 1 I think.
55 if (info.TryGetValue("speed", out speedString))
57 int? speed = Parse.Integer(speedString);
58 if (speed != null) mMotion.Speed = speed.Value;
61 // Get the "idle path" coordinates loaded from the map.
63 if (info.TryGetValue("path", out idlePath))
65 string[] idlePathPoints = Parse.List(idlePath);
66 foreach (string pathPoint in idlePathPoints)
68 Point? point = Parse.Coordinates(pathPoint);
69 if (point != null) mWaypoints.Add(point.Value);
72 mPath = new List<Point>();
74 // Start doing something...
77 public void DefaultAction()
81 public void Chasing(Point Chase)
87 /// Call this to switch the monster AI state to pacing and set up
88 /// the initial paths. The monster will start following the path it
89 /// was defined with in the map file.
91 public void StartPacing()
93 if (mWaypoints.Count == 0) return;
95 mState = AiState.Pacing;
97 // Determine the best (closest) waypoint to start at.
98 // We may not be on the path, so we have to walk to get on it.
100 int closest = int.MaxValue;
101 for (int i = 0; i < mWaypoints.Count; i++)
103 int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);
104 if (distance < closest)
111 // Find the path to get to the closest waypoint.
115 Direction GetDirectionToNextCell()
119 if (mPathIndex >= mPath.Count)
121 // We're done with the current path, so find the path to
122 // the next waypoint... forever.
127 // We need to make sure our direction is set to the next cell
128 // we want to be. If our current coordinates match that, we need
129 // to change our direction to get to the next cell.
130 else if (mPath[mPathIndex] == mMotion.Coordinates)
133 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
136 return mPathDirection;
138 else return Direction.None;
143 if (mPathSearch == null)
145 Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
146 PathFinder pathFinder = new PathFinder(mGame.Grid);
147 Point? nearby = pathFinder.GetNearbyOpenCell(waypoint);
148 if (nearby != null) mPathSearch = pathFinder.GetPathAsync(mMotion.Coordinates, nearby.Value);
153 else if (mPathSearch.IsCompleted)
155 mPath = (List<Point>)mPathSearch.Path;
158 if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]);
163 #region IMonster Members
166 /// I don't know what this is for.
170 get { throw new NotImplementedException(); }
176 #region ICharacter Members
179 /// Load the monster's content. This is called by the map when
180 /// the game requests the entities.
182 /// <param name="contentManager">The zaphnod.</param>
183 public void LoadContent(ContentManager contentManager)
185 mTexture = contentManager.Load<Texture2D>("default");
189 /// Update the monster's state. This should be called by the game
190 /// every "frame" (whenever the game is updating its state). In this
191 /// simple monster, all we need to do is update the motion manager.
193 /// <param name="timeSpan"></param>
194 public void Update(TimeSpan timeSpan)
200 Direction direction = GetDirectionToNextCell();
201 Point destination = MovementManager.GetNeighbor(Coordinates, direction);
203 if (mGame.IsCellOpen(destination))
205 mMotion.Update(timeSpan, direction);
209 if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath();
210 mMotion.Update(timeSpan);
215 case AiState.Standing:
217 mMotion.Update(timeSpan);
223 /// Draw the monster. We just ask the map for our screen position,
224 /// passing it the position which the motion manager keeps track of.
226 /// <param name="spriteBatch">The </param>
227 public void Draw(SpriteBatch spriteBatch)
229 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
230 spriteBatch.Draw(mTexture, position, Color.White);
234 /// A monster should keep track of its health. This one doesn't.
238 get { return this.health; }
243 /// This monster is invincible.
245 /// <param name="amount"></param>
246 public void causeDamageTo(int amount)
248 this.health -= amount;
251 public bool IsCollidable { get { return true; } }
254 /// Get the smoothed position.
256 public Vector2 Position { get { return mMotion.Position; } }
259 /// Get the grid coordinates.
261 public Point Coordinates {
262 get { return mMotion.Coordinates; }
263 set { mMotion = new MovementManager(value, mMotion.Speed); }
267 /// Get the entity identifier.
269 public char Identifier { get { return mId; } }
274 #region Private Variables
279 MovementManager mMotion;
281 List<Point> mWaypoints = new List<Point>(); // List of waypoints that we got from the map.
282 int mWaypointIndex; // Index to the waypoint we're heading for.
284 List<Point> mPath; // List of cells in the path between the position between where
285 // we started and the waypoint we're heading for.
286 int mPathIndex; // Index to the cell we're heading for.
287 Direction mPathDirection; // The direction between our current position and the place we're going.
289 PathFinder.AsyncTask mPathSearch; // If a path search is in progress, this is the task object.
291 AiState mState; // What is the monster doing?
293 Texture2D mTexture; // Obvious.