static void CreateFoodstuffStates( ) { foreach (GameObject selectedObject in Selection.gameObjects) { WIStates states = selectedObject.GetOrAdd <WIStates> (); states.EditorCreateStates(); FoodStuff foodstuff = selectedObject.GetOrAdd <FoodStuff> (); foodstuff.EditorCreateProps(); } }
//editor function to quickly set up a new foodstuff object public void EditorCreateProps() { if (State.PotentialProps.Count == 0) { WIStates states = gameObject.GetComponent <WIStates>(); if (states != null) { foreach (WIState state in states.States) { FoodStuffProps props = new FoodStuffProps(); props.Name = state.Name; switch (props.Name) { case "Raw": props.HungerRestore = PlayerStatusRestore.D_ThreeFifths; props.Type = FoodStuffEdibleType.Edible; break; case "Rotten": props.ConditionName = "FoodPoisoning"; props.HungerRestore = PlayerStatusRestore.C_TwoFifths; props.ConditionChance = 0.75f; props.Type = FoodStuffEdibleType.Edible; break; case "Cooked": props.HealthRestore = PlayerStatusRestore.F_Full; props.Type = FoodStuffEdibleType.Edible; break; case "Preserved": props.HungerRestore = PlayerStatusRestore.F_Full; props.Type = FoodStuffEdibleType.Edible; break; case "Burned": props.HungerRestore = PlayerStatusRestore.D_ThreeFifths; props.Type = FoodStuffEdibleType.Edible; break; } State.PotentialProps.Add(props); } } else { FoodStuffProps props = new FoodStuffProps(); props.HungerRestore = PlayerStatusRestore.C_TwoFifths; props.Type = FoodStuffEdibleType.Edible; State.PotentialProps.Add(props); } } }
public void DrawGenericWorldItemSelector(GenericWorldItem item, ref bool open, bool useStrictness) { if (WorldItems.Get == null) { Manager.WakeUp <WorldItems>("Frontiers_WorldItems"); WorldItems.Get.Initialize(); } bool drawEditor = true; UnityEditor.EditorStyles.miniButton.stretchWidth = true; UnityEditor.EditorStyles.textField.stretchWidth = true; UnityEditor.EditorStyles.popup.stretchWidth = true; GUILayout.BeginVertical(); if (string.IsNullOrEmpty(item.PackName)) { drawEditor = false; UnityEngine.GUI.color = Color.green; if (GUILayout.Button("(Add)", UnityEditor.EditorStyles.miniButton)) { item.PackName = "AncientArtifacts"; item.PrefabName = string.Empty; drawEditor = true; } UnityEngine.GUI.color = Color.yellow; if (GUILayout.Button("(Done)", UnityEditor.EditorStyles.miniButton)) { drawEditor = false; selectedItemIndex = -1; selectedRowIndex = -1; } } else { UnityEngine.GUI.color = Color.red; if (GUILayout.Button("(Clear)", UnityEditor.EditorStyles.miniButton)) { item.Clear(); drawEditor = false; } } if (drawEditor) { UnityEngine.GUI.color = Color.yellow; int packIndex = 0; List <string> packNames = new List <string>(); WorldItemPack pack = null; WorldItem worlditem = null; for (int i = 0; i < WorldItems.Get.WorldItemPacks.Count; i++) { string packName = WorldItems.Get.WorldItemPacks[i].Name; packNames.Add(packName); if (item.PackName == packName) { packIndex = i; pack = WorldItems.Get.WorldItemPacks[i]; } } if (pack == null) { pack = WorldItems.Get.WorldItemPacks[0]; } packIndex = UnityEditor.EditorGUILayout.Popup("Pack:", packIndex, packNames.ToArray(), UnityEditor.EditorStyles.popup); List <string> prefabNames = new List <string>(); int prefabIndex = 0; for (int i = 0; i < pack.Prefabs.Count; i++) { prefabNames.Add(pack.Prefabs[i].name); if (item.PrefabName == pack.Prefabs[i].name) { prefabIndex = i; worlditem = pack.Prefabs[i].GetComponent <WorldItem>(); } } prefabIndex = UnityEditor.EditorGUILayout.Popup("Prefab:", prefabIndex, prefabNames.ToArray(), UnityEditor.EditorStyles.popup); item.PackName = packNames[packIndex]; item.PrefabName = prefabNames[prefabIndex]; if (worlditem != null) { UnityEngine.GUI.color = Color.yellow; item.DisplayName = worlditem.DisplayName; if (!useStrictness || Flags.Check((uint)Blueprint.Strictness, (uint)BlueprintStrictness.StackName, Flags.CheckType.MatchAny)) { GUILayout.BeginHorizontal(); GUILayout.Label("StackName: "); if (string.IsNullOrEmpty(item.StackName) || !worlditem.StackName.Contains(item.StackName)) { item.StackName = worlditem.StackName; } item.StackName = GUILayout.TextField(item.StackName); GUILayout.EndHorizontal(); } if (!useStrictness || Flags.Check((uint)Blueprint.Strictness, (uint)BlueprintStrictness.StateName, Flags.CheckType.MatchAny)) { GUILayout.BeginHorizontal(); int stateIndex = 0; List <string> stateNames = new List <string>(); stateNames.Add("Default"); WIStates states = worlditem.GetComponent <WIStates>(); if (states != null) { for (int i = 0; i < states.States.Count; i++) { stateNames.Add(states.States[i].Name); if (item.State == states.States[i].Name) { stateIndex = i + 1; //since the first one is Default } } } stateIndex = UnityEditor.EditorGUILayout.Popup("State:", stateIndex, stateNames.ToArray(), UnityEditor.EditorStyles.popup); GUILayout.EndHorizontal(); item.State = stateNames[stateIndex]; } } UnityEngine.GUI.color = Color.white; if (!useStrictness || Flags.Check((uint)Blueprint.Strictness, (uint)BlueprintStrictness.Subcategory, Flags.CheckType.MatchAny)) { GUILayout.BeginHorizontal(); GUILayout.Label("Subcategory: "); item.Subcategory = GUILayout.TextField(item.Subcategory); GUILayout.EndHorizontal(); } UnityEngine.GUI.color = Color.yellow; if (GUILayout.Button("(Done)", UnityEditor.EditorStyles.miniButton)) { open = false; } } GUILayout.EndVertical(); UnityEditor.EditorStyles.miniButton.stretchWidth = true; UnityEditor.EditorStyles.textField.stretchWidth = true; UnityEditor.EditorStyles.popup.stretchWidth = true; }
//this editor function does a bunch of sanity-check stuff //to make sure that the foodstuff is set up properly public override void InitializeTemplate() { base.InitializeTemplate(); if (NonStandardProps) { return; } mCurrentProps = null; if (State.PotentialProps.Count > 0) { WIStates states = null; if (gameObject.HasComponent <WIStates>(out states)) { for (int i = 0; i < State.PotentialProps.Count; i++) { if (State.PotentialProps[i].Name == states.DefaultState) { mCurrentProps = State.PotentialProps[i]; break; } } } if (mCurrentProps == null) { for (int i = 0; i < State.PotentialProps.Count; i++) { if (State.PotentialProps[i].Name == "Raw") { mCurrentProps = State.PotentialProps[i]; break; } } } if (mCurrentProps == null) { //f**k it mCurrentProps = State.PotentialProps[0]; } } return; if (Application.isPlaying) { return; } if (worlditem.Props.Global.MaterialType != WIMaterialType.Plant && worlditem.Props.Global.MaterialType != WIMaterialType.Flesh) { worlditem.Props.Global.MaterialType = WIMaterialType.Food; } if (State.PotentialProps.Count == 0) { Debug.Log("Didn't find any potential food props, creating 'Raw' now"); FoodStuffProps props = new FoodStuffProps(); props.Name = "Raw"; props.Type = FoodStuffEdibleType.Edible; props.HungerRestore = PlayerStatusRestore.B_OneFifth; props.Perishable = false; State.PotentialProps.Add(props); } else { Debug.Log("Found " + State.PotentialProps.Count.ToString() + " props"); } if (worlditem.States != null) { Debug.Log("worlditem has states"); foreach (WIState state in worlditem.States.States) { bool foundAccompanyingFoodState = false; foreach (FoodStuffProps props in this.State.PotentialProps) { if (props.Name == state.Name) { foundAccompanyingFoodState = true; } } if (!foundAccompanyingFoodState) { Debug.Log("Didn't find accompanying food state in " + worlditem.name + " for worlditem state: " + state.Name + ", adding now"); FoodStuffProps newProps = new FoodStuffProps(); newProps.Name = state.Name; //there are some common states that we can make guesses for switch (state.Name) { case "Rotten": newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.B_OneFifth; newProps.ConditionChance = 0.95f; newProps.ConditionName = "FoodPoisoning"; newProps.Perishable = false; break; case "Raw": newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.C_TwoFifths; newProps.Perishable = true; break; case "Cooked": newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.F_Full; newProps.Perishable = true; break; case "Burned": newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.B_OneFifth; newProps.Perishable = false; break; case "Preserved": case "Dried": newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.D_ThreeFifths; newProps.Perishable = false; break; default: newProps.Type = FoodStuffEdibleType.None; //make it inedible break; } State.PotentialProps.Add(newProps); } } } bool requiresRottenState = false; foreach (FoodStuffProps props in State.PotentialProps) { if (props.Name == "Raw") { bool foundCooked = false; foreach (FoodStuffProps cookedProps in State.PotentialProps) { if (cookedProps.Name == "Cooked") { foundCooked = true; props.ConditionName = "FoodPoisoning"; props.ConditionChance = 0.25f; break; } } if (!foundCooked) { props.ConditionName = string.Empty; props.ConditionChance = 0f; } else { gameObject.GetOrAdd <Photosensitive>(); } } requiresRottenState |= props.Perishable; if (props.Name == "Rotten") { requiresRottenState |= true; } } //check our states against our props and make sure there's parity if (State.PotentialProps.Count > 1) { if (worlditem.States == null) { worlditem.States = worlditem.gameObject.AddComponent <WIStates>(); worlditem.InitializeTemplate(); return; } //now attempt to match our props up against our states and make sure there's parity foreach (FoodStuffProps props in State.PotentialProps) { WIState existingState = null; bool foundAccompanyingState = false; foreach (WIState state in worlditem.States.States) { if (state.Name == props.Name) { if (!foundAccompanyingState) { existingState = state; foundAccompanyingState = true; } if (state.Name == "Rotten") { requiresRottenState = true; } } } if (!foundAccompanyingState) { WIState newState = null; Debug.Log("Didn't find accompanying worlditem state in " + worlditem.name + " for food state: " + props.Name + ", adding now"); if (worlditem.States.States.Count == 0) { //create a state from the base object - this will also strip the base object of renderers etc. newState = WorldItems.CreateTemplateState(worlditem, props.Name, worlditem.gameObject); } else { //otherwise just make a copy from the first existing state, we'll clean it up later newState = WorldItems.CreateTemplateState(worlditem, props.Name, worlditem.States.States[0].StateObject); } existingState = newState; } if (existingState != null) { existingState.IsInteractive = true; existingState.Suffix = props.Name; existingState.UnloadWhenStacked = true; existingState.CanEnterInventory = true; existingState.CanBePlaced = true; existingState.CanBeDropped = true; existingState.CanBeCarried = true; switch (existingState.Name) { case "Raw": existingState.IsPermanent = false; break; case "Cooked": existingState.IsPermanent = true; existingState.FXOnChange = "FoodstuffCookedSmoke"; break; case "Rotten": existingState.IsPermanent = true; break; case "Burned": existingState.IsPermanent = true; existingState.FXOnChange = "FoodstuffBurnedSmoke"; break; case "Preserved": case "Dried": existingState.IsPermanent = true; break; default: break; } } } } else { Debug.Log("Only 1 foodstuff props so no need for states"); if (worlditem.States != null && worlditem.States.States.Count == 0) { GameObject.DestroyImmediate(worlditem.States); } } if (requiresRottenState) { Debug.Log("Requires perishable, checking now"); bool hasRottenState = false; bool hasRawState = false; foreach (FoodStuffProps props in State.PotentialProps) { if (props.Name == "Raw") { props.Perishable = true; hasRawState = true; } else if (props.Name == "Rotten") { hasRottenState = true; } } if (!hasRawState) { FoodStuffProps newProps = new FoodStuffProps(); newProps.Name = "Raw"; newProps.Perishable = true; newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.C_TwoFifths; newProps.Perishable = true; State.PotentialProps.Add(newProps); } if (!hasRottenState) { FoodStuffProps newProps = new FoodStuffProps(); newProps.Name = "Rotten"; newProps.Type = FoodStuffEdibleType.Edible; newProps.HungerRestore = PlayerStatusRestore.C_TwoFifths; newProps.ConditionChance = 0.5f; newProps.ConditionName = "FoodPoisoning"; State.PotentialProps.Add(newProps); } } else { Debug.Log("Didn't require rotten state"); } if (worlditem.States != null) { bool foundDefaultState = false; foreach (WIState state in worlditem.States.States) { if (worlditem.States.DefaultState == state.Name) { foundDefaultState = true; break; } } if (!foundDefaultState) { Debug.Log("Didin't find default state " + worlditem.States.DefaultState + ", setting to Raw"); worlditem.States.DefaultState = "Raw"; } } if (worlditem.HasStates) { //now that the worlditem has states it shouldn't have its own mesh stuff //destroy the me now MeshFilter worldItemMF = null; if (worlditem.gameObject.HasComponent <MeshFilter>(out worldItemMF)) { GameObject.DestroyImmediate(worldItemMF); } MeshRenderer worldItemMR = null; if (worlditem.gameObject.HasComponent <MeshRenderer>(out worldItemMR)) { GameObject.DestroyImmediate(worldItemMR); } if (worlditem.GetComponent <Collider>() != null) { GameObject.DestroyImmediate(worlditem.GetComponent <Collider>()); } } foreach (FoodStuffProps props in State.PotentialProps) { if (props.IsLiquid) { props.EatFoodSound = "DrinkLiquidGeneric"; } else { props.EatFoodSound = "EatFoodGeneric"; } } mCurrentProps = null; base.InitializeTemplate(); }