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 SaberMonster : 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 SaberMonster(char identifier, Point position, Dictionary<string, string> info, Game game)
44 mMotion = new MovementManager(position);
45 mRetryInterval = 20 + (position.X * 25789 + position.Y * 259) % 30;
47 // We need to keep the game reference in order to get the grid when we
48 // need to find paths.
51 // Get the speed of the monster. If not set in the map, it defaults to
52 // whatever the default of MovementManager is... 1 I think.
54 if (info.TryGetValue("speed", out speedString))
56 int? speed = Parse.Integer(speedString);
57 if (speed != null) mMotion.Speed = speed.Value;
60 // Get the "idle path" coordinates loaded from the map.
62 if (info.TryGetValue("path", out idlePath))
64 string[] idlePathPoints = Parse.List(idlePath);
65 foreach (string pathPoint in idlePathPoints)
67 Point? point = Parse.Coordinates(pathPoint);
68 if (point != null) mWaypoints.Add(point.Value);
72 // Start doing something...
78 /// Call this to switch the monster AI state to pacing and set up
79 /// the initial paths. The monster will start following the path it
80 /// was defined with in the map file.
82 public void StartPacing()
84 if (mWaypoints.Count == 0) return;
86 mState = AiState.Pacing;
88 // Determine the best (closest) waypoint to start at.
89 // We may not be on the path, so we have to walk to get on it.
91 int closest = int.MaxValue;
92 for (int i = 0; i < mWaypoints.Count; i++)
94 int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);
95 if (distance < closest)
102 // Find the path to get to the closest waypoint.
106 Direction GetDirectionToNextCell()
108 if (mPathIndex >= mPath.Count)
110 // We're done with the current path, so find the path to
111 // the next waypoint... forever.
116 // We need to make sure our direction is set to the next cell
117 // we want to be. If our current coordinates match that, we need
118 // to change our direction to get to the next cell.
119 if (mPathIndex < mPath.Count && mPath[mPathIndex] == mMotion.Coordinates)
122 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
125 return mPathDirection;
130 mPath = new List<Point>(32);
132 Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
133 PathFinder pathFinder = new PathFinder(mGame.Grid);
134 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, waypoint);
135 if (path != null) mPath.AddRange(path);
138 if (mPathIndex < mPath.Count) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex]);
139 else mPathDirection = Direction.None;
143 #region IMonster Members
146 /// I don't know what this is for.
150 get { throw new NotImplementedException(); }
156 #region ICharacter Members
159 /// Load the monster's content. This is called by the map when
160 /// the game requests the entities.
162 /// <param name="contentManager">The zaphnod.</param>
163 public void LoadContent(ContentManager contentManager)
165 mTexture = contentManager.Load<Texture2D>("default");
169 /// Update the monster's state. This should be called by the game
170 /// every "frame" (whenever the game is updating its state). In this
171 /// simple monster, all we need to do is update the motion manager.
173 /// <param name="timeSpan"></param>
174 public void Update(TimeSpan timeSpan)
180 Direction direction = GetDirectionToNextCell();
181 Point destination = MovementManager.GetNeighbor(Coordinates, direction);
183 if (mGame.IsCellOpen(destination))
185 mMotion.Update(timeSpan, direction);
189 if (mGame.CurrentFrameNumber % mRetryInterval == 0)
191 // Something is in our way, so let's chart a new course.
194 direction = GetDirectionToNextCell();
195 /*if (direction == Direction.None)
197 // If we still can't chart a course, just stand there
198 // and try to chart again later.
199 mState = AiState.Standing;
202 mMotion.Update(timeSpan, direction);
204 else mMotion.Update(timeSpan);
209 case AiState.Standing:
211 mMotion.Update(timeSpan);
217 /// Draw the monster. We just ask the map for our screen position,
218 /// passing it the position which the motion manager keeps track of.
220 /// <param name="spriteBatch">The </param>
221 public void Draw(SpriteBatch spriteBatch)
223 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
224 spriteBatch.Draw(mTexture, position, Color.White);
228 /// A monster should keep track of its health. This one doesn't.
232 get { return this.health; }
237 /// This monster is invincible.
239 /// <param name="amount"></param>
240 public void causeDamageTo(int amount)
242 this.health -= amount;
245 public bool IsCollidable { get { return true; } }
248 /// Get the smoothed position.
250 public Vector2 Position { get { return mMotion.Position; } }
253 /// Get the grid coordinates.
255 public Point Coordinates {
256 get { return mMotion.Coordinates; }
257 set { mMotion = new MovementManager(value, mMotion.Speed); }
261 /// Get the entity identifier.
263 public char Identifier { get { return mId; } }
268 #region Private Variables
273 MovementManager mMotion;
275 List<Point> mWaypoints = new List<Point>(); // List of waypoints that we got from the map.
276 int mWaypointIndex; // Index to the waypoint we're heading for.
278 List<Point> mPath; // List of cells in the path between the position between where
279 // we started and the waypoint we're heading for.
280 int mPathIndex; // Index to the cell we're heading for.
281 Direction mPathDirection; // The direction between our current position and the place we're going.
284 AiState mState; // What is the monster doing?
286 Texture2D mTexture; // Obvious.