/
WithSpriteBody.cs
141 lines (116 loc) · 4.73 KB
/
WithSpriteBody.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#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;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Default trait for rendering sprite-based actors.")]
public class WithSpriteBodyInfo : PausableConditionalTraitInfo, IRenderActorPreviewSpritesInfo, Requires<RenderSpritesInfo>
{
[Desc("Animation to play when the actor is created."), SequenceReference]
public readonly string StartSequence = null;
[Desc("Animation to play when the actor is idle."), SequenceReference]
public readonly string Sequence = "idle";
[Desc("Identifier used to assign modifying traits to this sprite body.")]
public readonly string Name = "body";
public override object Create(ActorInitializer init) { return new WithSpriteBody(init, this); }
public virtual IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
{
if (!EnabledByDefault)
yield break;
var anim = new Animation(init.World, image);
anim.PlayRepeating(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), Sequence));
yield return new SpriteActorPreview(anim, () => WVec.Zero, () => 0, p, rs.Scale);
}
}
public class WithSpriteBody : PausableConditionalTrait<WithSpriteBodyInfo>, INotifyDamageStateChanged, INotifyBuildComplete, IAutoMouseBounds
{
public readonly Animation DefaultAnimation;
readonly RenderSprites rs;
readonly Animation boundsAnimation;
public WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info)
: this(init, info, () => 0) { }
protected WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info, Func<int> baseFacing)
: base(info)
{
rs = init.Self.Trait<RenderSprites>();
Func<bool> paused = () => IsTraitPaused &&
DefaultAnimation.CurrentSequence.Name == NormalizeSequence(init.Self, Info.Sequence);
DefaultAnimation = new Animation(init.World, rs.GetImage(init.Self), baseFacing, paused);
rs.Add(new AnimationWithOffset(DefaultAnimation, null, () => IsTraitDisabled));
// Cache the bounds from the default sequence to avoid flickering when the animation changes
boundsAnimation = new Animation(init.World, rs.GetImage(init.Self), baseFacing, paused);
boundsAnimation.PlayRepeating(info.Sequence);
if (info.StartSequence != null)
PlayCustomAnimation(init.Self, info.StartSequence,
() => PlayCustomAnimationRepeating(init.Self, info.Sequence));
else
DefaultAnimation.PlayRepeating(NormalizeSequence(init.Self, info.Sequence));
}
public string NormalizeSequence(Actor self, string sequence)
{
return RenderSprites.NormalizeSequence(DefaultAnimation, self.GetDamageState(), sequence);
}
protected virtual void OnBuildComplete(Actor self)
{
DefaultAnimation.PlayRepeating(NormalizeSequence(self, Info.Sequence));
}
// TODO: Get rid of INotifyBuildComplete in favor of using the condition system
void INotifyBuildComplete.BuildingComplete(Actor self)
{
OnBuildComplete(self);
}
public void PlayCustomAnimation(Actor self, string name, Action after = null)
{
DefaultAnimation.PlayThen(NormalizeSequence(self, name), () =>
{
CancelCustomAnimation(self);
if (after != null)
after();
});
}
public void PlayCustomAnimationRepeating(Actor self, string name)
{
var sequence = NormalizeSequence(self, name);
DefaultAnimation.PlayRepeating(NormalizeSequence(self, sequence));
}
public void PlayCustomAnimationBackwards(Actor self, string name, Action after = null)
{
DefaultAnimation.PlayBackwardsThen(NormalizeSequence(self, name), () =>
{
CancelCustomAnimation(self);
if (after != null)
after();
});
}
public void CancelCustomAnimation(Actor self)
{
DefaultAnimation.PlayRepeating(NormalizeSequence(self, Info.Sequence));
}
protected virtual void DamageStateChanged(Actor self)
{
if (DefaultAnimation.CurrentSequence != null)
DefaultAnimation.ReplaceAnim(NormalizeSequence(self, DefaultAnimation.CurrentSequence.Name));
}
void INotifyDamageStateChanged.DamageStateChanged(Actor self, AttackInfo e)
{
DamageStateChanged(self);
}
Rectangle IAutoMouseBounds.AutoMouseoverBounds(Actor self, WorldRenderer wr)
{
return boundsAnimation.ScreenBounds(wr, self.CenterPosition, WVec.Zero, rs.Info.Scale);
}
}
}