You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AgEnvironment.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using uqac_ia_aspirobot.Common;
  5. using uqac_ia_aspirobot.Extensions;
  6. using uqac_ia_aspirobot.Interfaces;
  7. namespace uqac_ia_aspirobot.Agent
  8. {
  9. public class AgEnvironment : IEnvironment
  10. {
  11. private readonly IServiceProvider _serviceProvider;
  12. private readonly ArClient _arClient;
  13. protected int _width;
  14. protected int _height;
  15. protected readonly IDictionary<string, IRoom> _rooms = new Dictionary<string, IRoom>();
  16. public AgEnvironment(IServiceProvider serviceProvider, ArClient arClient)
  17. {
  18. _serviceProvider = serviceProvider;
  19. _arClient = arClient;
  20. }
  21. public void Setup()
  22. {
  23. _arClient.Setup();
  24. _width = _arClient.GetEnvWidth();
  25. _height = _arClient.GetEnvWidth();
  26. for (var x = 0; x < _width; ++x)
  27. {
  28. for (var y = 0; y < _height; ++y)
  29. {
  30. var room = _serviceProvider.GetService<IRoom>();
  31. room.X = x;
  32. room.Y = y;
  33. _rooms.Add(GetKey(x, y), room);
  34. }
  35. }
  36. }
  37. public void Update()
  38. {
  39. this.ForeachRoom<AgRoom>(room =>
  40. {
  41. room.Update();
  42. return true;
  43. });
  44. }
  45. protected string GetKey(int x, int y)
  46. {
  47. return $"{x},{y}";
  48. }
  49. public int GetWidth()
  50. {
  51. return _width;
  52. }
  53. public int GetHeight()
  54. {
  55. return _height;
  56. }
  57. public IRoom GetRoom(int x, int y)
  58. {
  59. return _rooms[GetKey(x, y)];
  60. }
  61. }
  62. }