using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; namespace CarFire { /// /// A trigger is an invisible entity whose only purpose is /// to check a condition and run a script when the condition /// is right. /// public class Trigger : IEntity { #region Public Methods /// /// Construct a trigger entity. /// /// The entity identifier. /// The position. /// The key-value pairs. /// The game reference. public Trigger(char identifier, Point position, Dictionary info, Game game) { mGame = game; mPrevCondition = false; mCoordinates = position; string condition; if (info.TryGetValue("condition", out condition)) { mCondition = new Script(condition, game); } else throw new Exception("Triggers must define a condition script."); string eventt; if (info.TryGetValue("event", out eventt)) { mEvent = new Script(eventt, game); } else throw new Exception("Triggers must define an event script."); } /// /// Check the trigger condition and execute the event if the /// condition evaluates to true. /// public void Call() { Player player = mGame.GetPlayerAtCoordinates(mCoordinates); if (player != null) { bool condition = mCondition.Run(player); if (condition && condition != mPrevCondition) { mEvent.Run(player); } mPrevCondition = condition; } else { mPrevCondition = false; } } /// /// Calls the trigger. /// /// Unused. public virtual void Update(TimeSpan timeSpan) { Call(); } public virtual void LoadContent(ContentManager contentManager) { // No implementation needed. } public virtual void Draw(SpriteBatch spriteBatch) { // No implementation needed. } public bool IsCollidable { get { return false; } } public Vector2 Position { get { return Vector2.Zero; } } public Point Coordinates { get { return mCoordinates; } } #endregion #region Private Variables Script mCondition; Script mEvent; Game mGame; bool mPrevCondition; Point mCoordinates; #endregion } }