Cat.cs 3.2 KB

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