Simplifying(?) some of the render stuff.

This commit is contained in:
Bob
2009-12-22 02:14:42 +13:00
parent 0413de89db
commit 86dc6d53e7
10 changed files with 114 additions and 93 deletions

View File

@@ -13,6 +13,7 @@ namespace OpenRa.Game.Graphics
public Animation( string name )
{
this.name = name.ToLowerInvariant();
tickFunc = () => { };
}
public Sprite Image

View File

@@ -169,6 +169,8 @@ namespace OpenRa.Game.Traits.Activities
var oldTotal = moveFractionTotal;
moveFraction += (int)Util.GetEffectiveSpeed(self);
if( moveFraction >= moveFractionTotal )
moveFraction = moveFractionTotal;
UpdateCenterLocation( self, mobile );
if( moveFraction >= moveFractionTotal )
{

View File

@@ -13,27 +13,19 @@ namespace OpenRa.Game.Traits
public RenderBuilding(Actor self)
: base(self)
{
Make(() =>
{
anim.PlayRepeating("idle");
}, self);
if( Game.skipMakeAnims )
Complete( self );
else
anim.PlayThen( "make", () => Complete( self ) );
DoBib(self, false);
}
protected void Make( Action after, Actor self )
void Complete( Actor self )
{
Action newAfter = () =>
{
after();
foreach (var x in self.traits.WithInterface<INotifyBuildComplete>())
x.BuildingComplete(self);
};
if (Game.skipMakeAnims)
newAfter();
else
anim.PlayThen("make", newAfter);
anim.PlayRepeating( "idle" );
foreach( var x in self.traits.WithInterface<INotifyBuildComplete>() )
x.BuildingComplete( self );
}
void DoBib(Actor self, bool isRemove)

View File

@@ -1,13 +1,17 @@

using System;
namespace OpenRa.Game.Traits
{
class RenderBuildingOre : RenderBuilding
class RenderBuildingOre : RenderBuilding, INotifyBuildComplete
{
public RenderBuildingOre(Actor self)
: base(self)
{
Make( () => anim.PlayFetchIndex("idle",
() => (int)(4.9 * self.Owner.GetSiloFullness())), self); /* hack */
}
public void BuildingComplete( Actor self )
{
anim.PlayFetchIndex( "idle", () => (int)( 4.9 * self.Owner.GetSiloFullness() ) );
}
}
}

View File

@@ -1,17 +1,21 @@

namespace OpenRa.Game.Traits
{
class RenderBuildingTurreted : RenderBuilding
class RenderBuildingTurreted : RenderBuilding, INotifyBuildComplete
{
public RenderBuildingTurreted(Actor self)
: base(self)
{
Make( () => PlayTurretAnim( self, "idle" ), self);
}
void PlayTurretAnim(Actor self, string a)
public void BuildingComplete( Actor self )
{
anim.PlayFacing(a, () => self.traits.Get<Turreted>().turretFacing);
PlayTurretAnim( self, "idle" );
}
void PlayTurretAnim( Actor self, string a )
{
anim.PlayFacing( a, () => self.traits.Get<Turreted>().turretFacing );
}
public override void Damaged(Actor self, AttackInfo e)

View File

@@ -3,7 +3,7 @@ using OpenRa.Game.Graphics;
namespace OpenRa.Game.Traits
{
class RenderWarFactory : RenderBuilding
class RenderWarFactory : RenderBuilding, INotifyBuildComplete
{
public Animation roof;
bool doneBuilding;
@@ -15,14 +15,14 @@ namespace OpenRa.Game.Traits
: base(self)
{
this.self = self;
roof = new Animation(self.Info.Image ?? self.Info.Name);
Make( () =>
{
doneBuilding = true;
anim.Play("idle");
roof.Play(prefix + "idle-top");
}, self);
}
public void BuildingComplete( Actor self )
{
doneBuilding = true;
anim.Play( "idle" );
roof.Play( prefix + "idle-top" );
}
public IEnumerable<Tuple<Sprite, float2, int>> RenderRoof(Actor self)

View File

@@ -1,22 +1,63 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using IjwFramework.Collections;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Traits
{
abstract class RenderSimple : IRender, ITick
{
public Animation anim;
public Dictionary<string, AnimationWithOffset> anims = new Dictionary<string, AnimationWithOffset>();
public Animation anim { get { return anims[ "" ].Animation; } }
public RenderSimple(Actor self)
{
anim = new Animation(self.Info.Image ?? self.Info.Name);
anims.Add( "", new Animation( self.Info.Image ?? self.Info.Name ) );
}
public abstract IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self);
public virtual IEnumerable<Tuple<Sprite, float2, int>> Render( Actor self )
{
foreach( var a in anims.Values )
if( a.DisableFunc == null || !a.DisableFunc() )
yield return a.Image( self );
}
public virtual void Tick(Actor self)
{
anim.Tick();
foreach( var a in anims.Values )
a.Animation.Tick();
}
public class AnimationWithOffset
{
public Animation Animation;
public Func<float2> OffsetFunc;
public Func<bool> DisableFunc;
public AnimationWithOffset( Animation a )
: this( a, null, null )
{
}
public AnimationWithOffset( Animation a, Func<float2> o, Func<bool> d )
{
this.Animation = a;
this.OffsetFunc = o;
this.DisableFunc = d;
}
public Tuple<Sprite, float2, int> Image( Actor self )
{
if( OffsetFunc != null )
return Util.Centered( self, Animation.Image, self.CenterLocation + OffsetFunc() );
else
return Util.Centered( self, Animation.Image, self.CenterLocation );
}
public static implicit operator AnimationWithOffset( Animation a )
{
return new AnimationWithOffset( a );
}
}
}
}

View File

@@ -11,7 +11,8 @@ namespace OpenRa.Game.Traits
: base(self)
{
PlayFacingAnim(self);
smoke = new Animation("smoke_m");
anims.Add( "smoke", new AnimationWithOffset( new Animation( "smoke_m" ), null, () => !isSmoking ) );
}
void PlayFacingAnim(Actor self)
@@ -25,15 +26,7 @@ namespace OpenRa.Game.Traits
anim.PlayThen(newAnim, () => { PlayFacingAnim(self); if (after != null) after(); });
}
public override IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self)
{
yield return Util.Centered(self, anim.Image, self.CenterLocation);
if (isSmoking)
yield return Util.Centered(self, smoke.Image, self.CenterLocation);
}
bool isSmoking;
Animation smoke;
public void Damaged(Actor self, AttackInfo e)
{
@@ -41,17 +34,11 @@ namespace OpenRa.Game.Traits
if (isSmoking) return;
isSmoking = true;
smoke.PlayThen("idle",
() => smoke.PlayThen("loop",
() => smoke.PlayBackwardsThen("end",
() => isSmoking = false)));
}
public override void Tick(Actor self)
{
base.Tick(self);
if (isSmoking)
smoke.Tick();
var smoke = anims[ "smoke" ].Animation;
smoke.PlayThen( "idle",
() => smoke.PlayThen( "loop",
() => smoke.PlayBackwardsThen( "end",
() => isSmoking = false ) ) );
}
}
}

