/// <summary> /// Adds a new command to the command manager /// </summary> /// <param name="command"></param> public bool AddCommand(CommandDescriptor command) { if (!registeredCommands.ContainsKey(command.Command)) { registeredCommands.Add(command.Command, command); return(true); } return(false); }
/// <summary> /// Removes a given command /// </summary> /// <param name="command">Command that should be removed</param> /// <returns>Returns false when the given command doesn't exists</returns> public bool RemoveCommand(CommandDescriptor command) { if (Commands.ContainsKey(command.Command)) { Commands.Remove(command.Command); return(true); } return(false); }
/// <summary> /// Enqueues add command which will be executed on the next update /// </summary> /// <param name="commandName">Command name</param> public bool EnqueueCommand(string commandName) { if (registeredCommands.ContainsKey(commandName)) { CommandDescriptor cmd = registeredCommands[commandName]; if (cmd.RepeatAllowed || !commandsToExecute.Contains(cmd)) { commandsToExecute.Enqueue(cmd); return(true); } } return(false); }
/// <summary> /// Executes all commands since the last command execution (Update) /// </summary> /// <param name="elapsedSeconds">ElapsedSeconds since last update</param> public void Update(float elapsedSeconds) { commands = commandsToExecute.ToArray(); commandsToExecute.Clear(); float stackedSleep = 0; for (int i = 0; i < commands.Length; i++) { CommandDescriptor cmd = commands[i]; if (cmd.IgnoreSleep || stackedSleep < elapsedSeconds) { stackedSleep += cmd.Sleep; cmd.ExecuteCommand(); } else { commandsToExecute.Enqueue(cmd); } } }