Cache passenger bounty traits.
This avoids querying from potentially dead actors.
This commit is contained in:
committed by
Oliver Brakmann
parent
49621bebd0
commit
f9cf45e634
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Effects;
|
||||
using OpenRA.Primitives;
|
||||
@@ -35,20 +36,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public override object Create(ActorInitializer init) { return new GivesBounty(this); }
|
||||
}
|
||||
|
||||
class GivesBounty : ConditionalTrait<GivesBountyInfo>, INotifyKilled
|
||||
class GivesBounty : ConditionalTrait<GivesBountyInfo>, INotifyKilled, INotifyPassengerEntered, INotifyPassengerExited
|
||||
{
|
||||
Cargo cargo;
|
||||
Dictionary<Actor, GivesBounty[]> passengerBounties = new Dictionary<Actor, GivesBounty[]>();
|
||||
|
||||
public GivesBounty(GivesBountyInfo info)
|
||||
: base(info) { }
|
||||
|
||||
protected override void Created(Actor self)
|
||||
{
|
||||
base.Created(self);
|
||||
|
||||
cargo = self.TraitOrDefault<Cargo>();
|
||||
}
|
||||
|
||||
int GetBountyValue(Actor self)
|
||||
{
|
||||
return self.GetSellValue() * Info.Percentage / 100;
|
||||
@@ -57,15 +51,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
int GetDisplayedBountyValue(Actor self)
|
||||
{
|
||||
var bounty = GetBountyValue(self);
|
||||
if (cargo == null)
|
||||
return bounty;
|
||||
|
||||
foreach (var a in cargo.Passengers)
|
||||
{
|
||||
var givesBounties = a.TraitsImplementing<GivesBounty>().Where(gb => !gb.IsTraitDisabled);
|
||||
foreach (var givesBounty in givesBounties)
|
||||
bounty += givesBounty.GetDisplayedBountyValue(a);
|
||||
}
|
||||
foreach (var pb in passengerBounties)
|
||||
foreach (var b in pb.Value)
|
||||
if (!b.IsTraitDisabled)
|
||||
bounty += b.GetDisplayedBountyValue(pb.Key);
|
||||
|
||||
return bounty;
|
||||
}
|
||||
@@ -87,5 +76,15 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().ChangeCash(GetBountyValue(self));
|
||||
}
|
||||
|
||||
void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger)
|
||||
{
|
||||
passengerBounties.Add(passenger, passenger.TraitsImplementing<GivesBounty>().ToArray());
|
||||
}
|
||||
|
||||
void INotifyPassengerExited.OnPassengerExited(Actor self, Actor passenger)
|
||||
{
|
||||
passengerBounties.Remove(passenger);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user