]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Trigger.cs
Fixed path finder thrown exception when finding a path to the cell you are already at.
[chaz/carfire] / CarFire / CarFire / CarFire / Trigger.cs
diff --git a/CarFire/CarFire/CarFire/Trigger.cs b/CarFire/CarFire/CarFire/Trigger.cs
new file mode 100644 (file)
index 0000000..25cc11e
--- /dev/null
@@ -0,0 +1,92 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using Microsoft.Xna.Framework;\r
+using Microsoft.Xna.Framework.Graphics;\r
+using Microsoft.Xna.Framework.Content;\r
+\r
+namespace CarFire\r
+{\r
+    /// <summary>\r
+    /// A trigger is an invisible entity whose only purpose is\r
+    /// to check a condition and run a script when the condition\r
+    /// is right.\r
+    /// </summary>\r
+    public class Trigger : IEntity\r
+    {\r
+        #region Public Methods\r
+\r
+        public Trigger(char identifier, Point position, Dictionary<string, string> info, Game game)\r
+        {\r
+            mGame = game;\r
+            mPrevCondition = false;\r
+            mCoordinates = position;\r
+\r
+            string condition;\r
+            if (info.TryGetValue("condition", out condition))\r
+            {\r
+                mCondition = new Script(condition, game);\r
+            }\r
+            else throw new Exception("Triggers must define a condition script.");\r
+\r
+            string eventt;\r
+            if (info.TryGetValue("event", out eventt))\r
+            {\r
+                mEvent = new Script(eventt, game);\r
+            }\r
+            else throw new Exception("Triggers must define an event script.");\r
+        }\r
+\r
+        public void Update(TimeSpan timeSpan)\r
+        {\r
+            Player player = mGame.GetPlayerAtCoordinates(mCoordinates);\r
+            if (player != null)\r
+            {\r
+                bool condition = mCondition.Run(player);\r
+                if (condition && condition != mPrevCondition)\r
+                {\r
+                    mEvent.Run(player);\r
+                }\r
+                mPrevCondition = condition;\r
+            }\r
+            else\r
+            {\r
+                mPrevCondition = false;\r
+            }\r
+        }\r
+\r
+        public void LoadContent(ContentManager contentManager)\r
+        {\r
+            // No implementation needed.\r
+        }\r
+\r
+        public void Draw(SpriteBatch spriteBatch)\r
+        {\r
+            // No implementation needed.\r
+        }\r
+\r
+        public Vector2 Position\r
+        {\r
+            get { return Vector2.Zero; }\r
+        }\r
+\r
+        public Point Coordinates\r
+        {\r
+            get { return new Point(-1, -1); }\r
+        }\r
+\r
+        #endregion\r
+\r
+\r
+        #region Private Variables\r
+\r
+        Script mCondition;\r
+        Script mEvent;\r
+        Game mGame;\r
+        bool mPrevCondition;\r
+        Point mCoordinates;\r
+\r
+        #endregion\r
+    }\r
+}\r
This page took 0.024408 seconds and 4 git commands to generate.