Exemple #1
0
    private ShipSchedule PathToSchedule(ShipPath path)
    {
        double       defaultShipSpeed = GameObject.Find("SceneSetting").GetComponent <SceneSetting>().ShipSpeed;
        double       shipSpeed        = defaultShipSpeed;
        ShipSchedule schedule         = new ShipSchedule();
        MapUtil      mapUtil          = GameObject.Find("MapUtil").GetComponent <MapUtil>();

        DateTime currentTime        = GameObject.Find("Timer").GetComponent <Timer>().VirtualTime;
        Vector2  currentPosition    = new Vector2((float)ship.Ship.X, (float)ship.Ship.Y);
        bool     unloadingScheduled = false;

        Node previousNode = null;

        foreach (Node node in path.path)
        {
            ShipMoveTask moveTask = new ShipMoveTask();
            moveTask.Position = new Vector2((float)node.X, (float)node.Y);

            if (previousNode != null)
            {
                moveTask.connection = mapUtil.GetConnection(previousNode, node);
                shipSpeed           = moveTask.connection.Speed;
            }
            else
            {
                shipSpeed = defaultShipSpeed;
            }


            double   distance = Math.Pow(Math.Pow(node.X - currentPosition.x, 2) + Math.Pow(node.Y - currentPosition.y, 2), 0.5);
            TimeSpan duration = new TimeSpan(0, 0, (int)Math.Round(distance / shipSpeed));
            moveTask.StartTime = currentTime;
            moveTask.EndTime   = currentTime.Add(duration);


            previousNode = node;

            currentTime     = currentTime.Add(duration);
            currentPosition = new Vector2((float)node.X, (float)node.Y);

            schedule.AppendTask(moveTask);

            // Unloading task
            double unloadingSpeed = GameObject.Find("SceneSetting").GetComponent <SceneSetting>().UnloadingSpeed;
            Dock   dock           = mapUtil.GetDockByNode(node);
            if (!unloadingScheduled && dock != null)
            {
                TimeSpan      unloadingDuration = new TimeSpan(0, 0, (int)Math.Round(ship.Ship.cargo / unloadingSpeed));
                UnloadingTask unloadingTask     = new UnloadingTask();
                unloadingTask.Position  = currentPosition;
                unloadingTask.StartTime = currentTime;
                unloadingTask.EndTime   = currentTime.Add(unloadingDuration);
                unloadingTask.dock      = dock;
                currentTime             = currentTime.Add(unloadingDuration);
                schedule.AppendTask(unloadingTask);
                unloadingScheduled = true;
            }

            // Task
            List <Node> exits = mapUtil.ExitNodes();
            if (exits.Contains(node))
            {
                VanishTask vanishTask = new VanishTask();
                vanishTask.StartTime = currentTime;
                vanishTask.EndTime   = currentTime;
                schedule.AppendTask(vanishTask);
            }
        }
        return(schedule);
    }