Ocean.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MultiAgentSystemPCL
  5. {
  6. public delegate void OceanUpdated(FishAgent[] _fish, List<BadZone> _obstacles);
  7. public class Ocean
  8. {
  9. public event OceanUpdated oceanUpdatedEvent;
  10. FishAgent[] fishList = null;
  11. List<BadZone> obstacles = null;
  12. Random randomGenerator;
  13. protected double MAX_WIDTH;
  14. protected double MAX_HEIGHT;
  15. public Ocean(int _fishNb, double _width, double _height)
  16. {
  17. MAX_WIDTH = _width;
  18. MAX_HEIGHT = _height;
  19. randomGenerator = new Random();
  20. fishList = new FishAgent[_fishNb];
  21. obstacles = new List<BadZone>();
  22. for (int i = 0; i < _fishNb; i++)
  23. {
  24. fishList[i] = new FishAgent(randomGenerator.NextDouble() * MAX_WIDTH, randomGenerator.NextDouble() * MAX_HEIGHT, randomGenerator.NextDouble() * 2 * Math.PI);
  25. }
  26. }
  27. public void UpdateEnvironnement()
  28. {
  29. UpdateObstacles();
  30. UpdateFish();
  31. if (oceanUpdatedEvent != null)
  32. {
  33. oceanUpdatedEvent(fishList, obstacles);
  34. }
  35. }
  36. private void UpdateObstacles()
  37. {
  38. foreach (BadZone obstacle in obstacles)
  39. {
  40. obstacle.Update();
  41. }
  42. obstacles.RemoveAll(x => x.Dead());
  43. }
  44. private void UpdateFish()
  45. {
  46. foreach (FishAgent fish in fishList)
  47. {
  48. fish.Update(fishList, obstacles, MAX_WIDTH, MAX_HEIGHT);
  49. }
  50. }
  51. public void AddObstacle(double _posX, double _posY, double _radius) {
  52. obstacles.Add(new BadZone(_posX, _posY, _radius));
  53. }
  54. }
  55. }