//********************************************************//
// //
// Copyright © All rights reserved. MyNameIsVoo. 2020. //
// //
// COPYING FORBIDEN //
// //
//********************************************************//
using System.Collections;
using UnityEngine;
using ItemsClassList;
using ICWM.HelperSystem;
using ICWM.ItemSystem;
using ICWM.SaveSystem;
///
/// The class generates things in a box with a loot
///
namespace ICWM
{
namespace Loot
{
public class LootBox : MonoBehaviour
{
#region Attributes
[Header("ATTRIBUTES")]
public bool IsOpenSeach = true;
public Vector2 MinMaxSecondsForSeach = new Vector2(2f, 8f);
[Header("SIZE of LOOT")]
public StashSystem.LootSize SizeLoot = StashSystem.LootSize.Size4x2;
[Header("PARAMETERS")]
public GameObject Root; // The place where prefab models will drag & drop
public string LootBoxName = "LOOT BOX"; //
[Header("DEBUG")]
public int lootID = 0;
public bool itWasOpened;
public bool isSeach;
public GameObject[] lootItems;
private bool isCreatedItems;
#endregion
#region PUBLIC
#region Create
public void CreateLootBox()
{
if (isCreatedItems)
return;
int maxAmount = LootBase.instance.GetLootItemsLenght(); // The total amount of items that we have
int sizeLootBox = GetSizeIndexLoot();
// maybe more (but I've done it yet)
if (sizeLootBox > maxAmount)
sizeLootBox = maxAmount;
int AmountLoot = (int)(Random.Range(0, sizeLootBox)); // Randomly choose the amount of loot in the box
lootItems = new GameObject[AmountLoot];
ItemList[] CL = new ItemList[AmountLoot];
//
// Randomly we will choose items
//
lootID = 0;
for (int i = 0; i < AmountLoot; i++)
{
int rand = Helper.GetRandomIndex(maxAmount); // Of all we choose randomly one
lootItems[i] = LootBase.instance.GetNewItem(Root.transform, rand);
if (lootItems[i] == null)
continue;
lootItems[i].name = "Loot_" + i.ToString();
CL[i] = lootItems[i].GetComponent().CL;
CL[i] = Helper.SetParameters(CL[i], lootItems[i]);
CL[i].lootID = lootID;
lootID++;
lootItems[i].GetComponent().CL = CL[i];
lootItems[i].transform.localScale = Vector3.one;
lootItems[i].transform.localPosition = Vector3.zero;
lootItems[i].GetComponent().CL.itemModding.transform.SetParent(lootItems[i].transform);
//
// Чтобы работыли аттачи необходимо для каждого определить класс лист и только после этого
// назначать параметры скрипты хелпер
//
if (lootItems[i].GetComponent().CL.IsRenderItem() && lootItems[i].GetComponent().WithAttachments)
{
Helper.AddAttachmentsToItem(lootItems[i].GetComponent().WeaponAttachments, lootItems[i].GetComponent().CL, transform);
string filename = lootItems[i].GetComponent().CL.id.ToString();
InventoryBase.instance.RenderRoot.GetComponentInChildren().SaveButtonParameters(filename);
lootItems[i].GetComponent().CL.itemModding.GetComponentInChildren().SaveParameters(filename, true);
}
}
isCreatedItems = true;
}
#endregion
#region Seach
// After re-opening the lootbox we need to find all the remaining items and update the array
public void SeachItems()
{
Component[] p = Root.GetComponentsInChildren(typeof(ItemsDrop), true);
if (p != null)
{
lootItems = new GameObject[p.Length];
ItemList[] CL = new ItemList[p.Length];
lootID = 0;
foreach (ItemsDrop n in p)
{
lootItems[lootID] = n.gameObject;
CL[lootID] = lootItems[lootID].GetComponent().CL;
CL[lootID].lootID = lootID;
lootItems[lootID].GetComponent().CL = CL[lootID];
lootID++;
}
}
}
public IEnumerator SeachItemsForSeconds(float seconds)
{
yield return new WaitForSeconds(seconds);
Component[] p = Root.GetComponentsInChildren(typeof(ItemsDrop), true);
if (p != null)
{
lootItems = new GameObject[p.Length];
ItemList[] CL = new ItemList[p.Length];
int i = 0;
foreach (ItemsDrop n in p)
{
lootItems[i] = n.gameObject;
CL[i] = lootItems[i].GetComponent().CL;
CL[i].lootID = i;
lootItems[i].GetComponent().CL = CL[i];
i++;
}
// Update array StashSystem
StashSystem.instance.referenceToOpenedLootBox = this;
}
}
#endregion
#region Set, Get, ...
public Vector2 GetSizeVectorLoot()
{
Vector2 sizeLoot = Vector2.zero;
if (SizeLoot == StashSystem.LootSize.Size4x2)
{
sizeLoot.x = 4;
sizeLoot.y = 2;
}
else if (SizeLoot == StashSystem.LootSize.Size6x2)
{
sizeLoot.x = 6;
sizeLoot.y = 2;
}
else if (SizeLoot == StashSystem.LootSize.Size4x4)
{
sizeLoot.x = 4;
sizeLoot.y = 4;
}
else if (SizeLoot == StashSystem.LootSize.Size6x4)
{
sizeLoot.x = 6;
sizeLoot.y = 4;
}
else if (SizeLoot == StashSystem.LootSize.Size4x5)
{
sizeLoot.x = 4;
sizeLoot.y = 5;
}
else if (SizeLoot == StashSystem.LootSize.Size4x6)
{
sizeLoot.x = 4;
sizeLoot.y = 6;
}
else if (SizeLoot == StashSystem.LootSize.Size5x6)
{
sizeLoot.x = 5;
sizeLoot.y = 6;
}
return sizeLoot;
}
#endregion
#region Close, Reset, Destroy, ...
public void CloseLootBox()
{
SaveItems();
}
public void DestroyItemFromLootBox(int lootID)
{
if (lootID >= 0 && lootItems[lootID] && lootID < lootItems.Length)
Destroy(lootItems[lootID]);
lootItems[lootID] = null;
}
#endregion
#endregion
#region PRIVATE
#region Save
private void SaveItems()
{
if (lootItems != null && lootItems.Length != 0)
{
foreach (GameObject item in lootItems)
{
if (item == null)
continue;
else if (item.GetComponent().CL.itemIcon == null) // If we transfered the item into backpack or attached to weapon
{
DestroyItemFromLootBox(item.GetComponent().CL.lootID);
continue;
}
item.GetComponent().CL.startPositionInInventory = new Vector2(item.GetComponent().CL.itemIcon.transform.localPosition.x, item.GetComponent().CL.itemIcon.transform.localPosition.y);
}
}
}
#endregion
#region Get
private int GetSizeIndexLoot()
{
if (SizeLoot == StashSystem.LootSize.Size4x2)
return 8;
else if (SizeLoot == StashSystem.LootSize.Size6x2 || SizeLoot == StashSystem.LootSize.Size4x4)
return 12;
else if (SizeLoot == StashSystem.LootSize.Size6x4)
return 24;
else if (SizeLoot == StashSystem.LootSize.Size4x5)
return 20;
else if (SizeLoot == StashSystem.LootSize.Size4x6)
return 24;
else if (SizeLoot == StashSystem.LootSize.Size5x6)
return 30;
else
return 0;
}
#endregion
#endregion
}
}
}