Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
81
OpenRA.Mods.Common/Traits/Rearmable.cs
Normal file
81
OpenRA.Mods.Common/Traits/Rearmable.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class RearmableInfo : TraitInfo
|
||||
{
|
||||
[ActorReference]
|
||||
[FieldLoader.Require]
|
||||
[Desc("Actors that this actor can dock to and get rearmed by.")]
|
||||
public readonly FrozenSet<string> RearmActors = FrozenSet<string>.Empty;
|
||||
|
||||
[Desc("Name(s) of AmmoPool(s) that use this trait to rearm.")]
|
||||
public readonly FrozenSet<string> AmmoPools = new HashSet<string> { "primary" }.ToFrozenSet();
|
||||
|
||||
public override object Create(ActorInitializer init) { return new Rearmable(this); }
|
||||
}
|
||||
|
||||
public class Rearmable : INotifyCreated, INotifyDockClient
|
||||
{
|
||||
public readonly RearmableInfo Info;
|
||||
|
||||
public Rearmable(RearmableInfo info)
|
||||
{
|
||||
Info = info;
|
||||
}
|
||||
|
||||
public AmmoPool[] RearmableAmmoPools { get; private set; }
|
||||
|
||||
void INotifyCreated.Created(Actor self)
|
||||
{
|
||||
RearmableAmmoPools = self.TraitsImplementing<AmmoPool>().Where(p => Info.AmmoPools.Contains(p.Info.Name)).ToArray();
|
||||
}
|
||||
|
||||
void INotifyDockClient.Docked(Actor self, Actor dock)
|
||||
{
|
||||
// Reset the ReloadDelay to avoid any issues with early cancellation
|
||||
// from previous reload attempts (explicit order, host building died, etc).
|
||||
foreach (var pool in RearmableAmmoPools)
|
||||
pool.RemainingTicks = pool.Info.ReloadDelay;
|
||||
}
|
||||
|
||||
void INotifyDockClient.Undocked(Actor self, Actor dock) { }
|
||||
|
||||
public bool RearmTick(Actor self)
|
||||
{
|
||||
var rearmComplete = true;
|
||||
foreach (var ammoPool in RearmableAmmoPools)
|
||||
{
|
||||
if (!ammoPool.HasFullAmmo)
|
||||
{
|
||||
if (--ammoPool.RemainingTicks <= 0)
|
||||
{
|
||||
ammoPool.RemainingTicks = ammoPool.Info.ReloadDelay;
|
||||
if (!string.IsNullOrEmpty(ammoPool.Info.RearmSound))
|
||||
Game.Sound.PlayToPlayer(SoundType.World, self.Owner, ammoPool.Info.RearmSound, self.CenterPosition);
|
||||
|
||||
ammoPool.GiveAmmo(self, ammoPool.Info.ReloadCount);
|
||||
}
|
||||
|
||||
rearmComplete = false;
|
||||
}
|
||||
}
|
||||
|
||||
return rearmComplete;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user