From 34616e058ac20072ef012791742affcde7a135a4 Mon Sep 17 00:00:00 2001 From: npackard Date: Mon, 16 Sep 2024 14:09:24 -0400 Subject: [PATCH 1/4] move board items to unused & prev unused slots on board --- .../Code/ArrangementController.cs | 52 +++++- .../Code/UIBoardItemController.cs | 42 ++++- .../HandMadeGame/Code/UiInvItemController.cs | 2 +- Assets/Scenes/GameflowCopiedForGrid.unity | 153 ++++++++++++++++++ 4 files changed, 242 insertions(+), 7 deletions(-) diff --git a/Assets/HandMadeGame/Code/ArrangementController.cs b/Assets/HandMadeGame/Code/ArrangementController.cs index 5cb0225..02e1ba4 100644 --- a/Assets/HandMadeGame/Code/ArrangementController.cs +++ b/Assets/HandMadeGame/Code/ArrangementController.cs @@ -44,7 +44,7 @@ private void HideGrid() { grid.SetActive(false); } - public bool UpdateBoard(Vector2 pos, int invPos, Image img) { + public bool UpdateBoardFromInv(Vector2 pos, int invPos, Image img) { if (pos[0] == -1 || pos[1] == -1) return false; // invalid tile replacement // check if board already has something in that slot // if it does, move it to inventory @@ -80,7 +80,7 @@ public bool UpdateBoard(Vector2 pos, int invPos, Image img) { hotbar[invPos].SetActive(false); return true; } - } else { + } else { // otherwise, just move it Board[(int)pos[0], (int)pos[1]] = Inventory[invPos]; Inventory[invPos] = -1; int internalPosAgain = (int)pos[1] + (3 * (int)pos[0]); @@ -90,4 +90,52 @@ public bool UpdateBoard(Vector2 pos, int invPos, Image img) { return true; } } + + public bool UpdateBoardFromBoard(Vector2 newPos, int invPos, Vector2 curPos, Image img) { + if (newPos[0] == -1 || newPos[1] == -1) return false; // invalid tile replacement + // check if board already has something in that slot + // if it does, move it to inventory + if (Board[(int)newPos[0], (int)newPos[1]] != 0) { + // find first empty inventory slot + int emptyInvIndex = -1; + for (int i = 0; i < Inventory.Count; i++) { + if (i == invPos || Inventory[i] == -1) { + emptyInvIndex = i; + break; + } + } + if (emptyInvIndex == invPos) { + int intPos = (int)newPos[1] + (3 * (int)newPos[0]); + Image temp = Image.Instantiate(hotbar[emptyInvIndex].GetComponent()); + hotbar[emptyInvIndex].GetComponent().color = internalDisplay[intPos].GetComponent().color; + int temp2 = Inventory[emptyInvIndex]; + Inventory[emptyInvIndex] = Board[(int)newPos[0], (int)newPos[1]]; + Inventory[invPos] = Board[(int)newPos[0], (int)newPos[1]]; + Board[(int)newPos[0], (int)newPos[1]] = temp2; + internalDisplay[intPos].SetActive(true); + internalDisplay[intPos].GetComponent().color = temp.color; + return true; + } else { + hotbar[emptyInvIndex].SetActive(true); + int internalPos = (int)newPos[1] + (3 * (int)newPos[0]); + hotbar[emptyInvIndex].GetComponent().color = internalDisplay[internalPos].GetComponent().color; + Inventory[emptyInvIndex] = Board[(int)newPos[0], (int)newPos[1]]; + Board[(int)newPos[0], (int)newPos[1]] = Inventory[invPos]; + Inventory[invPos] = -1; + internalDisplay[internalPos].SetActive(true); + internalDisplay[internalPos].GetComponent().color = img.color; + hotbar[invPos].SetActive(false); + return true; + } + } else { // otherwise, just move it + Board[(int)newPos[0], (int)newPos[1]] = Board[(int)curPos[0], (int)curPos[1]]; + Board[(int)curPos[0], (int)curPos[1]] = -1; + int newInternalPos = (int)newPos[1] + (3 * (int)newPos[0]); + int curInternalPos = (int)curPos[1] + (3 * (int)curPos[0]); + internalDisplay[newInternalPos].SetActive(true); + internalDisplay[newInternalPos].GetComponent().color = img.color; + internalDisplay[curInternalPos].SetActive(false); + return true; + } + } } diff --git a/Assets/HandMadeGame/Code/UIBoardItemController.cs b/Assets/HandMadeGame/Code/UIBoardItemController.cs index ceaa966..0d47ecf 100644 --- a/Assets/HandMadeGame/Code/UIBoardItemController.cs +++ b/Assets/HandMadeGame/Code/UIBoardItemController.cs @@ -4,7 +4,7 @@ using UnityEngine.EventSystems; using UnityEngine.UI; -public class UIBoardItemController : MonoBehaviour +public class UIBoardItemController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public ArrangementController ac; // I realize this is jank, will polish later @@ -14,6 +14,7 @@ public class UIBoardItemController : MonoBehaviour public Vector2 grabbyOffset = Vector2.zero; public int invIndex; + public Vector2 boardPos; // Start is called before the first frame update void Start() @@ -34,9 +35,42 @@ public void OnPointerDown(PointerEventData pointerEventData) { public void OnPointerUp(PointerEventData pointerEventData) { Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto); Vector2 mouseCoords = Input.mousePosition; - Vector2 boardLoc = FindTileLocation(mouseCoords); - ac.UpdateBoard(boardLoc, invIndex, this.gameObject.GetComponent()); - this.gameObject.SetActive(false); + int invPos = FindInventoryLocation(mouseCoords); + Debug.Log(invPos); + if (invPos == -1) { // not in inventory, check board + Vector2 boardLoc = FindTileLocation(mouseCoords); + Debug.Log(boardLoc); + bool success = ac.UpdateBoardFromBoard(boardLoc, -1, boardPos, this.gameObject.GetComponent()); + } + } + + private int FindInventoryLocation(Vector2 mouseCoords) { + int boardPos = -1; + // first, make sure on inventory + if (mouseCoords[0] < .15 * Screen.width || mouseCoords[0] > .85 * Screen.width || mouseCoords[1] < .8 * Screen.height || mouseCoords[1] > .15 * Screen.height) return -1; + if (mouseCoords[0] < .22 * Screen.width) { + boardPos = 0; + } else if (mouseCoords[0] < .3 * Screen.width) { + boardPos = 1; + } else if (mouseCoords[0] < .38 * Screen.width) { + boardPos = 2; + } else if (mouseCoords[0] < .46 * Screen.width) { + boardPos = 3; + } else if (mouseCoords[0] < .54 * Screen.width) { + boardPos = 4; + } else if (mouseCoords[0] < .62 * Screen.width) { + boardPos = 5; + } else if (mouseCoords[0] < .7 * Screen.width) { + boardPos = 6; + } else if (mouseCoords[0] < .78 * Screen.width) { + boardPos = 7; + } else if (mouseCoords[0] < .85 * Screen.width) { + boardPos = 8; + } else { + boardPos = -1; + Debug.Log("stop breaking my code >:("); + } + return boardPos; } private Vector2 FindTileLocation(Vector2 mouseCoords) { diff --git a/Assets/HandMadeGame/Code/UiInvItemController.cs b/Assets/HandMadeGame/Code/UiInvItemController.cs index af69072..311920e 100644 --- a/Assets/HandMadeGame/Code/UiInvItemController.cs +++ b/Assets/HandMadeGame/Code/UiInvItemController.cs @@ -35,7 +35,7 @@ public void OnPointerUp(PointerEventData pointerEventData) { Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto); Vector2 mouseCoords = Input.mousePosition; Vector2 boardLoc = FindTileLocation(mouseCoords); - bool success = ac.UpdateBoard(boardLoc, invIndex, this.gameObject.GetComponent()); + bool success = ac.UpdateBoardFromInv(boardLoc, invIndex, this.gameObject.GetComponent()); } private Vector2 FindTileLocation(Vector2 mouseCoords) { diff --git a/Assets/Scenes/GameflowCopiedForGrid.unity b/Assets/Scenes/GameflowCopiedForGrid.unity index 039d229..7dee1eb 100644 --- a/Assets/Scenes/GameflowCopiedForGrid.unity +++ b/Assets/Scenes/GameflowCopiedForGrid.unity @@ -274,6 +274,7 @@ GameObject: - component: {fileID: 37091591} - component: {fileID: 37091593} - component: {fileID: 37091592} + - component: {fileID: 37091594} m_Layer: 5 m_Name: Item7 (1) m_TagString: Untagged @@ -338,6 +339,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 37091590} m_CullTransparentMesh: 1 +--- !u!114 &37091594 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 37091590} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 2, y: 0} --- !u!1 &78828385 GameObject: m_ObjectHideFlags: 0 @@ -665,6 +684,7 @@ GameObject: - component: {fileID: 105355153} - component: {fileID: 105355155} - component: {fileID: 105355154} + - component: {fileID: 105355156} m_Layer: 5 m_Name: Item4 (1) m_TagString: Untagged @@ -729,6 +749,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 105355152} m_CullTransparentMesh: 1 +--- !u!114 &105355156 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 105355152} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 1, y: 0} --- !u!1 &190791634 GameObject: m_ObjectHideFlags: 0 @@ -1858,6 +1896,7 @@ GameObject: - component: {fileID: 722973592} - component: {fileID: 722973594} - component: {fileID: 722973593} + - component: {fileID: 722973595} m_Layer: 5 m_Name: Item3 (1) m_TagString: Untagged @@ -1922,6 +1961,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 722973591} m_CullTransparentMesh: 1 +--- !u!114 &722973595 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 722973591} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 0, y: 2} --- !u!1 &726462720 GameObject: m_ObjectHideFlags: 0 @@ -2984,6 +3041,7 @@ GameObject: - component: {fileID: 1181720785} - component: {fileID: 1181720787} - component: {fileID: 1181720786} + - component: {fileID: 1181720788} m_Layer: 5 m_Name: Item5 (1) m_TagString: Untagged @@ -3048,6 +3106,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1181720784} m_CullTransparentMesh: 1 +--- !u!114 &1181720788 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1181720784} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 1, y: 1} --- !u!1 &1271982229 GameObject: m_ObjectHideFlags: 0 @@ -3389,6 +3465,7 @@ GameObject: - component: {fileID: 1588185199} - component: {fileID: 1588185201} - component: {fileID: 1588185200} + - component: {fileID: 1588185202} m_Layer: 5 m_Name: Item2 (1) m_TagString: Untagged @@ -3453,6 +3530,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1588185198} m_CullTransparentMesh: 1 +--- !u!114 &1588185202 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1588185198} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 0, y: 1} --- !u!1 &1600910168 GameObject: m_ObjectHideFlags: 0 @@ -3745,6 +3840,7 @@ GameObject: - component: {fileID: 1675805999} - component: {fileID: 1675806001} - component: {fileID: 1675806000} + - component: {fileID: 1675806002} m_Layer: 5 m_Name: Item6 (1) m_TagString: Untagged @@ -3809,6 +3905,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1675805998} m_CullTransparentMesh: 1 +--- !u!114 &1675806002 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1675805998} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 1, y: 2} --- !u!1 &1702595147 GameObject: m_ObjectHideFlags: 0 @@ -3902,6 +4016,7 @@ MonoBehaviour: grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} grabbyOffset: {x: 50, y: 0} invIndex: 0 + boardPos: {x: 0, y: 0} --- !u!1 &1708591712 GameObject: m_ObjectHideFlags: 0 @@ -4239,6 +4354,7 @@ GameObject: - component: {fileID: 1826728679} - component: {fileID: 1826728678} - component: {fileID: 1826728677} + - component: {fileID: 1826728680} m_Layer: 5 m_Name: Item9 (1) m_TagString: Untagged @@ -4303,6 +4419,24 @@ RectTransform: m_AnchoredPosition: {x: 200, y: -100} m_SizeDelta: {x: 100, y: 100} m_Pivot: {x: 0.5, y: 0} +--- !u!114 &1826728680 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1826728676} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 2, y: 2} --- !u!1 &1836543077 GameObject: m_ObjectHideFlags: 0 @@ -4407,6 +4541,7 @@ GameObject: - component: {fileID: 1893078987} - component: {fileID: 1893078989} - component: {fileID: 1893078988} + - component: {fileID: 1893078990} m_Layer: 5 m_Name: Item8 (1) m_TagString: Untagged @@ -4471,6 +4606,24 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1893078986} m_CullTransparentMesh: 1 +--- !u!114 &1893078990 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1893078986} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cf1c6f0a352542498241b56d2a4d7f6, type: 3} + m_Name: + m_EditorClassIdentifier: + ac: {fileID: 400214485} + normalCursor: {fileID: 2800000, guid: ae82820897e199d4a93407fdd6b0b729, type: 3} + grabbyCursor: {fileID: 2800000, guid: 194b9c69e3ec7c74196ea636b722da79, type: 3} + grabbyOffset: {x: 50, y: 0} + invIndex: 0 + boardPos: {x: 2, y: 1} --- !u!1 &1917757602 GameObject: m_ObjectHideFlags: 0 From 47937494d192c5a755e21f4c70957f2f40c32c11 Mon Sep 17 00:00:00 2001 From: npackard Date: Mon, 16 Sep 2024 18:05:33 -0400 Subject: [PATCH 2/4] move items freely around board --- .../Code/ArrangementController.cs | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/Assets/HandMadeGame/Code/ArrangementController.cs b/Assets/HandMadeGame/Code/ArrangementController.cs index 02e1ba4..c7043b9 100644 --- a/Assets/HandMadeGame/Code/ArrangementController.cs +++ b/Assets/HandMadeGame/Code/ArrangementController.cs @@ -104,32 +104,19 @@ public bool UpdateBoardFromBoard(Vector2 newPos, int invPos, Vector2 curPos, Ima break; } } - if (emptyInvIndex == invPos) { - int intPos = (int)newPos[1] + (3 * (int)newPos[0]); - Image temp = Image.Instantiate(hotbar[emptyInvIndex].GetComponent()); - hotbar[emptyInvIndex].GetComponent().color = internalDisplay[intPos].GetComponent().color; - int temp2 = Inventory[emptyInvIndex]; - Inventory[emptyInvIndex] = Board[(int)newPos[0], (int)newPos[1]]; - Inventory[invPos] = Board[(int)newPos[0], (int)newPos[1]]; - Board[(int)newPos[0], (int)newPos[1]] = temp2; - internalDisplay[intPos].SetActive(true); - internalDisplay[intPos].GetComponent().color = temp.color; - return true; - } else { - hotbar[emptyInvIndex].SetActive(true); - int internalPos = (int)newPos[1] + (3 * (int)newPos[0]); - hotbar[emptyInvIndex].GetComponent().color = internalDisplay[internalPos].GetComponent().color; - Inventory[emptyInvIndex] = Board[(int)newPos[0], (int)newPos[1]]; - Board[(int)newPos[0], (int)newPos[1]] = Inventory[invPos]; - Inventory[invPos] = -1; - internalDisplay[internalPos].SetActive(true); - internalDisplay[internalPos].GetComponent().color = img.color; - hotbar[invPos].SetActive(false); - return true; - } + hotbar[emptyInvIndex].SetActive(true); + int newInternalPos = (int)newPos[1] + (3 * (int)newPos[0]); + int curInternalPos = (int)curPos[1] + (3 * (int)curPos[0]); + hotbar[emptyInvIndex].GetComponent().color = internalDisplay[newInternalPos].GetComponent().color; + Inventory[emptyInvIndex] = Board[(int)newPos[0], (int)newPos[1]]; + Board[(int)newPos[0], (int)newPos[1]] = Board[(int)curPos[0], (int)curPos[1]]; + Board[(int)curPos[0], (int)curPos[1]] = 0; + internalDisplay[newInternalPos].GetComponent().color = img.color; + internalDisplay[curInternalPos].SetActive(false); + return true; } else { // otherwise, just move it Board[(int)newPos[0], (int)newPos[1]] = Board[(int)curPos[0], (int)curPos[1]]; - Board[(int)curPos[0], (int)curPos[1]] = -1; + Board[(int)curPos[0], (int)curPos[1]] = 0; int newInternalPos = (int)newPos[1] + (3 * (int)newPos[0]); int curInternalPos = (int)curPos[1] + (3 * (int)curPos[0]); internalDisplay[newInternalPos].SetActive(true); From 49237b632ad23484d394a8a1a239c6f98743cde6 Mon Sep 17 00:00:00 2001 From: npackard Date: Mon, 16 Sep 2024 22:02:51 -0400 Subject: [PATCH 3/4] board to hotbar done --- Assets/HandMadeGame/Code/ArrangementController.cs | 13 +++++++++++++ Assets/HandMadeGame/Code/UIBoardItemController.cs | 11 +++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Assets/HandMadeGame/Code/ArrangementController.cs b/Assets/HandMadeGame/Code/ArrangementController.cs index c7043b9..6f88aee 100644 --- a/Assets/HandMadeGame/Code/ArrangementController.cs +++ b/Assets/HandMadeGame/Code/ArrangementController.cs @@ -125,4 +125,17 @@ public bool UpdateBoardFromBoard(Vector2 newPos, int invPos, Vector2 curPos, Ima return true; } } + + public bool UpdateInvFromBoard(Vector2 curPos, int invPos, Image img) { + // check if inventory space is full + if (Inventory[invPos] != -1) return false; + // otherwise, move it to the inventory + Inventory[invPos] = Board[(int)curPos[0], (int)curPos[1]]; + hotbar[invPos].SetActive(true); + hotbar[invPos].GetComponent().color = img.color; + int internalPos = (int)curPos[1] + (3 * (int)curPos[0]); + Board[(int)curPos[0], (int)curPos[1]] = 0; + internalDisplay[internalPos].SetActive(false); + return true; + } } diff --git a/Assets/HandMadeGame/Code/UIBoardItemController.cs b/Assets/HandMadeGame/Code/UIBoardItemController.cs index 0d47ecf..ebb2c93 100644 --- a/Assets/HandMadeGame/Code/UIBoardItemController.cs +++ b/Assets/HandMadeGame/Code/UIBoardItemController.cs @@ -36,18 +36,21 @@ public void OnPointerUp(PointerEventData pointerEventData) { Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto); Vector2 mouseCoords = Input.mousePosition; int invPos = FindInventoryLocation(mouseCoords); - Debug.Log(invPos); + bool success = false; if (invPos == -1) { // not in inventory, check board + Debug.Log("noooooo"); Vector2 boardLoc = FindTileLocation(mouseCoords); - Debug.Log(boardLoc); - bool success = ac.UpdateBoardFromBoard(boardLoc, -1, boardPos, this.gameObject.GetComponent()); + success = ac.UpdateBoardFromBoard(boardLoc, -1, boardPos, this.gameObject.GetComponent()); + } else { + success = ac.UpdateInvFromBoard(boardPos, invPos, this.gameObject.GetComponent()); } } private int FindInventoryLocation(Vector2 mouseCoords) { int boardPos = -1; + Debug.Log(new Vector2(mouseCoords[0], Screen.width)); // first, make sure on inventory - if (mouseCoords[0] < .15 * Screen.width || mouseCoords[0] > .85 * Screen.width || mouseCoords[1] < .8 * Screen.height || mouseCoords[1] > .15 * Screen.height) return -1; + if (mouseCoords[0] < .15 * Screen.width || mouseCoords[0] > .85 * Screen.width || mouseCoords[1] < .08 * Screen.height || mouseCoords[1] > .18 * Screen.height) return -1; if (mouseCoords[0] < .22 * Screen.width) { boardPos = 0; } else if (mouseCoords[0] < .3 * Screen.width) { From 03b5b96e507850cd857c3c2e2802a845c0cb77e9 Mon Sep 17 00:00:00 2001 From: npackard Date: Mon, 16 Sep 2024 23:51:36 -0400 Subject: [PATCH 4/4] move items around hotbar slots --- .../Code/ArrangementController.cs | 22 ++++++++++ .../HandMadeGame/Code/UiInvItemController.cs | 40 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Assets/HandMadeGame/Code/ArrangementController.cs b/Assets/HandMadeGame/Code/ArrangementController.cs index 6f88aee..6029256 100644 --- a/Assets/HandMadeGame/Code/ArrangementController.cs +++ b/Assets/HandMadeGame/Code/ArrangementController.cs @@ -138,4 +138,26 @@ public bool UpdateInvFromBoard(Vector2 curPos, int invPos, Image img) { internalDisplay[internalPos].SetActive(false); return true; } + + public bool UpdateInvFromInv(int curInvPos, int newInvPos, Image img) { + // check if inventory space is full. if so, swap items + if (Inventory[newInvPos] != -1) { + //Image temp = Image.Instantiate(hotbar[emptyInvIndex].GetComponent()); + int tempInt = Inventory[newInvPos]; + Inventory[newInvPos] = Inventory[curInvPos]; + Inventory[newInvPos] = tempInt; + Image tempCurImage = Image.Instantiate(hotbar[curInvPos].GetComponent()); + Image tempNewImage = Image.Instantiate(hotbar[newInvPos].GetComponent()); + hotbar[newInvPos].GetComponent().color = tempCurImage.color; + hotbar[curInvPos].GetComponent().color = tempNewImage.color; + return true; + } + // otherwise, just place it + Inventory[newInvPos] = Inventory[curInvPos]; + hotbar[newInvPos].SetActive(true); + hotbar[newInvPos].GetComponent().color = img.color; + Inventory[curInvPos] = -1; + hotbar[curInvPos].SetActive(false); + return true; + } } diff --git a/Assets/HandMadeGame/Code/UiInvItemController.cs b/Assets/HandMadeGame/Code/UiInvItemController.cs index 311920e..8c34337 100644 --- a/Assets/HandMadeGame/Code/UiInvItemController.cs +++ b/Assets/HandMadeGame/Code/UiInvItemController.cs @@ -34,8 +34,14 @@ public void OnPointerDown(PointerEventData pointerEventData) { public void OnPointerUp(PointerEventData pointerEventData) { Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto); Vector2 mouseCoords = Input.mousePosition; - Vector2 boardLoc = FindTileLocation(mouseCoords); - bool success = ac.UpdateBoardFromInv(boardLoc, invIndex, this.gameObject.GetComponent()); + int invPos = FindInventoryLocation(mouseCoords); + bool success = false; + if (invPos == -1) { // not in inventory, check board + Vector2 boardLoc = FindTileLocation(mouseCoords); + success = ac.UpdateBoardFromInv(boardLoc, invIndex, this.gameObject.GetComponent()); + } else { + success = ac.UpdateInvFromInv(invIndex, invPos, this.gameObject.GetComponent()); + } } private Vector2 FindTileLocation(Vector2 mouseCoords) { @@ -70,4 +76,34 @@ private Vector2 FindTileLocation(Vector2 mouseCoords) { } return boardPos; } + + private int FindInventoryLocation(Vector2 mouseCoords) { + int boardPos = -1; + Debug.Log(new Vector2(mouseCoords[0], Screen.width)); + // first, make sure on inventory + if (mouseCoords[0] < .15 * Screen.width || mouseCoords[0] > .85 * Screen.width || mouseCoords[1] < .08 * Screen.height || mouseCoords[1] > .18 * Screen.height) return -1; + if (mouseCoords[0] < .22 * Screen.width) { + boardPos = 0; + } else if (mouseCoords[0] < .3 * Screen.width) { + boardPos = 1; + } else if (mouseCoords[0] < .38 * Screen.width) { + boardPos = 2; + } else if (mouseCoords[0] < .46 * Screen.width) { + boardPos = 3; + } else if (mouseCoords[0] < .54 * Screen.width) { + boardPos = 4; + } else if (mouseCoords[0] < .62 * Screen.width) { + boardPos = 5; + } else if (mouseCoords[0] < .7 * Screen.width) { + boardPos = 6; + } else if (mouseCoords[0] < .78 * Screen.width) { + boardPos = 7; + } else if (mouseCoords[0] < .85 * Screen.width) { + boardPos = 8; + } else { + boardPos = -1; + Debug.Log("stop breaking my code >:("); + } + return boardPos; + } }