Moved time-stepping out into Game. player ticks (including production) now run at the correct rate.

This commit is contained in:
Bob
2009-11-01 12:10:17 +13:00
parent dbef9e4a45
commit c1a49f715d
3 changed files with 64 additions and 62 deletions

View File

@@ -124,16 +124,35 @@ namespace OpenRa.Game
soundEngine.Play2D(sound, loop, false, false); 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() public static void Tick()
{ {
world.Update(); int t = Environment.TickCount;
UnitInfluence.Tick(); int dt = t - lastTime;
foreach( var player in players.Values ) if( dt >= timestep )
player.Tick(); {
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(); viewport.DrawRegions();
orderManager.Tick();
} }
public static bool IsCellBuildable(int2 a, UnitMovementType umt) public static bool IsCellBuildable(int2 a, UnitMovementType umt)

View File

@@ -71,7 +71,7 @@ namespace OpenRa.Game
ShowCursor(false); ShowCursor(false);
Game.world.ResetTimer(); Game.ResetTimer();
} }
internal void Run() internal void Run()
@@ -79,9 +79,6 @@ namespace OpenRa.Game
while (Created && Visible) while (Created && Visible)
{ {
Game.Tick(); Game.Tick();
Game.viewport.cursor = Game.controller.ChooseCursor();
if (Game.controller.orderGenerator != null)
Game.controller.orderGenerator.Tick();
Application.DoEvents(); Application.DoEvents();
} }
} }

View File

@@ -1,60 +1,46 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
using OpenRa.Game.Traits; using OpenRa.Game.Traits;
namespace OpenRa.Game namespace OpenRa.Game
{ {
class World class World
{ {
List<Actor> actors = new List<Actor>(); List<Actor> actors = new List<Actor>();
List<IEffect> effects = new List<IEffect>(); List<IEffect> effects = new List<IEffect>();
List<Action<World>> frameEndActions = new List<Action<World>>(); List<Action<World>> frameEndActions = new List<Action<World>>();
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(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 Add(IEffect b) { effects.Add(b); }
public void Remove(IEffect b) { effects.Remove(b); } public void AddFrameEndTask( Action<World> a ) { frameEndActions.Add( a ); }
public void AddFrameEndTask( Action<World> a ) { frameEndActions.Add( a ); }
public event Action<Actor> ActorAdded = _ => { }; public event Action<Actor> ActorAdded = _ => { };
public event Action<Actor> ActorRemoved = a => public event Action<Actor> ActorRemoved = a =>
{ {
a.Health = 0; /* make sure everyone sees it as dead */ a.Health = 0; /* make sure everyone sees it as dead */
foreach (var nr in a.traits.WithInterface<INotifyRemoved>()) foreach (var nr in a.traits.WithInterface<INotifyRemoved>())
nr.Removed(a); nr.Removed(a);
}; };
public void ResetTimer() public void Tick()
{ {
lastTime = Environment.TickCount; foreach (var a in actors) a.Tick();
} foreach (var e in effects) e.Tick();
public void Update() Renderer.waterFrame += 0.00125f * Game.timestep;
{ Game.viewport.Tick();
int t = Environment.TickCount;
int dt = t - lastTime; foreach (Action<World> a in frameEndActions) a(this);
if (dt >= timestep) frameEndActions.Clear();
{ }
lastTime += timestep;
public IEnumerable<Actor> Actors { get { return actors; } }
foreach (var a in actors) a.Tick();
foreach (var e in effects) e.Tick();
Renderer.waterFrame += 0.00125f * timestep;
Game.viewport.Tick();
}
foreach (Action<World> a in frameEndActions) a(this);
frameEndActions.Clear();
}
public IEnumerable<Actor> Actors { get { return actors; } }
public IEnumerable<IEffect> Effects { get { return effects; } } public IEnumerable<IEffect> Effects { get { return effects; } }
uint nextAID = 0; uint nextAID = 0;
@@ -62,5 +48,5 @@ namespace OpenRa.Game
{ {
return nextAID++; return nextAID++;
} }
} }
} }