]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/SaberMonster.cs
SaberMonster loads from map file and walks around using the path finder, and a lot...
[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 public enum AiState
12 {
13 Standing,
14 Pacing,
15 Chasing,
16 Dazed,
17 Fighting,
18 Retreating
19 }
20
21
22 public class SaberMonster : IMonster
23 {
24 public SaberMonster(char identifier, Point position, Dictionary<string, string> info, Game game)
25 {
26 mId = identifier;
27 mMotion = new MovementManager(position);
28 mGame = game;
29
30 string speedString;
31 if (info.TryGetValue("speed", out speedString))
32 {
33 int? speed = Parse.Integer(speedString);
34 if (speed != null) mMotion.Speed = speed.Value;
35 }
36
37 // Get the "idle path" coordinates loaded from the map.
38 string idlePath;
39 if (info.TryGetValue("path", out idlePath))
40 {
41 string[] idlePathPoints = Parse.List(idlePath);
42 foreach (string pathPoint in idlePathPoints)
43 {
44 Point? point = Parse.Coordinates(pathPoint);
45 if (point != null) mIdlePath.Add(point.Value);
46 }
47 }
48
49 StartPacing();
50 }
51
52
53 public void StartPacing()
54 {
55 mState = AiState.Pacing;
56
57 if (mIdlePath.Count == 0) return;
58
59 // Determine the best (closest) waypoint to start at.
60 mIdlePathIndex = 0;
61 int closest = int.MaxValue;
62 for (int i = 0; i < mIdlePath.Count; i++)
63 {
64 int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]);
65 if (distance < closest)
66 {
67 mIdlePathIndex = i;
68 closest = distance;
69 }
70 }
71
72 PathFinder pathFinder = new PathFinder(mGame.Grid);
73 mPath = new List<Point>(32);
74 mPath.Add(Coordinates);
75 mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]));
76 mPath.Add(mIdlePath[mIdlePathIndex]);
77 mPathIndex = 0;
78 }
79
80 Direction GetDirectionToNextCell()
81 {
82 if (mPathIndex >= mPath.Count)
83 {
84 mIdlePathIndex++;
85 PathFinder pathFinder = new PathFinder(mGame.Grid);
86 mPath = new List<Point>(32);
87 mPath.Add(Coordinates);
88 mPath.AddRange(pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex % mIdlePath.Count]));
89 mPath.Add(mIdlePath[mIdlePathIndex % mIdlePath.Count]);
90 mPathIndex = 0;
91 }
92
93 if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates)
94 {
95 mPathIndex++;
96 mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]);
97 }
98
99 return mPathDirection;
100 }
101
102
103 #region IMonster Members
104
105 public bool visible
106 {
107 get { throw new NotImplementedException(); }
108 }
109
110 #endregion
111
112
113 #region ICharacter Members
114
115 public void LoadContent(ContentManager contentManager)
116 {
117 mTexture = contentManager.Load<Texture2D>("menuItem");
118 }
119
120 public void Update(TimeSpan timeSpan)
121 {
122 if (mState == AiState.Pacing)
123 {
124 mMotion.Update(timeSpan, GetDirectionToNextCell());
125 }
126 }
127
128 public void Draw(SpriteBatch spriteBatch)
129 {
130 Rectangle position = mGame.State.Map.GetRectangleFromCoordinates(mMotion.Position);
131 spriteBatch.Draw(mTexture, position, Color.White);
132 }
133
134 public int Health
135 {
136 get { throw new NotImplementedException(); }
137 }
138
139 public void causeDamageTo(int amount)
140 {
141 throw new NotImplementedException();
142 }
143
144 public Vector2 Position { get { return mMotion.Position; } }
145
146 public Point Coordinates { get { return mMotion.Coordinates; } }
147
148 #endregion
149
150
151 #region Private Variables
152
153 Game mGame;
154
155 char mId;
156 MovementManager mMotion;
157
158 List<Point> mIdlePath = new List<Point>();
159 int mIdlePathIndex;
160
161 List<Point> mPath;
162 int mPathIndex;
163 Direction mPathDirection;
164
165 AiState mState;
166
167 Texture2D mTexture;
168
169 #endregion
170 }
171 }
This page took 0.043055 seconds and 4 git commands to generate.