working towards real queues

This commit is contained in:
Chris Forbes
2009-12-28 16:03:27 +13:00
parent c914491776
commit 10ed07cfe2
5 changed files with 46 additions and 61 deletions

View File

@@ -144,7 +144,7 @@ namespace OpenRa.Game
continue; continue;
} }
var producing = queue.Producing(groupName); var producing = queue.CurrentItem(groupName);
var index = q.Key == currentTab ? 2 : (producing != null && producing.Done) ? 1 : 0; var index = q.Key == currentTab ? 2 : (producing != null && producing.Done) ? 1 : 0;
@@ -173,10 +173,8 @@ namespace OpenRa.Game
void CheckDeadTab( string groupName ) void CheckDeadTab( string groupName )
{ {
var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>(); var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>();
var item = queue.Producing( groupName ); foreach( var item in queue.AllItems( groupName ) )
if (item != null) Game.controller.AddOrder(Order.CancelProduction(Game.LocalPlayer, item.Item));
for( var n = 0; n <= item.Repeats; n++ )
Game.controller.AddOrder(Order.CancelProduction(Game.LocalPlayer, item.Item));
} }
void ChooseAvailableTab() void ChooseAvailableTab()
@@ -227,7 +225,7 @@ namespace OpenRa.Game
return () => return () =>
{ {
var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>(); var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>();
var producing = queue.Producing( group ); var producing = queue.CurrentItem( group );
if (producing == null) return 0; if (producing == null) return 0;
return (producing.TotalTime - producing.RemainingTime) * NumClockFrames / producing.TotalTime; return (producing.TotalTime - producing.RemainingTime) * NumClockFrames / producing.TotalTime;
}; };
@@ -252,7 +250,7 @@ namespace OpenRa.Game
.OrderBy(a => Rules.UnitInfo[a].TechLevel); .OrderBy(a => Rules.UnitInfo[a].TechLevel);
var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>(); var queue = Game.LocalPlayer.PlayerActor.traits.Get<Traits.ProductionQueue>();
var currentItem = queue.Producing( queueName ); var currentItem = queue.CurrentItem( queueName );
var overlayBits = new List<Pair<Sprite, float2>>(); var overlayBits = new List<Pair<Sprite, float2>>();
@@ -273,14 +271,14 @@ namespace OpenRa.Game
if (!buildableItems.Contains(item) || isBuildingSomethingElse) if (!buildableItems.Contains(item) || isBuildingSomethingElse)
overlayBits.Add(Pair.New(cantBuild.Image, drawPos)); overlayBits.Add(Pair.New(cantBuild.Image, drawPos));
var overlayPos = drawPos + new float2((64 - ready.Image.size.X) / 2, 2);
if (isBuildingThis) if (isBuildingThis)
{ {
clockAnimations[queueName].Tick(); clockAnimations[queueName].Tick();
buildPaletteRenderer.DrawSprite(clockAnimations[queueName].Image, buildPaletteRenderer.DrawSprite(clockAnimations[queueName].Image,
drawPos, PaletteType.Chrome); drawPos, PaletteType.Chrome);
var overlayPos = drawPos + new float2((64 - ready.Image.size.X) / 2, 2);
if (currentItem.Done) if (currentItem.Done)
{ {
ready.Play("ready"); ready.Play("ready");
@@ -291,18 +289,19 @@ namespace OpenRa.Game
ready.Play("hold"); ready.Play("hold");
overlayBits.Add(Pair.New(ready.Image, overlayPos)); overlayBits.Add(Pair.New(ready.Image, overlayPos));
} }
}
if (currentItem.Repeats > 0) var repeats = queue.AllItems(queueName).Count(a => a.Item == item);
if (repeats > 1 || isBuildingThis)
{
var offset = -20;
var digits = repeats.ToString();
foreach (var d in digits)
{ {
var offset = -20; ready.PlayFetchIndex("groups", () => d - '0');
var digits = (currentItem.Repeats + 1).ToString(); ready.Tick();
foreach (var d in digits) overlayBits.Add(Pair.New(ready.Image, overlayPos + new float2(offset, 0)));
{ offset += 6;
ready.PlayFetchIndex("groups", () => d - '0');
ready.Tick();
overlayBits.Add(Pair.New(ready.Image, overlayPos + new float2(offset, 0)));
offset += 6;
}
} }
} }
@@ -350,7 +349,7 @@ namespace OpenRa.Game
var player = Game.LocalPlayer; var player = Game.LocalPlayer;
var group = Rules.UnitCategory[item]; var group = Rules.UnitCategory[item];
var queue = player.PlayerActor.traits.Get<Traits.ProductionQueue>(); var queue = player.PlayerActor.traits.Get<Traits.ProductionQueue>();
var producing = queue.Producing( group ); var producing = queue.CurrentItem( group );
Sound.Play("ramenu1.aud"); Sound.Play("ramenu1.aud");

View File

@@ -40,7 +40,7 @@ namespace OpenRa.Game
public void Tick() public void Tick()
{ {
var producing = Producer.traits.Get<Traits.ProductionQueue>().Producing( Rules.UnitCategory[ Building.Name ] ); var producing = Producer.traits.Get<Traits.ProductionQueue>().CurrentItem( Rules.UnitCategory[ Building.Name ] );
if( producing == null || producing.Item != Building.Name || producing.RemainingTime != 0 ) if( producing == null || producing.Item != Building.Name || producing.RemainingTime != 0 )
Game.world.AddFrameEndTask( _ => { Game.controller.orderGenerator = null; } ); Game.world.AddFrameEndTask( _ => { Game.controller.orderGenerator = null; } );
} }

