#region Copyright & License Information /* * Copyright 2007-2018 The OpenRA Developers (see AUTHORS) * 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.Linq; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits.Render { [Desc("Render trait for buildings that change the sprite according to the remaining resource storage capacity across all depots.")] class WithSiloAnimationInfo : ConditionalTraitInfo, Requires, Requires { [Desc("Sequence to use for resources-dependent 'stages'."), SequenceReference] public readonly string Sequence = "stages"; [Desc("Internal resource stages. Does not have to match number of sequence frames.")] public readonly int Stages = 10; [Desc("Which sprite body to play the animation on.")] public readonly string Body = "body"; public override object Create(ActorInitializer init) { return new WithSiloAnimation(init, this); } } class WithSiloAnimation : ConditionalTrait, INotifyOwnerChanged { readonly WithSpriteBody wsb; PlayerResources playerResources; public WithSiloAnimation(ActorInitializer init, WithSiloAnimationInfo info) : base(info) { wsb = init.Self.TraitsImplementing().Single(w => w.Info.Name == info.Body); playerResources = init.Self.Owner.PlayerActor.Trait(); } void PlayAnimation(Actor self) { wsb.DefaultAnimation.PlayFetchIndex(wsb.NormalizeSequence(self, Info.Sequence), () => playerResources.ResourceCapacity != 0 ? ((Info.Stages * wsb.DefaultAnimation.CurrentSequence.Length - 1) * playerResources.Resources) / (Info.Stages * playerResources.ResourceCapacity) : 0); } void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { playerResources = newOwner.PlayerActor.Trait(); PlayAnimation(self); } protected override void TraitEnabled(Actor self) { PlayAnimation(self); } } }