// React to taking damage public virtual void TakeDamage() { // Take a hit, return if still alive if (--m_nHitPoints > 0) { return; } // Otherwise this enemy is dead DestroyObject(gameObject); if (lootTable != null) { if (lootTable.data != null) { if (lootTable.data.Count > 0) { LootTableEntry entry = lootTable.data[Random.Range(0, lootTable.data.Count)]; float random = Random.Range(0.0f, 1.0f); if (random < entry.chance) { // Drop loot print(this.gameObject.name + " dropped a " + entry.item.name + "!"); // instantiate an object using the prefab GameObject droppedItem = Instantiate((GameObject)entry.item, new Vector3(Random.Range(-14f, 14), Random.Range(2, 18), 0), Quaternion.identity) as GameObject; } } else { print("loot table data is not null but is empty"); } } else { print("loot table data is null"); } } else { print("loot table is null"); } }
public static LootTable CreateLootTable() { string assetPath = "Assets/LootTable.asset"; LootTable loadedAsset = AssetDatabase.LoadAssetAtPath <LootTable>(assetPath); if (loadedAsset == null) { LootTable lt = ScriptableObject.CreateInstance <LootTable>(); AssetDatabase.CreateAsset(lt, "Assets/LootTable.asset"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); loadedAsset = AssetDatabase.LoadAssetAtPath <LootTable>(assetPath); } if (loadedAsset.data.Count == 0) { // add few loot table entries there LootTableEntry Coins = new LootTableEntry(); Coins.chance = 0.85f; Coins.itemLevel = 0; Coins.item = (GameObject)Resources.Load("Treasures/Coins"); LootTableEntry goldFruit = new LootTableEntry(); goldFruit.chance = 0.25f; goldFruit.itemLevel = 0; goldFruit.item = (GameObject)Resources.Load("Treasures/Gold_Fruit"); LootTableEntry specialBullet = new LootTableEntry(); specialBullet.chance = 0.65f; specialBullet.itemLevel = 0; specialBullet.item = (GameObject)Resources.Load("Treasures/Special_Bullet"); LootTableEntry specialTurret = new LootTableEntry(); specialTurret.chance = 0.90f; specialTurret.itemLevel = 0; specialTurret.item = (GameObject)Resources.Load("Treasures/Special_Turret"); loadedAsset.data.Add(Coins); loadedAsset.data.Add(specialTurret); loadedAsset.data.Add(specialBullet); loadedAsset.data.Add(goldFruit); } return(loadedAsset); }
// Edit or Create public override void DrawEditor(Rect box, bool newItem) { if (!linkedTablesLoaded) { // Load items LoadItemList(); linkedTablesLoaded = true; } // Setup the layout Rect pos = box; pos.x += ImagePack.innerMargin; pos.y += ImagePack.innerMargin; pos.width -= ImagePack.innerMargin; pos.height = ImagePack.fieldHeight; // Draw the content database info if (newItem) { ImagePack.DrawLabel (pos.x, pos.y, "Create a new loot table"); pos.y += ImagePack.fieldHeight; } editingDisplay.name = ImagePack.DrawField (pos, "Name:", editingDisplay.name, 0.5f); pos.y += ImagePack.fieldHeight; if (editingDisplay.entries.Count == 0) { editingDisplay.entries.Add(new LootTableEntry(-1, 100, 1)); } for (int i = 0; i < editingDisplay.maxEntries; i++) { if (editingDisplay.entries.Count > i) { pos.width = pos.width / 2; int selectedItem = GetPositionOfItem(editingDisplay.entries[i].itemId); selectedItem = ImagePack.DrawSelector (pos, "Item " + (i+1) + ":", selectedItem, itemsList); editingDisplay.entries[i].itemId = itemIds[selectedItem]; pos.x += pos.width; editingDisplay.entries[i].count = ImagePack.DrawField (pos, "Count:", editingDisplay.entries[i].count); pos.x -= pos.width; pos.y += ImagePack.fieldHeight; editingDisplay.entries[i].chance = ImagePack.DrawField (pos, "Chance:", editingDisplay.entries[i].chance); pos.x += pos.width; if (ImagePack.DrawButton (pos.x, pos.y, "Remove Item")) { editingDisplay.entries.RemoveAt(i); } pos.x -= pos.width; pos.y += ImagePack.fieldHeight; pos.width = pos.width * 2; } } if (editingDisplay.entries.Count < editingDisplay.maxEntries) { if (ImagePack.DrawButton (pos.x, pos.y, "Add Item")) { LootTableEntry lootTableEntry = new LootTableEntry(-1, 0, 1); editingDisplay.entries.Add(lootTableEntry); } } //pos.width = pos.width * 2; // Save data pos.x -= ImagePack.innerMargin; pos.y += 1.4f * ImagePack.fieldHeight; pos.width /=3; if (ImagePack.DrawButton (pos.x, pos.y, "Save Data")) { if (newItem) InsertEntry (); else UpdateEntry (); state = State.Loaded; } // Delete data if (!newItem) { pos.x += pos.width; if (ImagePack.DrawButton (pos.x, pos.y, "Delete Data")) { DeleteEntry (); newSelectedDisplay = 0; state = State.Loaded; } } // Cancel editing pos.x += pos.width; if (ImagePack.DrawButton (pos.x, pos.y, "Cancel")) { editingDisplay = originalDisplay.Clone(); if (newItem) state = State.New; else state = State.Loaded; } if (resultTimeout != -1 && resultTimeout > Time.realtimeSinceStartup) { pos.y += ImagePack.fieldHeight; ImagePack.DrawText(pos, result); } }
// Load Database Data public override void Load() { if (!dataLoaded) { // Clean old data dataRegister.Clear (); displayKeys.Clear (); // Read all entries from the table string query = "SELECT * FROM " + tableName; // If there is a row, clear it. if (rows != null) rows.Clear (); // Load data rows = DatabasePack.LoadData (DatabasePack.contentDatabasePrefix, query); //Debug.Log("#Rows:"+rows.Count); // Read all the data if ((rows!=null) && (rows.Count > 0)) { foreach (Dictionary<string,string> data in rows) { //foreach(string key in data.Keys) // Debug.Log("Name[" + key + "]:" + data[key]); //return; LootTable display = new LootTable (); display.id = int.Parse (data ["id"]); display.name = data ["name"]; for (int i = 1; i <= display.maxEntries; i++) { int itemId = int.Parse (data ["item" + i]); if (itemId != -1) { int chance = int.Parse (data ["item" + i + "chance"]); int count = int.Parse (data ["item" + i + "count"]); LootTableEntry entry = new LootTableEntry(itemId, chance, count); display.entries.Add(entry); } } display.isLoaded = true; //Debug.Log("Name:" + display.name + "=[" + display.id + "]"); dataRegister.Add (display.id, display); displayKeys.Add (display.id); } LoadSelectList(); } dataLoaded = true; } }