Files
OpenRA/OpenRA.Mods.RA/Render/RenderBuilding.cs
2010-09-19 19:13:15 +12:00

117 lines
3.8 KiB
C#
Executable File

#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System;
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
using OpenRA.GameRules;
using System.Collections.Generic;
using System.Linq;
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);}
}
public class RenderBuilding : RenderSimple, INotifyDamage, INotifySold, IRenderModifier
{
readonly float2 Origin;
public RenderBuilding( ActorInitializer init )
: this( init, () => 0 )
{
Origin = init.self.Info.Traits.Get<RenderBuildingInfo>().Origin;
}
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r)
{
var disabled = self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
foreach (var a in r)
{
var ret = a.WithPos(a.Pos - Origin);
yield return ret;
if (disabled)
yield return ret.WithPalette("disabled").WithZOffset(1);
}
}
public RenderBuilding( ActorInitializer init, Func<int> baseFacing )
: base(init.self, baseFacing)
{
var self = init.self;
if( init.Contains<SkipMakeAnimsInit>() || !self.Info.Traits.Get<RenderBuildingInfo>().HasMakeAnimation )
anim.PlayThen( "idle", () => self.World.AddFrameEndTask( _ => Complete( self ) ) );
else
anim.PlayThen( "make", () => self.World.AddFrameEndTask( _ => Complete( self ) ) );
}
void Complete( Actor self )
{
anim.PlayRepeating( NormalizeSequence(self, "idle") );
foreach( var x in self.TraitsImplementing<INotifyBuildComplete>() )
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)
{
var hasSequence = anim.HasSequence(NormalizeSequence(self, name));
anim.PlayBackwardsThen(NormalizeSequence(self, name),
() => { anim.PlayRepeating(NormalizeSequence(self, "idle")); a(); });
}
public virtual void Damaged(Actor self, AttackInfo e)
{
if (!e.DamageStateChanged)
return;
if (e.DamageState == DamageState.Dead)
foreach (var t in Footprint.UnpathableTiles( self.Info.Name, self.Info.Traits.Get<BuildingInfo>(), self.Location ))
{
var cell = t; // required: c# fails at bindings
self.World.AddFrameEndTask(w => w.Add(new Explosion(w, Util.CenterOfCell(cell).ToInt2(), "building", false)));
}
else if (e.DamageState >= DamageState.Heavy && e.PreviousDamageState < DamageState.Heavy)
{
anim.ReplaceAnim("damaged-idle");
Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound, self.CenterLocation);
}
else if (e.DamageState < DamageState.Heavy)
anim.ReplaceAnim("idle");
}
public virtual void Selling( Actor self )
{
if( self.Info.Traits.Get<RenderBuildingInfo>().HasMakeAnimation )
anim.PlayBackwardsThen( "make", null );
foreach (var s in self.Info.Traits.Get<BuildingInfo>().SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterLocation);
}
public void Sold(Actor self) {}
}
}