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
32 /// Construct this type of monster. This constructor is called
33 /// by the map when the game requests entities.
35 /// <param name="identifier">The single character ID.</param>
36 /// <param name="position">The initial position on the map.</param>
37 /// <param name="info">More parameters.</param>
38 /// <param name="game">The game object reference.</param>
39 public SaberMonster(char identifier, Point position, Dictionary<string, string> info, Game game)
42 mMotion = new MovementManager(position);
44 // We need to keep the game reference in order to get the grid when we
45 // need to find paths.
48 // Get the speed of the monster. If not set in the map, it defaults to
49 // whatever the default of MovementManager is... 1 I think.
51 if (info.TryGetValue("speed", out speedString))
53 int? speed = Parse.Integer(speedString);
54 if (speed != null) mMotion.Speed = speed.Value;
57 // Get the "idle path" coordinates loaded from the map.
59 if (info.TryGetValue("path", out idlePath))
61 string[] idlePathPoints = Parse.List(idlePath);
62 foreach (string pathPoint in idlePathPoints)
64 Point? point = Parse.Coordinates(pathPoint);
65 if (point != null) mIdlePath.Add(point.Value);
69 // Start doing something...
75 /// Call this to switch the monster AI state to pacing and set up
76 /// the initial paths. The monster will start following the path it
77 /// was defined with in the map file.
79 public void StartPacing()
81 mState = AiState.Pacing;
83 if (mIdlePath.Count == 0) return;
85 // Determine the best (closest) waypoint to start at.
86 // We may not be on the path, so we have to walk to get on it.
88 int closest = int.MaxValue;
89 for (int i = 0; i < mIdlePath.Count; i++)
91 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
92 if (distance < closest)
99 // Find the path to get to the closest waypoint.
100 PathFinder pathFinder = new PathFinder(mGame.Grid);
101 mPath = new List<Point>(32);
102 mPath.Add(Coordinates);
103 mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]));
104 mPath.Add(mIdlePath[mIdlePathIndex]);
108 Direction GetDirectionToNextCell()
110 if (mPathIndex >= mPath.Count)
112 // We're done with the current path, so find the path to
113 // the next waypoint... forever.
115 PathFinder pathFinder = new PathFinder(mGame.Grid);
116 mPath = new List<Point>(32);
117 mPath.Add(Coordinates);
118 mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]));
119 mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);
123 // We need to make sure out direction is set to the next cell
124 // we want to be. If our current coordinates match that, we need
125 // to change our direction to get to the next cell.
126 if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
129 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
132 return mPathDirection;
136 #region IMonster Members
139 /// I don't know what this is for.
143 get { throw new NotImplementedException(); }
149 #region ICharacter Members
152 /// Load the monster's content. This is called by the map when
153 /// the game requests the entities.
155 /// <param name="contentManager">The zaphnod.</param>
156 public void LoadContent(ContentManager contentManager)
158 mTexture = contentManager.Load<Texture2D>("menuItem");
162 /// Update the monster's state. This should be called by the game
163 /// every "frame" (whenever the game is updating its state). In this
164 /// simple monster, all we need to do is update the motion manager.
166 /// <param name="timeSpan"></param>
167 public void Update(TimeSpan timeSpan)
169 if (mState == AiState.Pacing)
171 mMotion.Update(timeSpan, GetDirectionToNextCell());
176 /// Draw the monster. We just ask the map for our screen position,
177 /// passing it the position which the motion manager keeps track of.
179 /// <param name="spriteBatch">The </param>
180 public void Draw(SpriteBatch spriteBatch)
182 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
183 spriteBatch.Draw(mTexture, position, Color.White);
187 /// A monster should keep track of its health. This one doesn't.
191 get { throw new NotImplementedException(); }
195 /// This monster is invincible.
197 /// <param name="amount"></param>
198 public void causeDamageTo(int amount)
200 throw new NotImplementedException();
204 /// Get the smoothed position.
206 public Vector2 Position { get { return mMotion.Position; } }
209 /// Get the grid coordinates.
211 public Point Coordinates { get { return mMotion.Coordinates; } }
216 #region Private Variables
221 MovementManager mMotion;
223 List<Point> mIdlePath = new List<Point>(); // List of waypoints that we got from the map.
224 int mIdlePathIndex; // Index to the waypoint we're heading for.
226 List<Point> mPath; // List of cells in the path between the position between where
227 // we started and the waypoint we're heading for.
228 int mPathIndex; // Index to the cell we're heading for.
229 Direction mPathDirection; // The direction between our current position and the place we're going.
231 AiState mState; // What is the monster doing?
233 Texture2D mTexture; // Obvious.