示例#1
0
        private void AssignTask()
        {
            Task parent = new Task();
            Subtask constructBuilding = new Subtask();
            parent.AddSubtask(constructBuilding);

            if (building.GetPrimitiveObject().GetBuildingType() == BuildingType.Infrastructure)
            {
                for (int x = 0; x < building.GetTileSize().X; x++)
                {
                    for (int y = 0; y < building.GetTileSize().Y; y++)
                    {
                        Instruction workerTask = new Instruction();
                        World.Tile workTile = World.WorldController.GetTileAt(building.GetTilePosition() + new Vector2(x, y), isTileCoords: true);
                        if (workTile == null)
                            continue;

                        Process waitAtTile = new Process(workTile, 1f, onComplete => { });
                        workerTask.AddProcess(waitAtTile);

                        Process constructProcess = new Process(workTile, timeForEachJob, onComplete => {
                            Vector2 position = building.WorldSpaceToLocalSpace(onComplete.GetWorkTile().GetTileNumber());
                            RemoveConstructionRenderer(position);
                        });
                        constructProcess.RegisterOnTickHandle(onTick =>
                        {
                            float percentage = 80 - (onTick.GetRemainingTime() / onTick.GetProcessTime()) * 80;
                            Vector2 position = building.WorldSpaceToLocalSpace(onTick.GetWorkTile().GetTileNumber());
                            constructionRenderers[(int) position.X, (int) position.Y]?.SetPercentage(percentage);
                        });
                        workerTask.AddProcess(constructProcess);
                        constructBuilding.AddInstruction(workerTask);
                    }
                }
            }
            else
            {
                Instruction workerTask = new Instruction();
                World.Tile workTile = World.WorldController.GetTileAt(building.GetTilePosition(), isTileCoords: true);
                if (workTile == null)
                    return;

                Process waitAtTile = new Process(workTile, 1f, onComplete => { });
                workerTask.AddProcess(waitAtTile);

                Process constructProcess = new Process(workTile, timeForEachJob, onComplete => {
                    Vector2 position = building.WorldSpaceToLocalSpace(onComplete.GetWorkTile().GetTileNumber());
                    RemoveConstructionRenderer(position);
                });
                constructProcess.RegisterOnTickHandle(onTick =>
                {
                    float percentage = 80 - (onTick.GetRemainingTime() / onTick.GetProcessTime()) * 80;
                    Vector2 position = building.WorldSpaceToLocalSpace(onTick.GetWorkTile().GetTileNumber());
                    constructionRenderers[0, 0]?.SetPercentage(percentage);
                });
                workerTask.AddProcess(constructProcess);
                constructBuilding.AddInstruction(workerTask);
            }
        }