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);
46 // We need to keep the game reference in order to get the grid when we
47 // 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) mIdlePath.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 // Find the path to get to the closest waypoint.
111 if (pathFinder == null)
112 pathFinder = new PathFinder(mGame.Grid);
114 mPath = new List<Point>(32);
115 mPath.Add(Coordinates);
116 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, new Point(X,Y));
119 mPath.AddRange(path);
120 //mPath.Add(mIdlePath[mIdlePathIndex]);
126 /// Call this to switch the monster AI state to pacing and set up
127 /// the initial paths. The monster will start following the path it
128 /// was defined with in the map file.
130 public void StartPacing()
132 mState = AiState.Pacing;
134 if (mIdlePath.Count == 0) return;
136 // Determine the best (closest) waypoint to start at.
137 // We may not be on the path, so we have to walk to get on it.
139 int closest = int.MaxValue;
140 for (int i = 0; i < mIdlePath.Count; i++)
142 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
143 if (distance < closest)
150 // Find the path to get to the closest waypoint.
151 PathFinder pathFinder = new PathFinder(mGame.Grid);
152 mPath = new List<Point>(32);
153 mPath.Add(Coordinates);
154 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]);
157 mPath.AddRange(path);
158 mPath.Add(mIdlePath[mIdlePathIndex]);
163 Direction GetDirectionToNextCell()
165 if (mPathIndex >= mPath.Count)
167 mState = AiState.Standing;
170 // We need to make sure out direction is set to the next cell
171 // we want to be. If our current coordinates match that, we need
172 // to change our direction to get to the next cell.
173 if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
176 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
179 return mPathDirection;
183 #region IMonster Members
186 /// I don't know what this is for.
190 get { throw new NotImplementedException(); }
196 #region ICharacter Members
199 /// Load the monster's content. This is called by the map when
200 /// the game requests the entities.
202 /// <param name="contentManager">The zaphnod.</param>
203 public void LoadContent(ContentManager contentManager)
205 mTexture = contentManager.Load<Texture2D>("menuItem");
209 /// Update the monster's state. This should be called by the game
210 /// every "frame" (whenever the game is updating its state). In this
211 /// simple monster, all we need to do is update the motion manager.
213 /// <param name="timeSpan"></param>
214 public void Update(TimeSpan timeSpan)
217 int closestCharDist = 1000;
218 for (int c = 0; c < mGame.State.mCharacters.Length; c++)
220 if (mGame.State.mCharacters[c] != null && mGame.AIData.spaceVisible(Coordinates, mGame.State.mCharacters[c].Coordinates))
222 if (PathFinder.GetManhattanDistance(Coordinates, mGame.State.mCharacters[c].Coordinates) < closestCharDist)
224 closestCharDist = PathFinder.GetManhattanDistance(Coordinates, mGame.State.mCharacters[c].Coordinates);
230 if (mGame.State.mCharacters[closestChar] != null && mGame.AIData.spaceVisible(Coordinates, mGame.State.mCharacters[closestChar].Coordinates))
232 Chasing(mGame.State.mCharacters[0].Coordinates);
242 case AiState.Chasing:
243 mMotion.Update(timeSpan, GetDirectionToNextCell());
245 case AiState.Standing:
247 case AiState.Fighting:
248 case AiState.Retreating:
255 /// Draw the monster. We just ask the map for our screen position,
256 /// passing it the position which the motion manager keeps track of.
258 /// <param name="spriteBatch">The </param>
259 public void Draw(SpriteBatch spriteBatch)
261 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
262 spriteBatch.Draw(mTexture, position, Color.White);
266 /// A monster should keep track of its health. This one doesn't.
270 get { return this.health; }
275 /// This monster is invincible.
277 /// <param name="amount"></param>
278 public void causeDamageTo(int amount)
280 this.health -= amount;
283 public bool IsCollidable { get { return true; } }
286 /// Get the smoothed position.
288 public Vector2 Position { get { return mMotion.Position; } }
291 /// Get the grid coordinates.
293 public Point Coordinates {
294 get { return mMotion.Coordinates; }
295 set { mMotion = new MovementManager(value, mMotion.Speed); }
299 /// Get the entity identifier.
301 public char Identifier { get { return mId; } }
306 #region Private Variables
311 MovementManager mMotion;
313 PathFinder pathFinder;
314 List<Point> mIdlePath = new List<Point>(); // List of waypoints that we got from the map.
315 int mIdlePathIndex; // Index to the waypoint we're heading for.
317 List<Point> mPath; // List of cells in the path between the position between where
318 // we started and the waypoint we're heading for.
319 int mPathIndex; // Index to the cell we're heading for.
320 Direction mPathDirection; // The direction between our current position and the place we're going.
322 AiState mState; // What is the monster doing?
324 Texture2D mTexture; // Obvious.