//public override void OnDestroy() //{ // if (!Application.isLoadingLevel) // { // the object should be removed from list of recreate-items // UniRPGGlobal.RemoveAutoRecreate(saveKey); // } //} //protected override void SaveState() //{ //} //protected override void LoadState() //{ // //if (UniRPGGlobal.Instance.DoNotLoad) return; //} //public void SaveAutoRecreate(string key) //{ //} //public void LoadAutoRecreate(string key) //{ // Debug.Log("LoadAutoRecreate: " + key); //} public void DoDrop(RPGLoot containedLoot) { IsPersistent = false; // for now drops can't be saved //UniRPGGlobal.RegisterAutoRecreate(typeof(RPGLootDrop), saveKey); loot = containedLoot; canBeTargeted = false; // player can't yet target it playDropAni = true; // calculate drop animation float x = Random.Range(0.5f, 1.5f) * (Random.Range(0, 10) > 5 ? -1 : 1); float z = Random.Range(0.5f, 1.5f) * (Random.Range(0, 10) > 5 ? -1 : 1); x += transform.position.x; z += transform.position.z; Vector3 mid = new Vector3(x, transform.position.y + 1.5f, z); Vector3 end = new Vector3(x, transform.position.y, z); // check that end is on the floor int mask = (1 << UniRPGGlobal.DB.floorLayerMask); RaycastHit hit; if (Physics.Raycast(mid, Vector3.down, out hit, 100f, mask)) { end = hit.point; } // create the path path = new Vector3[] { transform.position, mid, end }; }
void OnGUI() { if (!inited) Init(); UniRPGEdGui.UseSkin(); scroll = UniRPGEdGui.BeginScrollView(scroll); { if (UniRPGEditorGlobal.DB.loot.Count > 0) { foreach (RPGLoot cl in UniRPGEditorGlobal.DB.loot) { EditorGUILayout.BeginHorizontal(); { GUILayout.Space(5); if (UniRPGEdGui.ToggleButton(loot == cl, cl.screenName, UniRPGEdGui.ButtonStyle, UniRPGEdGui.ButtonOnColor, GUILayout.Width(170))) { loot = cl; } } EditorGUILayout.EndHorizontal(); } } else { GUILayout.Label("No Loot Tables defined", UniRPGEdGui.WarningLabelStyle); } } UniRPGEdGui.EndScrollView(); UniRPGEdGui.DrawHorizontalLine(1, UniRPGEdGui.DividerColor, 0, 10); EditorGUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); if (loot == null) GUI.enabled = false; if (GUILayout.Button("Accept", UniRPGEdGui.ButtonStyle)) accepted = true; GUI.enabled = true; if (GUILayout.Button("Cancel", UniRPGEdGui.ButtonStyle)) this.Close(); GUILayout.FlexibleSpace(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); }
// ================================================================================================================ public void CopyTo(RPGLoot l) { l.id = this.id.Copy(); l.screenName = this.screenName; l.dropAsBag = this.dropAsBag; l.bagPrefab = this.bagPrefab; foreach (Reward r in this.rewards) { l.rewards.Add(new Reward() { type = r.type, count = r.count, guid = r.guid.Copy(), chance = r.chance, lookatNpcLevel = r.lookatNpcLevel, npcMaxLevel = r.npcMaxLevel, group = r.group }); } }
private void LeftPanel() { EditorGUILayout.BeginVertical(GUILayout.Width(DatabaseEditor.LeftPanelWidth)); GUILayout.Space(5); // ------------------------------------------------------------- // the add button EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); if (GUILayout.Button(new GUIContent("Add Loot", UniRPGEdGui.Icon_Plus), EditorStyles.miniButtonLeft)) { GUI.FocusControl(""); curr = ScriptableObject.CreateInstance<RPGLoot>(); curr.id = UniRPG.GUID.Create(); ed.db.loot.Add(curr); UniRPGEdUtil.AddObjectToAssetFile(curr, UniRPGEditorGlobal.DB_DEF_LOOT_FILE); curr.screenName = "Loot"; EditorUtility.SetDirty(curr); EditorUtility.SetDirty(ed.db); } if (curr == null) GUI.enabled = false; if (GUILayout.Button(new GUIContent(UniRPGEdGui.Icon_Copy, "Copy"), EditorStyles.miniButtonMid)) { GUI.FocusControl(""); RPGLoot loot = ScriptableObject.CreateInstance<RPGLoot>(); curr.CopyTo(loot); loot.id = UniRPG.GUID.Create(); // needs to be diffent from copied loot's curr = loot; ed.db.loot.Add(curr); UniRPGEdUtil.AddObjectToAssetFile(curr, UniRPGEditorGlobal.DB_DEF_LOOT_FILE); EditorUtility.SetDirty(curr); EditorUtility.SetDirty(ed.db); } GUI.enabled = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); scroll[0] = UniRPGEdGui.BeginScrollView(scroll[0], GUILayout.Width(DatabaseEditor.LeftPanelWidth)); { if (ed.db.loot.Count > 0) { foreach (RPGLoot loot in ed.db.loot) { EditorGUILayout.BeginHorizontal(GUILayout.Width(DatabaseEditor.LeftPanelWidth - 20), GUILayout.ExpandWidth(false)); { if (UniRPGEdGui.ToggleButton(curr == loot, loot.screenName, UniRPGEdGui.ButtonLeftStyle, GUILayout.Width(160), GUILayout.ExpandWidth(false))) { curr = loot; GUI.FocusControl(""); } if (GUILayout.Button("X", UniRPGEdGui.ButtonRightStyle, GUILayout.Width(20))) { del = loot; } } EditorGUILayout.EndHorizontal(); } } else { GUILayout.Label("No Loot defined", UniRPGEdGui.WarningLabelStyle); } } UniRPGEdGui.EndScrollView(); // 0 // ------------------------------------------------------------- GUILayout.Space(3); EditorGUILayout.EndVertical(); if (del != null) { if (curr == del) curr = null; ed.db.loot.Remove(del); Object.DestroyImmediate(del, true); del = null; EditorUtility.SetDirty(ed.db); AssetDatabase.SaveAssets(); } }
public static void CreateLootDrops(RPGLoot loot, Vector3 fromPoint, int npcLevel) { // ** Figure out which rewards will be selected from groups Dictionary<int, RewardGroup> groups = new Dictionary<int, RewardGroup>(); for (int i = 0; i < loot.rewards.Count; i++) { if (loot.rewards[i].group != 0) { if (!groups.ContainsKey(loot.rewards[i].group)) { RewardGroup rg = new RewardGroup(); rg.rewards.Add(loot.rewards[i]); groups.Add(loot.rewards[i].group, rg); } else { groups[loot.rewards[i].group].rewards.Add(loot.rewards[i]); } } } if (groups.Count > 0) { foreach (RewardGroup rg in groups.Values) { int r = rg.rewards.Count > 1 ? Random.Range(0, rg.rewards.Count) : 0; rg.selected = rg.rewards[r]; } } // ** Drop a Loot Bag if (loot.dropAsBag) { if (loot.bagPrefab == null) { Debug.LogError("Create Loot Action Error: The Bag Prefab must be set."); return; } GameObject obj = (GameObject)GameObject.Instantiate(loot.bagPrefab); obj.transform.position = fromPoint; // make sure the prefab has a LootDrop component on it RPGLootDrop drop = obj.GetComponent<RPGLootDrop>(); if (drop == null) drop = obj.AddComponent<RPGLootDrop>(); // also make sure it has a trigger so that the player can actually interact with it Collider coll = obj.GetComponent<Collider>(); if (coll == null) coll = obj.AddComponent<BoxCollider>(); coll.isTrigger = true; RPGLoot lt = ScriptableObject.CreateInstance<RPGLoot>(); // create new loot bag which holds the rewards for (int i = 0; i < loot.rewards.Count; i++) { // first check if reward in a group and if it is the one chosen to be used from the group if (loot.rewards[i].group != 0) { if (groups[loot.rewards[i].group].selected != loot.rewards[i]) continue; } // check if the reward is allowed for specified NPC level if (loot.rewards[i].lookatNpcLevel && npcLevel < loot.rewards[i].npcMaxLevel) continue; // check the chance of spawning this loot if (!CanSpawnOnChance(loot.rewards[i].chance)) continue; // add the reward lt.rewards.Add(loot.rewards[i]); } drop.DoDrop(lt); } // ** Drop individual Loot Items else { for (int i = 0; i < loot.rewards.Count; i++) { // first check if reward in a group and if it is the one chosen to be used from the group if (loot.rewards[i].group != 0) { if (groups[loot.rewards[i].group].selected != loot.rewards[i]) continue; } // check if the reward is allowed for specified NPC level if (loot.rewards[i].lookatNpcLevel && npcLevel < loot.rewards[i].npcMaxLevel) continue; // check the chance of spawning this loot if (!CanSpawnOnChance(loot.rewards[i].chance)) continue; // CURRENCY if (loot.rewards[i].type == Reward.RewardType.Currency) { if (UniRPGGlobal.DB.currencyDropPrefab == null) { Debug.LogError("Could not create Currency Loot. The Currency LootDrop Prefab is not set."); continue; } GameObject obj = (GameObject)GameObject.Instantiate(UniRPGGlobal.DB.currencyDropPrefab); obj.transform.position = fromPoint; // make sure the prefab has a LootDrop component on it RPGLootDrop drop = obj.GetComponent<RPGLootDrop>(); if (drop == null) drop = obj.AddComponent<RPGLootDrop>(); // also make sure it has a trigger so that the player can actually interact with it Collider coll = drop.gameObject.GetComponent<Collider>(); if (coll == null) coll = drop.gameObject.AddComponent<BoxCollider>(); coll.isTrigger = true; RPGLoot lt = ScriptableObject.CreateInstance<RPGLoot>(); // create new loot lt.rewards.Add(loot.rewards[i]); // which holds the one reward drop.DoDrop(lt); } // ATTRIBUTE else if (loot.rewards[i].type == Reward.RewardType.Attribute) { RPGAttribute att = UniRPGGlobal.DB.GetAttribute(loot.rewards[i].guid); if (att == null) { Debug.LogError("Could not create Attribute Loot. The specified Attribute could not be found."); continue; } if (att.lootDropPrefab == null) { Debug.LogError("Could not create Attribute Loot. The Attribute's LootDrop Prefab is not set."); continue; } GameObject obj = (GameObject)GameObject.Instantiate(att.lootDropPrefab); obj.transform.position = fromPoint; // make sure the prefab has a LootDrop component on it RPGLootDrop drop = obj.GetComponent<RPGLootDrop>(); if (drop == null) drop = obj.AddComponent<RPGLootDrop>(); // also make sure it has a trigger so that the player can actually interact with it Collider coll = drop.gameObject.GetComponent<Collider>(); if (coll == null) coll = drop.gameObject.AddComponent<BoxCollider>(); coll.isTrigger = true; RPGLoot lt = ScriptableObject.CreateInstance<RPGLoot>(); // create new loot lt.rewards.Add(loot.rewards[i]); // which holds the one reward drop.DoDrop(lt); } // ITEM else if (loot.rewards[i].type == Reward.RewardType.Item) { RPGItem item = UniRPGGlobal.DB.GetItem(loot.rewards[i].guid); if (item == null) { Debug.LogError("Could not create Item Loot. The specified Item could not be found."); continue; } if (item.lootDropPrefab == null) { // no loot prefab, spawn item normal GameObject obj = (GameObject)GameObject.Instantiate(item.gameObject); obj.transform.position = fromPoint; RPGItem it = obj.GetComponent<RPGItem>(); it.IsPersistent = false; RPGLootDrop drop = obj.AddComponent<RPGLootDrop>(); drop.DoDrop(null); // no reward link. just play drop animation on the item } else { // in the case of item one of each copy must be created and dropped for (int j = 0; j < (int)loot.rewards[i].count; j++) { GameObject obj = (GameObject)GameObject.Instantiate(item.lootDropPrefab); obj.transform.position = fromPoint; // make sure the prefab has a LootDrop component on it RPGLootDrop drop = obj.GetComponent<RPGLootDrop>(); if (drop == null) drop = obj.AddComponent<RPGLootDrop>(); // also make sure it has a trigger so that the player can actually interact with it Collider coll = drop.gameObject.GetComponent<Collider>(); if (coll == null) coll = drop.gameObject.AddComponent<BoxCollider>(); coll.isTrigger = true; RPGLoot lt = ScriptableObject.CreateInstance<RPGLoot>(); // create new loot lt.rewards.Add(loot.rewards[i]); // which holds the one reward drop.DoDrop(lt); } } } } } }