2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
13 /// A type for the states of an artificually intelligent entity.
27 /// An example monster. This can serve as a starting place for
28 /// creating other monsters. This one just follows a path.
30 public class SaberMonster : IMonster
35 /// Construct this type of monster. This constructor is called
36 /// by the map when the game requests entities.
38 /// <param name="identifier">The single character ID.</param>
39 /// <param name="position">The initial position on the map.</param>
40 /// <param name="info">More parameters.</param>
41 /// <param name="game">The game object reference.</param>
42 public SaberMonster(char identifier, Point position, Dictionary<string, string> info, Game game)
45 mMotion = new MovementManager(position);
46 mRetryInterval = 20 + (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);
73 // Start doing something...
76 public void DefaultAction()
80 public void Chasing(Point Chase)
86 /// Call this to switch the monster AI state to pacing and set up
87 /// the initial paths. The monster will start following the path it
88 /// was defined with in the map file.
90 public void StartPacing()
92 if (mWaypoints.Count == 0) return;
94 mState = AiState.Pacing;
96 // Determine the best (closest) waypoint to start at.
97 // We may not be on the path, so we have to walk to get on it.
99 int closest = int.MaxValue;
100 for (int i = 0; i < mWaypoints.Count; i++)
102 int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]);
103 if (distance < closest)
110 // Find the path to get to the closest waypoint.
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.
124 // We need to make sure our direction is set to the next cell
125 // we want to be. If our current coordinates match that, we need
126 // to change our direction to get to the next cell.
127 if (mPathIndex < mPath.Count && mPath[mPathIndex] == mMotion.Coordinates)
130 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
133 return mPathDirection;
138 mPath = new List<Point>(32);
140 Point waypoint = mWaypoints[mWaypointIndex % mWaypoints.Count];
141 PathFinder pathFinder = new PathFinder(mGame.Grid);
142 List<Point> path = pathFinder.GetPath(mMotion.Coordinates, waypoint);
143 if (path != null) mPath.AddRange(path);
146 if (mPathIndex < mPath.Count) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex]);
147 else mPathDirection = Direction.None;
151 #region IMonster Members
154 /// I don't know what this is for.
158 get { throw new NotImplementedException(); }
164 #region ICharacter Members
167 /// Load the monster's content. This is called by the map when
168 /// the game requests the entities.
170 /// <param name="contentManager">The zaphnod.</param>
171 public void LoadContent(ContentManager contentManager)
173 mTexture = contentManager.Load<Texture2D>("default");
177 /// Update the monster's state. This should be called by the game
178 /// every "frame" (whenever the game is updating its state). In this
179 /// simple monster, all we need to do is update the motion manager.
181 /// <param name="timeSpan"></param>
182 public void Update(TimeSpan timeSpan)
188 Direction direction = GetDirectionToNextCell();
189 Point destination = MovementManager.GetNeighbor(Coordinates, direction);
191 if (mGame.IsCellOpen(destination))
193 mMotion.Update(timeSpan, direction);
197 if (mGame.CurrentFrameNumber % mRetryInterval == 0)
199 // Something is in our way, so let's chart a new course.
202 direction = GetDirectionToNextCell();
203 /*if (direction == Direction.None)
205 // If we still can't chart a course, just stand there
206 // and try to chart again later.
207 mState = AiState.Standing;
210 mMotion.Update(timeSpan, direction);
212 else mMotion.Update(timeSpan);
217 case AiState.Standing:
219 mMotion.Update(timeSpan);
225 /// Draw the monster. We just ask the map for our screen position,
226 /// passing it the position which the motion manager keeps track of.
228 /// <param name="spriteBatch">The </param>
229 public void Draw(SpriteBatch spriteBatch)
231 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
232 spriteBatch.Draw(mTexture, position, Color.White);
236 /// A monster should keep track of its health. This one doesn't.
240 get { return this.health; }
245 /// This monster is invincible.
247 /// <param name="amount"></param>
248 public void causeDamageTo(int amount)
250 this.health -= amount;
253 public bool IsCollidable { get { return true; } }
256 /// Get the smoothed position.
258 public Vector2 Position { get { return mMotion.Position; } }
261 /// Get the grid coordinates.
263 public Point Coordinates {
264 get { return mMotion.Coordinates; }
265 set { mMotion = new MovementManager(value, mMotion.Speed); }
269 /// Get the entity identifier.
271 public char Identifier { get { return mId; } }
276 #region Private Variables
281 MovementManager mMotion;
283 List<Point> mWaypoints = new List<Point>(); // List of waypoints that we got from the map.
284 int mWaypointIndex; // Index to the waypoint we're heading for.
286 List<Point> mPath; // List of cells in the path between the position between where
287 // we started and the waypoint we're heading for.
288 int mPathIndex; // Index to the cell we're heading for.
289 Direction mPathDirection; // The direction between our current position and the place we're going.
292 AiState mState; // What is the monster doing?
294 Texture2D mTexture; // Obvious.