public class InventorySystem : BaseSystem
{
public Filter[] filters;
public GameObject tempUIfix;
[System.Serializable]
public struct Filter
{
public Filter(
int id,
GameObject go,
InventoryComponent ic,
InputComponent inc,
InteractorComponent iac,
CartComponent cart
)
{
this.id = id;
gameObject = go;
inventoryComponent = ic;
inputComponent = inc;
interactorComponent = iac;
cartComponent = cart;
}
public int id;
public GameObject gameObject;
public InventoryComponent inventoryComponent;
public InputComponent inputComponent;
public InteractorComponent interactorComponent;
public CartComponent cartComponent;
}
public override void Initialize(Transform[] objects)
{
List<Filter> tmpFilters = new List<Filter>();
int index = 0;
for (int i = 0; i < objects.Length; i++)
{
InventoryComponent ic = objects[i].GetComponent<InventoryComponent>();
InteractorComponent iac = objects[i].GetComponent<InteractorComponent>();
InputComponent inc = objects[i].GetComponent<InputComponent>();
CartComponent cart = objects[i].GetComponent<CartComponent>();
if (ic && iac && inc)
{
tmpFilters.Add(new Filter(index, objects[i].gameObject, ic, inc, iac, cart));
}
}
filters = tmpFilters.ToArray();
}
public override void Tick(float deltaTime)
{
for (int i = 0; i < filters.Length; i++)
{
Filter filter = filters[i];
InventoryComponent inventoryComp = filter.inventoryComponent;
InputComponent inputComp = filter.inputComponent;
InteractorComponent interactComp = filter.interactorComponent;
CartComponent cartComp = filter.cartComponent;
// ----- logic -----
if (inputComp.GetKeyDown(KeyCode.E) || inputComp.GetButtonDown("Fire1"))
{
AttemptWorldInteract(interactComp, player.inventoryComponent, inputComp.worldInteractMask);
interactComp.SetInteractMode(InteractMode.Object);
}
if (inputComp.upDPad)
{
DropItem(0);
}
else if (inputComp.rightDPad)
{
DropItem(1);
}
else if (inputComp.downDPad)
{
DropItem(2);
}
else if (inputComp.leftDPad)
{
DropItem(3);
}
}
}
public void SetNewPositionandActivateObjectatDrop(Vector3 newPosition, GameObject droppedItem)
{
droppedItem.SetActive(true);
if (droppedItem.GetComponent<CollectibleComponent>().TypeofCollectible != CollectibleComponent.CollectibleType.BATTLEPOTION)
{
droppedItem.transform.position = player.go.transform.position + player.go.transform.forward;//new Vector3(newPosition.x, newPosition.y, newPosition.z);
}
RaycastHit rayhit;
Ray ray = new Ray(player.go.transform.position + player.go.transform.forward, Vector3.down);
if (Physics.Raycast(ray, out rayhit, 1000, player.interactorComponent.groundMask))
{
if (droppedItem.GetComponent<CombatPotion>())
{
if (droppedItem.GetComponent<CombatPotion>().BattleType != CombatPotion.BattlePoition.QUEST)
{
droppedItem.transform.position = player.go.transform.position;
droppedItem.transform.position = new Vector3(newPosition.x, rayhit.point.y /*+(droppedItem.GetComponent<MeshRenderer>().bounds.center.y / 2)*/, newPosition.z);
}
}
else
{
droppedItem.transform.position = player.go.transform.position;
droppedItem.transform.position = new Vector3(newPosition.x, rayhit.point.y /*+(droppedItem.GetComponent<MeshRenderer>().bounds.center.y / 2)*/, newPosition.z);
}
}
if (droppedItem.GetComponent<CollectibleComponent>().TypeofCollectible == CollectibleComponent.CollectibleType.BATTLEPOTION)
{
droppedItem.GetComponent<CollectibleComponent>().isGatherable = false;
CombatPotion combatPotion = droppedItem.GetComponent<CombatPotion>();
HealthComponent playerHealth = player.go.GetComponent<HealthComponent>();
if (combatPotion.BattleType == CombatPotion.BattlePoition.HEALTH)
{
playerHealth.RestoreGranularDamageOverTime(100, 6f, true);
}
if (combatPotion.BattleType == CombatPotion.BattlePoition.LIGHT)
{
playerHealth.RestoreGranularDamageOverTime(10, 3f, true);
}
if (combatPotion.BattleType == CombatPotion.BattlePoition.QUEST)
{
playerHealth.RestoreGranularDamageOverTime(10, 5f, true);
if (Vector3.Distance(combatPotion.questObjective.transform.position, player.go.transform.position) < 5)
{
combatPotion.questComplete = true;
player.go.GetComponent<PlayerEventComponent>().OnSisterCleansed();
}
else
{
droppedItem.transform.position = new Vector3(newPosition.x, newPosition.y, newPosition.z);
RaycastHit rayhited;
Ray rayed = new Ray(player.go.transform.position + player.go.transform.forward, Vector3.down);
if (Physics.Raycast(rayed, out rayhited, 1000, player.interactorComponent.groundMask))
{
droppedItem.transform.position = player.go.transform.position;
droppedItem.transform.position = new Vector3(newPosition.x, rayhited.point.y, newPosition.z);
}
}
}
}
}
private void DropItem(int index)
{
if (player.inventoryComponent.InventoryBag[index].itemType.baseCollectible != player.inventoryComponent.tempColl.itemType.baseCollectible)
{
GatheringSystem gathering = GetComponent<GatheringSystem>();
for (int i = 0; i < gathering.pickedUpItems.Count; i++)
{
if (gathering.pickedUpItem[i].baseCollectible == player.inventoryComponent.InventoryBag[index].itemType.baseCollectible)
{
if (gathering.pickedUpItem[i].GetComponent<CombatPotion>())
{
if (!gathering.pickedUpItem[i].GetComponent<CombatPotion>().isCurrentlyActivated)
{
SetNewPositionandActivateObjectatDrop(player.go.transform.position + player.go.transform.forward, gathering.pickedUpItem[i].gameObject);
if (!gathering.pickedUpItem[i].GetComponent<CombatPotion>())
{
gathering.pickedUpItem.RemoveAt(i);
}
player.inventoryComponent.invUI.RemoveFromInventoryUI(player.inventoryComponent.InventoryBag[index].itemType);
player.inventoryComponent.RemoveItemFromInventory(player.inventoryComponent.InventoryBag[index].itemType, 1);
player.animationComponent.OnPlayThrowAnimation();
break;
}
}
else
{
SetNewPositionandActivateObjectatDrop(player.go.transform.position + player.go.transform.forward, gathering.pickedUpItem[i].gameObject);
if (!gathering.pickedUpItem[i].GetComponent<CombatPotion>())
{
gathering.pickedUpItem.RemoveAt(i);
}
player.inventoryComponent.invUI.RemoveFromInventoryUI(player.inventoryComponent.InventoryBag[index].itemType);
player.inventoryComponent.RemoveItemFromInventory(player.inventoryComponent.InventoryBag[index].itemType, 1);
break;
}
}
}
}
player.uiComponent.groceryList.UpdateList();
}
private void AttemptWorldInteract(InteractorComponent interactorComp, InventoryComponent inventoryComp, LayerMask mask)
{
Transform t = interactorComp.GetOwnerGO().transform;
Vector3 position = t.position + t.forward;
RaycastHit[] hits;
hits = Physics.BoxCastAll(position, Vector3.one, t.forward, Quaternion.identity, 1f, mask);
for (int i = 0; i < hits.Length; i++)
{
CartComponent tc = hits[i].transform.GetComponentInParent<CartComponent>();
if (tc)
{
interactorComp.currentInteractable = tc;
tc.isTraveling = false;
interactorComp.SetInteractMode(InteractMode.Object);
break;
}
}
}
public class UseInventorySystem : MonoBehaviour
{
private GameObject player;
public Image[] sprites;
public List<Text> amountTexts = new List<Text>();
public Sprite StandardSprite;
private InventorySlot[] slots;
private void Start()
{
List<Image> tempSprites = new List<Image>();
player = GameObject.FindObjectOfType<MovementComponent>().gameObject;
slots = GetComponentsInChildren<InventorySlot>();
for (int i = 0; i < slots.Length; i++)
{
tempSprites.Add(slots[i].GetComponent<Image>());
}
sprites = tempSprites.ToArray();
for (int i = 0; i < slots.Length; i++)
{
slots[i].image.sprite = StandardSprite;
}
}
public void UpdateInventory(CollectibleComponent br)
{
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].image.sprite == br.baseCollectible.GuiSprite)
{
slots[i].amount += br.Quantity;
slots[i].amountTXT.text = slots[i].amount.ToString();
return;
}
}
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].image.sprite == StandardSprite)
{
slots[i].image.sprite = br.baseCollectible.GuiSprite;
slots[i].amount += br.Quantity;
slots[i].amountTXT.text = slots[i].amount.ToString();
return;
}
}
}
public void RemoveFromInventoryUI(CollectibleComponent br)
{
int slotIndex = 10;
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].image.sprite == br.baseCollectible.GuiSprite)
{
slotIndex = i;
}
}
if(slotIndex < 6
{
if (slots[slotIndex].image.sprite != StandardSprite && slotIndex < slots.Length)
{
slots[slotIndex].amount -= 1;
if (slots[slotIndex].amount < 1)
{
sprites[slotIndex].sprite = StandardSprite;
}
slots[slotIndex].amountTXT.text = slots[slotIndex].amount.ToString();
if (slots[slotIndex].amount < 1)
{
slots[slotIndex].amountTXT.text = "";
}
}
}
}
}