Advertisement
dmitryzenevich

Program_TrajectorySimulation

May 30th, 2020
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4.  
  5. namespace TrajectorySimulation
  6. {
  7.     class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             // Initialization
  12.             var countOfMoveableObjects = 3;
  13.             var moveableObjects = new MoveableObject[countOfMoveableObjects];
  14.            
  15.             // Spawn
  16.             for (var i = 0; i < moveableObjects.Length; i++)
  17.             {
  18.                 var index = i + 1;
  19.                 moveableObjects[i] = new MoveableObject(index.ToString(), new Vector2Int(index * 5, index * 5));
  20.             }
  21.            
  22.             while (true)
  23.             {
  24.                 // Get alive objects
  25.                 var aliveObjects = moveableObjects.Where(m => m.IsAlive).ToArray();
  26.                
  27.                 // Compare collisions
  28.                 for (var i = 0; i < aliveObjects.Length; i++)
  29.                 {
  30.                     for (var j = 0; j < aliveObjects.Length; j++)
  31.                     {
  32.                         if (j == i) continue;
  33.                        
  34.                         // If collided - destroy
  35.                         if (aliveObjects[i].Position == aliveObjects[j].Position)
  36.                         {
  37.                             aliveObjects[i].Destroy();
  38.                             aliveObjects[j].Destroy();
  39.                         }
  40.                     }
  41.                 }
  42.  
  43.                 // Move objects
  44.                 foreach (var aliveObj in aliveObjects)
  45.                 {
  46.                     aliveObj.RandomMove();
  47.                 }
  48.                
  49.                 // Render
  50.                 foreach (var aliveObj in aliveObjects)
  51.                 {
  52.                     aliveObj.Render();
  53.                 }
  54.                 Thread.Sleep(100);
  55.                 Console.Clear();
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement