|
@@ -117,22 +117,23 @@ namespace ChaseAndEvade
|
|
|
MouseAiState mouseState = MouseAiState.Wander;
|
|
|
float mouseOrientation;
|
|
|
Vector2 mouseWanderDirection;
|
|
|
- Random random = new Random ();
|
|
|
+ Random random = new Random();
|
|
|
|
|
|
|
|
|
|
|
|
- public ChaseAndEvadeGame ()
|
|
|
+ public ChaseAndEvadeGame()
|
|
|
{
|
|
|
|
|
|
- graphics = new GraphicsDeviceManager (this);
|
|
|
-
|
|
|
+ graphics = new GraphicsDeviceManager(this);
|
|
|
+
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
|
|
-#if MOBILE
|
|
|
- graphics.IsFullScreen = true;
|
|
|
graphics.SupportedOrientations = DisplayOrientation.Portrait;
|
|
|
graphics.PreferredBackBufferWidth = 480;
|
|
|
- graphics.PreferredBackBufferHeight = 800;
|
|
|
+ graphics.PreferredBackBufferHeight = 640;
|
|
|
+
|
|
|
+#if ___MOBILE___
|
|
|
+ graphics.IsFullScreen = true;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
@@ -140,9 +141,9 @@ namespace ChaseAndEvade
|
|
|
/// Overridden from the base Game.Initialize. Once the GraphicsDevice is setup,
|
|
|
/// we'll use the viewport to initialize some values.
|
|
|
/// </summary>
|
|
|
- protected override void Initialize ()
|
|
|
+ protected override void Initialize()
|
|
|
{
|
|
|
- base.Initialize ();
|
|
|
+ base.Initialize();
|
|
|
|
|
|
// once base.Initialize has finished, the GraphicsDevice will have been
|
|
|
// created, and we'll know how big the Viewport is. We want the tank, cat
|
|
@@ -150,9 +151,9 @@ namespace ChaseAndEvade
|
|
|
// to figure out where they should be.
|
|
|
Viewport vp = graphics.GraphicsDevice.Viewport;
|
|
|
|
|
|
- tankPosition = new Vector2 (vp.Width / 4, vp.Height / 2);
|
|
|
- catPosition = new Vector2 (vp.Width / 2, vp.Height / 2);
|
|
|
- mousePosition = new Vector2 (3 * vp.Width / 4, vp.Height / 2);
|
|
|
+ tankPosition = new Vector2(vp.Width / 4, vp.Height / 2);
|
|
|
+ catPosition = new Vector2(vp.Width / 2, vp.Height / 2);
|
|
|
+ mousePosition = new Vector2(3 * vp.Width / 4, vp.Height / 2);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -160,15 +161,15 @@ namespace ChaseAndEvade
|
|
|
/// <summary>
|
|
|
/// Load your graphics content.
|
|
|
/// </summary>
|
|
|
- protected override void LoadContent ()
|
|
|
+ protected override void LoadContent()
|
|
|
{
|
|
|
// create a SpriteBatch, and load the textures and font that we'll need
|
|
|
// during the game.
|
|
|
- spriteBatch = new SpriteBatch (graphics.GraphicsDevice);
|
|
|
- spriteFont = Content.Load<SpriteFont> ("Arial");
|
|
|
- tankTexture = Content.Load<Texture2D> ("tank");
|
|
|
- catTexture = Content.Load<Texture2D> ("cat");
|
|
|
- mouseTexture = Content.Load<Texture2D> ("mouse");
|
|
|
+ spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
|
|
|
+ spriteFont = Content.Load<SpriteFont>("Arial");
|
|
|
+ tankTexture = Content.Load<Texture2D>("tank");
|
|
|
+ catTexture = Content.Load<Texture2D>("cat");
|
|
|
+ mouseTexture = Content.Load<Texture2D>("mouse");
|
|
|
|
|
|
// once all the content is loaded, we can calculate the centers of each
|
|
|
// of the textures that we loaded. Just like in the previous sample in
|
|
@@ -177,12 +178,12 @@ namespace ChaseAndEvade
|
|
|
// center the sprite on the vector that we pass in as the "origin"
|
|
|
// parameter, so we'll just calculate that to be the middle of
|
|
|
// the texture.
|
|
|
- tankTextureCenter =
|
|
|
- new Vector2 (tankTexture.Width / 2, tankTexture.Height / 2);
|
|
|
- catTextureCenter =
|
|
|
- new Vector2 (catTexture.Width / 2, catTexture.Height / 2);
|
|
|
- mouseTextureCenter =
|
|
|
- new Vector2 (mouseTexture.Width / 2, mouseTexture.Height / 2);
|
|
|
+ tankTextureCenter =
|
|
|
+ new Vector2(tankTexture.Width / 2, tankTexture.Height / 2);
|
|
|
+ catTextureCenter =
|
|
|
+ new Vector2(catTexture.Width / 2, catTexture.Height / 2);
|
|
|
+ mouseTextureCenter =
|
|
|
+ new Vector2(mouseTexture.Width / 2, mouseTexture.Height / 2);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -190,25 +191,25 @@ namespace ChaseAndEvade
|
|
|
/// <summary>
|
|
|
/// Allows the game to run logic.
|
|
|
/// </summary>
|
|
|
- protected override void Update (GameTime gameTime)
|
|
|
+ protected override void Update(GameTime gameTime)
|
|
|
{
|
|
|
// handle input will read the controller input, and update the cat
|
|
|
// to move according to the user's whim.
|
|
|
- HandleInput ();
|
|
|
+ HandleInput();
|
|
|
|
|
|
// UpdateTank will run the AI code that controls the tank's movement...
|
|
|
- UpdateTank ();
|
|
|
+ UpdateTank();
|
|
|
|
|
|
// ... and UpdateMouse does the same thing for the mouse.
|
|
|
- UpdateMouse ();
|
|
|
+ UpdateMouse();
|
|
|
|
|
|
// Once we've finished that, we'll use the ClampToViewport helper function
|
|
|
// to clamp everyone's position so that they stay on the screen.
|
|
|
- tankPosition = ClampToViewport (tankPosition);
|
|
|
- catPosition = ClampToViewport (catPosition);
|
|
|
- mousePosition = ClampToViewport (mousePosition);
|
|
|
+ tankPosition = ClampToViewport(tankPosition);
|
|
|
+ catPosition = ClampToViewport(catPosition);
|
|
|
+ mousePosition = ClampToViewport(mousePosition);
|
|
|
|
|
|
- base.Update (gameTime);
|
|
|
+ base.Update(gameTime);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -219,11 +220,11 @@ namespace ChaseAndEvade
|
|
|
/// <param name="vector">an input vector</param>
|
|
|
/// <returns>the input vector, clamped between the minimum and maximum of the
|
|
|
/// viewport.</returns>
|
|
|
- private Vector2 ClampToViewport (Vector2 vector)
|
|
|
+ private Vector2 ClampToViewport(Vector2 vector)
|
|
|
{
|
|
|
Viewport vp = graphics.GraphicsDevice.Viewport;
|
|
|
- vector.X = MathHelper.Clamp (vector.X, vp.X, vp.X + vp.Width);
|
|
|
- vector.Y = MathHelper.Clamp (vector.Y, vp.Y, vp.Y + vp.Height);
|
|
|
+ vector.X = MathHelper.Clamp(vector.X, vp.X, vp.X + vp.Width);
|
|
|
+ vector.Y = MathHelper.Clamp(vector.Y, vp.Y, vp.Y + vp.Height);
|
|
|
return vector;
|
|
|
}
|
|
|
|
|
@@ -233,7 +234,7 @@ namespace ChaseAndEvade
|
|
|
/// it will attempt to flee. Otherwise, it will idly wander around the screen.
|
|
|
///
|
|
|
/// </summary>
|
|
|
- private void UpdateMouse ()
|
|
|
+ private void UpdateMouse()
|
|
|
{
|
|
|
// first, calculate how far away the mouse is from the cat, and use that
|
|
|
// information to decide how to behave. If they are too close, the mouse
|
|
@@ -241,14 +242,16 @@ namespace ChaseAndEvade
|
|
|
// will switch to "idle" mode, where it roams around the screen.
|
|
|
// we use a hysteresis constant in the decision making process, as described
|
|
|
// in the accompanying doc file.
|
|
|
- float distanceFromCat = Vector2.Distance (mousePosition, catPosition);
|
|
|
+ float distanceFromCat = Vector2.Distance(mousePosition, catPosition);
|
|
|
|
|
|
// the cat is a safe distance away, so the mouse should idle:
|
|
|
- if (distanceFromCat > MouseEvadeDistance + MouseHysteresis) {
|
|
|
+ if (distanceFromCat > MouseEvadeDistance + MouseHysteresis)
|
|
|
+ {
|
|
|
mouseState = MouseAiState.Wander;
|
|
|
}
|
|
|
// the cat is too close; the mouse should run:
|
|
|
- else if (distanceFromCat < MouseEvadeDistance - MouseHysteresis) {
|
|
|
+ else if (distanceFromCat < MouseEvadeDistance - MouseHysteresis)
|
|
|
+ {
|
|
|
mouseState = MouseAiState.Evading;
|
|
|
}
|
|
|
// if neither of those if blocks hit, we are in the "hysteresis" range,
|
|
@@ -262,7 +265,8 @@ namespace ChaseAndEvade
|
|
|
|
|
|
// the second step of the Update is to change the mouse's orientation based
|
|
|
// on its current state.
|
|
|
- if (mouseState == MouseAiState.Evading) {
|
|
|
+ if (mouseState == MouseAiState.Evading)
|
|
|
+ {
|
|
|
// If the mouse is "active," it is trying to evade the cat. The evasion
|
|
|
// behavior is accomplished by using the TurnToFace function to turn
|
|
|
// towards a point on a straight line facing away from the cat. In other
|
|
@@ -277,20 +281,22 @@ namespace ChaseAndEvade
|
|
|
// Aiming sample, to turn the mouse towards the seekPosition. Now when
|
|
|
// the mouse moves forward, it'll be trying to move in a straight line
|
|
|
// away from the cat.
|
|
|
- mouseOrientation = TurnToFace (mousePosition, seekPosition,
|
|
|
+ mouseOrientation = TurnToFace(mousePosition, seekPosition,
|
|
|
mouseOrientation, MouseTurnSpeed);
|
|
|
|
|
|
// set currentMouseSpeed to MaxMouseSpeed - the mouse should run as fast
|
|
|
// as it can.
|
|
|
currentMouseSpeed = MaxMouseSpeed;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
// if the mouse isn't trying to evade the cat, it should just meander
|
|
|
// around the screen. we'll use the Wander function, which the mouse and
|
|
|
// tank share, to accomplish this. mouseWanderDirection and
|
|
|
// mouseOrientation are passed by ref so that the wander function can
|
|
|
// modify them. for more information on ref parameters, see
|
|
|
// http://msdn2.microsoft.com/en-us/library/14akc2c7(VS.80).aspx
|
|
|
- Wander (mousePosition, ref mouseWanderDirection, ref mouseOrientation,
|
|
|
+ Wander(mousePosition, ref mouseWanderDirection, ref mouseOrientation,
|
|
|
MouseTurnSpeed);
|
|
|
|
|
|
// if the mouse is wandering, it should only move at 25% of its maximum
|
|
@@ -303,8 +309,8 @@ namespace ChaseAndEvade
|
|
|
// angle. To do this, we'll use Cosine and Sine to tell us the x and y
|
|
|
// components of the heading vector. See the accompanying doc for more
|
|
|
// information.
|
|
|
- Vector2 heading = new Vector2 (
|
|
|
- (float)Math.Cos (mouseOrientation), (float)Math.Sin (mouseOrientation));
|
|
|
+ Vector2 heading = new Vector2(
|
|
|
+ (float)Math.Cos(mouseOrientation), (float)Math.Sin(mouseOrientation));
|
|
|
|
|
|
// by multiplying the heading and speed, we can get a velocity vector. the
|
|
|
// velocity vector is then added to the mouse's current position, moving him
|
|
@@ -318,7 +324,7 @@ namespace ChaseAndEvade
|
|
|
/// complicated: where mouse only has two states, idle and active, the Tank has
|
|
|
/// three.
|
|
|
/// </summary>
|
|
|
- private void UpdateTank ()
|
|
|
+ private void UpdateTank()
|
|
|
{
|
|
|
// However, the tank's behavior is more complicated than the mouse's, and so
|
|
|
// the decision making process is a little different.
|
|
@@ -331,50 +337,63 @@ namespace ChaseAndEvade
|
|
|
// if the tank is idle, he prefers to stay idle. we do this by making the
|
|
|
// chase distance smaller, so the tank will be less likely to begin chasing
|
|
|
// the cat.
|
|
|
- if (tankState == TankAiState.Wander) {
|
|
|
+ if (tankState == TankAiState.Wander)
|
|
|
+ {
|
|
|
tankChaseThreshold -= TankHysteresis / 2;
|
|
|
}
|
|
|
// similarly, if the tank is active, he prefers to stay active. we
|
|
|
// accomplish this by increasing the range of values that will cause the
|
|
|
// tank to go into the active state.
|
|
|
- else if (tankState == TankAiState.Chasing) {
|
|
|
+ else if (tankState == TankAiState.Chasing)
|
|
|
+ {
|
|
|
tankChaseThreshold += TankHysteresis / 2;
|
|
|
tankCaughtThreshold -= TankHysteresis / 2;
|
|
|
}
|
|
|
// the same logic is applied to the finished state.
|
|
|
- else if (tankState == TankAiState.Caught) {
|
|
|
+ else if (tankState == TankAiState.Caught)
|
|
|
+ {
|
|
|
tankCaughtThreshold += TankHysteresis / 2;
|
|
|
}
|
|
|
|
|
|
// Second, now that we know what the thresholds are, we compare the tank's
|
|
|
// distance from the cat against the thresholds to decide what the tank's
|
|
|
// current state is.
|
|
|
- float distanceFromCat = Vector2.Distance (tankPosition, catPosition);
|
|
|
- if (distanceFromCat > tankChaseThreshold) {
|
|
|
+ float distanceFromCat = Vector2.Distance(tankPosition, catPosition);
|
|
|
+ if (distanceFromCat > tankChaseThreshold)
|
|
|
+ {
|
|
|
// just like the mouse, if the tank is far away from the cat, it should
|
|
|
// idle.
|
|
|
tankState = TankAiState.Wander;
|
|
|
- } else if (distanceFromCat > tankCaughtThreshold) {
|
|
|
+ }
|
|
|
+ else if (distanceFromCat > tankCaughtThreshold)
|
|
|
+ {
|
|
|
tankState = TankAiState.Chasing;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
tankState = TankAiState.Caught;
|
|
|
}
|
|
|
|
|
|
// Third, once we know what state we're in, act on that state.
|
|
|
float currentTankSpeed;
|
|
|
- if (tankState == TankAiState.Chasing) {
|
|
|
+ if (tankState == TankAiState.Chasing)
|
|
|
+ {
|
|
|
// the tank wants to chase the cat, so it will just use the TurnToFace
|
|
|
// function to turn towards the cat's position. Then, when the tank
|
|
|
// moves forward, he will chase the cat.
|
|
|
- tankOrientation = TurnToFace (tankPosition, catPosition, tankOrientation,
|
|
|
+ tankOrientation = TurnToFace(tankPosition, catPosition, tankOrientation,
|
|
|
TankTurnSpeed);
|
|
|
currentTankSpeed = MaxTankSpeed;
|
|
|
- } else if (tankState == TankAiState.Wander) {
|
|
|
+ }
|
|
|
+ else if (tankState == TankAiState.Wander)
|
|
|
+ {
|
|
|
// wander works just like the mouse's.
|
|
|
- Wander (tankPosition, ref tankWanderDirection, ref tankOrientation,
|
|
|
+ Wander(tankPosition, ref tankWanderDirection, ref tankOrientation,
|
|
|
TankTurnSpeed);
|
|
|
currentTankSpeed = .25f * MaxTankSpeed;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
// this part is different from the mouse. if the tank catches the cat,
|
|
|
// it should stop. otherwise it will run right by, then spin around and
|
|
|
// try to catch it all over again. The end result is that it will kind
|
|
@@ -386,8 +405,8 @@ namespace ChaseAndEvade
|
|
|
// this calculation is also just like the mouse's: we construct a heading
|
|
|
// vector based on the tank's orientation, and then make the tank move along
|
|
|
// that heading.
|
|
|
- Vector2 heading = new Vector2 (
|
|
|
- (float)Math.Cos (tankOrientation), (float)Math.Sin (tankOrientation));
|
|
|
+ Vector2 heading = new Vector2(
|
|
|
+ (float)Math.Cos(tankOrientation), (float)Math.Sin(tankOrientation));
|
|
|
tankPosition += heading * currentTankSpeed;
|
|
|
}
|
|
|
|
|
@@ -406,7 +425,7 @@ namespace ChaseAndEvade
|
|
|
/// <param name="orientation">the character's orientation. this parameter is
|
|
|
/// also passed by reference and is an input/output parameter.</param>
|
|
|
/// <param name="turnSpeed">the character's maximum turning speed.</param>
|
|
|
- private void Wander (Vector2 position, ref Vector2 wanderDirection,
|
|
|
+ private void Wander(Vector2 position, ref Vector2 wanderDirection,
|
|
|
ref float orientation, float turnSpeed)
|
|
|
{
|
|
|
// The wander effect is accomplished by having the character aim in a random
|
|
@@ -421,19 +440,20 @@ namespace ChaseAndEvade
|
|
|
// behavior is. Larger numbers will make the characters "wobble" more,
|
|
|
// smaller numbers will make them more stable. we want just enough
|
|
|
// wobbliness to be interesting without looking odd.
|
|
|
- wanderDirection.X +=
|
|
|
- MathHelper.Lerp (-.25f, .25f, (float)random.NextDouble ());
|
|
|
- wanderDirection.Y +=
|
|
|
- MathHelper.Lerp (-.25f, .25f, (float)random.NextDouble ());
|
|
|
+ wanderDirection.X +=
|
|
|
+ MathHelper.Lerp(-.25f, .25f, (float)random.NextDouble());
|
|
|
+ wanderDirection.Y +=
|
|
|
+ MathHelper.Lerp(-.25f, .25f, (float)random.NextDouble());
|
|
|
|
|
|
// we'll renormalize the wander direction, ...
|
|
|
- if (wanderDirection != Vector2.Zero) {
|
|
|
- wanderDirection.Normalize ();
|
|
|
+ if (wanderDirection != Vector2.Zero)
|
|
|
+ {
|
|
|
+ wanderDirection.Normalize();
|
|
|
}
|
|
|
// ... and then turn to face in the wander direction. We don't turn at the
|
|
|
// maximum turning speed, but at 15% of it. Again, this is a bit of a magic
|
|
|
// number: it works well for this sample, but feel free to tweak it.
|
|
|
- orientation = TurnToFace (position, position + wanderDirection, orientation,
|
|
|
+ orientation = TurnToFace(position, position + wanderDirection, orientation,
|
|
|
.15f * turnSpeed);
|
|
|
|
|
|
|
|
@@ -453,19 +473,19 @@ namespace ChaseAndEvade
|
|
|
// stuck on the walls. Larger numbers will hold the characters to center of
|
|
|
// the screen. If the number is too large, the characters may end up
|
|
|
// "orbiting" the center.
|
|
|
- float distanceFromScreenCenter = Vector2.Distance (screenCenter, position);
|
|
|
- float MaxDistanceFromScreenCenter =
|
|
|
- Math.Min (screenCenter.Y, screenCenter.X);
|
|
|
+ float distanceFromScreenCenter = Vector2.Distance(screenCenter, position);
|
|
|
+ float MaxDistanceFromScreenCenter =
|
|
|
+ Math.Min(screenCenter.Y, screenCenter.X);
|
|
|
|
|
|
- float normalizedDistance =
|
|
|
+ float normalizedDistance =
|
|
|
distanceFromScreenCenter / MaxDistanceFromScreenCenter;
|
|
|
|
|
|
- float turnToCenterSpeed = .3f * normalizedDistance * normalizedDistance *
|
|
|
+ float turnToCenterSpeed = .3f * normalizedDistance * normalizedDistance *
|
|
|
turnSpeed;
|
|
|
|
|
|
// once we've calculated how much we want to turn towards the center, we can
|
|
|
// use the TurnToFace function to actually do the work.
|
|
|
- orientation = TurnToFace (position, screenCenter, orientation,
|
|
|
+ orientation = TurnToFace(position, screenCenter, orientation,
|
|
|
turnToCenterSpeed);
|
|
|
}
|
|
|
|
|
@@ -474,7 +494,7 @@ namespace ChaseAndEvade
|
|
|
/// Calculates the angle that an object should face, given its position, its
|
|
|
/// target's position, its current angle, and its maximum turning speed.
|
|
|
/// </summary>
|
|
|
- private static float TurnToFace (Vector2 position, Vector2 faceThis,
|
|
|
+ private static float TurnToFace(Vector2 position, Vector2 faceThis,
|
|
|
float currentAngle, float turnSpeed)
|
|
|
{
|
|
|
// consider this diagram:
|
|
@@ -503,7 +523,7 @@ namespace ChaseAndEvade
|
|
|
// y / x for us, and has the added benefit that it will use the signs of x
|
|
|
// and y to determine what cartesian quadrant to put the result in.
|
|
|
// http://msdn2.microsoft.com/en-us/library/system.math.atan2.aspx
|
|
|
- float desiredAngle = (float)Math.Atan2 (y, x);
|
|
|
+ float desiredAngle = (float)Math.Atan2(y, x);
|
|
|
|
|
|
// so now we know where we WANT to be facing, and where we ARE facing...
|
|
|
// if we weren't constrained by turnSpeed, this would be easy: we'd just
|
|
@@ -513,14 +533,14 @@ namespace ChaseAndEvade
|
|
|
|
|
|
// first, figure out how much we want to turn, using WrapAngle to get our
|
|
|
// result from -Pi to Pi ( -180 degrees to 180 degrees )
|
|
|
- float difference = WrapAngle (desiredAngle - currentAngle);
|
|
|
+ float difference = WrapAngle(desiredAngle - currentAngle);
|
|
|
|
|
|
// clamp that between -turnSpeed and turnSpeed.
|
|
|
- difference = MathHelper.Clamp (difference, -turnSpeed, turnSpeed);
|
|
|
+ difference = MathHelper.Clamp(difference, -turnSpeed, turnSpeed);
|
|
|
|
|
|
// so, the closest we can get to our target is currentAngle + difference.
|
|
|
// return that, using WrapAngle again.
|
|
|
- return WrapAngle (currentAngle + difference);
|
|
|
+ return WrapAngle(currentAngle + difference);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -528,12 +548,14 @@ namespace ChaseAndEvade
|
|
|
/// <param name="radians">the angle to wrap, in radians.</param>
|
|
|
/// <returns>the input value expressed in radians from -Pi to Pi.</returns>
|
|
|
/// </summary>
|
|
|
- private static float WrapAngle (float radians)
|
|
|
+ private static float WrapAngle(float radians)
|
|
|
{
|
|
|
- while (radians < -MathHelper.Pi) {
|
|
|
+ while (radians < -MathHelper.Pi)
|
|
|
+ {
|
|
|
radians += MathHelper.TwoPi;
|
|
|
}
|
|
|
- while (radians > MathHelper.Pi) {
|
|
|
+ while (radians > MathHelper.Pi)
|
|
|
+ {
|
|
|
radians -= MathHelper.TwoPi;
|
|
|
}
|
|
|
return radians;
|
|
@@ -545,20 +567,20 @@ namespace ChaseAndEvade
|
|
|
/// mouse, and some overlay text. Once we're finished drawing, we'll call
|
|
|
/// SpriteBatch.End.
|
|
|
/// </summary>
|
|
|
- protected override void Draw (GameTime gameTime)
|
|
|
+ protected override void Draw(GameTime gameTime)
|
|
|
{
|
|
|
GraphicsDevice device = graphics.GraphicsDevice;
|
|
|
|
|
|
- device.Clear (Color.CornflowerBlue);
|
|
|
+ device.Clear(Color.CornflowerBlue);
|
|
|
|
|
|
- spriteBatch.Begin ();
|
|
|
+ spriteBatch.Begin();
|
|
|
|
|
|
// draw the tank, cat and mouse...
|
|
|
- spriteBatch.Draw (tankTexture, tankPosition, null, Color.White,
|
|
|
+ spriteBatch.Draw(tankTexture, tankPosition, null, Color.White,
|
|
|
tankOrientation, tankTextureCenter, 1.0f, SpriteEffects.None, 0.0f);
|
|
|
- spriteBatch.Draw (catTexture, catPosition, null, Color.White,
|
|
|
+ spriteBatch.Draw(catTexture, catPosition, null, Color.White,
|
|
|
0.0f, catTextureCenter, 1.0f, SpriteEffects.None, 0.0f);
|
|
|
- spriteBatch.Draw (mouseTexture, mousePosition, null, Color.White,
|
|
|
+ spriteBatch.Draw(mouseTexture, mousePosition, null, Color.White,
|
|
|
mouseOrientation, mouseTextureCenter, 1.0f, SpriteEffects.None, 0.0f);
|
|
|
|
|
|
// and then draw some text showing the tank's and mouse's current state.
|
|
@@ -566,19 +588,19 @@ namespace ChaseAndEvade
|
|
|
// and once white, to create a drop shadow effect.
|
|
|
Vector2 shadowOffset = Vector2.One;
|
|
|
|
|
|
- spriteBatch.DrawString (spriteFont, "Tank State: \n" + tankState.ToString (),
|
|
|
- new Vector2 (10, 10) + shadowOffset, Color.Black);
|
|
|
- spriteBatch.DrawString (spriteFont, "Tank State: \n" + tankState.ToString (),
|
|
|
- new Vector2 (10, 10), Color.White);
|
|
|
+ spriteBatch.DrawString(spriteFont, "Tank State: \n" + tankState.ToString(),
|
|
|
+ new Vector2(10, 10) + shadowOffset, Color.Black);
|
|
|
+ spriteBatch.DrawString(spriteFont, "Tank State: \n" + tankState.ToString(),
|
|
|
+ new Vector2(10, 10), Color.White);
|
|
|
|
|
|
- spriteBatch.DrawString (spriteFont, "Mouse State: \n" + mouseState.ToString (),
|
|
|
- new Vector2 (10, 90) + shadowOffset, Color.Black);
|
|
|
- spriteBatch.DrawString (spriteFont, "Mouse State: \n" + mouseState.ToString (),
|
|
|
- new Vector2 (10, 90), Color.White);
|
|
|
+ spriteBatch.DrawString(spriteFont, "Mouse State: \n" + mouseState.ToString(),
|
|
|
+ new Vector2(10, 90) + shadowOffset, Color.Black);
|
|
|
+ spriteBatch.DrawString(spriteFont, "Mouse State: \n" + mouseState.ToString(),
|
|
|
+ new Vector2(10, 90), Color.White);
|
|
|
|
|
|
- spriteBatch.End ();
|
|
|
+ spriteBatch.End();
|
|
|
|
|
|
- base.Draw (gameTime);
|
|
|
+ base.Draw(gameTime);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -586,15 +608,15 @@ namespace ChaseAndEvade
|
|
|
/// <summary>
|
|
|
/// Handles input for quitting the game.
|
|
|
/// </summary>
|
|
|
- void HandleInput ()
|
|
|
+ void HandleInput()
|
|
|
{
|
|
|
#if WINDOWS_PHONE
|
|
|
KeyboardState currentKeyboardState = new KeyboardState();
|
|
|
#else
|
|
|
- KeyboardState currentKeyboardState = Keyboard.GetState ();
|
|
|
- MouseState currentMouseState = Mouse.GetState ();
|
|
|
+ KeyboardState currentKeyboardState = Keyboard.GetState();
|
|
|
+ MouseState currentMouseState = Mouse.GetState();
|
|
|
#endif
|
|
|
-
|
|
|
+
|
|
|
#if IPHONE || PSM
|
|
|
GamePadState currentGamePadState = GamePad.GetState (PlayerIndex.One);
|
|
|
|
|
@@ -605,20 +627,21 @@ namespace ChaseAndEvade
|
|
|
}
|
|
|
#else
|
|
|
// Check for exit.
|
|
|
- if (currentKeyboardState.IsKeyDown (Keys.Escape)) {
|
|
|
- Exit ();
|
|
|
+ if (currentKeyboardState.IsKeyDown(Keys.Escape))
|
|
|
+ {
|
|
|
+ Exit();
|
|
|
}
|
|
|
-
|
|
|
-#endif
|
|
|
+
|
|
|
+#endif
|
|
|
|
|
|
// check to see if the user wants to move the cat. we'll create a vector
|
|
|
// called catMovement, which will store the sum of all the user's inputs.
|
|
|
Vector2 catMovement = Vector2.Zero;
|
|
|
|
|
|
//Move toward the touch point. We slow down the cat when it gets within a distance of MaxCatSpeed to the touch point.
|
|
|
- float smoothStop = 1;
|
|
|
-
|
|
|
-#if IPHONE || PSM
|
|
|
+ float smoothStop = 1;
|
|
|
+
|
|
|
+#if IPHONE || PSM
|
|
|
// check to see if the user wants to move the cat. we'll create a vector
|
|
|
// called catMovement, which will store the sum of all the user's inputs.
|
|
|
catMovement = currentGamePadState.ThumbSticks.Left;
|
|
@@ -662,35 +685,41 @@ namespace ChaseAndEvade
|
|
|
#else
|
|
|
|
|
|
|
|
|
- if (currentKeyboardState.IsKeyDown (Keys.Left)) {
|
|
|
+ if (currentKeyboardState.IsKeyDown(Keys.Left))
|
|
|
+ {
|
|
|
catMovement.X -= 1.0f;
|
|
|
}
|
|
|
- if (currentKeyboardState.IsKeyDown (Keys.Right)) {
|
|
|
+ if (currentKeyboardState.IsKeyDown(Keys.Right))
|
|
|
+ {
|
|
|
catMovement.X += 1.0f;
|
|
|
}
|
|
|
- if (currentKeyboardState.IsKeyDown (Keys.Up)) {
|
|
|
+ if (currentKeyboardState.IsKeyDown(Keys.Up))
|
|
|
+ {
|
|
|
catMovement.Y -= 1.0f;
|
|
|
}
|
|
|
- if (currentKeyboardState.IsKeyDown (Keys.Down)) {
|
|
|
+ if (currentKeyboardState.IsKeyDown(Keys.Down))
|
|
|
+ {
|
|
|
catMovement.Y += 1.0f;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- Vector2 mousePosition = new Vector2 (currentMouseState.X, currentMouseState.Y);
|
|
|
- if (currentMouseState.LeftButton == ButtonState.Pressed && mousePosition != catPosition) {
|
|
|
+ Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
|
|
|
+ if (currentMouseState.LeftButton == ButtonState.Pressed && mousePosition != catPosition)
|
|
|
+ {
|
|
|
catMovement = mousePosition - catPosition;
|
|
|
- float delta = MaxCatSpeed - MathHelper.Clamp (catMovement.Length (), 0, MaxCatSpeed);
|
|
|
+ float delta = MaxCatSpeed - MathHelper.Clamp(catMovement.Length(), 0, MaxCatSpeed);
|
|
|
smoothStop = 1 - delta / MaxCatSpeed;
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
// normalize the user's input, so the cat can never be going faster than
|
|
|
// CatSpeed.
|
|
|
- if (catMovement != Vector2.Zero) {
|
|
|
- catMovement.Normalize ();
|
|
|
+ if (catMovement != Vector2.Zero)
|
|
|
+ {
|
|
|
+ catMovement.Normalize();
|
|
|
}
|
|
|
|
|
|
catPosition += catMovement * MaxCatSpeed * smoothStop;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-}
|
|
|
+}
|