Ejemplo n.º 1
0
 public void Combine(InventoryItem a, InventoryItem b)
 {
     hasCombineFunction = hasCombineFunction || ink.story.HasFunction(inkCombineFunctionName);
     if (!hasCombineFunction)
     {
         throw new System.Exception($"Story doesn't include inventory combining function ({inkCombineFunctionName})!");
     }
     if (!list.ContainsItemNamed(a.name) && !list.ContainsItemNamed(b.name))
     {
         Debug.LogError($"Inventory doesn't currently include both of those items: {a}, {b}");
         return;
     }
     var result = ink.story.EvaluateFunction(inkCombineFunctionName, a.value.Union(b.value));
 }
        private void SyncListToFungus(string inkListName, object inkListNewValue)
        {
            InkList inkList = story.variablesState[inkListName] as InkList;

            if (inkList == null)
            {
                Debug.LogError("Sync list mismatch: " + inkListName + " is not a list in Ink");
            }
            else
            {
                foreach (string syncVariableName in syncVariables.Keys)
                {
                    if (syncVariableName.StartsWith(inkListName + ListItemSeparator))
                    {
                        string inkItemName = SplitFungusVariableName(syncVariableName)[1];
                        int    itemInList  = inkList.ContainsItemNamed(inkItemName) ? 1 : 0;
                        SyncToFungus(syncVariableName, itemInList);
                    }
                }
            }
        }
        public void OnVariablesChanged(Flowchart origin)
        {
            Debug.Log("OnVariablesChange origin=" + origin.name);
            if (beforeFirstSync)
            {
                Debug.Log("Still initializing, Fungus->Ink sync request ignored");
                return;
            }
            List <Variable> affectedFungusVariables;

            if (origin != null)
            {
                affectedFungusVariables = origin.Variables;
            }
            else
            {
                affectedFungusVariables = new List <Variable>();
                foreach (KeyValuePair <string, List <Flowchart> > syncVariable in syncVariables)
                {
                    // Picks the first flowchart: any flowchart is OK, it's a global variable
                    affectedFungusVariables.Add(syncVariable.Value[0].GetVariable(syncVariable.Key));
                }
            }
            bool useless = true;

            foreach (Variable fungusVariable in affectedFungusVariables)
            {
                if (fungusVariable.Scope == VariableScope.Global && syncVariables.ContainsKey(fungusVariable.Key))
                {
                    useless = false;
                    Type fungusVariableType = fungusVariable.GetValue().GetType();
                    if (fungusVariableType == typeof(int))
                    {
                        int intValue = (fungusVariable as IntegerVariable).Value;
                        Debug.Log(fungusVariable.Key + "=" + intValue + " (Fungus->Ink)");
                        story.variablesState[fungusVariable.Key] = intValue;
                    }
                    else if (fungusVariableType == typeof(string))
                    {
                        string stringValue = (fungusVariable as StringVariable).Value;
                        Debug.Log(fungusVariable.Key + "=" + stringValue + " (Fungus->Ink)");
                        story.variablesState[fungusVariable.Key] = stringValue;
                    }
                    else if (fungusVariableType == typeof(float))
                    {
                        float floatValue = (fungusVariable as FloatVariable).Value;
                        Debug.Log(fungusVariable.Key + "=" + floatValue + " (Fungus->Ink)");
                        story.variablesState[fungusVariable.Key] = floatValue;
                    }
                    else if (fungusVariableType == typeof(bool))
                    {
                        bool boolValue = (fungusVariable as BooleanVariable).Value;
                        Debug.Log(fungusVariable.Key + "=" + boolValue + " (Fungus->Ink)");
                        string[] listVariableParts = SplitFungusVariableName(fungusVariable.Key);
                        if (listVariableParts.Length <= 1)
                        {
                            story.variablesState[fungusVariable.Key] = (boolValue ? 1 : 0);
                        }
                        else
                        {
                            string  listName     = listVariableParts[0];
                            string  listItemName = listVariableParts[1];
                            InkList inkList      = story.variablesState[listName] as InkList;
                            if (inkList == null)
                            {
                                Debug.LogError("Sync list mismatch: " + listName + " is not a list in Ink");
                            }
                            else
                            {
                                if (boolValue)
                                {
                                    if (!inkList.ContainsItemNamed(listItemName))
                                    {
                                        inkList.AddItem(listItemName);
                                        Debug.Log(listItemName + " added in Ink list " + listName);
                                    }
                                }
                                else
                                {
                                    List <InkListItem> toRemove = new List <InkListItem>();
                                    foreach (InkListItem inkListItem in inkList.Keys)
                                    {
                                        if (inkListItem.itemName == listItemName)
                                        {
                                            toRemove.Add(inkListItem);
                                        }
                                    }
                                    foreach (InkListItem inkListItem in toRemove)
                                    {
                                        inkList.Remove(inkListItem);
                                        Debug.Log(inkListItem.fullName + " removed from Ink list " + listName);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Sync variable skipped: " + fungusVariable.Key + " is " + fungusVariableType
                                         + " in Fungus, which does not translate well to Ink");
                    }
                }
            }
            if (useless)
            {
                Debug.LogWarning("No global Fungus variables to sync, consider removing this OnVariablesChange call from the flowchart");
            }
        }