/// <summary> /// Check to see whether the player can place the item and if so, draw the telegraph. /// </summary> /// <returns>Whether the item can be placed in the target location and the target location</returns> public (bool canPlace, Vector2 position) ShowPlacementTarget(ItemVisual draggedItem) { //Check to see if it's hanging over the edge - if so, do not place. if (!m_InventoryGrid.layout.Contains(new Vector2(draggedItem.localBound.xMax, draggedItem.localBound.yMax))) { m_Telegraph.style.visibility = Visibility.Hidden; return(canPlace : false, position : Vector2.zero); } VisualElement targetSlot = m_InventoryGrid.Children().Where(x => x.layout.Overlaps(draggedItem.layout) && x != draggedItem).OrderBy(x => Vector2.Distance(x.worldBound.position, draggedItem.worldBound.position)).First(); m_Telegraph.style.width = draggedItem.style.width; m_Telegraph.style.height = draggedItem.style.height; SetItemPosition(m_Telegraph, new Vector2(targetSlot.layout.position.x, targetSlot.layout.position.y)); m_Telegraph.style.visibility = Visibility.Visible; var overlappingItems = StoredItems.Where(x => x.RootVisual != null && x.RootVisual.layout.Overlaps(m_Telegraph.layout)).ToArray(); if (overlappingItems.Length > 1) { m_Telegraph.style.visibility = Visibility.Hidden; return(canPlace : false, position : Vector2.zero); } return(canPlace : true, targetSlot.worldBound.position); }
/// <summary> /// Load all of the inventory items and set them in the default position. /// This is called from Start. /// </summary> private async void LoadInventory() { //make sure inventory is in ready state await UniTask.WaitUntil(() => m_IsInventoryReady); //load foreach (StoredItem loadedItem in StoredItems) { ItemVisual inventoryItemVisual = new ItemVisual(loadedItem.Details); AddItemToInventoryGrid(inventoryItemVisual); bool inventoryHasSpace = await GetPositionForItem(inventoryItemVisual); if (!inventoryHasSpace) { Debug.Log("No space - Cannot pick up the item"); RemoveItemFromInventoryGrid(inventoryItemVisual); continue; } ConfigureInventoryItem(loadedItem, inventoryItemVisual); } }
/// <summary> /// Call to associate item with visual element and set active /// </summary> private static void ConfigureInventoryItem(StoredItem item, ItemVisual visual) { item.RootVisual = visual; visual.style.visibility = Visibility.Visible; }