public void CreateSlotItemAsChildOfExistingSlot(GameObject item) { GameObject newSlotItem = Instantiate(slotItem, transform, false); Component comp = item.GetComponent <SlotObjectContainer>().obj.GetComponent(typeof(IObjectData)); IObjectData objectData = comp as IObjectData; string uiText; if (objectData.count() == 1) { uiText = objectData.objectName(); } else { uiText = objectData.objectName() + " x" + objectData.count(); } newSlotItem.GetComponent <SlotObjectContainer> ().obj = item.GetComponent <SlotObjectContainer>().obj; newSlotItem.GetComponent <Text> ().text = uiText; QuestTriggerWrapper questTriggerWrapper = newSlotItem.GetComponent <SlotObjectContainer> ().obj.GetComponent <QuestTriggerWrapper>(); if (questTriggerWrapper != null) { QuestTrigger trigger = questTriggerWrapper.questTrigger; if (trigger != null) { Debug.Log("Process Quest Trigger."); GameObject.Find("QuestManager").GetComponent <QuestManager> ().ProcessQuestTrigger(trigger); } } Destroy(item); }
// I don't like this much because i am copying and pasting code... // TODO Process Quest Untriggers. public void OnPointerClick(PointerEventData eventData) { int value = Convert.ToInt32(transform.parent.GetComponentInChildren <Text> ().text); GameObject player = transform.parent.GetComponent <PlayerReferenceAndItemContainer> ().Player; GameObject item = transform.parent.GetComponent <PlayerReferenceAndItemContainer> ().Item; SlotObjectContainer slotObjectContainer = transform.parent.GetComponent <PlayerReferenceAndItemContainer> ().SlotObjectContainer; Component comp = item.GetComponent(typeof(IObjectData)); IObjectData objData = comp as IObjectData; objData.decreaseCount(value); if (objData.count() == 0) { GameObject dropItem = Instantiate(item); dropItem.transform.position = player.transform.position; dropItem.transform.SetParent(null); dropItem.SetActive(true); Component component = dropItem.GetComponent(typeof(IObjectData)); IObjectData objectData = component as IObjectData; objectData.setCount(value); Destroy(slotObjectContainer.gameObject); Destroy(item); } else { GameObject dropItem = Instantiate(item); Component dropComp = dropItem.GetComponent(typeof(IObjectData)); IObjectData dropObjData = dropComp as IObjectData; if (dropObjData.count() == 1) { slotObjectContainer.gameObject.GetComponent <Text> ().text = dropObjData.objectName(); } else { slotObjectContainer.gameObject.GetComponent <Text> ().text = dropObjData.objectName() + " x" + dropObjData.count(); } dropObjData.setCount(value); dropItem.transform.position = player.transform.position; dropItem.transform.SetParent(null); dropItem.SetActive(true); } UIPlayerInventoryClickHandler.selectCountButtonCreated = false; Destroy(transform.parent.gameObject); }
// I decided that i didn't want to support the drag and drop on chests to create new items. /* * public void OnDrop (PointerEventData eventData) { * if (DragHandler.itemBeingDragged == null) { * return; * } * * GameObject containerSlot = null; * if (DragHandler.itemBeingDragged.transform.parent.GetComponent<ContainerSlot>()) { * containerSlot = DragHandler.startParent.gameObject; * } * * CreateSlotWithItem (DragHandler.itemBeingDragged.GetComponent<SlotObjectContainer>().obj); * * if (containerSlot) { * Destroy (containerSlot); * } * DestroyImmediate (DragHandler.itemBeingDragged.transform.gameObject); * * DragHandler.itemBeingDragged = null; * } */ public void CreateSlotWithItem(GameObject Item) { GameObject slot = Instantiate(Slot, transform.GetChild(0).transform.GetChild(0), false); GameObject slotItem = Instantiate(ContainerSlotItem, slot.transform, false); slotItem.GetComponent <SlotObjectContainer> ().obj = Item; GameObject item = slotItem.GetComponent <SlotObjectContainer> ().obj; Component objectData = item.GetComponent(typeof(IObjectData)); IObjectData data = objectData as IObjectData; slotItem.GetComponent <Text> ().text = data.objectName(); if (data.count() != 1) { slotItem.GetComponent <Text> ().text += " x" + data.count(); } // Process Quest UnTrigger. QuestTriggerWrapper questTriggerWrapper = item.GetComponent <QuestTriggerWrapper> (); if (questTriggerWrapper != null) { QuestTrigger trigger = questTriggerWrapper.questTrigger; if (trigger != null) { GameObject.Find("QuestManager").GetComponent <QuestManager> ().ProcessQuestUnTrigger(trigger); } } }
public void CreateFactoryItem(List <GameObject> inventory, GameObject playerReference) { PlayerReference = playerReference; if (references == null) { references = new List <GameObject>(); } if (Canvas == null) { Canvas = GameObject.Find("Canvas"); } references.Add(Instantiate(Container, Canvas.transform, false)); for (int i = 0; i < inventory.Count; ++i) { // Make a slot a child of the "Content". GameObject newSlot = Instantiate(ContainerSlot, references[0].transform.GetChild(0).transform.GetChild(0), false); // Make a slot item prefab a child of the slot that was just created. GameObject slotItem = Instantiate(SlotItemPrefab, newSlot.transform, false); Component component = inventory[i].GetComponent(typeof(IObjectData)); IObjectData objectData = component as IObjectData; if (objectData.count() == 1) { slotItem.GetComponent <Text> ().text = objectData.objectName(); } else { slotItem.GetComponent <Text> ().text = objectData.objectName() + " x" + objectData.count(); } slotItem.GetComponent <SlotObjectContainer> ().obj = inventory[i]; } references[0].transform.root.GetComponent <ContainerInventoryReferenceContainer> ().ContainerInventory = GetComponent <Container> (); }
public bool Interact(GameObject gameobject) { CharacterInventory characterInventory = gameobject.GetComponent <CharacterInventory> (); Debug.Assert(characterInventory != null); bool foundSlotForItem = false; Component component = GetComponent(typeof(IObjectData)); IObjectData objData = component as IObjectData; if (objData.stackable()) { for (int i = 0; i < characterInventory.INVENTORY_SIZE; ++i) { if (characterInventory.inventory[i] != null) { Component comp = characterInventory.inventory[i].GetComponent(typeof(IObjectData)); IObjectData objectData = comp as IObjectData; // This equality check is hacky. It incorrectly assumes every // object in the game will have a unique name. if (objectData.objectName() == objData.objectName()) { objectData.increaseCount(objData.count()); foundSlotForItem = true; // We added this item to a stack. Destroy(gameObject); break; } } } } if (!foundSlotForItem) { for (int i = 0; i < characterInventory.INVENTORY_SIZE; ++i) { if (characterInventory.inventory[i] == null) { // Assign the inventory slot to this gameObject (not the one that was passed in). characterInventory.inventory[i] = gameObject; foundSlotForItem = true; break; } } } if (foundSlotForItem) { gameObject.transform.position = gameobject.transform.position; QuestTriggerWrapper qt = GetComponent <QuestTriggerWrapper> (); if (qt != null) { GameObject.Find("QuestManager").GetComponent <QuestManager>().ProcessQuestTrigger(qt.questTrigger); } if (gameObject.activeInHierarchy) { gameObject.SetActive(false); } } return(foundSlotForItem); }
public void OnDrop(PointerEventData eventData) { if (DragHandler.itemBeingDragged == null) { return; } if (DragHandler.startParent.GetComponent <PlayerInventorySlot> ()) { // This is the easiest case. The Player is swapping items around in the inventory. // No need to process a quest trigger or anything like that. if (item) { item.transform.SetParent(DragHandler.startParent); } DragHandler.itemBeingDragged.transform.SetParent(transform); DragHandler.itemBeingDragged = null; transform.root.GetComponent <PlayerReferenceContainer> ().Player.GetComponent <UICharacterInventoryFactory> ().RefreshCharacterInventory(); } else if (DragHandler.startParent.GetComponent <EquipmentSlot> ()) { // The Player is trying to drag an item from an equipment slot to an inventory slot. // If there is no item in this slot, no big deal. Else they are trying to swap and we // need to be a little careful. if (item) { GameObject uiObject = DragHandler.itemBeingDragged.GetComponent <SlotObjectContainer> ().obj; Equipment equipmentComponent = uiObject.GetComponent <Equipment> (); if (equipmentComponent == null || item.GetComponent <SlotObjectContainer> ().obj.GetComponent <Equipment> () == null || !equipmentComponent.equipmentType.Equals(item.GetComponent <SlotObjectContainer> ().obj.GetComponent <Equipment> ().equipmentType)) { DragHandler.itemBeingDragged.transform.SetParent(DragHandler.startParent); return; } item.transform.SetParent(DragHandler.startParent); } DragHandler.itemBeingDragged.transform.SetParent(transform); DragHandler.itemBeingDragged = null; transform.root.GetComponent <PlayerReferenceContainer> ().Player.GetComponent <UICharacterInventoryFactory> ().RefreshCharacterInventory(); } else if (DragHandler.startParent.GetComponent <ContainerSlot>()) { // The Player is trying to move an item from a container to an inventory slot. // It may be a swap or it may not be. if (item) { GameObject itemCopy = item; itemCopy.transform.SetParent(null); DragHandler.itemBeingDragged.transform.SetParent(transform); transform.root.GetComponent <PlayerReferenceContainer> ().Player.GetComponent <UICharacterInventoryFactory> ().RefreshCharacterInventory(); DragHandler.itemBeingDragged.transform.SetParent(null); // Create a slot item. GameObject newSlotItem = Instantiate(slotItemInteractingWithContainer, transform, false); Component comp = DragHandler.itemBeingDragged.GetComponent <SlotObjectContainer>().obj.GetComponent(typeof(IObjectData)); IObjectData objectData = comp as IObjectData; string uiText; if (objectData.count() == 1) { uiText = objectData.objectName(); } else { uiText = objectData.objectName() + " x" + objectData.count(); } newSlotItem.GetComponent <Text> ().text = uiText; newSlotItem.GetComponent <SlotObjectContainer> ().obj = DragHandler.itemBeingDragged.GetComponent <SlotObjectContainer>().obj; // Tell the container to create its slot item. Destroy(DragHandler.itemBeingDragged); DragHandler.startParent.GetComponent <ContainerSlot> ().CreateSlotItemAsChildOfExistingSlot(itemCopy); QuestTriggerWrapper questTriggerWrapper = newSlotItem.GetComponent <SlotObjectContainer> ().obj.GetComponent <QuestTriggerWrapper>(); if (questTriggerWrapper != null) { QuestTrigger trigger = questTriggerWrapper.questTrigger; if (trigger != null) { Debug.Log("Process Quest Trigger."); GameObject.Find("QuestManager").GetComponent <QuestManager> ().ProcessQuestTrigger(trigger); } } } else { // Not a swap. GameObject itemCopy = item; Destroy(item); DragHandler.itemBeingDragged.transform.SetParent(transform); transform.root.GetComponent <PlayerReferenceContainer> ().Player.GetComponent <UICharacterInventoryFactory> ().RefreshCharacterInventory(); // Create a slot item. itemCopy = item; Destroy(item); GameObject newSlotItem = Instantiate(slotItemInteractingWithContainer, transform, false); Component comp = itemCopy.GetComponent <SlotObjectContainer>().obj.GetComponent(typeof(IObjectData)); IObjectData objectData = comp as IObjectData; string uiText; if (objectData.count() == 1) { uiText = objectData.objectName(); } else { uiText = objectData.objectName() + " x" + objectData.count(); } newSlotItem.GetComponent <Text> ().text = uiText; newSlotItem.GetComponent <SlotObjectContainer> ().obj = DragHandler.itemBeingDragged.GetComponent <SlotObjectContainer>().obj; QuestTriggerWrapper questTriggerWrapper = newSlotItem.GetComponent <SlotObjectContainer> ().obj.GetComponent <QuestTriggerWrapper>(); if (questTriggerWrapper != null) { QuestTrigger trigger = questTriggerWrapper.questTrigger; if (trigger != null) { Debug.Log("Process Quest Trigger."); GameObject.Find("QuestManager").GetComponent <QuestManager> ().ProcessQuestTrigger(trigger); } } Destroy(DragHandler.startParent.gameObject); } } }
public void DisplayCharacterInventory(CharacterInventory characterInventory, GameObject slotItemPrefab) { _slotItemPrefab = slotItemPrefab; GameObject RightHand = transform.GetChild(0).gameObject; GameObject LeftHand = transform.GetChild(1).gameObject; GameObject Equipment = transform.GetChild(2).gameObject; GameObject Inventory = transform.GetChild(3).gameObject; Debug.Assert(RightHand != null); Debug.Assert(LeftHand != null); Debug.Assert(Equipment != null); Debug.Assert(Inventory != null); GameObject Head = Equipment.transform.GetChild(0).gameObject; GameObject Chest = Equipment.transform.GetChild(1).gameObject; GameObject Hands = Equipment.transform.GetChild(2).gameObject; GameObject Feet = Equipment.transform.GetChild(3).gameObject; Debug.Assert(Head != null); Debug.Assert(Chest != null); Debug.Assert(Hands != null); Debug.Assert(Feet != null); // Destroy all the children so this function can be used to refresh the inventory. // Left hand right hand. for (int i = 0; i < RightHand.transform.childCount; ++i) { if (RightHand.transform.GetChild(i).childCount != 0) { Destroy(RightHand.transform.GetChild(i).GetChild(0).gameObject); } if (LeftHand.transform.GetChild(i).childCount != 0) { Destroy(LeftHand.transform.GetChild(i).GetChild(0).gameObject); } } // Head Chest Hands Feet if (Head.transform.GetChild(0).transform.childCount != 0) { Destroy(Head.transform.GetChild(0).transform.GetChild(0).gameObject); } if (Chest.transform.GetChild(0).transform.childCount != 0) { Destroy(Chest.transform.GetChild(0).transform.GetChild(0).gameObject); } if (Hands.transform.GetChild(0).transform.childCount != 0) { Destroy(Hands.transform.GetChild(0).transform.GetChild(0).gameObject); } if (Feet.transform.GetChild(0).transform.childCount != 0) { Destroy(Feet.transform.GetChild(0).transform.GetChild(0).gameObject); } // Inventory for (int i = 0; i < Inventory.transform.childCount; ++i) { if (Inventory.transform.GetChild(i).childCount != 0) { Destroy(Inventory.transform.GetChild(i).GetChild(0).gameObject); } } // Populate the UI. // Left and Right Hands. for (int i = 0; i < characterInventory.leftHand.Length - 1; ++i) { if (characterInventory.rightHand[i] != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, RightHand.transform.GetChild(i), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.rightHand[i]; Component objectDataComponent = characterInventory.rightHand[i].GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } if (characterInventory.leftHand[i] != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, LeftHand.transform.GetChild(i), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.leftHand[i]; Component objectDataComponent = characterInventory.leftHand[i].GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } } // Head if (characterInventory.head != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, Head.transform.GetChild(0), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.head; Component objectDataComponent = characterInventory.head.GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } // Chest if (characterInventory.chest != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, Chest.transform.GetChild(0), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.chest; Component objectDataComponent = characterInventory.chest.GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } // Hands if (characterInventory.hands != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, Hands.transform.GetChild(0), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.hands; Component objectDataComponent = characterInventory.hands.GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } // Feet if (characterInventory.feet != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, Feet.transform.GetChild(0), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.feet; Component objectDataComponent = characterInventory.feet.GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } // Inventory Debug.Assert(characterInventory.inventory.Count == 5); for (int i = 0; i < characterInventory.inventory.Count; ++i) { if (characterInventory.inventory[i] != null) { GameObject newSlotItem = Instantiate(_slotItemPrefab, Inventory.transform.GetChild(i), false); newSlotItem.GetComponent <SlotObjectContainer> ().obj = characterInventory.inventory[i]; Component objectDataComponent = characterInventory.inventory[i].GetComponent(typeof(IObjectData)); IObjectData objectData = objectDataComponent as IObjectData; newSlotItem.GetComponent <Text> ().text = objectData.objectName(); if (objectData.count() != 1) { newSlotItem.GetComponent <Text> ().text += " x" + objectData.count(); } } } }