#region Copyright & License Information /* * Copyright 2007-2011 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. For more information, * see COPYING. */ #endregion using System; using System.Collections.Generic; using System.Linq; using OpenRA.Graphics; using OpenRA.Mods.RA.Buildings; using OpenRA.Mods.RA.Effects; using OpenRA.Traits; using OpenRA.Mods.RA.Activities; namespace OpenRA.Mods.RA.Render { public class RenderBuildingInfo : RenderSimpleInfo { public readonly bool HasMakeAnimation = true; public readonly float2 Origin = float2.Zero; public override object Create(ActorInitializer init) { return new RenderBuilding(init, this);} public override IEnumerable RenderPreview(ActorInfo building, string Tileset, Player owner) { return base.RenderPreview(building, Tileset, owner) .Select(a => a.WithPos(a.Pos + building.Traits.Get().Origin)); } } public class RenderBuilding : RenderSimple, INotifyDamageStateChanged, IRenderModifier, INotifyKilled { readonly RenderBuildingInfo Info; public RenderBuilding( ActorInitializer init, RenderBuildingInfo info ) : this(init, info, () => 0) { } public RenderBuilding( ActorInitializer init, RenderBuildingInfo info, Func baseFacing ) : base(init.self, baseFacing) { Info = info; var self = init.self; // Work around a bogus crash anim.PlayRepeating( NormalizeSequence(self, "idle") ); // Can't call Complete() directly from ctor because other traits haven't been inited yet if (self.Info.Traits.Get().HasMakeAnimation && !init.Contains()) self.QueueActivity(new MakeAnimation(self, () => Complete(self))); else self.QueueActivity(new CallFunc(() => Complete(self))); } public IEnumerable ModifyRender(Actor self, IEnumerable r) { var disabled = self.TraitsImplementing().Any(d => d.Disabled); foreach (var a in r) { var ret = a.WithPos(a.Pos - Info.Origin); yield return ret; if (disabled) yield return ret.WithPalette("disabled").WithZOffset(1); } } void Complete( Actor self ) { anim.PlayRepeating( NormalizeSequence(self, "idle") ); foreach( var x in self.TraitsImplementing() ) x.BuildingComplete( self ); } public void PlayCustomAnimThen(Actor self, string name, Action a) { anim.PlayThen(NormalizeSequence(self, name), () => { anim.PlayRepeating(NormalizeSequence(self, "idle")); a(); }); } public void PlayCustomAnimRepeating(Actor self, string name) { anim.PlayThen(NormalizeSequence(self, name), () => { PlayCustomAnimRepeating(self, name); }); } public void PlayCustomAnimBackwards(Actor self, string name, Action a) { anim.PlayBackwardsThen(NormalizeSequence(self, name), () => { anim.PlayRepeating(NormalizeSequence(self, "idle")); a(); }); } public void CancelCustomAnim(Actor self) { anim.PlayRepeating( NormalizeSequence(self, "idle") ); } public virtual void DamageStateChanged(Actor self, AttackInfo e) { if (e.DamageState >= DamageState.Heavy && e.PreviousDamageState < DamageState.Heavy) anim.ReplaceAnim("damaged-idle"); else if (e.DamageState < DamageState.Heavy) anim.ReplaceAnim("idle"); } public void Killed(Actor self, AttackInfo e) { var bi = self.Info.Traits.Get(); FootprintUtils.UnpathableTiles(self.Info.Name, bi, self.Location).Do( t => self.World.AddFrameEndTask( w => w.Add(new Explosion(w, Traits.Util.CenterOfCell(t), "building", false, 0)))); } } }