|
@ -7,47 +7,68 @@ public class blockSpawn : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
[SerializeField] |
|
|
[SerializeField] |
|
|
public Inventory.Data[] spawnLogicList; |
|
|
public Inventory.Data[] spawnLogicList; |
|
|
public List<ClientData> ConnectedClients; |
|
|
|
|
|
|
|
|
List<ClientData> ConnectedClients; |
|
|
public ClientList clientDataList; |
|
|
public ClientList clientDataList; |
|
|
public Block[] SpawnBlocks; |
|
|
public Block[] SpawnBlocks; |
|
|
public List<Vector3> spawnedLocations; |
|
|
|
|
|
int scoreDifference = 0; |
|
|
|
|
|
float average = 0; |
|
|
|
|
|
|
|
|
List<Vector3> spawnedLocations; |
|
|
|
|
|
int min_z = -2, max_z = 2; |
|
|
|
|
|
Vector3 spawnposition = new Vector3(0, 0, 0); |
|
|
|
|
|
|
|
|
void Awake() |
|
|
void Awake() |
|
|
{ |
|
|
{ |
|
|
ConnectedClients = clientDataList.ConnectedClients; |
|
|
|
|
|
SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray(); |
|
|
|
|
|
|
|
|
ConnectedClients = clientDataList.ConnectedClients; |
|
|
spawnedLocations = new List<Vector3>(); |
|
|
spawnedLocations = new List<Vector3>(); |
|
|
} |
|
|
} |
|
|
|
|
|
public void wakeup() |
|
|
|
|
|
{ |
|
|
|
|
|
SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void Spawn() |
|
|
public void Spawn() |
|
|
{ |
|
|
{ |
|
|
getPlayerScores(); |
|
|
|
|
|
getPlayerLocations(ConnectedClients.ToArray(), 0.75f); |
|
|
|
|
|
|
|
|
|
|
|
//second spawning block only if needed
|
|
|
|
|
|
/* if (scoreDifference > average) |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
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++) |
|
|
{ |
|
|
{ |
|
|
List<ClientData> lessClients = new List<ClientData>(ConnectedClients); |
|
|
|
|
|
lessClients.RemoveAt(lessClients.Count - 1); |
|
|
|
|
|
getPlayerLocations(lessClients.ToArray(), 0.6f); |
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
spawnposition = new Vector3(Random.Range(min_x, max_x), -0.5f, Random.Range(min_z, max_z)); |
|
|
|
|
|
checkLocation(spawnposition); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void getPlayerScores() |
|
|
|
|
|
|
|
|
private void checkLocation(Vector3 spawnposition) |
|
|
{ |
|
|
{ |
|
|
ConnectedClients.Sort((b, a) => a.Lives.CompareTo(b.Lives)); |
|
|
|
|
|
int totalScores = 0; |
|
|
|
|
|
scoreDifference = ConnectedClients[0].Lives + ConnectedClients[ConnectedClients.Count-1].Lives; |
|
|
|
|
|
//Debug.Log("score difference " + scoreDifference);
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < ConnectedClients.Count; i++) |
|
|
|
|
|
|
|
|
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 |
|
|
{ |
|
|
{ |
|
|
totalScores += ConnectedClients[i].Lives; |
|
|
|
|
|
|
|
|
//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"); |
|
|
} |
|
|
} |
|
|
average = totalScores / ConnectedClients.Count; |
|
|
|
|
|
//Debug.Log("score average " + average);
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private bool checkDuplicatePosition(Vector3 spawnposition) |
|
|
private bool checkDuplicatePosition(Vector3 spawnposition) |
|
@ -67,6 +88,7 @@ public class blockSpawn : MonoBehaviour |
|
|
|
|
|
|
|
|
private bool checkValid(Vector3 spawnposition) |
|
|
private bool checkValid(Vector3 spawnposition) |
|
|
{ |
|
|
{ |
|
|
|
|
|
//is over a walkable block
|
|
|
for (int i = 0; i < SpawnBlocks.Length; i++) |
|
|
for (int i = 0; i < SpawnBlocks.Length; i++) |
|
|
{ |
|
|
{ |
|
|
if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z) |
|
|
if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z) |
|
@ -81,6 +103,7 @@ public class blockSpawn : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
GameObject prefab = Resources.Load("Logic Block") as GameObject; |
|
|
GameObject prefab = Resources.Load("Logic Block") as GameObject; |
|
|
GameObject block = Instantiate(prefab); |
|
|
GameObject block = Instantiate(prefab); |
|
|
|
|
|
spawnposition.y += 1.5f; |
|
|
|
|
|
|
|
|
int number = (int)Random.Range(1.0f, spawnLogicList.Length); |
|
|
int number = (int)Random.Range(1.0f, spawnLogicList.Length); |
|
|
block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element; |
|
|
block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element; |
|
@ -89,104 +112,4 @@ public class blockSpawn : MonoBehaviour |
|
|
Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position); |
|
|
Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position); |
|
|
spawnedLocations.Add(spawnposition); |
|
|
spawnedLocations.Add(spawnposition); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void checkLocation(Vector3 spawnposition) |
|
|
|
|
|
{ |
|
|
|
|
|
bool duplicate = false; |
|
|
|
|
|
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(clostest.position.x, clostest.position.y + 1.5f, clostest.position.z)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
//this needs to be changed
|
|
|
|
|
|
Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition)); |
|
|
|
|
|
checkLocation(new Vector3(clostest.position.x, clostest.position.y + 1.5f, clostest.position.z)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void getPlayerLocations(ClientData[] clients, float weightMultiplier) |
|
|
|
|
|
{ |
|
|
|
|
|
float[] weightings = new float[clients.Length]; |
|
|
|
|
|
if(clients.Length > 1) |
|
|
|
|
|
{ |
|
|
|
|
|
Vector3 spawnposition = new Vector3(0, 0, 0); |
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
spawnposition += clients[i].playerCharacter.CurrentBlock.VisualPosition; |
|
|
|
|
|
//Debug.Log("currentBlock.VisualPosition " + clients[i].playerCharacter.CurrentBlock.VisualPosition);
|
|
|
|
|
|
} |
|
|
|
|
|
spawnposition *= (1.0f / clients.Length); |
|
|
|
|
|
//Debug.Log("spawnposition " + spawnposition);
|
|
|
|
|
|
|
|
|
|
|
|
Vector3[] direction = new Vector3[clients.Length]; |
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
direction[i] = (spawnposition - clients[i].playerCharacter.CurrentBlock.VisualPosition); |
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log("direction " + i + " " + direction[i]);
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//weighting calculations
|
|
|
|
|
|
float[] difAvg = new float[clients.Length]; |
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
difAvg[i] = clients[i].Lives - average; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
float[] ratio = new float[clients.Length]; |
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log("difAvg[0] " + difAvg[0]);
|
|
|
|
|
|
|
|
|
|
|
|
if (difAvg[0] == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
weightings[i] = 0.5f * weightMultiplier; |
|
|
|
|
|
//Debug.Log("weightings " + i + " " + weightings[i]);
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
ratio[i] = difAvg[i] / difAvg[0]; |
|
|
|
|
|
ratio[i] /= 2.0f; |
|
|
|
|
|
ratio[i] += 0.5f; |
|
|
|
|
|
weightings[i] = ratio[i] * weightMultiplier; |
|
|
|
|
|
weightings[i] = Mathf.Clamp(weightings[i], 0, 1); |
|
|
|
|
|
//Debug.Log("weightings " + i + " " + weightings[i]);
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//multiply weightings by the direction
|
|
|
|
|
|
//take average point and add directions
|
|
|
|
|
|
for (int i = 0; i < clients.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
spawnposition += (weightings[i] * direction[i]); |
|
|
|
|
|
//Debug.Log("(weightings[i] * direction[i] " + (weightings[i] * direction[i]));
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
spawnposition += Vector3.one * 0.5f; |
|
|
|
|
|
//Debug.Log("spawnposition " + spawnposition);
|
|
|
|
|
|
//spawn first block
|
|
|
|
|
|
checkLocation(spawnposition); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
Vector2 playerOne = new Vector2(clients[0].playerCharacter.CurrentBlock.VisualPosition.x, clients[0].playerCharacter.CurrentBlock.VisualPosition.z); |
|
|
|
|
|
int xVal = (int)Random.Range(-3.0f, 3.0f) + (int)playerOne.x; |
|
|
|
|
|
int zVal = (int)Random.Range(-3.0f, 3.0f) + (int)playerOne.y; |
|
|
|
|
|
checkLocation(new Vector3(xVal, 1.0f, zVal)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |