2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
12 /// A type for the states of an artificually intelligent entity.
26 /// An example monster. This can serve as a starting place for
27 /// creating other monsters. This one just follows a path.
29 public class SaberMonster2 : IMonster
34 /// Construct this type of monster. This constructor is called
35 /// by the map when the game requests entities.
37 /// <param name="identifier">The single character ID.</param>
38 /// <param name="position">The initial position on the map.</param>
39 /// <param name="info">More parameters.</param>
40 /// <param name="game">The game object reference.</param>
41 public SaberMonster2(char identifier, Point position, Dictionary<string, string> info, Game game)
44 mMotion = new MovementManager(position);
45 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 // Start doing something...
76 public void DefaultAction()
78 mState = AiState.Standing;
81 public void Chasing(Point Chase)
83 Chasing(Chase.X, Chase.Y);
87 /// Call this to switch the monster AI state to Chasing and set up
88 /// the initial paths. A path to .
90 public void Chasing(int X, int Y)
92 mState = AiState.Chasing;
94 // Determine the best (closest) waypoint to start at.
95 // We may not be on the path, so we have to walk to get on it.
98 int closest = int.MaxValue;
99 for (int i = 0; i < mIdlePath.Count; i++)
101 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
102 if (distance < closest)
110 SetWaypoint(new Point(X, Y));
115 /// Call this to switch the monster AI state to pacing and set up
116 /// the initial paths. The monster will start following the path it
117 /// was defined with in the map file.
119 public void StartPacing()
121 if (mWaypoints.Count == 0) return;
123 mState = AiState.Pacing;
125 // Determine the best (closest) waypoint to start at.
126 // We may not be on the path, so we have to walk to get on it.
128 int closest = int.MaxValue;
129 for (int i = 0; i < mWaypoints.Count; i++)
131 int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);
132 if (distance < closest)
139 // Find the path to get to the closest waypoint.
143 Direction GetDirectionToNextCell()
147 if (mPathIndex >= mPath.Count)
149 // We're done with the current path, so find the path to
150 // the next waypoint... forever.
161 mPathDirection = Direction.None;
166 // We need to make sure our direction is set to the next cell
167 // we want to be. If our current coordinates match that, we need
168 // to change our direction to get to the next cell.
169 else if (mPath[mPathIndex] == mMotion.Coordinates)
172 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
175 return mPathDirection;
177 else return Direction.None;
183 mWaypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
186 void SetWaypoint(Point point)
193 if (mPathSearch == null)
195 PathFinder pathFinder = new PathFinder(mGame.Grid);
196 Point? nearby = pathFinder.GetNearbyOpenCell(mWaypoint);
197 if (nearby != null) mPathSearch = pathFinder.GetPathAsync(mMotion.Coordinates, nearby.Value);
202 else if (mPathSearch.IsCompleted)
204 mPath = (List<Point>)mPathSearch.Path;
207 if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]);
212 #region IMonster Members
215 /// I don't know what this is for.
219 get { throw new NotImplementedException(); }
225 #region ICharacter Members
228 /// Load the monster's content. This is called by the map when
229 /// the game requests the entities.
231 /// <param name="contentManager">The zaphnod.</param>
232 public void LoadContent(ContentManager contentManager)
234 mTexture = contentManager.Load<Texture2D>("menuItem");
238 /// Update the monster's state. This should be called by the game
239 /// every "frame" (whenever the game is updating its state). In this
240 /// simple monster, all we need to do is update the motion manager.
242 /// <param name="timeSpan"></param>
243 public void Update(TimeSpan timeSpan)
246 int closestCharDist = 1000;
247 for (int c = 0; c < mGame.State.mCharacters.Length; c++)
249 if (mGame.State.mCharacters[c] != null && mGame.AIData.spaceVisible(Coordinates, mGame.State.mCharacters[c].Coordinates))
251 if (PathFinder.GetManhattanDistance(Coordinates, mGame.State.mCharacters[c].Coordinates) < closestCharDist)
253 closestCharDist = PathFinder.GetManhattanDistance(Coordinates, mGame.State.mCharacters[c].Coordinates);
259 if (mGame.State.mCharacters[closestChar] != null && mGame.AIData.spaceVisible(Coordinates, mGame.State.mCharacters[closestChar].Coordinates))
261 Chasing(mGame.State.mCharacters[0].Coordinates);
268 Direction direction = GetDirectionToNextCell();
269 Point destination = MovementManager.GetNeighbor(Coordinates, direction);
275 if (mGame.IsCellOpen(destination))
277 mMotion.Update(timeSpan, direction);
281 if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath();
282 mMotion.Update(timeSpan);
286 case AiState.Chasing:
288 if (mGame.IsCellOpen(destination))
290 mMotion.Update(timeSpan, direction);
294 if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath();
295 mMotion.Update(timeSpan);
299 case AiState.Standing:
300 mMotion.Update(timeSpan);
303 case AiState.Fighting:
304 case AiState.Retreating:
311 /// Draw the monster. We just ask the map for our screen position,
312 /// passing it the position which the motion manager keeps track of.
314 /// <param name="spriteBatch">The </param>
315 public void Draw(SpriteBatch spriteBatch)
317 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
318 spriteBatch.Draw(mTexture, position, Color.White);
322 /// A monster should keep track of its health. This one doesn't.
326 get { return this.health; }
331 /// This monster is invincible.
333 /// <param name="amount"></param>
334 public void causeDamageTo(int amount)
336 this.health -= amount;
339 public bool IsCollidable { get { return true; } }
342 /// Get the smoothed position.
344 public Vector2 Position { get { return mMotion.Position; } }
347 /// Get the grid coordinates.
349 public Point Coordinates {
350 get { return mMotion.Coordinates; }
351 set { mMotion = new MovementManager(value, mMotion.Speed); }
355 /// Get the entity identifier.
357 public char Identifier { get { return mId; } }
362 #region Private Variables
367 MovementManager mMotion;
369 List<Point> mWaypoints = new List<Point>(); // List of waypoints that we got from the map.
370 int mWaypointIndex; // Index to the waypoint we're heading for.
373 List<Point> mPath; // List of cells in the path between the position between where
374 // we started and the waypoint we're heading for.
375 int mPathIndex; // Index to the cell we're heading for.
376 Direction mPathDirection; // The direction between our current position and the place we're going.
378 PathFinder.AsyncTask mPathSearch; // If a path search is in progress, this is the task object.
380 AiState mState; // What is the monster doing?
382 Texture2D mTexture; // Obvious.