]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/SaberMonster.cs
Documented the sample monster.
[chaz/carfire] / CarFire / CarFire / CarFire / SaberMonster.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8
9 namespace CarFire
10 {
11 /// <summary>
12 /// A type for the states of an artificually intelligent entity.
13 /// </summary>
14 public enum AiState
15 {
16 Standing,
17 Pacing,
18 Chasing,
19 Dazed,
20 Fighting,
21 Retreating
22 }
23
24
25 /// <summary>
26 /// An example monster. This can serve as a starting place for
27 /// creating other monsters. This one just follows a path.
28 /// </summary>
29 public class SaberMonster : IMonster
30 {
31 /// <summary>
32 /// Construct this type of monster. This constructor is called
33 /// by the map when the game requests entities.
34 /// </summary>
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)
40 {
41 mId = identifier;
42 mMotion = new MovementManager(position);
43
44 // We need to keep the game reference in order to get the grid when we
45 // need to find paths.
46 mGame = game;
47
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.
50 string speedString;
51 if (info.TryGetValue("speed", out speedString))
52 {
53 int? speed = Parse.Integer(speedString);
54 if (speed != null) mMotion.Speed = speed.Value;
55 }
56
57 // Get the "idle path" coordinates loaded from the map.
58 string idlePath;
59 if (info.TryGetValue("path", out idlePath))
60 {
61 string[] idlePathPoints = Parse.List(idlePath);
62 foreach (string pathPoint in idlePathPoints)
63 {
64 Point? point = Parse.Coordinates(pathPoint);
65 if (point != null) mIdlePath.Add(point.Value);
66 }
67 }
68
69 // Start doing something...
70 StartPacing();
71 }
72
73
74 /// <summary>
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.
78 /// </summary>
79 public void StartPacing()
80 {
81 mState = AiState.Pacing;
82
83 if (mIdlePath.Count == 0) return;
84
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.
87 mIdlePathIndex = 0;
88 int closest = int.MaxValue;
89 for (int i = 0; i < mIdlePath.Count; i++)
90 {
91 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
92 if (distance < closest)
93 {
94 mIdlePathIndex = i;
95 closest = distance;
96 }
97 }
98
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]);
105 mPathIndex = 0;
106 }
107
108 Direction GetDirectionToNextCell()
109 {
110 if (mPathIndex >= mPath.Count)
111 {
112 // We're done with the current path, so find the path to
113 // the next waypoint... forever.
114 mIdlePathIndex++;
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]);
120 mPathIndex = 0;
121 }
122
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)
127 {
128 mPathIndex++;
129 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
130 }
131
132 return mPathDirection;
133 }
134
135
136 #region IMonster Members
137
138 /// <summary>
139 /// I don't know what this is for.
140 /// </summary>
141 public bool visible
142 {
143 get { throw new NotImplementedException(); }
144 }
145
146 #endregion
147
148
149 #region ICharacter Members
150
151 /// <summary>
152 /// Load the monster's content. This is called by the map when
153 /// the game requests the entities.
154 /// </summary>
155 /// <param name="contentManager">The zaphnod.</param>
156 public void LoadContent(ContentManager contentManager)
157 {
158 mTexture = contentManager.Load<Texture2D>("menuItem");
159 }
160
161 /// <summary>
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.
165 /// </summary>
166 /// <param name="timeSpan"></param>
167 public void Update(TimeSpan timeSpan)
168 {
169 if (mState == AiState.Pacing)
170 {
171 mMotion.Update(timeSpan, GetDirectionToNextCell());
172 }
173 }
174
175 /// <summary>
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.
178 /// </summary>
179 /// <param name="spriteBatch">The </param>
180 public void Draw(SpriteBatch spriteBatch)
181 {
182 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
183 spriteBatch.Draw(mTexture, position, Color.White);
184 }
185
186 /// <summary>
187 /// A monster should keep track of its health. This one doesn't.
188 /// </summary>
189 public int Health
190 {
191 get { throw new NotImplementedException(); }
192 }
193
194 /// <summary>
195 /// This monster is invincible.
196 /// </summary>
197 /// <param name="amount"></param>
198 public void causeDamageTo(int amount)
199 {
200 throw new NotImplementedException();
201 }
202
203 /// <summary>
204 /// Get the smoothed position.
205 /// </summary>
206 public Vector2 Position { get { return mMotion.Position; } }
207
208 /// <summary>
209 /// Get the grid coordinates.
210 /// </summary>
211 public Point Coordinates { get { return mMotion.Coordinates; } }
212
213 #endregion
214
215
216 #region Private Variables
217
218 Game mGame;
219
220 char mId;
221 MovementManager mMotion;
222
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.
225
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.
230
231 AiState mState; // What is the monster doing?
232
233 Texture2D mTexture; // Obvious.
234
235 #endregion
236 }
237 }
This page took 0.039462 seconds and 4 git commands to generate.