Game Development Reference
In-Depth Information
using
System.Collections.Generic;
1
using
Microsoft.Xna.Framework;
2
3
4
public class
AnimatedGameObject : SpriteGameObject
{
5
protected
Dictionary<
string
,Animation> animations;
6
7
8
public
AnimatedGameObject(
int
layer = 0,
string
id = "")
:
base
("", layer, id)
9
{
10
animations =
new
Dictionary<
string
, Animation>();
11
}
12
13
14
public void
LoadAnimation(
string
assetname,
string
id,
bool
looping,
float
frametime = 0.1f)
15
{
16
Animation anim =
new
Animation(assetname, looping, frametime);
17
animations[id] = anim;
18
}
19
20
21
public void
PlayAnimation(
string
id)
{
22
if
(sprite == animations[id])
23
return
;
24
if
(sprite !=
null
)
25
animations[id].Mirror = sprite.Mirror;
26
animations[id].Play();
27
sprite = animations[id];
28
origin =
new
Vector2(sprite.Width / 2, sprite.Height);
29
}
30
31
32
public override void
Update(GameTime gameTime)
{
33
if
(sprite ==
null
)
34
return
;
35
Current.Update(gameTime);
36
base
.Update(gameTime);
37
}
38
39
40
public
Animation Current
{
41
get
{
return
sprite
as
Animation; }
42
}
43
}
44
Listing 26.1
A class that represents an animated game object