|
| 1 | +using UnityEngine; |
| 2 | +using TMPro; |
| 3 | +using UnityEngine.InputSystem; |
| 4 | +using UnityEngine.EventSystems; |
| 5 | +using Unity.VisualScripting; |
| 6 | +public class CameraScript2 : MonoBehaviour |
| 7 | +{ |
| 8 | + public InventoryScript invScript; |
| 9 | + public PlayerScript playerScript; |
| 10 | + public GameControlScript gameControlScript; |
| 11 | + |
| 12 | + private Vector3 playerCameraPos = new Vector3(0, 0, -5); |
| 13 | + public float interactRange = 3f; |
| 14 | + |
| 15 | + public GameObject player; |
| 16 | + public GameObject cursorText; |
| 17 | + |
| 18 | + public bool canEdit = true; |
| 19 | + public bool isOnTile = false; |
| 20 | + public int playerNumber = 1; |
| 21 | + |
| 22 | + void Start() |
| 23 | + { |
| 24 | + transform.position = new Vector3(4.6f, 3.3f, -5); |
| 25 | + canEdit = false; |
| 26 | + } |
| 27 | + public void StartGame() |
| 28 | + { |
| 29 | + canEdit = true; |
| 30 | + } |
| 31 | + |
| 32 | + // Update is called once per frame |
| 33 | + void Update() |
| 34 | + { |
| 35 | + //transform.position = player.transform.position + playerCameraPos; |
| 36 | + |
| 37 | + //RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero); |
| 38 | + |
| 39 | + |
| 40 | + Keyboard keyboard = Keyboard.current; |
| 41 | + |
| 42 | + if(!canEdit) return; |
| 43 | + |
| 44 | + GameObject target = ClosestTile(player); |
| 45 | + |
| 46 | + if(target == null) return; |
| 47 | + if(!target.name.Contains("Tile")) return; |
| 48 | + |
| 49 | + |
| 50 | + if(Vector3.Distance(target.transform.position, player.transform.position) >= 1.75f) |
| 51 | + return; |
| 52 | + else |
| 53 | + gameControlScript.makeTileChosen(target.gameObject); |
| 54 | + |
| 55 | + //TileScript tileScript = target.GetComponent<TileScript>(); |
| 56 | + |
| 57 | + TileScript tileScript = target.GetComponent<TileScript>(); |
| 58 | + |
| 59 | + if (tileScript == null) return; |
| 60 | + |
| 61 | + string currentItem; |
| 62 | + if(invScript.inventory.Count > 0) |
| 63 | + currentItem = invScript.inventory[invScript.equippedItem - 1]; |
| 64 | + else currentItem = ""; |
| 65 | + //Taking |
| 66 | + |
| 67 | + if(invScript.inventory.Count > 0) |
| 68 | + { |
| 69 | + if(keyboard.uKey.wasPressedThisFrame && target.transform.childCount == 1) |
| 70 | + { |
| 71 | + string childItemName = target.transform.GetChild(0).name; |
| 72 | + childItemName = childItemName.Replace("(Clone)", ""); |
| 73 | + Transform itemPlate = invScript.findPlayerTargetChild(currentItem); |
| 74 | + if(currentItem.Contains("Plate") && childItemName.Contains("Chopped")) |
| 75 | + { |
| 76 | + ItemScript ps = itemPlate.GetComponent<ItemScript>(); |
| 77 | + if(ps.storage.Count == 4) |
| 78 | + return; |
| 79 | + if(ps.foodCount("Bun") > 2) return; |
| 80 | + |
| 81 | + ps.storage.Add(childItemName); |
| 82 | + |
| 83 | + Destroy(target.transform.GetChild(0).gameObject); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + else if(invScript.inventory.Count == 0) |
| 88 | + { |
| 89 | + invScript.AddItem(childItemName, null); |
| 90 | + Transform newPlate = invScript.findPlayerTargetChild(currentItem); |
| 91 | + ItemScript ps = newPlate.GetComponent<ItemScript>(); |
| 92 | + ps = itemPlate.GetComponent<ItemScript>(); |
| 93 | + Destroy(target.transform.GetChild(0).gameObject); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + //giving plate |
| 99 | + if(keyboard.uKey.wasPressedThisFrame && target.transform.childCount == 0) |
| 100 | + { |
| 101 | + Transform item = invScript.findPlayerTargetChild(currentItem); |
| 102 | + item.name = item.name.Substring(0, item.name.Length - 1); |
| 103 | + |
| 104 | + string indexFood = invScript.findFood(item.name); |
| 105 | + |
| 106 | + if(item.name.Contains("Plate")) |
| 107 | + indexFood = item.name; |
| 108 | + |
| 109 | + GameObject prefab = Resources.Load<GameObject>(indexFood); |
| 110 | + |
| 111 | + |
| 112 | + GameObject clone = Instantiate(prefab, target.transform); |
| 113 | + clone.name = item.name; |
| 114 | + clone.name = invScript.orderWords(clone.name); |
| 115 | + clone.GetComponent<BoxCollider2D>().enabled = false; |
| 116 | + |
| 117 | + if(clone.name.Contains("Plate")) |
| 118 | + clone.GetComponent<ItemScript>().storage = item.GetComponent<ItemScript>().storage; |
| 119 | + |
| 120 | + invScript.inventory.RemoveAt(invScript.equippedItem - 1); |
| 121 | + Destroy(item.gameObject); |
| 122 | + invScript.equippedItem = invScript.inventory.Count == 0 ? 1 : Mathf.Clamp(invScript.equippedItem, 1, invScript.inventory.Count); |
| 123 | + invScript.UpdatePlayerChildren(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + //giving food to plate |
| 130 | + if(keyboard.uKey.wasPressedThisFrame && target.transform.childCount == 1) |
| 131 | + { |
| 132 | + string childItemName = target.transform.GetChild(0).name; |
| 133 | + Transform item = invScript.findPlayerTargetChild(currentItem); |
| 134 | + if(childItemName.Contains("Plate") && !currentItem.Contains("Plate")) |
| 135 | + { |
| 136 | + ItemScript ps = target.GetComponentInChildren<ItemScript>(); |
| 137 | + if(ps.storage.Count == 4) |
| 138 | + return; |
| 139 | + |
| 140 | + if(ps.foodCount("Bun") > 2) return; |
| 141 | + ps.storage.Add(item.name.Replace("(Clone)", "")); |
| 142 | + invScript.inventory.RemoveAt(invScript.equippedItem - 1); |
| 143 | + Destroy(item.gameObject); |
| 144 | + } |
| 145 | + return; |
| 146 | + } |
| 147 | + } |
| 148 | + if(invScript.inventory.Count == 0) |
| 149 | + { |
| 150 | + if(keyboard.uKey.wasPressedThisFrame && target.transform.childCount > 0) |
| 151 | + { |
| 152 | + string childItemName = target.transform.GetChild(0).name; |
| 153 | + childItemName = childItemName.Replace("(Clone)", ""); |
| 154 | + Transform itemPlate = invScript.findPlayerTargetChild(currentItem); |
| 155 | + if(currentItem.Contains("Plate")) |
| 156 | + { |
| 157 | + ItemScript ps = itemPlate.GetComponent<ItemScript>(); |
| 158 | + if(ps.storage.Count == 4) |
| 159 | + return; |
| 160 | + if(ps.foodCount("Bun") > 2) return; |
| 161 | + |
| 162 | + ps.storage.Add(childItemName); |
| 163 | + } |
| 164 | + /* |
| 165 | + if(itemPlate.name.Contains("Plate")) |
| 166 | + invScript.AddItem(childItemName, target.transform.GetChild(0).GetComponent<ItemScript>()); |
| 167 | + else |
| 168 | + invScript.AddItem(childItemName);*/ |
| 169 | + |
| 170 | + invScript.AddItem(childItemName, target.transform.GetChild(0).GetComponent<ItemScript>()); |
| 171 | + Destroy(target.transform.GetChild(0).gameObject); |
| 172 | + return; |
| 173 | + } |
| 174 | + if(keyboard.uKey.wasPressedThisFrame && target.transform.childCount == 0 && invScript.inventory.Count == 0) |
| 175 | + { |
| 176 | + if(tileScript.typeOfFood != "") |
| 177 | + { |
| 178 | + invScript.AddItem(tileScript.typeOfFood, null); |
| 179 | + } |
| 180 | + return; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + public GameObject ClosestTile(GameObject currentPlayer) |
| 186 | + { |
| 187 | + GameObject closest = null; |
| 188 | + float shortestDistance = Mathf.Infinity; |
| 189 | + Vector3 playerPos = currentPlayer.transform.position; |
| 190 | + foreach (GameObject tile in GameObject.FindGameObjectsWithTag("Tile")) |
| 191 | + { |
| 192 | + float sqrDist = (tile.transform.position - playerPos).sqrMagnitude; |
| 193 | + |
| 194 | + if (sqrDist < shortestDistance) |
| 195 | + { |
| 196 | + shortestDistance = sqrDist; |
| 197 | + closest = tile; |
| 198 | + } |
| 199 | + } |
| 200 | + return closest; |
| 201 | + } |
| 202 | + public GameObject Build(GameObject parent, GameObject prefab, Vector3 position) |
| 203 | + { |
| 204 | + if (parent.name.Contains("Tile")) |
| 205 | + parent.GetComponent<TileScript>().isFull = true; |
| 206 | + |
| 207 | + GameObject obj = Instantiate(prefab, position, Quaternion.identity); |
| 208 | + obj.transform.SetParent(parent.transform, false); |
| 209 | + obj.transform.localPosition = position; |
| 210 | + obj.name = prefab.name; |
| 211 | + return obj; |
| 212 | + } |
| 213 | +} |
0 commit comments