Ejemplo n.º 1
0
    public SavedComponentChip(ChipSaveData chipSaveData, Chip chip)
    {
        chipName = chip.chipName;

        // Store position in doubles and limit precision to reduce space in save file
        const double precision = 10000;

        posX = ((int)(chip.transform.position.x * precision)) / precision;
        posY = ((int)(chip.transform.position.y * precision)) / precision;

        // Input pins
        inputPins = new SavedInputPin[chip.inputPins.Length];
        for (int i = 0; i < inputPins.Length; i++)
        {
            inputPins[i] = new SavedInputPin(chipSaveData, chip.inputPins[i]);
        }

        // Output pins (only need to store names, connections are stored on input pins only)
        outputPinNames = new string[chip.outputPins.Length];
        for (int i = 0; i < chip.outputPins.Length; i++)
        {
            outputPinNames[i] = chip.outputPins[i].pinName;
        }
    }
Ejemplo n.º 2
0
    // Instantiates all components that make up the given clip, and connects them up with wires
    // The components are parented under a single "holder" object, which is returned from the function
    static ChipSaveData LoadChip(SavedChip chipToLoad, Dictionary <string, Chip> previouslyLoadedChips, Wire wirePrefab)
    {
        ChipSaveData loadedChipData = new ChipSaveData();
        int          numComponents  = chipToLoad.savedComponentChips.Length;

        loadedChipData.componentChips = new Chip[numComponents];
        loadedChipData.chipName       = chipToLoad.name;
        loadedChipData.chipColour     = chipToLoad.colour;
        loadedChipData.chipNameColour = chipToLoad.nameColour;
        loadedChipData.creationIndex  = chipToLoad.creationIndex;

        // Spawn component chips (the chips used to create this chip)
        // These will have been loaded already, and stored in the previouslyLoadedChips dictionary
        for (int i = 0; i < numComponents; i++)
        {
            SavedComponentChip componentToLoad = chipToLoad.savedComponentChips[i];
            string             componentName   = componentToLoad.chipName;
            Vector2            pos             = new Vector2((float)componentToLoad.posX, (float)componentToLoad.posY);

            if (!previouslyLoadedChips.ContainsKey(componentName))
            {
                Debug.LogError("Failed to load sub component: " + componentName + " While loading " + chipToLoad.name);
            }

            Chip loadedComponentChip = GameObject.Instantiate(previouslyLoadedChips[componentName], pos, Quaternion.identity);
            loadedChipData.componentChips[i] = loadedComponentChip;

            // Load input pin names
            for (int inputIndex = 0; inputIndex < componentToLoad.inputPins.Length; inputIndex++)
            {
                loadedChipData.componentChips[i].inputPins[inputIndex].pinName = componentToLoad.inputPins[inputIndex].name;
            }

            // Load output pin names
            for (int ouputIndex = 0; ouputIndex < componentToLoad.outputPinNames.Length; ouputIndex++)
            {
                loadedChipData.componentChips[i].outputPins[ouputIndex].pinName = componentToLoad.outputPinNames[ouputIndex];
            }
        }

        // Connect pins with wires
        for (int chipIndex = 0; chipIndex < chipToLoad.savedComponentChips.Length; chipIndex++)
        {
            Chip loadedComponentChip = loadedChipData.componentChips[chipIndex];
            for (int inputPinIndex = 0; inputPinIndex < loadedComponentChip.inputPins.Length; inputPinIndex++)
            {
                SavedInputPin savedPin = chipToLoad.savedComponentChips[chipIndex].inputPins[inputPinIndex];
                Pin           pin      = loadedComponentChip.inputPins[inputPinIndex];

                // If this pin should receive input from somewhere, then wire it up to that pin
                if (savedPin.parentChipIndex != -1)
                {
                    Pin connectedPin = loadedChipData.componentChips[savedPin.parentChipIndex].outputPins[savedPin.parentChipOutputIndex];
                    pin.cyclic = savedPin.isCylic;
                    Pin.TryConnect(connectedPin, pin);
                    //if (Pin.TryConnect (connectedPin, pin)) {
                    //Wire loadedWire = GameObject.Instantiate (wirePrefab, parent : chipHolder);
                    //loadedWire.Connect (connectedPin, loadedComponentChip.inputPins[inputPinIndex]);
                    //}
                }
            }
        }

        return(loadedChipData);
    }