|
|
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using Networking.Server;
-
- public class blockSpawn : MonoBehaviour
- {
- [SerializeField]
- public Inventory.Data[] spawnLogicList;
- List<ClientData> ConnectedClients;
- public ClientList clientDataList;
- public Block[] SpawnBlocks;
- List<Vector3> spawnedLocations;
- int min_z = -2, max_z = 2;
- Vector3 spawnposition = new Vector3(0, 0, 0);
-
- void Awake()
- {
- ConnectedClients = clientDataList.ConnectedClients;
- spawnedLocations = new List<Vector3>();
- }
- public void wakeup()
- {
- SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
- }
-
- public void Spawn()
- {
- /*
- Get all player locations, get leader(biggest x position value) and min value
- +2 to each value, set as min and man in random.range()
- add locations to a list to ensure no duplicates
- check not over pit or hole
- */
-
- ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x));
- int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x + 2;
- int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 2;
-
- for(int i = 0; i < 2; i++)
- {
- spawnposition = new Vector3(Random.Range(min_x, max_x), -0.5f, Random.Range(min_z, max_z));
- checkLocation(spawnposition);
- }
-
- }
-
- private void checkLocation(Vector3 spawnposition)
- {
- bool duplicate = checkDuplicatePosition(spawnposition);
- if (duplicate == false)
- {
- bool valid = checkValid(spawnposition);
- if (valid == true)
- {
- spawnBlock(spawnposition);
- }
- else
- {
- //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition));
- //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z));
- Debug.Log("Fail one");
- }
- }
- else
- {
- //this needs to be changed
- //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition));
- //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z));
- Debug.Log("Fail two");
- }
- }
-
- private bool checkDuplicatePosition(Vector3 spawnposition)
- {
- if (spawnedLocations.Count > 0)
- {
- for (int k = 0; k < spawnedLocations.Count; k++)
- {
- if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
- {
- return true;
- }
- }
- }
- return false;
- }
-
- private bool checkValid(Vector3 spawnposition)
- {
- //is over a walkable block
- for (int i = 0; i < SpawnBlocks.Length; i++)
- {
- if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z)
- {
- return true;
- }
- }
- return false;
- }
-
- private void spawnBlock(Vector3 spawnposition)
- {
- GameObject prefab = Resources.Load("Logic Block") as GameObject;
- GameObject block = Instantiate(prefab);
- spawnposition.y += 1.5f;
-
- int number = (int)Random.Range(1.0f, spawnLogicList.Length);
- block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element;
- block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = spawnLogicList[number].Count;
- block.transform.position = spawnposition;
- //Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
- spawnedLocations.Add(spawnposition);
- }
- }
|