選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UiConsole.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using uqac_ia_aspirobot.Agent;
  4. using uqac_ia_aspirobot.Agent.Interfaces.Effectors;
  5. using uqac_ia_aspirobot.Agent.Interfaces.Sensors;
  6. using uqac_ia_aspirobot.Extensions;
  7. using uqac_ia_aspirobot.Interfaces;
  8. namespace uqac_ia_aspirobot.UI
  9. {
  10. public class UiConsole : IUi
  11. {
  12. private readonly IEnvironment _environment;
  13. private readonly IAgEngineEffector _agEngineEffector;
  14. private readonly IAgPickedSensor _agPickedSensor;
  15. private readonly IAgBatterySensor _agBatterySensor;
  16. private readonly IAgVaccumSensor _agVaccumSensor;
  17. private readonly AgState _agState;
  18. private readonly IAgPerformanceSensor _agPerformanceSensor;
  19. public UiConsole(IEnvironment environment, IAgEngineEffector agEngineEffector, IAgPickedSensor agPickedSensor,
  20. IAgBatterySensor agBatterySensor, IAgVaccumSensor agVaccumSensor, AgState agState, IAgPerformanceSensor agPerformanceSensor)
  21. {
  22. _environment = environment;
  23. _agEngineEffector = agEngineEffector;
  24. _agPickedSensor = agPickedSensor;
  25. _agBatterySensor = agBatterySensor;
  26. _agVaccumSensor = agVaccumSensor;
  27. _agState = agState;
  28. _agPerformanceSensor = agPerformanceSensor;
  29. }
  30. public void SetBackgroundColor(ConsoleColor? color)
  31. {
  32. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  33. {
  34. Console.BackgroundColor = color.GetValueOrDefault(ConsoleColor.Black);
  35. }
  36. else
  37. {
  38. var str = "";
  39. if (color == null)
  40. {
  41. str = $"{(char)27}[1;49m";
  42. }
  43. if (color == ConsoleColor.Black)
  44. {
  45. str = $"{(char)27}[1;40m";
  46. }
  47. if (color == ConsoleColor.Red)
  48. {
  49. str = $"{(char)27}[1;41m";
  50. }
  51. if (color == ConsoleColor.Green)
  52. {
  53. str = $"{(char)27}[1;42m";
  54. }
  55. if (color == ConsoleColor.Yellow)
  56. {
  57. str = $"{(char)27}[1;43m";
  58. }
  59. if (color == ConsoleColor.Blue)
  60. {
  61. str = $"{(char)27}[1;44m";
  62. }
  63. if (color == ConsoleColor.Magenta)
  64. {
  65. str = $"{(char)27}[1;45m";
  66. }
  67. if (color == ConsoleColor.Cyan)
  68. {
  69. str = $"{(char)27}[1;46m";
  70. }
  71. if (color == ConsoleColor.Gray)
  72. {
  73. str = $"{(char)27}[1;47m";
  74. }
  75. Console.Write(str);
  76. }
  77. }
  78. public void SetForegroundColor(ConsoleColor? color)
  79. {
  80. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  81. {
  82. Console.ForegroundColor = color.GetValueOrDefault(ConsoleColor.Black);
  83. }
  84. else
  85. {
  86. var str = "";
  87. if (color == null)
  88. {
  89. str = $"{(char)27}[1;39m";
  90. }
  91. if (color == ConsoleColor.Black)
  92. {
  93. str = $"{(char)27}[1;30m";
  94. }
  95. if (color == ConsoleColor.Red)
  96. {
  97. str = $"{(char)27}[1;31m";
  98. }
  99. if (color == ConsoleColor.Green)
  100. {
  101. str = $"{(char)27}[1;32m";
  102. }
  103. if (color == ConsoleColor.Yellow)
  104. {
  105. str = $"{(char)27}[1;33m";
  106. }
  107. if (color == ConsoleColor.Blue)
  108. {
  109. str = $"{(char)27}[1;34m";
  110. }
  111. if (color == ConsoleColor.Magenta)
  112. {
  113. str = $"{(char)27}[1;35m";
  114. }
  115. if (color == ConsoleColor.Cyan)
  116. {
  117. str = $"{(char)27}[1;36m";
  118. }
  119. if (color == ConsoleColor.Gray)
  120. {
  121. str = $"{(char)27}[1;37m";
  122. }
  123. Console.Write(str);
  124. }
  125. }
  126. public void Clear()
  127. {
  128. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  129. {
  130. Console.Clear();
  131. }
  132. else
  133. {
  134. Console.WriteLine();
  135. }
  136. }
  137. public void Update()
  138. {
  139. Clear();
  140. var bgColor = Console.BackgroundColor;
  141. for (var x = 0; x < _environment.GetWidth(); ++x)
  142. {
  143. Console.Write("|");
  144. for (var y = 0; y < _environment.GetHeight(); ++y)
  145. {
  146. var state = _environment.GetRoomState(x, y);
  147. ConsoleColor? color = null;
  148. if (state == RoomState.Dust)
  149. {
  150. color = ConsoleColor.Gray;
  151. }
  152. else if (state == RoomState.Jewel)
  153. {
  154. color = ConsoleColor.Cyan;
  155. }
  156. else if (state == RoomState.Unknown)
  157. {
  158. color = ConsoleColor.Yellow;
  159. }
  160. else if (state == (RoomState.Jewel | RoomState.Dust))
  161. {
  162. color = ConsoleColor.Red;
  163. }
  164. SetBackgroundColor(color);
  165. var isDest = _agState.Destination?.IsInPosition(x, y) ?? false;
  166. if (_agEngineEffector.IsInPosition(x, y))
  167. {
  168. Console.Write("X");
  169. }
  170. else if (isDest)
  171. {
  172. SetForegroundColor(ConsoleColor.Red);
  173. Console.Write("+");
  174. }
  175. else
  176. {
  177. Console.Write(" ");
  178. }
  179. SetForegroundColor(null);
  180. SetBackgroundColor(null);
  181. }
  182. Console.WriteLine("|");
  183. }
  184. SetBackgroundColor(bgColor);
  185. var nextThink = Math.Max(0, (_agState.LastThinkTime.AddMilliseconds(_agState.ThinkTimeInterval) - DateTime.Now).TotalMilliseconds);
  186. Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
  187. Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
  188. Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
  189. Console.WriteLine($"Think Interval: {_agState.ThinkTimeInterval}ms");
  190. Console.WriteLine($"Next Think: {nextThink}ms");
  191. Console.WriteLine($"Performance: {_agPerformanceSensor.Performance}");
  192. }
  193. }
  194. }