2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7 using Microsoft.Xna.Framework.Content;
12 /// Trigger options modify trigger behavior.
15 public enum TriggerOptions
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.
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.
28 public class Trigger : IEntity
30 #region Public Methods
33 /// Construct a trigger entity.
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)
43 mCoordinates = position;
45 Functions functions = new Functions(this, game);
48 if (info.TryGetValue("script", out script))
50 mScript = new Script(script, functions);
52 else throw new Exception("Triggers must define a script.");
55 if (info.TryGetValue("options", out options))
57 string[] list = Parse.List(options);
60 foreach (string option in list)
62 TriggerOptions? flag = Parse.Constant<TriggerOptions>(option);
63 if (flag != null) mFlags |= flag.Value;
70 /// Check for a player and execute the trigger script if
71 /// there is a player on this cell.
77 Player player = mGame.GetPlayerAtCoordinates(mCoordinates);
80 if (!mIsFinished || (mFlags & TriggerOptions.Continuous) == TriggerOptions.Continuous)
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();
87 else mIsFinished = false;
93 /// Calls the trigger.
95 /// <param name="timeSpan">Unused.</param>
96 public virtual void Update(TimeSpan timeSpan)
101 public virtual void LoadContent(ContentManager contentManager)
103 // No implementation needed.
106 public virtual void Draw(SpriteBatch spriteBatch)
108 // No implementation needed.
111 public bool IsCollidable
113 get { return false; }
116 public Vector2 Position
118 get { return new Vector2(mCoordinates.X, mCoordinates.Y); }
121 public Point Coordinates
123 get { return mCoordinates; }
124 set { mCoordinates = value; }
127 public char Identifier
134 get { return mGame; }
140 #region Private Types
143 /// The script bindings.
147 // Always evaluates to true.
148 public bool True(Player player, string[] args)
153 // Always evaluates to false.
154 public bool False(Player player, string[] args)
159 // Check the player's inventory for an entity.
160 // Arg1: Entity identifier.
161 public bool Has(Player player, string[] args)
163 if (args.Length == 0) return false;
165 char? entity = Parse.Char(args[0]);
168 foreach (IEntity item in player.Inventory)
170 if (entity == item.Identifier) return true;
176 // Put the trigger in the player's inventory.
177 public bool PickUp(Player player, string[] args)
179 IEntity entity = mGame.RemoveEntity(mTrigger);
182 player.Inventory.Add(entity);
188 // Drop an entity from the player's inventory here.
189 // Arg1: Entity identifier.
190 public bool Drop(Player player, string[] args)
192 if (args.Length == 0) return false;
194 char? entity = Parse.Char(args[0]);
197 foreach (IEntity item in player.Inventory)
199 if (entity == item.Identifier)
201 player.Inventory.Remove(item);
202 mGame.State.Entities.Add(item);
203 item.Coordinates = mTrigger.Coordinates;
211 // Use an entity in the player's inventory, disposing it.
212 // Arg1: Entity identifier.
213 public bool Use(Player player, string[] args)
215 if (args.Length == 0) return false;
217 char? entity = Parse.Char(args[0]);
220 foreach (IEntity item in player.Inventory)
222 if (entity == item.Identifier)
224 player.Inventory.Remove(item);
232 // Wait for a ceretain number of ticks.
233 // Arg1: Number of ticks.
234 public bool Wait(Player player, string[] args)
236 if (args.Length == 0) return false;
240 int? timer = Parse.Integer(args[0]);
244 mTimerTicks = timer.Value;
247 else if (++mTicks >= mTimerTicks)
255 // Print each argument to the console as a string.
256 public bool Print(Player player, string[] args)
258 foreach (string arg in args)
260 string line = Parse.String(arg);
261 if (line != null) Console.WriteLine(line);
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)
271 if (args.Length < 2) return false;
273 Point? coord = Parse.Coordinates(args[0]);
276 char? tile = Parse.Char(args[1]);
279 mGame.State.Map.SetCell(coord.Value, tile.Value);
286 // Move on to the next level.
287 public bool Next(Player player, string[] args)
289 mGame.AdvanceLevel();
293 // Restart the current level.
294 public bool Reset(Player player, string[] args)
301 public Functions(IEntity trigger, Game game)
316 #region Private Variables
320 TriggerOptions mFlags;