PageRenderTime 120ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/CSBot.Robots/CSRobots.ViewModels/MainView.cs

http://github.com/corynabours/SharpBots
C# | 325 lines | 263 code | 45 blank | 17 comment | 13 complexity | fdcf641e062dcd95cc332627fcad7c1d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Timers;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows.Threading;
  10. using CSBot.Robots;
  11. using Timer = System.Timers.Timer;
  12. namespace CSRobots.ViewModels
  13. {
  14. public class MainView : BaseViewModel
  15. {
  16. private readonly Timer _timer;
  17. private readonly Battlefield _battlefield;
  18. //private readonly IList<Line> _radarLines = new List<Line>();
  19. private readonly IList<BitmapSource> _explosionImages = new List<BitmapSource>();
  20. private readonly bool _showRadar;
  21. private const int SpeedMultiplier = 1;
  22. public static RunOptions RunOptions { get; set; }
  23. private readonly Thread _uiThread;
  24. public MainView()
  25. {
  26. _uiThread = Thread.CurrentThread;
  27. _timer = new Timer {Interval = 5};
  28. _timer.Elapsed += TimerElapsed;
  29. Height = RunOptions.Y;
  30. Width = RunOptions.X;
  31. _battlefield = RunOptions.Battlefield;
  32. _showRadar = false;
  33. InitCanvas();
  34. DrawFrame();
  35. _timer.Enabled = true;
  36. }
  37. private int _height;
  38. public int Height
  39. {
  40. get { return _height; }
  41. set { SetPropertyValue(out _height, value); }
  42. }
  43. private int _width;
  44. public int Width
  45. {
  46. get { return _width; }
  47. set { SetPropertyValue(out _width, value); }
  48. }
  49. private static bool _inTimer;
  50. private int _gameOverTicks;
  51. private void TimerElapsed(object sender, ElapsedEventArgs e)
  52. {
  53. try
  54. {
  55. if (_inTimer) return;
  56. _inTimer = true;
  57. DrawFrame();
  58. if (_gameOverTicks == 50)
  59. {
  60. Close();
  61. }
  62. _inTimer = false;
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine(ex.ToString());
  67. throw;
  68. }
  69. }
  70. private void Close()
  71. {
  72. }
  73. public void InitCanvas()
  74. {
  75. var robotImages = new List<RobotColorImages>();
  76. for (var i = 0; i < 8; i++)
  77. {
  78. robotImages.Add(new RobotColorImages(i));
  79. }
  80. var index = 0;
  81. Bots = new ObservableCollection<RobotView>();
  82. foreach(var robot in _battlefield.Robots)
  83. {
  84. var displayRobot = new RobotView(robot, robotImages[robot.Team], index++);
  85. _bots.Add(displayRobot);
  86. var displayStatus = new RobotStatus {Color = robotImages[robot.Team].TextColor};
  87. _status.Add(displayStatus);
  88. }
  89. for (var i = 0; i < 15; i++)
  90. {
  91. try
  92. {
  93. var boom = EmbeddedImage.GetEmbeddedImageResource("CSRobots.ViewModels.images.explosion" + i.ToString("D2") + ".gif");
  94. _explosionImages.Add(boom);
  95. }
  96. catch (Exception ex)
  97. {
  98. Console.WriteLine(ex.ToString());
  99. throw;
  100. }
  101. }
  102. }
  103. private void DrawFrame()
  104. {
  105. Simulate(SpeedMultiplier);
  106. DrawBattlefield();
  107. }
  108. private void Simulate(int ticks)
  109. {
  110. var explosionRemovals = _explosions.Where(explosion => explosion.Dead).ToList();
  111. foreach(var explosion in explosionRemovals)
  112. {
  113. var explosion1 = explosion;
  114. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => RemoveExplosion(explosion1)));
  115. }
  116. var bulletRemovals = _bullets.Where(bullet => bullet.Dead).ToList();
  117. foreach (var bullet in bulletRemovals)
  118. {
  119. var bullet1 = bullet;
  120. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => RemoveBullet(bullet1)));
  121. }
  122. //foreach (var line in _radarLines)
  123. //{
  124. //Canvas.Children.Remove(line);
  125. //}
  126. //_radarLines.Clear();
  127. var botRemovals = Bots.Where(bot => bot.Dead()).ToList();
  128. foreach (var bot in botRemovals)
  129. {
  130. var bot1 = bot;
  131. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => RemoveRobot(bot1)));
  132. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => SetStatusText(bot1.Index, bot1.RobotName.PadRight(20, ' ') + "dead")));
  133. }
  134. for (var index = 0; index < ticks; index++)
  135. {
  136. if (_battlefield.GameOver)
  137. {
  138. _gameOverTicks++;
  139. var winners = _battlefield.Robots.Where(robot => !robot.Dead()).ToList();
  140. if (winners.Count == 0)
  141. {
  142. DisplayDraw();
  143. return;
  144. }
  145. var team = winners[0].Team;
  146. var draw = _battlefield.Robots.Any(robot => !robot.Dead() && (robot.Team != team));
  147. if (draw)
  148. {
  149. DisplayDraw();
  150. return;
  151. }
  152. GameOver = "Team #" + team.ToString(CultureInfo.InvariantCulture) + " won!";
  153. }
  154. _battlefield.Tick();
  155. }
  156. }
  157. private string _gameOver;
  158. public string GameOver
  159. {
  160. get { return _gameOver; }
  161. set {SetPropertyValue(out _gameOver, value);}
  162. }
  163. private void DisplayDraw()
  164. {
  165. GameOver ="Draw!";
  166. }
  167. private void DrawBattlefield()
  168. {
  169. /*while (!_radar_lines.empty ? )
  170. {
  171. old_line = @radar_lines.pop
  172. @canvas.delete(old_line)
  173. }*/
  174. DrawRobots();
  175. DrawBullets();
  176. DrawExplosions();
  177. }
  178. private void DrawRobots()
  179. {
  180. foreach (var robot in Bots.Where(robot => !robot.Dead()))
  181. {
  182. robot.Draw();
  183. var robot1 = robot;
  184. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => SetStatusText(robot1.Index, robot1.RobotName.PadRight(20, ' ') + robot1.Energy.ToString("F", CultureInfo.InvariantCulture))));
  185. DrawRadar(robot);
  186. }
  187. }
  188. private void SetStatusText(int index, string value)
  189. {
  190. Status[index].Text = value;
  191. }
  192. private void DrawRadar(RobotView robot)
  193. {
  194. if (!_showRadar) return;
  195. var angle = robot.RadarHeading;
  196. var x = robot.X + (Math.Cos(angle*Math.PI/180)*3200);
  197. var y = robot.Y - (Math.Sin(angle*Math.PI/180)*3200);
  198. //var radarLine = new Line {X1 = robot.X/2, Y1 = robot.Y/2, X2 = x/2, Y2 = y/2};
  199. //_radarLines.Add(radarLine);
  200. //radar_line = TkcLine.new(@canvas, robot.x/2, robot.y/2, x/2, y/2)
  201. //radar_line.fill @text_colors[robot.team]
  202. //@radar_lines << radar_line
  203. }
  204. private void DrawBullets()
  205. {
  206. foreach (var bullet in _bullets)
  207. {
  208. bullet.Draw();
  209. }
  210. var bullets = new List<Bullet>(_battlefield.NewBullets);
  211. foreach (var bullet in bullets)
  212. {
  213. var newBullet = new BulletView(bullet);
  214. //Dispatcher.Invoke(DispatcherPriority.Render,new Action(Close));
  215. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => AddBullet(newBullet)));
  216. _battlefield.Process(bullet);
  217. newBullet.Draw();
  218. }
  219. }
  220. private void AddBullet(BulletView newBullet)
  221. {
  222. _bullets.Add(newBullet);
  223. }
  224. private void RemoveBullet(BulletView bullet)
  225. {
  226. _bullets.Remove(bullet);
  227. }
  228. private void AddExplosion(ExplosionView newExplosion)
  229. {
  230. _explosions.Add(newExplosion);
  231. }
  232. private void RemoveExplosion(ExplosionView explosion)
  233. {
  234. _explosions.Remove(explosion);
  235. }
  236. private void RemoveRobot(RobotView robot)
  237. {
  238. _bots.Remove(robot);
  239. }
  240. private void DrawExplosions()
  241. {
  242. foreach (var explosion in _explosions)
  243. {
  244. explosion.Draw();
  245. }
  246. var explosions = new List<Explosion>(_battlefield.NewExplosions);
  247. foreach (var explosion in explosions)
  248. {
  249. var newExplosion = new ExplosionView(explosion, _explosionImages);
  250. Dispatcher.FromThread(_uiThread).Invoke(DispatcherPriority.Render, new Action(() => AddExplosion(newExplosion)));
  251. _battlefield.Process(explosion);
  252. newExplosion.Draw();
  253. }
  254. }
  255. private ObservableCollection<RobotView> _bots = new ObservableCollection<RobotView>();
  256. public ObservableCollection<RobotView> Bots
  257. {
  258. get { return _bots; }
  259. set { SetPropertyValue(out _bots, value); }
  260. }
  261. private ObservableCollection<BulletView> _bullets = new ObservableCollection<BulletView>();
  262. public ObservableCollection<BulletView> Bullets
  263. {
  264. get { return _bullets; }
  265. set { SetPropertyValue(out _bullets, value); }
  266. }
  267. private ObservableCollection<ExplosionView> _explosions = new ObservableCollection<ExplosionView>();
  268. public ObservableCollection<ExplosionView> Explosions
  269. {
  270. get { return _explosions; }
  271. set { SetPropertyValue(out _explosions, value); }
  272. }
  273. private ObservableCollection<RobotStatus> _status = new ObservableCollection<RobotStatus>();
  274. public ObservableCollection<RobotStatus> Status
  275. {
  276. get { return _status; }
  277. set { SetPropertyValue(out _status, value); }
  278. }
  279. }
  280. }