Skip to content

Commit

Permalink
Merge pull request #21 from npackard/main
Browse files Browse the repository at this point in the history
improvements to arrangement UI/logic, can now replace items w/ items from hotbar
  • Loading branch information
npackard authored Sep 16, 2024
2 parents 4020c98 + f586153 commit 90905f5
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 28 deletions.
52 changes: 45 additions & 7 deletions Assets/HandMadeGame/Code/ArrangementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,50 @@ private void HideGrid() {
grid.SetActive(false);
}

public void UpdateBoard(Vector2 pos, int invPos, Image img) {
Debug.Log(Board[(int)pos[0], (int)pos[1]]);
Board[(int)pos[0], (int)pos[1]] = Inventory[invPos];
Inventory[invPos] = -1;
int internalPos = (int)pos[1] + (3 * (int)pos[0]);
internalDisplay[internalPos].SetActive(true);
internalDisplay[internalPos].GetComponent<Image>().color = img.color;
public bool UpdateBoard(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
if (Board[(int)pos[0], (int)pos[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)pos[1] + (3 * (int)pos[0]);
Image temp = Image.Instantiate(hotbar[emptyInvIndex].GetComponent<Image>());
hotbar[emptyInvIndex].GetComponent<Image>().color = internalDisplay[intPos].GetComponent<Image>().color;
int temp2 = Inventory[emptyInvIndex];
Inventory[emptyInvIndex] = Board[(int)pos[0], (int)pos[1]];
Inventory[invPos] = Board[(int)pos[0], (int)pos[1]];
Board[(int)pos[0], (int)pos[1]] = temp2;
internalDisplay[intPos].SetActive(true);
internalDisplay[intPos].GetComponent<Image>().color = temp.color;
return true;
} else {
hotbar[emptyInvIndex].SetActive(true);
int internalPos = (int)pos[1] + (3 * (int)pos[0]);
hotbar[emptyInvIndex].GetComponent<Image>().color = internalDisplay[internalPos].GetComponent<Image>().color;
Inventory[emptyInvIndex] = Board[(int)pos[0], (int)pos[1]];
Board[(int)pos[0], (int)pos[1]] = Inventory[invPos];
Inventory[invPos] = -1;
internalDisplay[internalPos].SetActive(true);
internalDisplay[internalPos].GetComponent<Image>().color = img.color;
hotbar[invPos].SetActive(false);
return true;
}
} else {
Board[(int)pos[0], (int)pos[1]] = Inventory[invPos];
Inventory[invPos] = -1;
int internalPosAgain = (int)pos[1] + (3 * (int)pos[0]);
internalDisplay[internalPosAgain].SetActive(true);
internalDisplay[internalPosAgain].GetComponent<Image>().color = img.color;
hotbar[invPos].SetActive(false);
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UiItemController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
public class UIBoardItemController : MonoBehaviour
{
public ArrangementController ac; // I realize this is jank, will polish later

Expand All @@ -18,7 +18,7 @@ public class UiItemController : MonoBehaviour, IPointerDownHandler, IPointerUpHa
// Start is called before the first frame update
void Start()
{
Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto);

}

// Update is called once per frame
Expand All @@ -30,9 +30,8 @@ void Update()
public void OnPointerDown(PointerEventData pointerEventData) {
Cursor.SetCursor(grabbyCursor, grabbyOffset, CursorMode.Auto);
}

public void OnPointerUp(PointerEventData pointerEventData) {
Debug.Log("what the fajita");
Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto);
Vector2 mouseCoords = Input.mousePosition;
Vector2 boardLoc = FindTileLocation(mouseCoords);
Expand All @@ -41,7 +40,6 @@ public void OnPointerUp(PointerEventData pointerEventData) {
}

private Vector2 FindTileLocation(Vector2 mouseCoords) {
Debug.Log("what the taco");
float relativeX = mouseCoords[0];
float relativeY = mouseCoords[1];
Vector2 boardPos = new Vector2(-1, -1);
Expand All @@ -52,14 +50,10 @@ private Vector2 FindTileLocation(Vector2 mouseCoords) {
// find col
if (relativeX < .45 * Screen.width) {
boardPos = new Vector2(boardPos[0], 0);
Debug.Log("what is happening");
Debug.Log(0);
} else if (relativeX < .55 * Screen.width) {
boardPos = new Vector2(boardPos[0], 1);
Debug.Log(1);
} else if (relativeX < .65 * Screen.width) {
boardPos = new Vector2(boardPos[0], 2);
Debug.Log(2);
} else {
boardPos = new Vector2(boardPos[0], -1);
Debug.Log("you broke it. good job.");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions Assets/HandMadeGame/Code/UiInvItemController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UiInvItemController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public ArrangementController ac; // I realize this is jank, will polish later

public Texture2D normalCursor;
public Texture2D grabbyCursor;

public Vector2 grabbyOffset = Vector2.zero;

public int invIndex;

// Start is called before the first frame update
void Start()
{
Cursor.SetCursor(normalCursor, Vector2.zero, CursorMode.Auto);
}

// Update is called once per frame
void Update()
{

}

public void OnPointerDown(PointerEventData pointerEventData) {
Cursor.SetCursor(grabbyCursor, grabbyOffset, CursorMode.Auto);
}

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

private Vector2 FindTileLocation(Vector2 mouseCoords) {
float relativeX = mouseCoords[0];
float relativeY = mouseCoords[1];
Vector2 boardPos = new Vector2(-1, -1);
// first, make sure on board
if (relativeX < Screen.width / 3 || relativeX > 2 * Screen.width / 3 || relativeY < .314 * Screen.height || relativeY > .87 * Screen.height) {
return boardPos;
}
// find col
if (relativeX < .45 * Screen.width) {
boardPos = new Vector2(boardPos[0], 0);
} else if (relativeX < .55 * Screen.width) {
boardPos = new Vector2(boardPos[0], 1);
} else if (relativeX < .65 * Screen.width) {
boardPos = new Vector2(boardPos[0], 2);
} else {
boardPos = new Vector2(boardPos[0], -1);
Debug.Log("you broke it. good job.");
}
// find row
if (relativeY < Screen.height / 2) {
boardPos = new Vector2(2, boardPos[1]);
} else if (relativeY < .68 * Screen.height) {
boardPos = new Vector2(1, boardPos[1]);
} else if (relativeY < .86 * Screen.height) {
boardPos = new Vector2(0, boardPos[1]);
} else {
boardPos = new Vector2(-1, boardPos[1]);
Debug.Log("wow. you still broke it. i feel attacked.");
}
return boardPos;
}
}
11 changes: 11 additions & 0 deletions Assets/HandMadeGame/Code/UiInvItemController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 41 additions & 11 deletions Assets/Scenes/GameflowCopiedForGrid.unity
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ MonoBehaviour:
m_GameObject: {fileID: 11601873}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -385,7 +385,7 @@ MonoBehaviour:
m_GameObject: {fileID: 78828385}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -478,7 +478,7 @@ MonoBehaviour:
m_GameObject: {fileID: 79469934}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -571,7 +571,7 @@ MonoBehaviour:
m_GameObject: {fileID: 99728926}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ MonoBehaviour:
m_GameObject: {fileID: 213238441}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -1132,7 +1132,7 @@ MonoBehaviour:
m_GameObject: {fileID: 222354248}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -1328,8 +1328,17 @@ MonoBehaviour:
- {fileID: 37091590}
- {fileID: 1893078986}
- {fileID: 1826728676}
Inventory: 000000000100000002000000030000000400000005000000060000000700000008000000
hotbar: []
Inventory: 010000000200000003000000040000000500000006000000070000000800000009000000
hotbar:
- {fileID: 459920592}
- {fileID: 222354248}
- {fileID: 1836543077}
- {fileID: 78828385}
- {fileID: 213238441}
- {fileID: 79469934}
- {fileID: 1768954753}
- {fileID: 11601873}
- {fileID: 99728926}
--- !u!1 &400972227
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1648,7 +1657,7 @@ MonoBehaviour:
m_GameObject: {fileID: 459920592}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -2306,7 +2315,9 @@ MonoBehaviour:
m_EditorClassIdentifier:
Inventory: []
Reputation: 0
WinReputation: 3
DialogueController: {fileID: 1612036605}
InfoPopupController: {fileID: 0}
ArrangementModeController: {fileID: 400214483}
ShowDebugger: 1
--- !u!4 &911570137
Expand Down Expand Up @@ -3301,6 +3312,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
DialogueController: {fileID: 1612036605}
ControllerPrompt: {fileID: 951513879}
InfoPopupController: {fileID: 0}
Snel: {fileID: 21300000, guid: 72bfa0719427ef14f943674560e9cecd, type: 3}
Snel2: {fileID: 21300000, guid: 6494f05a182e28a43a340338bbefb9d9, type: 3}
--- !u!4 &1356150163
Expand Down Expand Up @@ -3808,6 +3820,7 @@ GameObject:
- component: {fileID: 1702595148}
- component: {fileID: 1702595150}
- component: {fileID: 1702595149}
- component: {fileID: 1702595151}
m_Layer: 5
m_Name: Item1 (1)
m_TagString: Untagged
Expand Down Expand Up @@ -3872,6 +3885,23 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1702595147}
m_CullTransparentMesh: 1
--- !u!114 &1702595151
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1702595147}
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
--- !u!1 &1708591712
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -4152,7 +4182,7 @@ MonoBehaviour:
m_GameObject: {fileID: 1768954753}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down Expand Up @@ -4320,7 +4350,7 @@ MonoBehaviour:
m_GameObject: {fileID: 1836543077}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7d0f908edfef924ab51c6b65beecc3d, type: 3}
m_Script: {fileID: 11500000, guid: 66b0573c64250a14e8a4b5db5bcf3f6e, type: 3}
m_Name:
m_EditorClassIdentifier:
ac: {fileID: 400214485}
Expand Down

0 comments on commit 90905f5

Please sign in to comment.