using System; using System.Diagnostics; namespace SpaceInvaders { public class Simulation { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- public enum SimState { Realtime, FixedStep, SingleStep, Pause } private static Simulation pInstance; private SimState simState; private float stopwatch_tic; private float stopwatch_toc; private float totalWatch; private float timestep; private const int SIM_NUM_WAKE_CYCLES = 0; private const float SIM_SINGLE_TIMESTEP = 0.016666f; private static bool oldKey = false; //------------------------------------------------------------------- // PRIVATE CONSTRUCTION //------------------------------------------------------------------- private Simulation() { //default singlestep this.simState = SimState.SingleStep; this.timestep = 0.0f; this.totalWatch = 0.0f; this.stopwatch_tic = 0.0f; this.stopwatch_toc = 0.0f; } //------------------------------------------------------------------- // CLASS METHODS //------------------------------------------------------------------- public static void Create() { //if instance is null, create it if(pInstance == null) { pInstance = new Simulation(); } } public static void SetSimState(SimState state) { Simulation pSim = Simulation.GetInstance(); pSim.simState = state; } public static SimState GetSimState() { Simulation pSim = Simulation.GetInstance(); return pSim.simState; } public static float GetTimestep() { Simulation pSim = Simulation.GetInstance(); return pSim.timestep; } public static float GetTotalTime() { Simulation pSim = Simulation.GetInstance(); return pSim.totalWatch; } public static void Update(float systemTime) { //get instance Simulation pSim = Simulation.GetInstance(); //update input pSim.ProcessInput(); //get elapsed time pSim.stopwatch_toc = systemTime - pSim.stopwatch_tic; pSim.stopwatch_tic = systemTime; //determine which sim state your in if(GetSimState() == SimState.FixedStep) { pSim.timestep = SIM_SINGLE_TIMESTEP; } else if(GetSimState() == SimState.Realtime) { pSim.timestep = pSim.stopwatch_toc; } else if(GetSimState() == SimState.SingleStep) { pSim.timestep = SIM_SINGLE_TIMESTEP; SetSimState(SimState.Pause); } else { pSim.timestep = 0.0f; } pSim.totalWatch += pSim.timestep; } //------------------------------------------------------------------- // PRIVATE METHODS //------------------------------------------------------------------- private static Simulation GetInstance() { //if null, create before return if (Simulation.pInstance == null) { Create(); Debug.Assert(Simulation.pInstance != null); } return Simulation.pInstance; } private void ProcessInput() { //Simulation Controls // S --> single step // D --> hold to repeat step // A --> start simulation fixed step // W --> start sim realtime stepping if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_A) == true) { SetSimState(SimState.FixedStep); } if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_W) == true) { SetSimState(SimState.Realtime); } if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_S) && (oldKey == false)) { SetSimState(SimState.SingleStep); } if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_D) == true) { SetSimState(SimState.SingleStep); } oldKey = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_S); } } // end class } // end namespace