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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user