View File

@@ -6,51 +6,41 @@ namespace OpenRa.Game.Traits
{
class RenderUnitTurreted : RenderUnit
{
public Animation turretAnim;
public Animation muzzleFlash;
public RenderUnitTurreted(Actor self)
: base(self)
{
self.traits.Get<Turreted>();
turretAnim = new Animation(self.Info.Name);
if (self.Info.MuzzleFlash)
{
var attack = self.traits.WithInterface<AttackBase>().First();
muzzleFlash = new Animation(self.Info.Name);
muzzleFlash.PlayFetchIndex("muzzle",
() => (Util.QuantizeFacing(self.traits.Get<Turreted>().turretFacing,8)) * 6
+ (int)(attack.primaryRecoil * 5.9f));
/* hack: recoil can be 1.0f, but don't overflow into next anim */
}
turretAnim.PlayFacing("turret", () => self.traits.Get<Turreted>().turretFacing);
}
public override IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self)
{
var unit = self.traits.Get<Unit>();
var turreted = self.traits.Get<Turreted>();
var attack = self.traits.WithInterface<AttackBase>().FirstOrDefault();
yield return Util.Centered(self, anim.Image, self.CenterLocation);
yield return Util.Centered(self, turretAnim.Image, self.CenterLocation
+ Util.GetTurretPosition(self, unit, self.Info.PrimaryOffset, attack.primaryRecoil));
if (self.Info.SecondaryOffset != null)
yield return Util.Centered(self, turretAnim.Image, self.CenterLocation
+ Util.GetTurretPosition(self, unit, self.Info.SecondaryOffset, attack.secondaryRecoil));
var turretAnim = new Animation(self.Info.Name);
turretAnim.PlayFacing( "turret", () => turreted.turretFacing );
if (muzzleFlash != null && attack.primaryRecoil > 0)
yield return Util.Centered(self, muzzleFlash.Image, self.CenterLocation
+ Util.GetTurretPosition(self, unit, self.Info.PrimaryOffset, attack.primaryRecoil));
}
if( self.Info.PrimaryOffset != null )
anims.Add( "turret_1", new AnimationWithOffset(
turretAnim,
() => Util.GetTurretPosition( self, unit, self.Info.PrimaryOffset, attack.primaryRecoil ),
null ) );
public override void Tick(Actor self)
{
base.Tick(self);
turretAnim.Tick();
if (muzzleFlash != null)
muzzleFlash.Tick();
if( self.Info.SecondaryOffset != null )
anims.Add( "turret_2", new AnimationWithOffset(
turretAnim,
() => Util.GetTurretPosition( self, unit, self.Info.SecondaryOffset, attack.secondaryRecoil ),
null ) );
if( self.Info.MuzzleFlash )
{
muzzleFlash = new Animation( self.Info.Name );
muzzleFlash.PlayFetchIndex( "muzzle",
() => ( Util.QuantizeFacing( self.traits.Get<Turreted>().turretFacing, 8 ) ) * 6
+ (int)( attack.primaryRecoil * 5.9f ) ); /* hack: recoil can be 1.0f, but don't overflow into next anim */
anims.Add( "muzzle_flash", new AnimationWithOffset(
muzzleFlash,
() => Util.GetTurretPosition( self, unit, self.Info.PrimaryOffset, attack.primaryRecoil ),
() => attack.primaryRecoil <= 0 ) );
}
}
}
}

View File

@@ -86,7 +86,7 @@ namespace OpenRa.Game.Traits
if (rut == null) return float2.Zero;
var facing = self.traits.Get<Turreted>().turretFacing;
var quantizedFacing = QuantizeFacing(facing, rut.turretAnim.CurrentSequence.Length) * (256 / rut.turretAnim.CurrentSequence.Length);
var quantizedFacing = QuantizeFacing(facing, rut.anim.CurrentSequence.Length) * (256 / rut.anim.CurrentSequence.Length);
return RotateVectorByFacing(new float2(0, recoil * self.Info.Recoil), quantizedFacing, .7f);
}