#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it 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.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
namespace OpenRA.Traits
{
public abstract class RenderSimpleInfo : ITraitInfo
{
public readonly string Image = null;
public readonly string Palette = null;
public abstract object Create(ActorInitializer init);
}
public abstract class RenderSimple : IRender, ITick
{
public Dictionary anims = new Dictionary();
public Animation anim { get { return anims[""].Animation; } protected set { anims[""].Animation = value; } }
public string GetImage(Actor self)
{
return self.Info.Traits.Get().Image ?? self.Info.Name;
}
public RenderSimple(Actor self, Func baseFacing)
{
anims.Add( "", new Animation( GetImage(self), baseFacing ) );
}
public virtual IEnumerable Render( Actor self )
{
var palette = self.Info.Traits.Get().Palette;
foreach( var a in anims.Values )
if( a.DisableFunc == null || !a.DisableFunc() )
yield return ( palette == null ) ? a.Image( self ) : a.Image( self ).WithPalette(palette);
}
public virtual void Tick(Actor self)
{
foreach( var a in anims.Values )
a.Animation.Tick();
}
public class AnimationWithOffset
{
public Animation Animation;
public Func OffsetFunc;
public Func DisableFunc;
public int ZOffset;
public AnimationWithOffset( Animation a )
: this( a, null, null )
{
}
public AnimationWithOffset( Animation a, Func o, Func d )
{
this.Animation = a;
this.OffsetFunc = o;
this.DisableFunc = d;
}
public Renderable Image( Actor self )
{
var r = Util.Centered( self, Animation.Image, self.CenterLocation
+ (OffsetFunc != null ? OffsetFunc() : float2.Zero) );
return ZOffset != 0 ? r.WithZOffset(ZOffset) : r;
}
public static implicit operator AnimationWithOffset( Animation a )
{
return new AnimationWithOffset( a );
}
}
}
}