/* * ScriptManager.cs * Authors: August Zinsser * * Copyright Matthew Belmonte 2007 */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Nuclex.Fonts; using Pina3D.Particles; namespace tAC_Engine { /// /// Interprets Lua commands, waiting between commands if appropriate. /// public static class ScriptManager { private static SpriteBatch mSpriteBatch = new SpriteBatch(GenericBaseApplication.GameManager.GraphicsDevice); private static List mScriptEventQueue = new List(); // Holds the events (lua commands) to execute private static List mEntities = new List(); // Entities to draw private static float mSleepTimer = 0f; // Time to sleep before executing the next lua command private static List mWaitingFor = new List(); // Codes to receive before waking up /// /// Causes the ScriptManager to wait for the specified time before executing the next command /// /// public static void Sleep(float seconds) { mSleepTimer = seconds; } /// /// Causes the ScriptManager to wait until it recieves the specified code before executing the next command /// /// Case-insensitive public static void Sleep(string code) { mWaitingFor.Add(code.ToLower()); } /// /// Tells the ScriptManager to stop waiting for the specified code and continue execution /// /// Case-insensitive public static void SendEventCode(string code) { mWaitingFor.Remove(code.ToLower()); } /// /// Enqueues the lua command into the interpreter queue. /// /// public static void Enqueue(string luaCommand) { mScriptEventQueue.Add(luaCommand); } /// /// Inserts the lua command at the head of the interpreter queue. /// /// public static void Insert(string luaCommand) { mScriptEventQueue.Insert(0, luaCommand); } /// /// Adds an entity to be updated and drawn /// /// public static void AddEntity(Entity entity) { mEntities.Add(entity); mEntities.Sort(); } /// /// Removes an entity from the update/draw list /// /// public static void RemoveEntity(Entity entity) { mEntities.Remove(entity); } /// /// Removes all entities from the update/draw list /// /// public static void FlushEntities() { mEntities.Clear(); } /// /// Process the event queue /// public static void Update() { // Update sleep timer mSleepTimer -= GenericBaseApplication.GameManager.ElapsedSeconds; // Execute each command until a sleep has been reached or all commands have been executed while (mSleepTimer <= 0f && mWaitingFor.Count <= 0 && mScriptEventQueue.Count > 0) { string currentCommand = mScriptEventQueue[0]; mScriptEventQueue.RemoveAt(0); GenericBaseApplication.GameManager.ExecuteLuaCommand(currentCommand); } // Update all entities in memory for (int i = 0; i < mEntities.Count; i++) { mEntities[i].Update(); } } /// /// Draw all the entities held in the ScriptManager's Memory /// /// /// public static void Draw() { if (mEntities.Count > 0) { mSpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); GenericBaseApplication.GameManager.GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1f, 0); GenericBaseApplication.GameManager.GraphicsDevice.RenderState.DepthBufferEnable = true; GenericBaseApplication.GameManager.GraphicsDevice.RenderState.DepthBufferWriteEnable = true; for (int i = 0; i < mEntities.Count; i++) { mEntities[i].Draw(mSpriteBatch); } mSpriteBatch.End(); } } } }