예제 #1
0
    private async void initializeScene(string sceneName) //test stuff inside! will need to be rewritten for actual game
    {
        running        = true;
        saveDataThread = new Thread(saveData);
        saveDataThread.Start();
        if (currentSettings == "")
        {
            currentSettings = SettingsManager.DEFAULT_SETTINGS;
        }
        SettingPairs settingPairs = settingsManager.getSettingPairs(currentSettings);

        settingList = settingsManager.getSettingList(currentSettings);
        fieldSize   = Field.getFieldSize(settingPairs.field_size);
        foreach (PartData partData in partsData)
        {
            Part part = partManager.partDataToPart(partData);
            if (part.GAME_OBJECT != null)
            {
                part.toggleGameObject(false);
            }
            parts.Add(part);
        }
        if (currentTest == TESTS.BUILD_HUB)
        {
            IconGenerator iconGenerator = new IconGenerator();
            Camera        camera        = null;
            foreach (Camera currCam in Camera.allCameras)
            {
                if (currCam.name == "IconCamera")
                {
                    camera = currCam;
                    camera.forceIntoRenderTexture = true;
                    break;
                }
            }
            iconGenerator.camera      = camera;
            Camera.main.targetDisplay = 1;
            foreach (Part part in parts)
            {
                if (part.GAME_OBJECT == null)
                {
                    continue;
                }
                part.toggleGameObject(true);
                part.GAME_OBJECT.transform.Rotate(new Vector3(9, 120, -15));
                iconGenerator.gameObjectOfIcon = part.GAME_OBJECT;
                while (part.getIcon() == null)
                {
                    if (part.GAME_OBJECT.GetComponent <Renderer>().isVisible)
                    {
                        iconGenerator.initialize();
                        if (iconGenerator.getIcon() != null)
                        {
                            part.setIcon(iconGenerator.getIcon());
                        }
                    }
                    if (part.getIcon() == null)
                    {
                        await Task.Delay(25);
                    }
                }
                part.toggleGameObject(false);
                part.destroyGameObject();
            }
            Camera.main.targetDisplay = 0;
        }
        else
        {
            foreach (Part part in parts)
            {
                if (part.GAME_OBJECT != null)
                {
                    part.destroyGameObject();
                }
            }
        }
        experience = playerData.experience;
        credits    = playerData.credits;
        previousRoundDamageDifference    = playerData.previousRoundDamageDifference;
        previousRoundMaxDamageDifference = playerData.previousRoundMaxDamageDifference;
        previousRoundTimeElapsed         = playerData.previousRoundTimeElapsed;
        ObstacleGenerator obstacleGenerator = new ObstacleGenerator(experience, settingPairs.max_obstacles, fieldSize);

        humanRobotParts = new List <Part>();
        obstaclesData   = new List <ObstacleData>();
        List <int> badIndices = new List <int>();
        PerformanceMetricCalculator performanceMetricCalculator = new PerformanceMetricCalculator();
        Head     cheapestHead     = null;
        Body     cheapestBody     = null;
        Mobility cheapestMobility = null;
        Blaster  cheapestBlaster  = null;

        foreach (Part part in parts)
        {
            if (part is Head && (cheapestHead == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestHead)))
            {
                cheapestHead = (Head)part;
            }
            else if (part is Body && (cheapestBody == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestBody)))
            {
                cheapestBody = (Body)part;
            }
            else if (part is Mobility && (cheapestMobility == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestMobility)))
            {
                cheapestMobility = (Mobility)part;
            }
            else if (part is Blaster && (cheapestBlaster == null || performanceMetricCalculator.calculateCost(part) < performanceMetricCalculator.calculateCost(cheapestBlaster)))
            {
                cheapestBlaster = (Blaster)part;
            }
        }
        if (playerData.humanRobotParts != default)
        {
            for (int partIndex = 0; partIndex < playerData.humanRobotParts.Length; ++partIndex)
            {
                PlayerPartData playerPartData = playerData.humanRobotParts[partIndex];
                if (parts.Exists(p => p.getID() == playerPartData.id))
                {
                    Part part = parts.Find(p => p.getID() == playerPartData.id);
                    part.damage(part.getDurability() - playerPartData.remainingDurability);
                    if (part is Head)
                    {
                        humanRobotParts.Add((Head)part.clone(true));
                    }
                    else if (part is Body)
                    {
                        humanRobotParts.Add((Body)part.clone(true));
                    }
                    else if (part is Mobility)
                    {
                        humanRobotParts.Add((Mobility)part.clone(true));
                    }
                    else if (part is Attachment)
                    {
                        humanRobotParts.Add((Attachment)part.clone(true));
                    }
                }
                else
                {
                    humanRobotParts.Add(null);
                    badIndices.Add(partIndex);
                }
            }
        }
        else
        {
            humanRobotParts.AddRange(new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster });
        }
        myRobots = new List <Robot>();
        if (playerData.myRobots != default)
        {
            foreach (RobotData robotData in playerData.myRobots)
            {
                List <Part> robotPartList = new List <Part>();
                foreach (int partIndex in robotData.partIndices)
                {
                    if (!badIndices.Contains(partIndex))
                    {
                        robotPartList.Add(humanRobotParts[partIndex]);
                    }
                }
                try
                {
                    Robot robot = new Robot(robotData.name, true, robotData.human, robotPartList.ToArray());
                    myRobots.Add(robot);
                }
                catch {}
            }
        }
        foreach (int index in badIndices)
        {
            humanRobotParts.RemoveAt(index);
        }
        if (!humanRobotParts.Exists(part => part is Head))
        {
            humanRobotParts.Add(cheapestHead);
        }
        if (!humanRobotParts.Exists(part => part is Body))
        {
            humanRobotParts.Add(cheapestBody);
        }
        if (!humanRobotParts.Exists(part => part is Mobility))
        {
            humanRobotParts.Add(cheapestMobility);
        }
        if (!humanRobotParts.Exists(part => part is Attachment && ((Attachment)part).isWeapon()))
        {
            humanRobotParts.Add(cheapestBlaster);
        }
        if (humanRobotParts.Contains(null))
        {
            throw new Exception("There are missing part files. There neeeds to be at least one part file each of types Head, Body, Mobility, and a weapon Attachment.");
        }
        if (playerData.obstacles != default)
        {
            obstaclesData.AddRange(playerData.obstacles);
        }
        else
        {
            obstaclesData.AddRange(obstacleGenerator.getObstaclesData());
        }
        if (currentSettingValueList.Count == 0)
        {
            foreach (Setting setting in settingList)
            {
                currentSettingValueList.Add(setting.currentValue);
            }
        }
        Head       head1       = null;
        Head       head2       = null;
        Body       body1       = null;
        Body       body2       = null;
        Mobility   mobility    = null;
        Attachment attachment1 = null;
        Attachment attachment2 = null;
        Attachment attachment3 = null;

        foreach (Part part in parts)
        {
            if (part is Head)
            {
                if (head1 == null)
                {
                    head1 = (Head)part.clone(true);
                    head1.damage(2);
                }
                else if (head2 == null)
                {
                    head2 = (Head)part.clone(true);
                    head2.damage(3);
                }
            }
            else if (part is Body)
            {
                if (body1 == null)
                {
                    body1 = (Body)part.clone(true);
                    body1.damage(3);
                }
                else if (body2 == null)
                {
                    body2 = (Body)part.clone(true);
                    body2.damage(4);
                }
            }
            else if (part is Mobility)
            {
                if (mobility == null)
                {
                    mobility = (Mobility)part.clone(true);
                    mobility.damage(1);
                }
            }
            else if (part is Attachment)
            {
                if (attachment1 == null)
                {
                    attachment1 = (Attachment)part.clone(true);
                    attachment1.damage(1);
                }
                else if (attachment2 == null)
                {
                    attachment2 = (Attachment)part.clone(true);
                    attachment2.damage(1);
                }
                else if (attachment3 == null)
                {
                    attachment3 = (Attachment)part.clone(true);
                    attachment3.damage(3);
                }
            }
        }
        List <Robot> robots = new List <Robot>();
        int          numberOfNonHumanRobots = 0;
        int          numberOfHumanRobots    = 0;

        Obstacle[]           obstacles      = null;
        List <BuildHubState> buildHubStates = default;

        if (buildHubStatesData != default && buildHubStatesData.Count > 0)
        {
            buildHubStates = new List <BuildHubState>();
            foreach (BuildHubStateData buildHubStateData in buildHubStatesData)
            {
                buildHubStates.Add(buildHubStateDataManager.stateDataToState(buildHubStateData));
            }
        }
        List <FieldState> fieldStates = default;

        if (fieldStatesData != default && fieldStatesData.Count > 0)
        {
            fieldStates = new List <FieldState>();
            foreach (FieldStateData fieldStateData in fieldStatesData)
            {
                fieldStates.Add(fieldStateDataManager.stateDataToState(fieldStateData, parts));
            }
        }
        BuildHub.MODES mode;
        switch (currentTest)
        {
        case TESTS.FIELD:
        case TESTS.HUMAN_V_VOID_ROBOT:
            List <Part> humanParts = new List <Part>();
            foreach (Part part in humanRobot.getParts())
            {
                Part clonePart = part.clone(false);
                if (clonePart is Head)
                {
                    clonePart = (Head)clonePart;
                }
                else if (clonePart is Body)
                {
                    clonePart = (Body)clonePart;
                }
                else if (clonePart is Mobility)
                {
                    clonePart = (Mobility)clonePart;
                }
                else if (clonePart is Attachment)
                {
                    clonePart = (Attachment)clonePart;
                }
                humanParts.Add(clonePart);
            }
            humanRobot = new Robot(humanRobot.getName(), humanRobot.isHuman(), humanParts.ToArray());
            currentHumanRobotHealth = humanRobot.getRemainingDurability();
            Attachment[] nonHumanAttachments = { (Attachment)attachment1.clone(true) };
            numberOfNonHumanRobots = RANDOM.Next(1, settingPairs.max_ai_robots + 1);
            numberOfHumanRobots    = 1;
            obstacles = obstacleGenerator.obstaclesDataToObstacles(obstaclesData.ToArray());
            aiManager = new AIManager(numberOfNonHumanRobots, numberOfNonHumanRobots - 1 + numberOfHumanRobots, new Robot[] { humanRobot }, parts.ToArray(), new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster }, obstacles, fieldSize, buildHubStates, fieldStates, experience);
            robots.Add(humanRobot);
            robots.AddRange(aiManager.getAgentRobots());
            setScreen(new Field(settingList, robots.ToArray(), obstacles, previousRoundDamageDifference, previousRoundMaxDamageDifference, previousRoundTimeElapsed));
            break;

        case TESTS.AI_AGENT_TRAINING:
            numberOfNonHumanRobots = NUMBER_OF_AGENTS;
            numberOfHumanRobots    = 0;
            obstacles = obstacleGenerator.obstaclesDataToObstacles(obstaclesData.ToArray());
            aiManager = new AIManager(numberOfNonHumanRobots, numberOfNonHumanRobots - 1 + numberOfHumanRobots, null, parts.ToArray(), new Part[] { cheapestHead, cheapestBody, cheapestMobility, cheapestBlaster }, obstacles, fieldSize, buildHubStates, fieldStates, experience);
            Robot[] agentRobots = aiManager.getAgentRobots();
            for (int agentIndex = 0; agentIndex < NUMBER_OF_AGENTS; ++agentIndex)
            {
                FIELD_AGENT_ROBOTS[agentIndex] = agentRobots[agentIndex];
            }
            robots.AddRange(FIELD_AGENT_ROBOTS);
            setScreen(new Field(settingList, robots.ToArray(), obstacles, previousRoundDamageDifference, previousRoundMaxDamageDifference, previousRoundTimeElapsed));
            break;

        case TESTS.BUILD_HUB:
            mode = BuildHub.MODES.MY_ROBOTS;
            setScreen(new BuildHub(settingList, obstaclesData, myRobots, humanRobotParts, parts.ToArray(), credits, mode));
            break;
        }
    }