Пример #1
0
    public BuildingModule GetModuleOfType(NeighborState type)
    {
        int moduleIndex;

        switch (type)
        {
        case NeighborState.type0:
            moduleIndex = Random.Range(0, mType0Modules.Count);
            return(mType0Modules[moduleIndex]);

        case NeighborState.type1:
            moduleIndex = Random.Range(0, mType1Modules.Count);
            return(mType1Modules[moduleIndex]);

        case NeighborState.type2:
            moduleIndex = Random.Range(0, mType2Modules.Count);
            return(mType2Modules[moduleIndex]);

        case NeighborState.type3:
            moduleIndex = Random.Range(0, mType3Modules.Count);
            return(mType3Modules[moduleIndex]);

        case NeighborState.type4:
            moduleIndex = Random.Range(0, mType4Modules.Count);
            return(mType4Modules[moduleIndex]);

        case NeighborState.type5:
            moduleIndex = Random.Range(0, mType5Modules.Count);
            return(mType5Modules[moduleIndex]);

        case NeighborState.type6:
            moduleIndex = Random.Range(0, mType6Modules.Count);
            return(mType6Modules[moduleIndex]);

        case NeighborState.type7:
            moduleIndex = Random.Range(0, mType7Modules.Count);
            return(mType7Modules[moduleIndex]);

        default:
            Debug.LogError("Provided invalid input to GetModuleOfTpe in BuildingModuleSet. Look at this");
            return(mDoorModules[0]);
        }
    }
Пример #2
0
    /// ------------------------------------- PUBLIC METHODS ----------------------------------- ///

    /// <summary>
    /// Public method calls to have this building build a new module if not already full
    /// </summary>
    public void BuildNewModule()
    {
        if (mEmptySlots == 0)
        {
            Debug.Log("This building is full and can't build anymore modules");
            return;
        }

        CameraShaker camShake = GetComponent <CameraShaker>();
        AudioSource  audio    = GetComponent <AudioSource>();

        // special case - if it's the first module, make it a door.
        if (mEmptySlots == mBuildingWidth * mBuildingHeight)
        {
            int doorSlot = (int)(((mBuildingWidth * 0.5f) - 1));
            mSlots[doorSlot].GetComponent <BuildingSlot>().SetBuildingModule(mModuleSet.mDoorModules[0]);
            mSlots[doorSlot].GetComponent <BuildingSlot>().mIsDoor = true;
            mLastModuleBuiltIndex = doorSlot;
            mEmptySlots          -= 1;

            // add in little camera shake when building a new module
            if (camShake)
            {
                camShake.ShakeCamera();
            }

            if (audio)
            {
                audio.Play();
            }

            return;
        }


        //Debug.Log("Building a new module in building " + gameObject);
        // select which slot to fill next based on what module was built last
        int nextSlot = SelectNextSlotToBuild();

        if (nextSlot < 0)
        {
            Debug.LogError("Took too long choosing a random slot, didn't build anything. Try again.");
            return;
        }

        NeighborState  neighbors   = CheckNeighborSlots(nextSlot);
        BuildingModule firstModule = mModuleSet.GetModuleOfType(neighbors);

        mSlots[nextSlot].GetComponent <BuildingSlot>().SetBuildingModule(firstModule);
        mEmptySlots -= 1;

        // add in little camera shake when building a new module
        if (camShake)
        {
            camShake.ShakeCamera();
        }

        if (audio)
        {
            audio.Play();
        }

        // update neighbor state for all slots to adjust module types if needed
        for (int i = 0; i < mSlots.Count; i++)
        {
            BuildingSlot slot = mSlots[i].GetComponent <BuildingSlot>();

            // if this slot is still empty, don't care about its neighbors yet
            if (slot.IsSlotEmpty())
            {
                continue;
            }

            NeighborState nState = CheckNeighborSlots(i);

            // if this slot's neighbors haven't changed, no need to update
            if (nState == slot.mModuleType)
            {
                continue;
            }

            if (slot.mModuleType != NeighborState.empty)
            {
                slot.mModuleType = nState;
                BuildingModule module = mModuleSet.GetModuleOfType(slot.mModuleType);
                if (slot.mIsDoor)
                {
                    slot.SetBuildingModule(mModuleSet.mDoorModules[(int)slot.mModuleType]);
                }
                else
                {
                    slot.SetBuildingModule(module);
                }
            }
        }
    }