Example #1
0
    public void Initialize(Module toBuild, string depositID = null)
    {
        Data.ModuleTypeUnderConstruction = toBuild;
        Data.DepositInstanceID           = depositID;
        ModulePrefab = PrefabCache <Module> .Cache.GetPrefab(toBuild);

        InitializeRequirements();
        LastPlacedZone = this;
    }
Example #2
0
    void OnDestroy()
    {
        if (this == LastPlacedZone)
        {
            LastPlacedZone = null;
        }

        if (this == ZoneThatPlayerIsStandingIn)
        {
            ZoneThatPlayerIsStandingIn = null;
        }
    }
Example #3
0
    internal void Deconstruct()
    {
        if (ResourceList != null && ResourceList.Count > 0)
        {
            foreach (var r in ResourceList)
            {
                r.IsInConstructionZone = false;
            }
        }

        if (ZoneThatPlayerIsStandingIn == this)
        {
            ZoneThatPlayerIsStandingIn = null;
            GuiBridge.Instance.HideConstruction();
        }

        Destroy(this.gameObject);
    }
Example #4
0
 void OnTriggerExit(Collider other)
 {
     if (Data.ResourceCount != null)
     {
         if (other.CompareTag("Player"))
         {
             GuiBridge.Instance.HideConstruction();
             ZoneThatPlayerIsStandingIn = null;
         }
         else if (other.CompareTag("movable"))
         {
             ResourceComponent removedResources = other.GetComponent <ResourceComponent>();
             //todo: bug: removes surplus resources
             if (removedResources != null && removedResources.IsInConstructionZone && Data.ResourceCount.ContainsKey(removedResources.data.Container.MatterType))
             {
                 Data.ResourceCount[removedResources.Data.Container.MatterType] -= removedResources.Data.Container.CurrentAmount;
                 ResourceList.Remove(removedResources);
                 removedResources.IsInConstructionZone = false;
                 RefreshCanConstruct();
                 GuiBridge.Instance.ShowConstruction(Construction.BuildData[Data.ModuleTypeUnderConstruction].Requirements, Data.ResourceCount, Data.ModuleTypeUnderConstruction);
             }
         }
     }
 }
Example #5
0
    void OnTriggerEnter(Collider other)
    {
        if (Data.ResourceCount != null)
        {
            if (other.CompareTag("Player"))
            {
                GuiBridge.Instance.ShowConstruction(Construction.BuildData[Data.ModuleTypeUnderConstruction].Requirements, Data.ResourceCount, Data.ModuleTypeUnderConstruction);
                ZoneThatPlayerIsStandingIn = this;
            }
            else if (other.CompareTag("movable"))
            {
                ResourceComponent addedResources = other.GetComponent <ResourceComponent>();

                if (addedResources != null && !addedResources.IsInConstructionZone && Data.ResourceCount.ContainsKey(addedResources.data.Container.MatterType))
                {
                    Data.ResourceCount[addedResources.Data.Container.MatterType] += addedResources.Data.Container.CurrentAmount;
                    ResourceList.Add(addedResources);
                    addedResources.IsInConstructionZone = true;
                    RefreshCanConstruct();
                    GuiBridge.Instance.ShowConstruction(Construction.BuildData[Data.ModuleTypeUnderConstruction].Requirements, Data.ResourceCount, Data.ModuleTypeUnderConstruction);
                }
            }
        }
    }
Example #6
0
    public void Complete()
    {
#warning band-aid fix to stop InitializeStartingData from being called twice
        Game.Current.IsNewGame = false;
        //todo: move player out of the way
        //actually, we _should_ only be able to complete construction when the player
        //is outside the zone looking in, so maybe not

        Transform newT = (Transform)GameObject.Instantiate(ModulePrefab, this.transform.position, this.transform.rotation);

        //link ore extractor to deposit
        if (!String.IsNullOrEmpty(Data.DepositInstanceID))
        {
            OreExtractor drill = newT.GetComponent <OreExtractor>();
            if (drill != null)
            {
                drill.InitializeDeposit(Data.DepositInstanceID);
            }
        }

        newT.GetComponent <IBuildable>().InitializeStartingData();

        if (ZoneThatPlayerIsStandingIn == this)
        {
            ZoneThatPlayerIsStandingIn = null;
            GuiBridge.Instance.HideConstruction();
        }

        Game.Current.Score.ModulesBuilt++;

        //copy the requirements by adding them to a dictionary
        Dictionary <Matter, float> matterToVolumeMap = Construction.BuildData[Data.ModuleTypeUnderConstruction].Requirements.ToDictionary(x => x.Type, y => y.AmountByVolume);
        //copy the resource list by toArray it
        ResourceComponent[] components = ResourceList.ToArray();

        //go backwards through the components
        for (int i = components.Length - 1; i > -1; i--)
        {
            ResourceComponent component    = components[i];
            float             volumeToPull = 0f;
            //if the component is used in the requirements
            if (matterToVolumeMap.TryGetValue(component.Data.Container.MatterType, out volumeToPull) && volumeToPull > 0f)
            {
                //decrease the matterToVolumeMap by as much pull as possible
                matterToVolumeMap[component.Data.Container.MatterType] -= component.Data.Container.Pull(volumeToPull);

                //then if the crate is spent
                if (component.Data.Container.CurrentAmount <= 0f)
                {
                    //delete it
                    Destroy(component.gameObject);
                }

                //if the requirement is met
                if (matterToVolumeMap[component.Data.Container.MatterType] <= 0f)
                {
                    //remove the requirement (to stop the next component from depleting any)
                    matterToVolumeMap.Remove(component.Data.Container.MatterType);
                }
            }
        }
        if (matterToVolumeMap.Keys.Count > 0)
        {
            Debug.LogError("Construction zone built something but had requirements left over!");
        }
        this.ResourceList = null;

        Destroy(this.gameObject);
    }