Refactors LimitedAmmo to AmmoPool.

Removes Reloads trait.

This enables adding multiple AmmoPools via @ differentiators and
Name which adds the possibility to assign each armament to a specific
ammo pool.

Furthermore, this moves all Reloads functionality onto AmmoPool.

Now a combination of all three is possible on a single actor: no limited
ammo, limited ammo that can reload on its own, and limited ammo which
needs to be reloaded at a rearm actor.

Additionally moves RearmSound from Minelayer to AmmoPool.
This commit is contained in:
reaperrr
2015-01-04 19:52:07 +01:00
parent d631d8b780
commit 38d5163062
23 changed files with 337 additions and 255 deletions

View File

@@ -8,53 +8,64 @@
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class Rearm : Activity
{
readonly LimitedAmmo limitedAmmo;
int ticksPerPip = 25 * 2;
int remainingTicks = 25 * 2;
string sound = null;
readonly IEnumerable<AmmoPool> ammoPools;
readonly Dictionary<AmmoPool, int> ammoPoolsReloadTimes;
public Rearm(Actor self, string sound = null)
public Rearm(Actor self)
{
limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
if (limitedAmmo != null)
ticksPerPip = limitedAmmo.ReloadTimePerAmmo();
remainingTicks = ticksPerPip;
this.sound = sound;
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads);
if (ammoPools == null)
return;
ammoPoolsReloadTimes = ammoPools.ToDictionary(x => x, y => y.Info.ReloadTicks);
}
public override Activity Tick(Actor self)
{
if (IsCanceled || limitedAmmo == null)
if (IsCanceled || ammoPoolsReloadTimes == null)
return NextActivity;
if (--remainingTicks == 0)
var needsReloading = false;
foreach (var pool in ammoPools)
{
var hostBuilding = self.World.ActorMap.GetUnitsAt(self.Location)
.FirstOrDefault(a => a.HasTrait<RenderBuilding>());
if (pool.FullAmmo())
continue;
needsReloading = true;
if (--ammoPoolsReloadTimes[pool] > 0)
continue;
// HACK to check if we are on the helipad/airfield/etc.
var hostBuilding = self.World.ActorMap.GetUnitsAt(self.Location).FirstOrDefault(a => a.HasTrait<RenderBuilding>());
if (hostBuilding == null || !hostBuilding.IsInWorld)
return NextActivity;
if (!limitedAmmo.GiveAmmo())
return NextActivity;
if (!pool.GiveAmmo())
continue;
hostBuilding.Trait<RenderBuilding>().PlayCustomAnim(hostBuilding, "active");
var sound = pool.Info.RearmSound;
if (sound != null)
Sound.Play(sound, self.CenterPosition);
remainingTicks = limitedAmmo.ReloadTimePerAmmo();
ammoPoolsReloadTimes[pool] = pool.Info.ReloadTicks;
}
return this;
return needsReloading ? this : NextActivity;
}
}
}