Game Development Reference
In-Depth Information
using System;
1
using Microsoft.Xna.Framework;
2
using Microsoft.Xna.Framework.Content;
3
using Microsoft.Xna.Framework.Graphics;
4
using Microsoft.Xna.Framework.Input;
5
6
7
class Cannon : ThreeColorGameObject
{
8
Texture2D cannonBarrel;
9
float angle;
10
11
12
public Cannon(ContentManager Content)
: base (Content.Load<Texture2D>("spr_cannon_red"),
13
Content.Load<Texture2D>("spr_cannon_green"),
14
Content.Load<Texture2D>("spr_cannon_blue"))
15
{ this .cannonBarrel = Content.Load<Texture2D>("spr_cannon_barrel");
16
position = new Vector2(72, 405);
17
}
18
19
20
public override void HandleInput(InputHelper inputHelper)
{ if (inputHelper.KeyPressed(Keys.R))
21
Color = Color.Red;
22
else if (inputHelper.KeyPressed(Keys.G))
23
Color = Color.Green;
24
else if (inputHelper.KeyPressed(Keys.B))
25
Color = Color.Blue;
26
double opposite = inputHelper.MousePosition.Y
position.Y;
27
double adjacent = inputHelper.MousePosition.X
position.X;
28
angle = ( float )Math.Atan2(opposite, adjacent);
29
}
30
31
32
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{ spriteBatch.Draw(cannonBarrel, position, null , Color.White, angle,
33
new Vector2(34, 34), 1.0f, SpriteEffects.None, 0);
34
spriteBatch.Draw(currentColor, position
new Vector2(currentColor.Width,
35
currentColor.Height) / 2, Color.White);
36
}
37
38
39
public override void Reset()
{ base .Reset();
40
angle = 0.0f;
41
}
42
43
44
public Vector2 BallPosition
{ get
45
{ float opposite = ( float )Math.Sin(angle)
cannonBarrel.Width
0.5f;
46
float adjacent = ( float )Math.Cos(angle)
cannonBarrel.Width
0.5f;
47
return position + new Vector2(adjacent, opposite);
48
}
49
}
50
}
51
Listing 10.1
The final Cannon class
Search WWH ::




Custom Search