/// <inheritdoc/> public void ObjectMoved(ILocateable movedObject, MoveType moveType) { Point targetPoint = movedObject.Position; object steps = null; int walkDelay = 0; if (movedObject is ISupportWalk walker && moveType == MoveType.Walk) { targetPoint = walker.WalkTarget; walkDelay = (int)walker.StepDelay.TotalMilliseconds; Span <WalkingStep> walkingSteps = new WalkingStep[16]; var stepCount = walker.GetSteps(walkingSteps); var walkSteps = walkingSteps.Slice(0, stepCount).ToArray().Select(step => new { x = step.To.X, y = step.To.Y, direction = step.Direction }).ToList(); var lastStep = walkSteps.LastOrDefault(); if (lastStep != null) { var lastPoint = new Point(lastStep.x, lastStep.y); var lastDirection = lastPoint.GetDirectionTo(targetPoint); if (lastDirection != Direction.Undefined) { walkSteps.Add(new { x = targetPoint.X, y = targetPoint.Y, direction = lastDirection }); } } steps = walkSteps; } this.clientProxy.SendAsync("ObjectMoved", movedObject.Id, targetPoint.X, targetPoint.Y, moveType, walkDelay, steps); }
private Span <WalkingStep> GetSteps(Point start, Span <Direction> directions) { var result = new WalkingStep[directions.Length]; Point previousTarget = start; int i = 0; foreach (var direction in directions) { var currentTarget = previousTarget.CalculateTargetPoint(direction); result[i] = new WalkingStep { Direction = direction, To = currentTarget, From = previousTarget }; i++; previousTarget = currentTarget; } return(result); }
public void TestWalkStepsAreCorrect() { var player = this.DoTheWalk(); // the next check is questionable - there is a timer which is removing a direction every 500ms. If the test runs "too slow", the count is 3 ;-) Span <WalkingStep> steps = new WalkingStep[16]; var count = player.GetSteps(steps); Assert.That(count, Is.EqualTo(4)); steps = steps.Slice(0, count); Assert.That(steps[0].From, Is.EqualTo(StartPoint)); Assert.That(steps[steps.Length - 1].To, Is.EqualTo(EndPoint)); foreach (var direction in steps) { Assert.That(direction.From, Is.Not.EqualTo(direction.To)); } }
IEnumerator WalkToStep(WalkingStep ws) { var startPosition = transform.localPosition; Vector2 destinationWorldPosition = MapLayout.ScreenPointFromMapPoint(ws.step.MapPoint); Vector2 fromWorldPosition = MapLayout.ScreenPointFromMapPoint(ws.fromPoint); Tile fromTile, toTile; fromTile = controller.map.TileAtMapPoint(ws.fromPoint); toTile = controller.map.TileAtMapPoint(currentPosition); float timer = 0.0f; Vector2 edge = fromTile.edgeForDirection(ws.direction); float divisor = 1.0f; if (edge.x != -1 && edge.y != -1) { var edgeDestination = new Vector3(fromWorldPosition.x + ((edge.x - 50.0f) / 100.0f), fromWorldPosition.y + (edge.y / 100.0f), 0); while (transform.localPosition != edgeDestination) { transform.localPosition = Vector3.MoveTowards(startPosition, edgeDestination, walkingSpeed * timer); timer += Time.fixedDeltaTime; yield return(new WaitForEndOfFrame()); } divisor = 2.0f; startPosition = edgeDestination; } var destination = new Vector3(destinationWorldPosition.x, destinationWorldPosition.y + (toTile.centreY / 100.0f)); timer = 0.0f; while (transform.localPosition != destination) { transform.localPosition = Vector3.MoveTowards(startPosition, destination, walkingSpeed * timer); timer += Time.fixedDeltaTime; yield return(new WaitForEndOfFrame()); } }
IEnumerator WalkAlongPath() { // currentPath could be replaced underneath us if the user // changes destination mid-walk. StopCoroutine("WalkToStep"); while (currentPath.Count > 0) { MovementStep step = currentPath[0]; currentPath.RemoveAt(0); animator.SetBool("Walking", true); Map.Direction newDirection = Map.CalculateDirectionTravelling(currentPosition, step.MapPoint); animator.SetInteger("Direction", (int)newDirection); MapPoint fromPoint = currentPosition; currentPosition = step.MapPoint; WalkingStep ws = new WalkingStep { fromPoint = fromPoint, step = step, direction = newDirection }; yield return(StartCoroutine("WalkToStep", ws)); } animator.SetBool("Walking", false); walking = false; if (currentCompletionHandler != null) { currentCompletionHandler(true); } }