Game Development Reference
In-Depth Information
using
Microsoft.Xna.Framework;
1
2
3
class
Rocket : AnimatedGameObject
{
4
protected double
spawnTime;
5
protected
Vector2 startPosition;
6
7
8
public
Rocket(
bool
moveToLeft, Vector2 startPosition)
{
9
this
.LoadAnimation("Sprites/Rocket/spr_rocket@3", "default",
false
, 0.5f);
10
this
.PlayAnimation("default");
11
this
.Mirror = moveToLeft;
12
this
.startPosition = startPosition;
13
Reset();
14
}
15
16
17
public override void
Reset()
{
18
this
.Visible =
false
;
19
this
.position = startPosition;
20
this
.velocity = Vector2.Zero;
21
∗
this
.spawnTime = GameEnvironment.Random.NextDouble()
5;
22
}
23
24
25
public override void
Update(GameTime gameTime)
{
26
base
.Update(gameTime);
27
if
(spawnTime > 0)
28
{
29
−
= gameTime.ElapsedGameTime.TotalSeconds;
spawnTime
30
return
;
31
}
32
this
.Visible =
true
;
33
this
.velocity.X = 600;
34
if
(Mirror)
35
∗
=
−
1f;
this
.velocity.X
36
// check if we are outside the screen
37
Rectangle screenBox =
new
Rectangle(0, 0, GameEnvironment.Screen.X,
38
GameEnvironment.Screen.Y);
39
if
(!screenBox.Intersects(
this
.BoundingBox))
40
this
.Reset();
41
}
42
}
43
Listing 28.1
A class that represents a moving rocket