Skip to content

Commit 8c34c9c

Browse files
committed
Multiplayer and level edit
1 parent 8add3ea commit 8c34c9c

24 files changed

Lines changed: 9705 additions & 1821 deletions

Assets/CameraScript.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void Update()
4141

4242
if(!canEdit) return;
4343

44-
GameObject target = gameControlScript.ClosestTile(player);
44+
GameObject target = ClosestTile(player);
4545

4646
if(target == null) return;
4747
if(!target.name.Contains("Tile")) return;
@@ -181,6 +181,23 @@ void Update()
181181
}
182182
}
183183

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;
184201
}
185202
public GameObject Build(GameObject parent, GameObject prefab, Vector3 position)
186203
{

Assets/CameraScript2.cs

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
}

Assets/CameraScript2.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GameControlScript.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public class GameControlScript : MonoBehaviour
1313
public bool ISPAUSED = false;
1414

1515
public CameraScript cameraScript;
16-
public PlayerScript playerScript;
16+
public CameraScript2 cameraScript2;
17+
public PlayerScript playerScript, player2Script;
1718
public GameObject startButton;
18-
public GameObject gameCanvas, menuCanvas, settingsCanvas;
19+
public GameObject gameCanvas, menuCanvas, settingsCanvas, levelMainContainer;
1920
public GameObject foodOrderContainer, levelContainer, verticalContainer;
2021
public GameObject levelsUI;
2122
public GameObject instructions;
@@ -24,7 +25,7 @@ public class GameControlScript : MonoBehaviour
2425
public GameObject tilePrefab, emptyTilePrefab, foodOrderPrefab;
2526
public GameObject textTemplate;
2627
public GameObject[] foodList;
27-
public GameObject player;
28+
public GameObject player, player2;
2829
public GameObject enemyPrefab, bloodPrefab, gunPrefab, bountyPrefab, knifePrefab;
2930
public Coroutine enemyDeductionMain;
3031
public int amountOfInspectors = 0;
@@ -114,9 +115,10 @@ public class GameControlScript : MonoBehaviour
114115

115116
private string[] cannotBeCookedList = {"Lettuce", "Bun"};
116117

117-
public float foodOrderCooldown = 0, enemySpawnCooldown = 0;
118+
public float foodOrderCooldown = 0, enemySpawnCooldown = 0, peaceTime = 0;
118119
public bool canServe = false;
119120
public bool toggleFoodOrder = false;
121+
public bool canPlayer2Play = false;
120122
private string[] bloodSprites = {"Blood1", "Blood2", "Blood3"};
121123

122124
public bool canBeCooked(string food)
@@ -156,29 +158,35 @@ void Start()
156158
gameCanvas.SetActive(false);
157159
menuCanvas.SetActive(true);
158160
instructions.SetActive(false);
159-
levelContainer.SetActive(false);
161+
levelMainContainer.SetActive(false);
162+
player2.transform.position = new Vector3(100,100,1);
160163
GenerateLevel(Random.Range(0,2));
161164
canServe = false;
165+
peaceTime = 25;
166+
}
167+
public void SetPlayerCount(int num)
168+
{
169+
if(num == 1) canPlayer2Play = false;
170+
else canPlayer2Play = true;
162171
}
163-
164172
public void LoadUpMenu()
165173
{
166-
levelContainer.SetActive(true);
174+
levelMainContainer.SetActive(true);
167175
verticalContainer.SetActive(false);
168176
foreach(Transform j in levelContainer.transform)
169177
Destroy(j.gameObject);
170178

171-
GameObject textClone = Instantiate(textTemplate, verticalContainer.transform);
172-
textClone.GetComponent<TMP_Text>().text = "Choose the level from here.";
173-
174-
175179
for(int i = 0; i < levels.Count; i++)
176180
{
177181
Debug.Log("Generating Level");
178182
GameObject levelClone = Instantiate(levelsUI, levelContainer.transform);
179183
levelClone.transform.GetChild(0).GetComponent<TMP_Text>().text = "Level " + (i + 1).ToString();
180184
int temp = i;
181-
levelClone.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(() => Level(temp));
185+
186+
levelClone.transform.GetChild(1).GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/LevelScreenShots/Level" + (i + 1));
187+
if(levelClone.transform.GetChild(1).GetComponent<Image>().sprite == null)
188+
Debug.Log("Images/LevelScreenShots/Level" + (i + 1));
189+
levelClone.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() => Level(temp));
182190
}
183191
}
184192
public void Level(int level)
@@ -243,9 +251,18 @@ public void StartGame(int level)
243251
startButton.SetActive(false);
244252
menuCanvas.SetActive(false);
245253
gameCanvas.SetActive(true);
254+
246255
playerScript.canMove = true;
247256
cameraScript.canEdit = true;
248257

258+
if(canPlayer2Play)
259+
{
260+
player2.transform.position = new Vector3(2,2,1);
261+
player2Script.canMove = true;
262+
cameraScript2.canEdit = true;
263+
player2Script.canGrab = true;
264+
}
265+
249266
toggleFoodOrder = false;
250267

251268
canServe = true;
@@ -294,6 +311,9 @@ void Update()
294311
}
295312
if(!ISPAUSED )
296313
{
314+
if(canServe)
315+
peaceTime -= Time.deltaTime;
316+
297317
enemySpawnCooldown -= Time.deltaTime;
298318
foodOrderCooldown -= Time.deltaTime;
299319
}
@@ -334,14 +354,14 @@ public IEnumerator enemyDeduction()
334354
{
335355
for(int i = 0; i < GameObject.FindGameObjectsWithTag("Enemy").Length; i++)
336356
review -= 0.05f;
337-
if(Random.Range(0f,2f) < 0.1f)
357+
if(Random.Range(0f,2f) < 0.1f && peaceTime <= 0)
338358
SpawnEnemy(0);
339-
if(Random.Range(0f,2.5f) < 0.1f)
359+
if(Random.Range(0f,2.5f) < 0.05f && peaceTime <= 0)
340360
SpawnEnemy(1);
341361
}
342362

343363

344-
review -= amountOfInspectors * (GameObject.FindGameObjectsWithTag("Blood").Length / 20.0f);
364+
review -= amountOfInspectors * (GameObject.FindGameObjectsWithTag("Blood").Length / 25.0f);
345365
yield return new WaitForSeconds(3f);
346366
}
347367
}

Assets/GunScript.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Update()
1818
if(transform.parent != null && !transform.parent.name.Contains("Player"))
1919
transform.localPosition = new Vector3(0,0,1);
2020

21-
if(Keyboard.current.spaceKey.wasPressedThisFrame && inventoryScript.ammo > 0 && !gameObject.tag.Contains("Loot") && transform.parent != null && transform.parent.name.Contains("Player") && name.Contains("Gun"))
21+
if((Keyboard.current.rKey.wasPressedThisFrame || Keyboard.current.hKey.wasPressedThisFrame) && inventoryScript.ammo > 0 && !gameObject.tag.Contains("Loot") && transform.parent != null && transform.parent.name.Contains("Player") && name.Contains("Gun"))
2222
{
2323
GameObject bulletClone = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
2424
GameObject target = GetClosestEnemy(gameObject);

0 commit comments

Comments
 (0)