public void swapWith(MenuSlot releaseTo) { MenuSlotItem meTemp = this.getItem(); MenuSlotItem youTemp = releaseTo.getItem(); bool changedInventory = false; //If the two items can't be combined if (!combineSuccess(releaseTo)) { //Maybe add/remove the items to the home inventories //tip to future William: only use one | so the evaluation doesn't short curcuit changedInventory = changedInventory | maybeAddToInventory(releaseTo); changedInventory = changedInventory | maybeRemoveFromInventory(releaseTo); changedInventory = changedInventory | releaseTo.maybeAddToInventory(this); changedInventory = changedInventory | releaseTo.maybeRemoveFromInventory(this); //Swap them this.setItem(youTemp); releaseTo.setItem(meTemp); } if (changedInventory && releaseTo.type != SlotType.DisplaySlot) { HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); hbuic.updateInventoryItemCount(); } }
//Shifts all the flowers to the next slot in the list public void reSortFlowerList() { if (currentTab == TabType.Flowers) { int ii = 0; MenuSlotItem previousItem = null; while (ii < homeFlowerSlots.Count) { MenuSlot fromSlot = homeFlowerSlots[ii].GetComponent <MenuSlot>(); MenuSlotItem tempItem = fromSlot.getItem(); fromSlot.setItemNoSort(previousItem); previousItem = tempItem; //Break out of the lop if (previousItem == null) { break; } ii += 1; } if (previousItem != null) { Destroy(previousItem.gameObject); } } }
//Maybe removes this item from the inventory depending on where it's from and where it moves to public bool maybeRemoveFromInventory(MenuSlot moveTo) { Debug.Log("Asking to remove from inventory from " + this.type + " to " + moveTo.type); //Don't remove it from the inventory if it's null if (this.getItem() == null) { return(false); } //Don't remove it from the inventory if it comes from a player slot if (this.type == SlotType.PlayerInventory) { return(false); } //Don't remove it from the inventory if it goes to a home inventory slot if (moveTo.type == SlotType.HomeInventory) { return(false); } //Don't remove it from the inventory if it goes to a decoration slot if (moveTo.type == SlotType.DecorationSlot) { return(false); } //Don't remove it from the inventory if it goes to the sell display slot if (moveTo.type == SlotType.DisplaySlot) { return(false); } //If it made it here then remove it HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); hbuic.removeHomeInventoryItem(this.getItem().id, this.getItem().isFlower); return(true); }
//Gathers all the items in the player inventory and stores them in the home inventory public void storeAllItems() { dropItem(); //get the 0th home inventory slot //MenuSlot storeSlot = homeFlowerSlots[0].GetComponent<MenuSlot>(); //Loop through the inventory for (int ii = 0; ii < fpc.inventorySize; ii += 1) { //Store the item if it's not null MenuSlot storeItemSlot = playerInventoryContainer.GetChild(ii).GetComponent <MenuSlot>(); MenuSlotItem storeItem = storeItemSlot.getItem(); if (storeItem != null) { storeItemSlot.storeItem(false); } } updateInventoryItemCount(); //If the player is in the flower tab then refresh the tab if (currentTab == TabType.Flowers) { SetFlowersTab(); } }
private void FillPlayerInventoryPanel() { //Get the player's inventory UIControllerScript uic = otherCanvas.GetComponent <UIControllerScript>(); List <Placeable> inventoryData = uic.getInventoryData(); List <int> inventoryCount = uic.getInventoryCount(); //Get the panel to add to RectTransform pip = GameObject.Find("playerInventoryPanel").GetComponent <RectTransform>(); for (int ii = 0; ii < fpc.inventorySize; ii++) { //Add item to player inventory rect //Build the slot regardless of whether or not it will be empty GameObject slotObject = Instantiate(playerInventorySlotPrefab, pip); MenuSlot slot = slotObject.GetComponent <MenuSlot>(); slot.slotIndex = ii; //Add an item to the slot if it shouldn't be empty if (inventoryData[ii] != null && inventoryCount[ii] > 0) { //Add the slot item to the slot GameObject itemObject = Instantiate(slotItemPrefab, slot.GetComponent <RectTransform>()); MenuSlotItem item = itemObject.GetComponent <MenuSlotItem>(); item.setID(inventoryData[ii].id, inventoryData[ii].isFlower); item.setCount(inventoryCount[ii]); slot.GetComponent <MenuSlot>().setItem(item); } } }
private void sellDecoration(uint id, int count) { if (grabbedSlot == null) { int toSell = count; try { if (homeDecorationsBought[id] < toSell) { toSell = homeDecorationsBought[id]; } homeDecorationsBought[id] -= toSell; fpc.money += (int)(toSell * decorationValue[(uint)id] * 0.75f); } catch { } //todo decrease item's count by count MenuSlot sellSlot = GameObject.Find("sellFlowerSlot").GetComponent <MenuSlot>(); //Reduce flower count by "adding" a negative value addHomeInventoryItem(id, -count, false); sellSlot.setCount(sellSlot.getCount() - count);//this is neg for some reason updateMoneyText(); updateFlowerDisplaySlot(sellSlot.getItem()); } }
private bool combineSuccess(MenuSlot releaseTo) { MenuSlotItem meTemp = this.getItem(); MenuSlotItem youTemp = releaseTo.getItem(); //If the two items should be merged if (this.getItem() != null && releaseTo.getItem() != null && meTemp.id == youTemp.id && meTemp.isFlower == youTemp.isFlower) { HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); if (releaseTo.type == SlotType.PlayerInventory)//if moving to inventory { hbuic.addHomeInventoryItem(releaseTo.getItem().id, -releaseTo.getItem().count, releaseTo.getItem().isFlower); } else if (this.type == SlotType.PlayerInventory)//if moving out of inventory { hbuic.addHomeInventoryItem(this.getItem().id, this.getItem().count, this.getItem().isFlower); } //Set the other item's count int oldCount = releaseTo.getItem().count; //Destroy the item in the other slot because the grabbed item will be stored GameObject.Destroy(releaseTo.getItem().gameObject); //Set the other slot to the grabbed item releaseTo.setItem(this.getItem()); releaseTo.setCount(oldCount + this.getItem().count); //Clear the item in the grabbed slot this.setItem(null); //It was successful so return true return(true); } return(false); }
public void dropItem() { if (grabbedSlot != null) { grabbedSlot.getItem().release(); grabbedSlot.releaseItem(null); grabbedSlot = null; } }
//This is a long drawn out method to determine if the item in this slot can move to the given slot //The if statements aren't very well optimized but I want to be able to read it easily public bool canMoveToSlot(MenuSlot releaseTo) { /* h: home inventory * p: player inventory * s: sell slot * d: decoration home inventory * an * indicates that the transfer is conditional */ //Home inventory -> Anywhere h>h h>p h>s h>d(allowed since the sell and display slots are never shown together) if (this.type == SlotType.HomeInventory) { return(true); } //Sell slot -> Anywhere s>h s>p s>s s>d(allowed since the sell and display slots are never shown together) if (this.type == SlotType.DisplaySlot) { return(true); } //An item in the player inventory can move to any other slot in the player inventory //Player inventory -> Player inventory p>p if (this.type == SlotType.PlayerInventory && releaseTo.type == SlotType.PlayerInventory) { return(true); } //Decorations can move to the player inventory //Decoration slot -> Player inventory d>p if (this.type == SlotType.DecorationSlot && releaseTo.type == SlotType.PlayerInventory) { return(true); } //Player slot -> Home Inventory p>h * if (this.type == SlotType.PlayerInventory && releaseTo.type == SlotType.HomeInventory) { //Can transfer if it's a flower or empty return(this.holdsFlower() || this.getItem() == null); } //Player slot -> Sell slot p>s if (this.type == SlotType.PlayerInventory && releaseTo.type == SlotType.DisplaySlot) { return(true); } return(false); }
//Sell the flower the given number of times private void sellFlower(uint flowerID, int sellAmount) { if (grabbedSlot == null) { MenuSlot sellSlot = GameObject.Find("sellFlowerSlot").GetComponent <MenuSlot>(); //this was for multiple sells since processflowersell allways sells one //fpc.money += HomeBaseController.instance.getFlowerValue(flowerID) * (sellAmount-1); HomeBaseController.instance.processFlowerSell(flowerID); //Reduce flower count by "adding" a negative value addHomeInventoryItem(flowerID, -sellAmount, true); sellSlot.setCount(sellSlot.getCount() - sellAmount); updateMoneyText(); updateFlowerDisplaySlot(sellSlot.getItem()); } }
private void purchaseDecoration(uint id, MenuSlot item) { if (grabbedSlot == null) { if (fpc.money >= decorationValue[(uint)id]) { if (!homeDecorationsBought.ContainsKey(id)) { homeDecorationsBought.Add(id, 0); } homeDecorationsBought[id] += 1; fpc.money -= decorationValue[(uint)id]; updateMoneyText(); bool pushedToInventory = false; Transform playerInventoryPanel = GameObject.Find("playerInventoryPanel").transform; for (int i = 0; i < 10 && pushedToInventory == false; i++) { if (playerInventoryPanel.GetChild(i).childCount != 0) { MenuSlotItem msi = playerInventoryPanel.GetChild(i).GetChild(0).GetComponent <MenuSlotItem>(); if (msi.isFlower == false && msi.id == id) { msi.setCount(msi.count + 1); pushedToInventory = true; } } } if (!pushedToInventory) { item.setCount(item.getCount() + 1); addHomeInventoryItem(id, 1, false); } for (int i = 0; i < 10 && pushedToInventory == false; i++) { if (playerInventoryPanel.GetChild(i).childCount == 0) { item.releaseItem(playerInventoryPanel.GetChild(i).GetComponent <MenuSlot>()); pushedToInventory = true; } } } } }
public override void performHoverAction(int x, int y) { base.performHoverAction(x, y); if (!(hoverText == "")) { return; } for (int i = 0; i < slotButtons.Count; i++) { if (currentItemIndex + i < MenuSlots.Count && slotButtons[i].containsPoint(x, y)) { MenuSlot slot = MenuSlots[currentItemIndex + i]; if (slot is FarmhandSlot && (slot as FarmhandSlot).BelongsToAnotherPlayer()) { hoverText = Game1.content.LoadString("Strings\\UI:Farmhand_Locked"); } } } }
public MenuSlot CheckForTouchedSlot(Vector3 position) { pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = position; List <RaycastResult> results = new List <RaycastResult>(); graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { MenuSlot slot = result.gameObject.GetComponent <MenuSlot>(); if (slot != null) { return(slot); } } return(null); }
//When the mouse is being held down public void ClickSlot(GameObject slot) { clickedOn = slot; //Debug.Log("Home base clicked: " + data.pointerCurrentRaycast.gameObject.name); MenuSlot slotClicked = clickedOn.GetComponent <MenuSlot>(); if (grabbedSlot == null) { //Grab the selected item if nothing is currently being selected if (slotClicked != null) { //If there's something in the slot to grab if (slotClicked.getItem() != null) { if (slotClicked.getItem().count > 0) { //If it's a slot with an item then grab it slotClicked.grabItem(); grabbedSlot = slotClicked; grabbedSlot.getItem().grab(); } } } } else { if (slotClicked.gameObject.name == "sellFlowerSlot" && grabbedSlot.getItem().isFlower != (currentTab == TabType.Flowers)) { } else { grabbedSlot.getItem().release(); grabbedSlot.releaseItem(slotClicked); grabbedSlot = null; } } }
//Maybe adds this item to the home inventory depending on where it's from and where it moves to public bool maybeAddToInventory(MenuSlot moveTo) { Debug.Log("Asking to add to inventory from " + this.type + " to " + moveTo.type); //Add the item to the inventory if it's coming from the player slot and going to the sell display slot //Don't add it to the inventory if it's null if (this.getItem() == null) { return(false); } //Don't add it to the home inventory if it's a home inventory slot if (this.type == SlotType.HomeInventory) { return(false); } //Don't add it to the home inventory if it's a decoration slot if (this.type == SlotType.DecorationSlot) { return(false); } //Don't add it to the home inventory if it's a sell display slot if (this.type == SlotType.DisplaySlot) { return(false); } //Don't add it to the home inventory if it's moving to a player inventory slot if (moveTo.type == SlotType.PlayerInventory) { return(false); } //If it made it here then add it HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); hbuic.addHomeInventoryItem(this.getItem().id, this.getItem().count, this.getItem().isFlower); return(true); }
public void CheckForTouch() { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = touch.position; List<RaycastResult> results = new List<RaycastResult>(); graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { MenuSlot slot = result.gameObject.GetComponent<MenuSlot>(); if (slot != null) { currentEquipmentSlot = slot as EquipmentSlot; break; } } } else if (touch.phase == TouchPhase.Ended) { bool touchedSomething = false; pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = touch.position; List<RaycastResult> results = new List<RaycastResult>(); graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { MenuSlot slot = result.gameObject.GetComponent<MenuSlot>(); if (slot != null && slot == currentEquipmentSlot) { EquipmentSlot equipSlot = (slot as EquipmentSlot); if (equipSlot != null) { if (equipSlot.GetPlacedEquipment != null && !equipSlot.JustBeenPlace) { equipmentsInformationsManager.OpenInformationsPanel(equipSlot, false); touchedSomething = true; break; } } currentEquipmentSlot = null; equipmentsInformationsManager.CloseInformationsPanel(); break; } else if (result.gameObject.GetComponent<Button>() != null) { touchedSomething = true; break; } touchedSomething = false; } if (!touchedSomething) equipmentsInformationsManager.CloseInformationsPanel(); } } }
protected virtual void drawSlotBackground(SpriteBatch b, int i, MenuSlot slot) { IClickableMenu.drawTextureBox(b, Game1.mouseCursors, new Rectangle(384, 396, 15, 15), slotButtons[i].bounds.X, slotButtons[i].bounds.Y, slotButtons[i].bounds.Width, slotButtons[i].bounds.Height, ((currentItemIndex + i != selected || timerToLoad % 150 <= 75 || timerToLoad <= 1000) && (selected != -1 || !(slotButtons[i].scale > 1f) || scrolling || deleteConfirmationScreen)) ? Color.White : ((deleteButtons.Count > i && deleteButtons[i].containsPoint(Game1.getOldMouseX(), Game1.getOldMouseY())) ? Color.White : Color.Wheat), 4f, drawShadow: false); }
/// <summary> /// Syncs the player inventory with the flowers in the player inventory home base panel /// </summary> private void PlayerInventorySync() { FlowerProperties fp = GameObject.Find("World").GetComponent <FlowerProperties>(); UIControllerScript uic = otherCanvas.GetComponent <UIControllerScript>(); //Now fill the other canvas and the UI inventory with the right items //Loop through the entire inventory for (int ii = 0; ii < fpc.inventorySize; ii++) { MenuSlot hInventoryItem = playerInventoryContainer.GetChild(ii).GetComponent <MenuSlot>(); Placeable slotPlaceable = uic.getInventoryDataAtSlot(ii); uint currentSlotID = 0; bool currentSlotIsFlower = false; if (slotPlaceable != null) { currentSlotID = slotPlaceable.id; currentSlotIsFlower = slotPlaceable.isFlower; } //If there's an item in the home base player inventory if (hInventoryItem.getItem() != null) { uic.inventoryClearSlot(ii); GameObject newObj; //Create a placeable based on the slot's id if (hInventoryItem.getItem().isFlower) { //Make a flower newObj = Instantiate(Resources.Load("Prefabs/FlowerPrefab", typeof(GameObject)) as GameObject); FlowerObj p = newObj.GetComponent <FlowerObj>(); p.id = hInventoryItem.getItem().id; int[] petal = new int[2] { fp.getPetalIndexFromIndex(p.id), fp.getPetalColorIndexFromIndex(p.id) }; int[] pistil = new int[2] { fp.getPistilIndexFromIndex(p.id), fp.getPistilColorIndexFromIndex(p.id) }; int[] leaf = new int[2] { fp.getLeafIndexFromIndex(p.id), fp.getLeafColorIndexFromIndex(p.id) }; int[] stem = new int[2] { fp.getStemIndexFromIndex(p.id), fp.getStemColorIndexFromIndex(p.id) }; int[] position = new int[2] { -1, -1 }; p.init(petal, stem, pistil, leaf, position, transform.root); p.alive = true; uic.setSlot(ii, p); uic.setSlotCount(ii, hInventoryItem.getItem().count); Destroy(newObj); } else { //Make a decoration newObj = Instantiate(fp.getDecorationObject(hInventoryItem.getItem().id)); Placeable p = newObj.GetComponent <Placeable>(); p.id = hInventoryItem.getItem().id; p.isFlower = hInventoryItem.getItem().isFlower; p.flowerGridPos = new int[2] { -1, -1 }; uic.setSlot(ii, p); uic.setSlotCount(ii, hInventoryItem.getItem().count); Destroy(newObj); } } else //If there's not an item in the home base player inventory slot { //Check to see if the player has an item in the inventory slot if (uic.getInventoryDataAtSlot(ii) != null) { //The player had an item in the slot but moved it to the home base //So clear the slot uic.inventoryClearSlot(ii); } } } }
/// <summary> /// Releases the item to the given slot, if it's null then nothing happens /// </summary> public void releaseItem(MenuSlot releaseTo) { Debug.Log("vvvvvvvvv Start releasing vvvvvvvvvv"); //Note: "this" always refers to the item slot that holds the grabbed item HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); GetComponent <Image>().color = Color.white; isGrabbed = false; //If the item is actually dropped onto a slot //And the slot is different than the one it was picked up in if (releaseTo != null && releaseTo != this) { //First thing to do is see if the player is "splitting" items //If there is a visible item with the same id in the player inventory or the home inventory then merge the two items together if (releaseTo.type == SlotType.HomeInventory || releaseTo.type == MenuSlot.SlotType.DisplaySlot) { //Get a matching slot MenuSlot matchingSlot = hbuic.findSlotWithItem(this.getItem()); //Make sure there is a matching slot and it's not this one and it has more than 0 items if (matchingSlot != null && matchingSlot != this && matchingSlot.getCount() > 0) { //If the player didn't already try to merge the items if (matchingSlot != releaseTo) { if (releaseTo.slotIndex != 0 || releaseTo.type == SlotType.DisplaySlot) { //Pretend the player clicked on the matching slot matchingSlot.swapWith(releaseTo); } else { releaseTo = matchingSlot; } } } } //This only happens if the player is moving something to a slot bool iCanMove, uCanMove, weSwap; iCanMove = canMoveToSlot(releaseTo); uCanMove = releaseTo.canMoveToSlot(this); Debug.Log("iCanMove: " + iCanMove + " uCanMove: " + uCanMove); weSwap = (iCanMove && uCanMove); if (weSwap) { this.swapWith(releaseTo); } else //It can't be a straight up swap { //If my item can move to the given slot if (iCanMove) { //If it can't combine them then if (!combineSuccess(releaseTo)) { //Store the other one in its respective inventory releaseTo.storeItem(false); //Make this one empty by swapping the two items this.swapWith(releaseTo); } } else { //If this item isn't a decoration slot if (this.type != SlotType.DecorationSlot) { //Store this item into its proper home inventory (and set it to null) this.storeItem(true); } else { //The the player is trying to store an item that was picked up from a decoration slot returnItemToSlot(); } //If the item in the released slot can move if (uCanMove) { //Only swap the items if the other slot isn't a decoration slot if (releaseTo.type != SlotType.DecorationSlot) { this.swapWith(releaseTo); } } } } } else //If the item was dropped somewhere that's not a slot (or dropped back onto itself) { this.returnItemToSlot(); } Debug.Log("^^^^^^^^^^ Done releasing ^^^^^^^^^^"); }
public void SetOriginSlot(MenuSlot slot) { originSlot = slot; objectIconImage.sprite = slot.GetSlotIcon(); }
public void CheckForTouch() { if (mapManager.GetConfirmationPanel.ConfirmingSomething) { return; } foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = touch.position; List <RaycastResult> results = new List <RaycastResult>(); graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { MenuSlot slot = result.gameObject.GetComponent <MenuSlot>(); if (slot != null) { currentEquipmentSlot = slot as EquipmentSlot; break; } } } else if (touch.phase == TouchPhase.Ended) { bool touchedSomething = false; pointerEventData = new PointerEventData(eventSystem); pointerEventData.position = touch.position; List <RaycastResult> results = new List <RaycastResult>(); graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { MenuSlot slot = result.gameObject.GetComponent <MenuSlot>(); if (slot != null && slot == currentEquipmentSlot) { EquipmentSlot equipSlot = (slot as EquipmentSlot); if (equipSlot != null) { if (equipSlot.GetPlacedEquipment != null && !equipSlot.JustBeenPlace) { /*if (!mapManager.DocksManager.PlayerInventoryOnly) * mapManager.EquipmentsInfoManager.OpenInformationsPanel(equipSlot, false); * else * mapManager.EquipmentsInfoManager.OpenInformationsPanel(equipSlot);*/ mapManager.EquipmentsInfoManager.OpenInformationsPanel(equipSlot, mapManager.DocksManager.PlayerInventoryOnly); touchedSomething = true; /*if (equipSlot as EquipmentSlotInventory != null) * mapManager.ShipCompoManager.ShowEquipButton(equipSlot as EquipmentSlotInventory);*/ break; } } currentEquipmentSlot = null; mapManager.EquipmentsInfoManager.CloseInformationsPanel(); break; } else if (result.gameObject.GetComponent <Button>() != null) { touchedSomething = true; break; } touchedSomething = false; } if (!touchedSomething) { mapManager.EquipmentsInfoManager.CloseInformationsPanel(); } } } }