dmitryzenevich

MoveableObject_TrajectorySimulation

May 30th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TrajectorySimulation
  4. {
  5.     public class MoveableObject
  6.     {
  7.         private string _name;
  8.         private bool _isAlive;
  9.         private Vector2Int _position;
  10.         private Random _random = new Random();
  11.  
  12.         public string Name
  13.         {
  14.             get => _name;
  15.             set => _name = value;
  16.         }
  17.         public bool IsAlive => _isAlive;
  18.         public Vector2Int Position
  19.         {
  20.             get => _position;
  21.             private set => _position = value;
  22.         }
  23.  
  24.         public MoveableObject(string name, Vector2Int position)
  25.         {
  26.             _name = name;
  27.             _isAlive = true;
  28.             _position = position;
  29.         }
  30.  
  31.         public void RandomMove()
  32.         {
  33.             var x = _random.Next(-1, 2);
  34.             var y = _random.Next(-1, 2);
  35.             var direction = new Vector2Int(x, y);
  36.             Move(direction);
  37.             CheckBounds();
  38.         }
  39.         public void Move(Vector2Int direction)
  40.         {
  41.             Position += direction;
  42.         }
  43.  
  44.         private void CheckBounds()
  45.         {
  46.             var x = Position.X < 0 ? 0 : Position.X > 50 ? 50 : Position.X;
  47.             var y = Position.Y < 0 ? 0 : Position.Y > 50 ? 50 : Position.Y;
  48.             Position = new Vector2Int(x, y);
  49.         }
  50.  
  51.         public void Destroy()
  52.         {
  53.             _isAlive = false;
  54.         }
  55.  
  56.         public void Render()
  57.         {
  58.             if (IsAlive == false)
  59.                 return;
  60.            
  61.             Console.SetCursorPosition(Position.X, Position.Y);
  62.             Console.Write(Name);
  63.         }
  64.     }
  65. }
Add Comment
Please, Sign In to add comment