private void SetPointerLocomotionTarget(Vector2 target, ObjectDirection moveDirection) { //Logger.Warning($"SetPointerLocomotionTarget to target {target.x}, {target.y} in the direction {moveDirection}"); GridLocation targetGridLocation = GridLocation.FindClosestGridTile(target); if (!ValidateTarget(new TargetLocation(targetGridLocation, moveDirection))) { return; } Vector2 gridVectorTarget = GridLocation.GridToVector(targetGridLocation); if (CurrentGridLocation.X == targetGridLocation.X && CurrentGridLocation.Y == targetGridLocation.Y) { return; } if (!_animationHandler.InLocomotion) { _animationHandler.SetLocomotion(true); } IsCalculatingPath = true; PathToTarget = _pathfinding.FindNodePath(CurrentGridLocation, targetGridLocation); PathToTarget.RemoveAt(0); IsCalculatingPath = false; SetHasCalculatedTarget(true); }
private void MoveTowardsTarget() { if (Move()) { // Finish orders Simulation.ClosedOrders.AddRange(CurrentOrders); // Clear order-related variables CurrentOrders.Clear(); PathToTarget.Clear(); CurrentTarget = null; DistanceToCurrentTarget = 0d; TimeToCurrentTarget = 0d; DistanceTraveled = 0d; State = VehicleState.Idle; } }
/// <summary> /// Move along the path towards the specified target /// <returns>Whether it arrived at the final target</returns> /// </summary> private bool Move() { if (CurrentTarget == null && PathToTarget.Count >= 2) { CurrentTarget = PathToTarget[1]; DistanceToCurrentTarget = Vehicle.TravelMode == GoogleMapsComponents.Maps.TravelMode.Transit ? PathToTarget[0].Edges.First(e => e.Destination == PathToTarget[1]).Info.Distance : PathToTarget[0].Edges.First(e => e.Destination == PathToTarget[1]).Info.GMapsDistanceAndTime[Vehicle.TravelMode].Item1; TimeToCurrentTarget = Vehicle.TravelMode == GoogleMapsComponents.Maps.TravelMode.Transit ? 0d : PathToTarget[0].Edges.First(e => e.Destination == PathToTarget[1]).Info.GMapsDistanceAndTime[Vehicle.TravelMode].Item2; } if (DistanceTraveled >= DistanceToCurrentTarget) { CurrentVertexPosition = CurrentTarget; var currentIndexInPath = PathToTarget.IndexOf(CurrentVertexPosition); if (currentIndexInPath == PathToTarget.Count - 1) { return(true); } CurrentTarget = PathToTarget[currentIndexInPath + 1]; DistanceTraveled = 0d; DistanceToCurrentTarget = Vehicle.TravelMode == GoogleMapsComponents.Maps.TravelMode.Transit ? PathToTarget[currentIndexInPath].Edges.First(e => e.Destination == PathToTarget[currentIndexInPath + 1]).Info.Distance : PathToTarget[currentIndexInPath].Edges.First(e => e.Destination == PathToTarget[currentIndexInPath + 1]).Info.GMapsDistanceAndTime[Vehicle.TravelMode].Item1; TimeToCurrentTarget = Vehicle.TravelMode == GoogleMapsComponents.Maps.TravelMode.Transit ? 0d : PathToTarget[currentIndexInPath].Edges.First(e => e.Destination == PathToTarget[currentIndexInPath + 1]).Info.GMapsDistanceAndTime[Vehicle.TravelMode].Item2; } else { DistanceTraveled += GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); if (State == VehicleState.MovingToTarget) { CurrentFuelLoaded -= GetFuelConsumptionForOneMeter(CurrentOrders?.Sum(o => o.Order.PayloadWeight) ?? 0d) * GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); } else if (State == VehicleState.PickingUpOrder) { CurrentFuelLoaded -= BaseFuelConsumption * GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); } // Record progress in order CurrentOrders.ForEach(o => { if (State == VehicleState.MovingToTarget) { o.DeliveryTime++; o.DeliveryDistance += GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); o.DeliveryCost = CalculateJourneyCost(o.DeliveryDistance, o.DeliveryTime) / CurrentOrders.Count; // divide cost depending how many orders are loaded } else if (State == VehicleState.PickingUpOrder) { o.PickupTime++; o.PickupDistance += GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); o.PickupCost = CalculateJourneyCost(o.PickupDistance, o.PickupTime) / CurrentOrders.Count; // divide cost depending how many orders are loaded } }); // Record statistics TotalTravelDistance += GetSpeedInMetersPerSecond(DistanceToCurrentTarget, TimeToCurrentTarget, TravelMode); TotalTravelTime++; } return(false); }
public void TryStartCharacterMovement(ObjectDirection direction) { // check if character is in tile position, if so, start movement in direction. if (HasCalculatedTarget) { // if already in locomotion, it means that we are between tiles and we are moving. Return. return; } if (IsCalculatingPath) { return; } GridLocation currentGridLocation = GridLocation.VectorToGrid(transform.position); //Order character to go to another tile _animationHandler.SetDirection(direction); switch (direction) { case ObjectDirection.Down: TargetGridLocation = new TargetLocation(new GridLocation(currentGridLocation.X, currentGridLocation.Y - 1), direction); break; case ObjectDirection.Left: TargetGridLocation = new TargetLocation(new GridLocation(currentGridLocation.X - 1, currentGridLocation.Y), direction); break; case ObjectDirection.Right: TargetGridLocation = new TargetLocation(new GridLocation(currentGridLocation.X + 1, currentGridLocation.Y), direction); break; case ObjectDirection.Up: TargetGridLocation = new TargetLocation(new GridLocation(currentGridLocation.X, currentGridLocation.Y + 1), direction); break; default: Logger.Warning("Unhandled locomotion direction {0}", direction); return; } if (!ValidateTarget(TargetGridLocation)) { // This prevents the character from displaying locomotion animation when walking into an unwalkable tile _animationHandler.SetLocomotion(false); return; } //Logger.Warning("Start path!"); IsCalculatingPath = true; //Logger.Log($"TryStartCharacterMovement. {CurrentGridLocation.X},{CurrentGridLocation.Y} to {TargetGridLocation.TargetGridLocation.X}, {TargetGridLocation.TargetGridLocation.Y}"); PathToTarget = _pathfinding.FindNodePath(CurrentGridLocation, TargetGridLocation.TargetGridLocation); PathToTarget.RemoveAt(0); IsCalculatingPath = false; SetHasCalculatedTarget(true); if (!_animationHandler.InLocomotion) { _animationHandler.SetLocomotion(true); } }