View File

@@ -11,8 +11,6 @@ namespace OpenRa.Game
public int RemainingTime { get; private set; } public int RemainingTime { get; private set; }
public int RemainingCost { get; private set; } public int RemainingCost { get; private set; }
public int Repeats;
public bool Paused = false, Done = false; public bool Paused = false, Done = false;
public Action OnComplete; public Action OnComplete;
@@ -26,14 +24,6 @@ namespace OpenRa.Game
OnComplete = onComplete; OnComplete = onComplete;
} }
public void DoRepeat()
{
RemainingTime = TotalTime;
RemainingCost = TotalCost;
Done = false;
--Repeats;
}
public void Tick(Player player) public void Tick(Player player)
{ {
if (Done) if (Done)

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using IjwFramework.Collections;
namespace OpenRa.Game.Traits namespace OpenRa.Game.Traits
{ {
@@ -12,15 +13,13 @@ namespace OpenRa.Game.Traits
public ProductionQueue( Actor self ) public ProductionQueue( Actor self )
{ {
this.self = self; this.self = self;
foreach( var cat in Rules.Categories.Keys )
ProductionInit( cat );
} }
public void Tick( Actor self ) public void Tick( Actor self )
{ {
foreach( var p in production ) foreach( var p in production )
if( p.Value != null ) if( p.Value.Count > 0 )
p.Value.Tick( self.Owner ); (p.Value)[0].Tick( self.Owner );
} }
public Order IssueOrder( Actor self, int2 xy, MouseInput mi, Actor underCursor ) public Order IssueOrder( Actor self, int2 xy, MouseInput mi, Actor underCursor )
@@ -67,14 +66,14 @@ namespace OpenRa.Game.Traits
} }
case "PauseProduction": case "PauseProduction":
{ {
var producing = Producing( Rules.UnitCategory[ order.TargetString ] ); var producing = CurrentItem( Rules.UnitCategory[ order.TargetString ] );
if( producing != null && producing.Item == order.TargetString ) if( producing != null && producing.Item == order.TargetString )
producing.Paused = ( order.TargetLocation.X != 0 ); producing.Paused = ( order.TargetLocation.X != 0 );
break; break;
} }
case "CancelProduction": case "CancelProduction":
{ {
var producing = Producing( Rules.UnitCategory[ order.TargetString ] ); var producing = CurrentItem( Rules.UnitCategory[ order.TargetString ] );
if( producing != null && producing.Item == order.TargetString ) if( producing != null && producing.Item == order.TargetString )
CancelProduction( Rules.UnitCategory[ order.TargetString ] ); CancelProduction( Rules.UnitCategory[ order.TargetString ] );
break; break;
@@ -82,27 +81,33 @@ namespace OpenRa.Game.Traits
} }
} }
// Key: Production category. Categories are: Building, Infantry, Vehicle, Ship, Plane (and one per super, if they're done in here) // Key: Production category.
readonly Dictionary<string, ProductionItem> production = new Dictionary<string, ProductionItem>(); readonly Cache<string, List<ProductionItem>> production
= new Cache<string, List<ProductionItem>>( _ => new List<ProductionItem>() );
void ProductionInit( string category ) public ProductionItem CurrentItem(string category)
{ {
production.Add( category, null ); return production[category].ElementAtOrDefault(0);
} }
public ProductionItem Producing( string category ) public IEnumerable<ProductionItem> AllItems(string category)
{ {
return production[ category ]; return production[category];
} }
public void CancelProduction( string category ) public void CancelProduction( string category )
{ {
var item = production[ category ]; var queue = production[ category ];
if( item == null ) return; if (queue.Count == 0) return;
if (item.Repeats > 0)
--item.Repeats; var lastIndex = queue.FindLastIndex( a => a.Item == queue[0].Item );
if (lastIndex > 0)
{
queue.RemoveAt(lastIndex);
}
else else
{ {
var item = queue[0];
self.Owner.GiveCash(item.TotalCost - item.RemainingCost); // refund what's been paid so far. self.Owner.GiveCash(item.TotalCost - item.RemainingCost); // refund what's been paid so far.
FinishProduction(category); FinishProduction(category);
} }
@@ -110,23 +115,14 @@ namespace OpenRa.Game.Traits
public void FinishProduction( string category ) public void FinishProduction( string category )
{ {
var item = production[category]; var queue = production[category];
if (item == null) return; if (queue.Count == 0) return;
if (item.Repeats > 0) queue.RemoveAt(0);
item.DoRepeat();
else
production[category] = null;
} }
public void BeginProduction( string group, ProductionItem item ) public void BeginProduction( string group, ProductionItem item )
{ {
if (production[group] != null) production[group].Add(item);
{
if (production[group].Item == item.Item)
++production[group].Repeats;
return;
}
production[ group ] = item;
} }
public void BuildUnit( string name ) public void BuildUnit( string name )

View File

@@ -19,7 +19,7 @@ namespace OpenRa.Game
{ {
var queue = order.Player.PlayerActor.traits.Get<Traits.ProductionQueue>(); var queue = order.Player.PlayerActor.traits.Get<Traits.ProductionQueue>();
var building = (BuildingInfo)Rules.UnitInfo[ order.TargetString ]; var building = (BuildingInfo)Rules.UnitInfo[ order.TargetString ];
var producing = queue.Producing(Rules.UnitCategory[order.TargetString]); var producing = queue.CurrentItem(Rules.UnitCategory[order.TargetString]);
if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 )
return; return;