Skip to content

Commit

Permalink
Merge pull request #23 from npackard/main
Browse files Browse the repository at this point in the history
arrangement UI done
  • Loading branch information
npackard authored Sep 17, 2024
2 parents 04e99d9 + 03b5b96 commit 4492136
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 8 deletions.
74 changes: 72 additions & 2 deletions Assets/HandMadeGame/Code/ArrangementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
Expand All @@ -90,4 +90,74 @@ 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;
}
}
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<Image>().color = internalDisplay[newInternalPos].GetComponent<Image>().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<Image>().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]] = 0;
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<Image>().color = img.color;
internalDisplay[curInternalPos].SetActive(false);
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<Image>().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;
}

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<Image>());
int tempInt = Inventory[newInvPos];
Inventory[newInvPos] = Inventory[curInvPos];
Inventory[newInvPos] = tempInt;
Image tempCurImage = Image.Instantiate(hotbar[curInvPos].GetComponent<Image>());
Image tempNewImage = Image.Instantiate(hotbar[newInvPos].GetComponent<Image>());
hotbar[newInvPos].GetComponent<Image>().color = tempCurImage.color;
hotbar[curInvPos].GetComponent<Image>().color = tempNewImage.color;
return true;
}
// otherwise, just place it
Inventory[newInvPos] = Inventory[curInvPos];
hotbar[newInvPos].SetActive(true);
hotbar[newInvPos].GetComponent<Image>().color = img.color;
Inventory[curInvPos] = -1;
hotbar[curInvPos].SetActive(false);
return true;
}
}
45 changes: 41 additions & 4 deletions Assets/HandMadeGame/Code/UIBoardItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -34,9 +35,45 @@ 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<Image>());
this.gameObject.SetActive(false);
int invPos = FindInventoryLocation(mouseCoords);
bool success = false;
if (invPos == -1) { // not in inventory, check board
Debug.Log("noooooo");
Vector2 boardLoc = FindTileLocation(mouseCoords);
success = ac.UpdateBoardFromBoard(boardLoc, -1, boardPos, this.gameObject.GetComponent<Image>());
} else {
success = ac.UpdateInvFromBoard(boardPos, invPos, this.gameObject.GetComponent<Image>());
}
}

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;
}

private Vector2 FindTileLocation(Vector2 mouseCoords) {
Expand Down
40 changes: 38 additions & 2 deletions Assets/HandMadeGame/Code/UiInvItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.UpdateBoard(boardLoc, invIndex, this.gameObject.GetComponent<Image>());
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<Image>());
} else {
success = ac.UpdateInvFromInv(invIndex, invPos, this.gameObject.GetComponent<Image>());
}
}

private Vector2 FindTileLocation(Vector2 mouseCoords) {
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 4492136

Please sign in to comment.