public virtual void Move(MoveEventArgs e) { if (e.distance == 0) { return; } lock (privateLock) { if (!this._movable) { return; } if ((DateTime.Now - lastMoveTime).TotalSeconds < 1 / _frameRate) { return; } MoveStart?.Invoke(this); XYPosition previousPosition = _position; _position = _position + new XYPosition(e.distance * Math.Cos(e.angle), e.distance * Math.Sin(e.angle)); Debug(this, "Move from " + previousPosition.ToString() + " angle : " + e.angle + " distance : " + e.distance + " aim : " + _position.ToString()); OnMove?.Invoke(this, e, previousPosition); Debug(this, "Move result poition : " + this._position.ToString()); MoveComplete?.Invoke(this); } }
public void Update(object sender, OnTickEventArgs eventArgs) { if (!Waypoints.Any()) { return; } float speed = LocationModel.Parent.UnitModel.Speed; if (DistanceCalculator.DiagonalDistance(Waypoints.Peek(), LocationModel.FloatCoords) < speed) { // Arrived near destination LocationModel.FloatCoords = Waypoints.Dequeue(); if (!Waypoints.Any()) { MoveComplete?.Invoke(this, new EventArgsWithPayload <FloatCoords>(LocationModel.FloatCoords)); } return; } float xDifference = (float)DistanceCalculator.CalcDistance(LocationModel.FloatCoords.x, Waypoints.Peek().x); float yDifference = (float)DistanceCalculator.CalcDistance(LocationModel.FloatCoords.y, Waypoints.Peek().y); // calculate new coords float diagonalDifference = (float)DistanceCalculator.Pythagoras(xDifference, yDifference); float v = diagonalDifference / speed; FloatCoords difference = new FloatCoords { x = xDifference / v, y = yDifference / v }; difference.x = Waypoints.Peek().x < LocationModel.FloatCoords.x ? -difference.x : difference.x; difference.y = Waypoints.Peek().y < LocationModel.FloatCoords.y ? -difference.y : difference.y; LocationModel.FloatCoords = new FloatCoords() { x = LocationModel.FloatCoords.x + difference.x, y = LocationModel.FloatCoords.y + difference.y }; }
private void CompleteMoveHandler() { IsMoving = false; MoveComplete?.Invoke(); }
public void MoveToPosition(IntVector2 position) { MoveStart?.Invoke(); MoveComplete?.Invoke(); }