Cat.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Cat.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.GamerServices;
  17. using Microsoft.Xna.Framework.Graphics;
  18. using Microsoft.Xna.Framework.Input;
  19. #endregion
  20. namespace Flocking
  21. {
  22. /// <summary>
  23. /// Represents a dangerous object that the flocks flees from
  24. /// </summary>
  25. class Cat : Animal
  26. {
  27. #region Fields
  28. protected Vector2 center;
  29. #endregion
  30. #region Initialization
  31. /// <summary>
  32. /// Sets up the Cat's move speed and puts it in the center of the screen
  33. /// </summary>
  34. /// <param name="tex">Texture to use</param>
  35. /// <param name="screenSize">Size of the sample screen</param>
  36. public Cat(Texture2D tex, int screenWidth, int screenHeight)
  37. : base(tex, screenWidth, screenHeight)
  38. {
  39. if (tex != null)
  40. {
  41. texture = tex;
  42. textureCenter = new Vector2(texture.Width / 2, texture.Height / 2);
  43. }
  44. center.X = screenWidth / 2;
  45. center.Y = screenHeight / 2;
  46. location = center;
  47. moveSpeed = 500.0f;
  48. animaltype = AnimalType.Cat;
  49. }
  50. #endregion
  51. #region Update and Draw
  52. /// <summary>
  53. /// Move the cat, keeping in inside screen boundry
  54. /// </summary>
  55. /// <param name="gameTime"></param>
  56. public override void Update(GameTime gameTime)
  57. {
  58. if (gameTime != null)
  59. {
  60. if (direction.Length() > .01f)
  61. {
  62. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  63. location.X += direction.X * moveSpeed * elapsedTime;
  64. if (location.X < 0.0f)
  65. {
  66. location.X = 0.0f;
  67. }
  68. else if (location.X > boundryWidth)
  69. {
  70. location.X = boundryWidth;
  71. }
  72. location.Y += direction.Y * moveSpeed * elapsedTime;
  73. if (location.Y < 0.0f)
  74. {
  75. location.Y = 0.0f;
  76. }
  77. else if (location.Y > boundryHeight)
  78. {
  79. location.Y = boundryHeight;
  80. }
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// Draw the cat
  86. /// </summary>
  87. /// <param name="spriteBatch"></param>
  88. /// <param name="gameTime"></param>
  89. public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
  90. {
  91. spriteBatch.Draw(texture, location, null, color,
  92. 0f, textureCenter, 1.0f, SpriteEffects.None, 0.0f);
  93. }
  94. #endregion
  95. #region Handle Input
  96. /// <summary>
  97. /// Poll the input state for movement
  98. /// </summary>
  99. /// <param name="input"></param>
  100. public void HandleInput(InputState input)
  101. {
  102. direction.X = input.MoveCatX;
  103. direction.Y = input.MoveCatY;
  104. }
  105. #endregion
  106. }
  107. }