]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Trigger.cs
git-svn-id: https://bd85.net/svn/cs3505_group@163 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / Trigger.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.Graphics;
7 using Microsoft.Xna.Framework.Content;
8
9 namespace CarFire
10 {
11 /// <summary>
12 /// Trigger options modify trigger behavior.
13 /// </summary>
14 [Flags]
15 public enum TriggerOptions
16 {
17 Default = 0x00,
18 Reset = 0x01, // The script will be reset each time.
19 Once = 0x02, // Trigger can only be fired once.
20 Continuous = 0x04, // Trigger is always checked each update.
21 }
22
23
24 /// <summary>
25 /// A trigger is an invisible entity whose only purpose is
26 /// to check for a player to step on it and then run a script.
27 /// </summary>
28 public class Trigger : IEntity
29 {
30 #region Public Methods
31
32 /// <summary>
33 /// Construct a trigger entity.
34 /// </summary>
35 /// <param name="identifier">The entity identifier.</param>
36 /// <param name="position">The position.</param>
37 /// <param name="info">The key-value pairs.</param>
38 /// <param name="game">The game reference.</param>
39 public Trigger(char identifier, Point position, Dictionary<string, string> info, Game game)
40 {
41 mId = identifier;
42 mGame = game;
43 mCoordinates = position;
44
45 Functions functions = new Functions(this, game);
46
47 string script;
48 if (info.TryGetValue("script", out script))
49 {
50 mScript = new Script(script, functions);
51 }
52 else throw new Exception("Triggers must define a script.");
53
54 string options;
55 if (info.TryGetValue("options", out options))
56 {
57 string[] list = Parse.List(options);
58 if (list != null)
59 {
60 foreach (string option in list)
61 {
62 TriggerOptions? flag = Parse.Constant<TriggerOptions>(option);
63 if (flag != null) mFlags |= flag.Value;
64 }
65 }
66 }
67 }
68
69 /// <summary>
70 /// Check for a player and execute the trigger script if
71 /// there is a player on this cell.
72 /// </summary>
73 public void Call()
74 {
75 if (!mIsExpired)
76 {
77 Player player = mGame.GetPlayerAtCoordinates(mCoordinates);
78 if (player != null)
79 {
80 if (!mIsFinished || (mFlags & TriggerOptions.Continuous) == TriggerOptions.Continuous)
81 {
82 mIsFinished = mScript.Run(player);
83 if (mIsFinished && (mFlags & TriggerOptions.Once) == TriggerOptions.Once) mIsExpired = true;
84 else if ((mFlags & TriggerOptions.Reset) == TriggerOptions.Reset) mScript.Reset();
85 }
86 }
87 else mIsFinished = false;
88 }
89 }
90
91
92 /// <summary>
93 /// Calls the trigger.
94 /// </summary>
95 /// <param name="timeSpan">Unused.</param>
96 public virtual void Update(TimeSpan timeSpan)
97 {
98 Call();
99 }
100
101 public virtual void LoadContent(ContentManager contentManager)
102 {
103 // No implementation needed.
104 }
105
106 public virtual void Draw(SpriteBatch spriteBatch)
107 {
108 // No implementation needed.
109 }
110
111 public bool IsCollidable
112 {
113 get { return false; }
114 }
115
116 public Vector2 Position
117 {
118 get { return new Vector2(mCoordinates.X, mCoordinates.Y); }
119 }
120
121 public Point Coordinates
122 {
123 get { return mCoordinates; }
124 set { mCoordinates = value; }
125 }
126
127 public char Identifier
128 {
129 get { return mId; }
130 }
131
132 public Game Game
133 {
134 get { return mGame; }
135 }
136
137 #endregion
138
139
140 #region Private Types
141
142 /// <summary>
143 /// The script bindings.
144 /// </summary>
145 class Functions
146 {
147 // Always evaluates to true.
148 public bool True(Player player, string[] args)
149 {
150 return true;
151 }
152
153 // Always evaluates to false.
154 public bool False(Player player, string[] args)
155 {
156 return false;
157 }
158
159 // Check the player's inventory for an entity.
160 // Arg1: Entity identifier.
161 public bool Has(Player player, string[] args)
162 {
163 if (args.Length == 0) return false;
164
165 char? entity = Parse.Char(args[0]);
166 if (entity != null)
167 {
168 foreach (IEntity item in player.Inventory)
169 {
170 if (entity == item.Identifier) return true;
171 }
172 }
173 return false;
174 }
175
176 // Put the trigger in the player's inventory.
177 public bool PickUp(Player player, string[] args)
178 {
179 IEntity entity = mGame.RemoveEntity(mTrigger);
180 if (entity != null)
181 {
182 player.Inventory.Add(entity);
183 return true;
184 }
185 return false;
186 }
187
188 // Drop an entity from the player's inventory here.
189 // Arg1: Entity identifier.
190 public bool Drop(Player player, string[] args)
191 {
192 if (args.Length == 0) return false;
193
194 char? entity = Parse.Char(args[0]);
195 if (entity != null)
196 {
197 foreach (IEntity item in player.Inventory)
198 {
199 if (entity == item.Identifier)
200 {
201 player.Inventory.Remove(item);
202 mGame.State.Entities.Add(item);
203 item.Coordinates = mTrigger.Coordinates;
204 return true;
205 }
206 }
207 }
208 return false;
209 }
210
211 // Use an entity in the player's inventory, disposing it.
212 // Arg1: Entity identifier.
213 public bool Use(Player player, string[] args)
214 {
215 if (args.Length == 0) return false;
216
217 char? entity = Parse.Char(args[0]);
218 if (entity != null)
219 {
220 foreach (IEntity item in player.Inventory)
221 {
222 if (entity == item.Identifier)
223 {
224 player.Inventory.Remove(item);
225 return true;
226 }
227 }
228 }
229 return false;
230 }
231
232 // Wait for a ceretain number of ticks.
233 // Arg1: Number of ticks.
234 public bool Wait(Player player, string[] args)
235 {
236 if (args.Length == 0) return false;
237
238 if (mTicks == 0)
239 {
240 int? timer = Parse.Integer(args[0]);
241 if (timer != null)
242 {
243 mTicks = 1;
244 mTimerTicks = timer.Value;
245 }
246 }
247 else if (++mTicks >= mTimerTicks)
248 {
249 mTicks = 0;
250 return true;
251 }
252 return false;
253 }
254
255 // Print each argument to the console as a string.
256 public bool Print(Player player, string[] args)
257 {
258 foreach (string arg in args)
259 {
260 string line = Parse.String(arg);
261 if (line != null) Console.WriteLine(line);
262 }
263 return true;
264 }
265
266 // Change a cell's tile on the map.
267 // Arg1: The coordinates.
268 // Arg2: The character representing the tile.
269 public bool Set(Player player, string[] args)
270 {
271 if (args.Length < 2) return false;
272
273 Point? coord = Parse.Coordinates(args[0]);
274 if (coord != null)
275 {
276 char? tile = Parse.Char(args[1]);
277 if (tile != null)
278 {
279 mGame.State.Map.SetCell(coord.Value, tile.Value);
280 return true;
281 }
282 }
283 return false;
284 }
285
286 // Move on to the next level.
287 public bool Next(Player player, string[] args)
288 {
289 mGame.AdvanceLevel();
290 return true;
291 }
292
293 // Restart the current level.
294 public bool Reset(Player player, string[] args)
295 {
296 mGame.Reset();
297 return true;
298 }
299
300
301 public Functions(IEntity trigger, Game game)
302 {
303 mTrigger = trigger;
304 mGame = game;
305 }
306
307 IEntity mTrigger;
308 Game mGame;
309 int mTicks;
310 int mTimerTicks;
311 }
312
313 #endregion
314
315
316 #region Private Variables
317
318 Script mScript;
319
320 TriggerOptions mFlags;
321 bool mIsFinished;
322 bool mIsExpired;
323
324 char mId;
325 Game mGame;
326 Point mCoordinates;
327
328 #endregion
329 }
330 }
This page took 0.044869 seconds and 4 git commands to generate.