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);
46 // We need to keep the game reference in order to get the grid when we
47 // need to find paths.
50 // Get the speed of the monster. If not set in the map, it defaults to
51 // whatever the default of MovementManager is... 1 I think.
53 if (info.TryGetValue("speed", out speedString))
55 int? speed = Parse.Integer(speedString);
56 if (speed != null) mMotion.Speed = speed.Value;
59 // Get the "idle path" coordinates loaded from the map.
61 if (info.TryGetValue("path", out idlePath))
63 string[] idlePathPoints = Parse.List(idlePath);
64 foreach (string pathPoint in idlePathPoints)
66 Point? point = Parse.Coordinates(pathPoint);
67 if (point != null) mIdlePath.Add(point.Value);
71 // Start doing something...
77 /// Call this to switch the monster AI state to pacing and set up
78 /// the initial paths. The monster will start following the path it
79 /// was defined with in the map file.
81 public void StartPacing()
83 mState = AiState.Pacing;
85 if (mIdlePath.Count == 0) return;
87 // Determine the best (closest) waypoint to start at.
88 // We may not be on the path, so we have to walk to get on it.
90 int closest = int.MaxValue;
91 for (int i = 0; i < mIdlePath.Count; i++)
93 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
94 if (distance < closest)
101 // Find the path to get to the closest waypoint.
102 PathFinder pathFinder = new PathFinder(mGame.Grid);
103 mPath = new List<Point>(32);
104 mPath.Add(Coordinates);
105 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]);
108 mPath.AddRange(path);
109 mPath.Add(mIdlePath[mIdlePathIndex]);
114 Direction GetDirectionToNextCell()
116 if (mPathIndex >= mPath.Count)
118 // We're done with the current path, so find the path to
119 // the next waypoint... forever.
121 PathFinder pathFinder = new PathFinder(mGame.Grid);
122 mPath = new List<Point>(32);
123 mPath.Add(Coordinates);
124 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]);
127 mPath.AddRange(path);
128 mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);
133 // We need to make sure out direction is set to the next cell
134 // we want to be. If our current coordinates match that, we need
135 // to change our direction to get to the next cell.
136 if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
139 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
142 return mPathDirection;
146 #region IMonster Members
149 /// I don't know what this is for.
153 get { throw new NotImplementedException(); }
159 #region ICharacter Members
162 /// Load the monster's content. This is called by the map when
163 /// the game requests the entities.
165 /// <param name="contentManager">The zaphnod.</param>
166 public void LoadContent(ContentManager contentManager)
168 mTexture = contentManager.Load<Texture2D>("menuItem");
172 /// Update the monster's state. This should be called by the game
173 /// every "frame" (whenever the game is updating its state). In this
174 /// simple monster, all we need to do is update the motion manager.
176 /// <param name="timeSpan"></param>
177 public void Update(TimeSpan timeSpan)
179 if (mState == AiState.Pacing)
181 mMotion.Update(timeSpan, GetDirectionToNextCell());
186 /// Draw the monster. We just ask the map for our screen position,
187 /// passing it the position which the motion manager keeps track of.
189 /// <param name="spriteBatch">The </param>
190 public void Draw(SpriteBatch spriteBatch)
192 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
193 spriteBatch.Draw(mTexture, position, Color.White);
197 /// A monster should keep track of its health. This one doesn't.
201 get { return this.health; }
206 /// This monster is invincible.
208 /// <param name="amount"></param>
209 public void causeDamageTo(int amount)
211 this.health -= amount;
214 public bool IsCollidable { get { return true; } }
217 /// Get the smoothed position.
219 public Vector2 Position { get { return mMotion.Position; } }
222 /// Get the grid coordinates.
224 public Point Coordinates { get { return mMotion.Coordinates; } }
229 #region Private Variables
234 MovementManager mMotion;
236 List<Point> mIdlePath = new List<Point>(); // List of waypoints that we got from the map.
237 int mIdlePathIndex; // Index to the waypoint we're heading for.
239 List<Point> mPath; // List of cells in the path between the position between where
240 // we started and the waypoint we're heading for.
241 int mPathIndex; // Index to the cell we're heading for.
242 Direction mPathDirection; // The direction between our current position and the place we're going.
244 AiState mState; // What is the monster doing?
246 Texture2D mTexture; // Obvious.