Flock.cs 4.4 KB

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