From c1a49f715d519dc320b2b609dfe763cdf01f3bb1 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 1 Nov 2009 12:10:17 +1300 Subject: [PATCH] Moved time-stepping out into Game. player ticks (including production) now run at the correct rate. --- OpenRa.Game/Game.cs | 31 +++++++++++--- OpenRa.Game/MainWindow.cs | 5 +-- OpenRa.Game/World.cs | 90 +++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 62 deletions(-) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 7e9919d5ad..ff12aad0b4 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -124,16 +124,35 @@ namespace OpenRa.Game soundEngine.Play2D(sound, loop, false, false); } + static int lastTime = Environment.TickCount; + public const int timestep = 40; + + public static void ResetTimer() + { + lastTime = Environment.TickCount; + } + public static void Tick() { - world.Update(); - UnitInfluence.Tick(); - foreach( var player in players.Values ) - player.Tick(); + int t = Environment.TickCount; + int dt = t - lastTime; + if( dt >= timestep ) + { + lastTime += timestep; + if( controller.orderGenerator != null ) + controller.orderGenerator.Tick(); + + world.Tick(); + UnitInfluence.Tick(); + foreach( var player in players.Values ) + player.Tick(); + + orderManager.Tick(); + } + + viewport.cursor = controller.ChooseCursor(); viewport.DrawRegions(); - - orderManager.Tick(); } public static bool IsCellBuildable(int2 a, UnitMovementType umt) diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index e326fc9916..98f88e07ea 100755 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -71,7 +71,7 @@ namespace OpenRa.Game ShowCursor(false); - Game.world.ResetTimer(); + Game.ResetTimer(); } internal void Run() @@ -79,9 +79,6 @@ namespace OpenRa.Game while (Created && Visible) { Game.Tick(); - Game.viewport.cursor = Game.controller.ChooseCursor(); - if (Game.controller.orderGenerator != null) - Game.controller.orderGenerator.Tick(); Application.DoEvents(); } } diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 74f1edc411..22f2e14e03 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -1,60 +1,46 @@ -using System; -using System.Collections.Generic; -using System.Windows.Forms; +using System; +using System.Collections.Generic; +using System.Windows.Forms; using OpenRa.Game.Graphics; -using OpenRa.Game.Traits; - -namespace OpenRa.Game -{ - class World - { - List actors = new List(); - List effects = new List(); - List> frameEndActions = new List>(); - int lastTime = Environment.TickCount; - const int timestep = 40; - - public void Add(Actor a) { actors.Add(a); ActorAdded(a); } - public void Remove(Actor a) { actors.Remove(a); ActorRemoved(a); } - - public void Add(IEffect b) { effects.Add(b); } - public void Remove(IEffect b) { effects.Remove(b); } - - public void AddFrameEndTask( Action a ) { frameEndActions.Add( a ); } - +using OpenRa.Game.Traits; + +namespace OpenRa.Game +{ + class World + { + List actors = new List(); + List effects = new List(); + List> frameEndActions = new List>(); + + public void Add(Actor a) { actors.Add(a); ActorAdded(a); } + public void Remove(Actor a) { actors.Remove(a); ActorRemoved(a); } + + public void Add(IEffect b) { effects.Add(b); } + public void Remove(IEffect b) { effects.Remove(b); } + + public void AddFrameEndTask( Action a ) { frameEndActions.Add( a ); } + public event Action ActorAdded = _ => { }; public event Action ActorRemoved = a => { a.Health = 0; /* make sure everyone sees it as dead */ foreach (var nr in a.traits.WithInterface()) nr.Removed(a); - }; - - public void ResetTimer() - { - lastTime = Environment.TickCount; - } - - public void Update() - { - int t = Environment.TickCount; - int dt = t - lastTime; - if (dt >= timestep) - { - lastTime += timestep; - - foreach (var a in actors) a.Tick(); - foreach (var e in effects) e.Tick(); - - Renderer.waterFrame += 0.00125f * timestep; - Game.viewport.Tick(); - } - - foreach (Action a in frameEndActions) a(this); - frameEndActions.Clear(); - } - - public IEnumerable Actors { get { return actors; } } + }; + + public void Tick() + { + foreach (var a in actors) a.Tick(); + foreach (var e in effects) e.Tick(); + + Renderer.waterFrame += 0.00125f * Game.timestep; + Game.viewport.Tick(); + + foreach (Action a in frameEndActions) a(this); + frameEndActions.Clear(); + } + + public IEnumerable Actors { get { return actors; } } public IEnumerable Effects { get { return effects; } } uint nextAID = 0; @@ -62,5 +48,5 @@ namespace OpenRa.Game { return nextAID++; } - } -} + } +}