Advertisement
SilverTES

C#MonoGame+Retro2D : Class MobilUnit

Dec 29th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1.     public class MobilUnit : Node
  2.     {
  3.         Queue<Vector2> _queueGoal = new Queue<Vector2>();
  4.  
  5.         float _animY = 0;
  6.         float _vY = -0.5f;
  7.         float _upY = -4;
  8.  
  9.         float _speed = 1;
  10.         Vector2 _currentGoal;
  11.         Vector2 _currentVector;
  12.  
  13.         bool _onMove = false;
  14.         bool _isMove = false;
  15.         bool _onGoal = false;
  16.         bool _isGoal = false;
  17.  
  18.  
  19.         public bool IsQueueGoal()
  20.         {
  21.             return _queueGoal.Count > 0;
  22.         }
  23.         public bool OnMove()
  24.         {
  25.             return _onMove;
  26.         }
  27.         public bool OnGoal()
  28.         {
  29.             return _onGoal;
  30.         }
  31.         public bool EndOfMove()
  32.         {
  33.             return (_onGoal && !IsQueueGoal());
  34.         }
  35.  
  36.         public Vector2 GetMoveVector(Vector2 start, Vector2 goal, float speed)
  37.         {
  38.             float vecX = goal.X - start.X;
  39.             float vecY = goal.Y - start.Y;
  40.  
  41.             Vector2 moveVector = new Vector2(0,0);
  42.  
  43.             moveVector.X = (float)Math.Cos(-Math.Atan2(vecX, vecY) + 1.5708) * speed;
  44.             moveVector.Y = (float)Math.Sin(-Math.Atan2(vecX, vecY) + 1.5708) * speed;
  45.  
  46.             return moveVector;
  47.         }
  48.         public void Init(float x, float y, float speed = 1)
  49.         {
  50.             _x = x;
  51.             _y = y;
  52.             _speed = speed;
  53.         }
  54.         public void AddGoal(float x, float y)
  55.         {
  56.             _queueGoal.Enqueue(new Vector2(x, y));
  57.         }
  58.         public void ClearAllGoal()
  59.         {
  60.             _queueGoal.Clear();
  61.         }
  62.  
  63.         public MobilUnit()
  64.         {
  65.  
  66.         }
  67.  
  68.         public void Move()
  69.         {
  70.             _x += _currentVector.X;
  71.             _y += _currentVector.Y;
  72.  
  73.             if (Math.Abs(_x - _currentGoal.X) < 1 &&
  74.                 Math.Abs(_y - _currentGoal.Y) < 1)
  75.             {
  76.                 _isGoal = true;
  77.                 _onGoal = true;
  78.                 _isMove = false;
  79.  
  80.                 _queueGoal.Dequeue();
  81.             }
  82.         }
  83.         public Position GetDirection(float vxMin = 0.3f)
  84.         {
  85.             float vx = _currentVector.X;
  86.             float vy = _currentVector.Y;
  87.  
  88.             if (vx >  vxMin) return Position.E;
  89.             if (vx < -vxMin) return Position.W;
  90.  
  91.             if (vy > 0) return Position.S;
  92.             if (vy < 0) return Position.N;
  93.  
  94.             return Position.CENTER;
  95.         }
  96.  
  97.         public override Node Update()
  98.         {
  99.             _onMove = false;
  100.             _onGoal = false;
  101.  
  102.             if (IsQueueGoal() && !_isMove)
  103.             {
  104.                 _isGoal = false;
  105.                 _isMove = true;
  106.                 _onMove = true;
  107.  
  108.                 _currentGoal = _queueGoal.Peek();
  109.                 _currentVector = GetMoveVector(new Vector2(_x, _y), _currentGoal, _speed);
  110.  
  111.             }
  112.  
  113.             if (_isMove) Move();
  114.  
  115.             _animY += _vY;
  116.  
  117.             if (_animY < _upY) _vY = 0.5f;
  118.             if (_animY > 0) _vY = -0.5f;
  119.  
  120.             return this;
  121.         }
  122.  
  123.         public override Node Render(SpriteBatch batch)
  124.         {
  125.             //if (IsQueueGoal())
  126.             //{
  127.             //    Vector2 prevVec = new Vector2(_x,_y);
  128.  
  129.             //    foreach  (Vector2 vec in _queueGoal)
  130.             //    {
  131.             //        batch.DrawLine(prevVec, vec, Color.Lavender * .6f, 1);
  132.             //        batch.DrawPoint(vec, Color.GreenYellow, 1);
  133.             //        prevVec = vec;
  134.             //    }
  135.             //}
  136.  
  137.             batch.DrawCircle(AbsX(),AbsY() + _animY, 8,16, Color.Coral,2);
  138.  
  139.             if (GetDirection() == Position.N) batch.DrawCircle(AbsX(), AbsY()- 8 + _animY, 4, 8, Color.Goldenrod, 2);
  140.             if (GetDirection() == Position.S) batch.DrawCircle(AbsX(), AbsY()+ 8 + _animY, 4, 8, Color.Goldenrod, 2);
  141.             if (GetDirection() == Position.W) batch.DrawCircle(AbsX()-8, AbsY() + _animY, 4, 8, Color.Goldenrod, 2);
  142.             if (GetDirection() == Position.E) batch.DrawCircle(AbsX()+8, AbsY() + _animY, 4, 8, Color.Goldenrod, 2);
  143.  
  144.  
  145.             return this;
  146.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement