Beispiel #1
0
    // Update is called once per defined interval (1 second)
    private void UpdateInterval()
    {
        if (currentResourceChange != null)
        {
            foreach (KeyValuePair <ResourceTypesModel, float> pair in currentResourceChange)
            {
                ResourceTypesModel resource = pair.Key;
                float value = pair.Value;
                Debug.Log("update resource storage for " + resource.GetName() + ": " + value);

                // re-calculate resource storage
                if (resourceStorage.ContainsKey(resource))
                {
                    // Aufruf von CbOnResourcesChanged, wenn ein if-Fall eintritt, um die neuen Produktionswerte auszurechnen (Veränderungen durch Consumption/Production)
                    // --> kein BuildingModel zum Übergeben
                    if (resourceStorage[resource] + value < 0)
                    {
                        resourceStorage[resource] = 0;
                    }
                    else if (resourceStorage[resource] + value > resourceCapacity[resource])
                    {
                        resourceStorage[resource] = resourceCapacity[resource];
                    }
                    else
                    {
                        resourceStorage[resource] += value;
                    }
                }
                else
                {
                    resourceStorage.Add(resource, value);
                }
                Debug.Log("New storage for " + resource.GetName() + ": " + resourceStorage[resource]);

                // update resource top bar
                GameObject  item     = resourceBarItems[resource];
                Component[] children = item.GetComponentsInChildren(typeof(Text), true);
                foreach (Component c in children)
                {
                    if (c.gameObject.name.Equals("Value"))
                    {
                        c.GetComponent <Text>().text = resourceStorage[resource].ToString();
                    }
                }
            }
        }
        else
        {
            Debug.LogError("currentResourceChange is null");
        }
    }
Beispiel #2
0
    /// <summary>
    /// Callback gets called when new buildings was built or got updated
    /// </summary>
    /// <param name="changedBuilding">BuildingModel of the changed building</param>
    public void CbOnResourcesChanged(BuildingModel changedBuilding)
    {
        Debug.Log("CbOnResourcesChanged (" + changedBuilding.buildingType.GetName() + ")");
        if (productionBuildings.Contains(changedBuilding))
        {
            Debug.Log("Existing building changed of " + productionBuildings.Count);
        }
        else
        {
            productionBuildings.Add(changedBuilding);
            Debug.Log("New building added to " + productionBuildings.Count);
        }

        // re-calculate production/capacity
        currentResourceChange = new Dictionary <ResourceTypesModel, float>();
        foreach (ResourceTypesModel resource in resourceTypes.Values)
        {
            currentResourceChange[resource] = 0;
        }

        resourceCapacity = new Dictionary <ResourceTypesModel, float>();
        foreach (ResourceTypesModel resource in resourceTypes.Values)
        {
            resourceCapacity[resource] = 0;
        }

        // TODO: real calculation
        // Iterierung sollte über alle Gebäude stattfinden, oder wenigstens alle mit Lagerkapazitäten

        // iterate through built production buildings
        foreach (BuildingModel building in productionBuildings)
        {
            Debug.Log("Production building " + building.buildingType.GetName());
            int buildingLevel = building.GetLevel();

            // iterate through building consume
            Debug.Log("Consume Entries: " + building.buildingType.GetConsumes().Count);
            if (building.buildingType.GetConsumes().ContainsKey(buildingLevel))
            {
                foreach (KeyValuePair <ResourceTypesModel, float> pair in building.buildingType.GetConsumes()[buildingLevel])
                {
                    ResourceTypesModel resource = pair.Key;
                    float value = pair.Value;
                    Debug.Log("Consume of " + resource.GetName() + ": " + value);
                    currentResourceChange[resource] -= value;
                }
            }

            // iterate through building production
            Debug.Log("Production Entries: " + building.buildingType.GetProduces().Count);
            if (building.buildingType.GetProduces().ContainsKey(buildingLevel))
            {
                foreach (KeyValuePair <ResourceTypesModel, float> pair in building.buildingType.GetProduces()[buildingLevel])
                {
                    ResourceTypesModel resource = pair.Key;
                    float value = pair.Value;
                    Debug.Log("Production of " + resource.GetName() + ": " + value);
                    currentResourceChange[resource] += value;
                }
            }

            // iterate through building capacity
            Debug.Log("Stores Entries: " + building.buildingType.GetStores().Count);
            if (building.buildingType.GetStores().ContainsKey(buildingLevel))
            {
                foreach (KeyValuePair <ResourceTypesModel, float> pair in building.buildingType.GetStores()[buildingLevel])
                {
                    ResourceTypesModel resource = pair.Key;
                    float value = pair.Value;
                    Debug.Log("Capacity of " + resource.GetName() + ": " + value);
                    resourceCapacity[resource] += value;
                }
            }
        }
    }