X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FTrigger.cs;fp=CarFire%2FCarFire%2FCarFire%2FTrigger.cs;h=6c92b03bb9f7e798563ec5e5c3167db666832e1f;hp=e405aa9fb027ca6432e977d2abe497845d4c9eeb;hb=08f41ef45f3c41ca6302150bc6d5270c8e7143db;hpb=60d05271b295d2ca94a0028059add525c1bbffb1 diff --git a/CarFire/CarFire/CarFire/Trigger.cs b/CarFire/CarFire/CarFire/Trigger.cs index e405aa9..6c92b03 100644 --- a/CarFire/CarFire/CarFire/Trigger.cs +++ b/CarFire/CarFire/CarFire/Trigger.cs @@ -8,10 +8,22 @@ using Microsoft.Xna.Framework.Content; namespace CarFire { + /// + /// Trigger options modify trigger behavior. + /// + [Flags] + public enum TriggerOptions + { + Default = 0x00, + Reset = 0x01, // The script will be reset each time. + Once = 0x02, // Trigger can only be fired once. + Continuous = 0x04, // Trigger is always checked each update. + } + + /// /// A trigger is an invisible entity whose only purpose is - /// to check a condition and run a script when the condition - /// is right. + /// to check for a player to step on it and then run a script. /// public class Trigger : IEntity { @@ -26,44 +38,53 @@ namespace CarFire /// The game reference. public Trigger(char identifier, Point position, Dictionary info, Game game) { + mId = identifier; mGame = game; - mPrevCondition = false; mCoordinates = position; - string condition; - if (info.TryGetValue("condition", out condition)) + Functions functions = new Functions(this, game); + + string script; + if (info.TryGetValue("script", out script)) { - mCondition = new Script(condition, game); + mScript = new Script(script, functions); } - else throw new Exception("Triggers must define a condition script."); + else throw new Exception("Triggers must define a script."); - string eventt; - if (info.TryGetValue("event", out eventt)) + string options; + if (info.TryGetValue("options", out options)) { - mEvent = new Script(eventt, game); + string[] list = Parse.List(options); + if (list != null) + { + foreach (string option in list) + { + TriggerOptions? flag = Parse.Constant(option); + if (flag != null) mFlags |= flag.Value; + } + } } - else throw new Exception("Triggers must define an event script."); } /// - /// Check the trigger condition and execute the event if the - /// condition evaluates to true. + /// Check for a player and execute the trigger script if + /// there is a player on this cell. /// public void Call() { - Player player = mGame.GetPlayerAtCoordinates(mCoordinates); - if (player != null) + if (!mIsExpired) { - bool condition = mCondition.Run(player); - if (condition && condition != mPrevCondition) + Player player = mGame.GetPlayerAtCoordinates(mCoordinates); + if (player != null) { - mEvent.Run(player); + if (!mIsFinished || (mFlags & TriggerOptions.Continuous) == TriggerOptions.Continuous) + { + mIsFinished = mScript.Run(player); + if (mIsFinished && (mFlags & TriggerOptions.Once) == TriggerOptions.Once) mIsExpired = true; + else if ((mFlags & TriggerOptions.Reset) == TriggerOptions.Reset) mScript.Reset(); + } } - mPrevCondition = condition; - } - else - { - mPrevCondition = false; + else mIsFinished = false; } } @@ -94,12 +115,199 @@ namespace CarFire public Vector2 Position { - get { return Vector2.Zero; } + get { return new Vector2(mCoordinates.X, mCoordinates.Y); } } public Point Coordinates { get { return mCoordinates; } + set { mCoordinates = value; } + } + + public char Identifier + { + get { return mId; } + } + + public Game Game + { + get { return mGame; } + } + + #endregion + + + #region Private Types + + /// + /// The script bindings. + /// + class Functions + { + // Always evaluates to true. + public bool True(Player player, string[] args) + { + return true; + } + + // Always evaluates to false. + public bool False(Player player, string[] args) + { + return false; + } + + // Check the player's inventory for an entity. + // Arg1: Entity identifier. + public bool Has(Player player, string[] args) + { + if (args.Length == 0) return false; + + char? entity = Parse.Char(args[0]); + if (entity != null) + { + foreach (IEntity item in player.Inventory) + { + if (entity == item.Identifier) return true; + } + } + return false; + } + + // Put the trigger in the player's inventory. + public bool PickUp(Player player, string[] args) + { + IEntity entity = mGame.RemoveEntity(mTrigger); + if (entity != null) + { + player.Inventory.Add(entity); + return true; + } + return false; + } + + // Drop an entity from the player's inventory here. + // Arg1: Entity identifier. + public bool Drop(Player player, string[] args) + { + if (args.Length == 0) return false; + + char? entity = Parse.Char(args[0]); + if (entity != null) + { + foreach (IEntity item in player.Inventory) + { + if (entity == item.Identifier) + { + player.Inventory.Remove(item); + mGame.State.Entities.Add(item); + item.Coordinates = mTrigger.Coordinates; + return true; + } + } + } + return false; + } + + // Use an entity in the player's inventory, disposing it. + // Arg1: Entity identifier. + public bool Use(Player player, string[] args) + { + if (args.Length == 0) return false; + + char? entity = Parse.Char(args[0]); + if (entity != null) + { + foreach (IEntity item in player.Inventory) + { + if (entity == item.Identifier) + { + player.Inventory.Remove(item); + return true; + } + } + } + return false; + } + + // Wait for a ceretain number of ticks. + // Arg1: Number of ticks. + public bool Wait(Player player, string[] args) + { + if (args.Length == 0) return false; + + if (mTicks == 0) + { + int? timer = Parse.Integer(args[0]); + if (timer != null) + { + mTicks = 1; + mTimerTicks = timer.Value; + } + } + else if (++mTicks >= mTimerTicks) + { + mTicks = 0; + return true; + } + return false; + } + + // Print each argument to the console as a string. + public bool Print(Player player, string[] args) + { + foreach (string arg in args) + { + string line = Parse.String(arg); + if (line != null) Console.WriteLine(line); + } + return true; + } + + // Change a cell's tile on the map. + // Arg1: The coordinates. + // Arg2: The character representing the tile. + public bool Set(Player player, string[] args) + { + if (args.Length < 2) return false; + + Point? coord = Parse.Coordinates(args[0]); + if (coord != null) + { + char? tile = Parse.Char(args[1]); + if (tile != null) + { + mGame.State.Map.SetCell(coord.Value, tile.Value); + return true; + } + } + return false; + } + + // Move on to the next level. + public bool Next(Player player, string[] args) + { + mGame.AdvanceLevel(); + return true; + } + + // Restart the current level. + public bool Reset(Player player, string[] args) + { + mGame.Reset(); + return true; + } + + + public Functions(IEntity trigger, Game game) + { + mTrigger = trigger; + mGame = game; + } + + IEntity mTrigger; + Game mGame; + int mTicks; + int mTimerTicks; } #endregion @@ -107,10 +315,14 @@ namespace CarFire #region Private Variables - Script mCondition; - Script mEvent; + Script mScript; + + TriggerOptions mFlags; + bool mIsFinished; + bool mIsExpired; + + char mId; Game mGame; - bool mPrevCondition; Point mCoordinates; #endregion