2
0

Flock.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Flock.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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. #endregion
  17. namespace Flocking
  18. {
  19. /// <summary>
  20. /// This class manages all the birds in the flock and handles
  21. /// their update and draw
  22. /// </summary>
  23. class Flock
  24. {
  25. #region Constants
  26. //Number of FLock members
  27. const int flockSize = 40;
  28. #endregion
  29. #region Fields
  30. //birds that fly out of the boundry(screen) will wrap around to
  31. //the other side
  32. int boundryWidth;
  33. int boundryHeight;
  34. /// <summary>
  35. /// Tecture used to draw the Flock
  36. /// </summary>
  37. Texture2D birdTexture;
  38. /// <summary>
  39. /// List of Flock Members
  40. /// </summary>
  41. List<Bird> flock;
  42. /// <summary>
  43. /// Parameters flock members use to move and think
  44. /// </summary>
  45. public AIParameters FlockParams
  46. {
  47. get
  48. {
  49. return FlockParams;
  50. }
  51. set
  52. {
  53. flockParams = value;
  54. }
  55. }
  56. protected AIParameters flockParams;
  57. #endregion
  58. #region Initialization
  59. /// <summary>
  60. /// Setup the flock boundaries and generate individual members of the flock
  61. /// </summary>
  62. /// <param name="tex"> The texture to be used by the birds</param>
  63. /// <param name="screenWidth">Width of the screen</param>
  64. /// <param name="screenHeight">Height of the screen</param>
  65. /// <param name="flockParameters">Behavior of the flock</param>
  66. public Flock(Texture2D tex, int screenWidth, int screenHeight,
  67. AIParameters flockParameters)
  68. {
  69. boundryWidth = screenWidth;
  70. boundryHeight = screenHeight;
  71. birdTexture = tex;
  72. flock = new List<Bird>();
  73. flockParams = flockParameters;
  74. ResetFlock();
  75. }
  76. #endregion
  77. #region Update and Draw
  78. /// <summary>
  79. /// Update each flock member, Each bird want to fly with or flee from everything
  80. /// it sees depending on what type it is
  81. /// </summary>
  82. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  83. /// <param name="cat"></param>
  84. public void Update(GameTime gameTime, Cat cat)
  85. {
  86. foreach (Bird thisBird in flock)
  87. {
  88. thisBird.ResetThink();
  89. foreach (Bird otherBird in flock)
  90. {
  91. //this check is so we don't try to fly to ourself!
  92. if (thisBird != otherBird)
  93. {
  94. thisBird.ReactTo(otherBird, ref flockParams);
  95. }
  96. }
  97. //Look for the cat
  98. thisBird.ReactTo(cat, ref flockParams);
  99. thisBird.Update(gameTime, ref flockParams);
  100. }
  101. }
  102. /// <summary>
  103. /// Calls Draw on every member of the Flock
  104. /// </summary>
  105. /// <param name="spriteBatch"></param>
  106. /// <param name="gameTime"></param>
  107. public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
  108. {
  109. foreach (Bird theBird in flock)
  110. {
  111. theBird.Draw(spriteBatch, gameTime);
  112. }
  113. }
  114. #endregion
  115. #region Methods
  116. /// <summary>
  117. /// Clear the current flock if it exists and randomly generate a new one
  118. /// </summary>
  119. public void ResetFlock()
  120. {
  121. flock.Clear();
  122. flock.Capacity = flockSize;
  123. Bird tempBird;
  124. Vector2 tempDir;
  125. Vector2 tempLoc;
  126. Random random = new Random();
  127. for (int i = 0; i < flockSize; i++)
  128. {
  129. tempLoc = new Vector2((float)
  130. random.Next(boundryWidth), (float)random.Next(boundryHeight));
  131. tempDir = new Vector2((float)
  132. random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
  133. tempDir.Normalize();
  134. tempBird = new Bird(birdTexture, tempDir, tempLoc,
  135. boundryWidth, boundryHeight);
  136. flock.Add(tempBird);
  137. }
  138. }
  139. #endregion
  140. }
  141. }