void bw_DoWork(object sender, DoWorkEventArgs e) { int lastElapsed = frameDelay; while (isRunning) { long elapsedMilliseconds = stopwatch.ElapsedMilliseconds; delta = elapsedMilliseconds / 1000.0; stopwatch.Reset(); stopwatch.Start(); InputContainer.getInstance().Execute(); MoveContainer.getInstance().Move(delta); CollisionContainer.getInstance().CheckCollision(); bw.ReportProgress(1); int elapsed = (int)Math.Ceiling((decimal)elapsedMilliseconds); //The time the thread sleeps should be less if the tick took longer to run int sleepTime = Math.Min(frameDelay, frameDelay - (lastElapsed - frameDelay)); //Can't wait less then 0 time if (sleepTime < 0) { sleepTime = 0; } lastElapsed = elapsed; Thread.Sleep(sleepTime); } }
public Game(Canvas canvas) { this.canvas = canvas; isRunning = true; unitFactory = new UnitFactory(canvas); stateFactory = new RoundStateFactory(); Score.getInstance(); inputHandler = new InputHandler(this, InputContainer.getInstance()); //create rounds createRounds(); bw = new BackgroundWorker(); bw.ProgressChanged += bw_ProgressChanged; bw.WorkerReportsProgress = true; bw.DoWork += bw_DoWork; stopwatch = new Stopwatch(); bw.RunWorkerAsync(); }
public void doAction(Game game) { InputContainer.getInstance().Add(new ShootInput(game, UnitContainer.getInstance().objects)); }