Browse Source

Block spawning update

master
ClairePeta 5 years ago
parent
commit
764cb2c537
2 changed files with 77 additions and 71 deletions
  1. +19
    -21
      Assets/Scripts/Character.cs
  2. +58
    -50
      Assets/Scripts/blockSpawn.cs

+ 19
- 21
Assets/Scripts/Character.cs View File

@ -135,8 +135,6 @@ public class Character : MonoBehaviour
StopAnimation(animation); StopAnimation(animation);
} }
public IEnumerator RotateInDirection(Direction direction,float angles, Animation animation, float time) public IEnumerator RotateInDirection(Direction direction,float angles, Animation animation, float time)
{ {
System.Func<float, float> yFunction = null; System.Func<float, float> yFunction = null;
@ -153,7 +151,6 @@ public class Character : MonoBehaviour
private IEnumerator LerpToBlock(Vector3 target, float time, System.Func<float, float> heightOffset = null) private IEnumerator LerpToBlock(Vector3 target, float time, System.Func<float, float> heightOffset = null)
{ {
Vector3 _startPos = transform.position; Vector3 _startPos = transform.position;
Vector3 _endPos = target; Vector3 _endPos = target;
@ -161,8 +158,6 @@ public class Character : MonoBehaviour
float elapsedTime = 0; float elapsedTime = 0;
while (elapsedTime / time < 1) while (elapsedTime / time < 1)
{ {
@ -187,7 +182,6 @@ public class Character : MonoBehaviour
private IEnumerator Rotate(Direction direction, float angles, float time, System.Func<float, float> heightOffset = null) private IEnumerator Rotate(Direction direction, float angles, float time, System.Func<float, float> heightOffset = null)
{ {
int RotationDir = 0; int RotationDir = 0;
switch (direction) switch (direction)
{ {
@ -207,7 +201,6 @@ public class Character : MonoBehaviour
lastRotation = angles * RotationDir; lastRotation = angles * RotationDir;
float elapsedTime = 0; float elapsedTime = 0;
float anglePerSecond = (angles * RotationDir) / time; float anglePerSecond = (angles * RotationDir) / time;
Vector3 _startPos = transform.position; Vector3 _startPos = transform.position;
@ -229,7 +222,6 @@ public class Character : MonoBehaviour
transform.position = _startPos + Vector3.up * heightOffset(1); transform.position = _startPos + Vector3.up * heightOffset(1);
} }
public IEnumerator AnimateToPosition(Vector3 position, Animation animation, float time) public IEnumerator AnimateToPosition(Vector3 position, Animation animation, float time)
{ {
System.Func<float, float> yFunction = null; System.Func<float, float> yFunction = null;
@ -288,8 +280,7 @@ public class Character : MonoBehaviour
break; break;
} }
} }
public void respawnCharacter() public void respawnCharacter()
{ {
/* Will introduce more complex criteria for choosing where to respawn the player in future /* Will introduce more complex criteria for choosing where to respawn the player in future
@ -377,21 +368,28 @@ public class Character : MonoBehaviour
void OnTriggerEnter(Collider other) void OnTriggerEnter(Collider other)
{ {
Collectable collectable = other.GetComponentInChildren<Collectable>(); Collectable collectable = other.GetComponentInChildren<Collectable>();
blockSpawn spawn = GetComponent<blockSpawn>();
if (collectable != null) if (collectable != null)
{ {
collectable.OnCollect(this);
}
if (other.gameObject.name == "collect_sphere")
{
other.gameObject.SetActive(false);
//player.collected +=1;
//get position from average;
ClientList list = spawn.clientDataList;
float average = 0;
int livePlayerCount = 0;
foreach (ClientData data in list.ConnectedClients)
{
if(data.Lives > 0)
{
average += data.playerCharacter.transform.position.x;
livePlayerCount++;
}
}
average /= livePlayerCount;
float tosend = lives + (transform.position.x - average);
}
if (other.gameObject.name == "End Portal")
{
other.GetComponent<Collider>().enabled = false;
SceneManager.LoadScene(nextScene);
spawn.assignLogicBlock(collectable.gameObject, tosend);
collectable.OnCollect(this);
} }
} }

+ 58
- 50
Assets/Scripts/blockSpawn.cs View File

@ -6,68 +6,74 @@ using Networking.Server;
public class blockSpawn : MonoBehaviour public class blockSpawn : MonoBehaviour
{ {
[SerializeField] [SerializeField]
public Inventory.Data[] spawnLogicList;
public Inventory.Data[] StrongLogicList;
public Inventory.Data[] NormalLogicList;
public Inventory.Data[] WeakLogicList;
Inventory.Data[] listtoUse;
List<ClientData> ConnectedClients; List<ClientData> ConnectedClients;
public ClientList clientDataList; public ClientList clientDataList;
public Block[] SpawnBlocks; public Block[] SpawnBlocks;
List<Vector3> spawnedLocations; List<Vector3> spawnedLocations;
int min_z = -2, max_z = 2;
Vector3 spawnposition = new Vector3(0, 0, 0);
List<Block> possibleSpawnLocations;
int spawnNumber = 2;
void Awake() void Awake()
{ {
ConnectedClients = clientDataList.ConnectedClients; ConnectedClients = clientDataList.ConnectedClients;
spawnedLocations = new List<Vector3>(); spawnedLocations = new List<Vector3>();
possibleSpawnLocations = new List<Block>();
} }
public void wakeup() public void wakeup()
{ {
SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isCollectableSpawnable).ToArray();
} }
public void Spawn() 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
*/
//Sort play locations so leader is first
ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x)); 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;
//add two to each to set bounds
int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x + 3;
int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 2; int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 2;
for(int i = 0; i < 2; i++)
//Check points within the bounds of players
foreach(Block point in SpawnBlocks)
{ {
spawnposition = new Vector3(Random.Range(min_x, max_x), -0.5f, Random.Range(min_z, max_z));
checkLocation(spawnposition);
if(point.transform.position.x > min_x && point.transform.position.x < max_x)
{
possibleSpawnLocations.Add(point);
}
} }
//pick a random value from those available, checks the location
//then removes it to remove the possibility of duplicates
while(spawnNumber > 0)
{
int choice = Random.Range(0, possibleSpawnLocations.Count - 1);
bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position);
if (spawned == true)
{
possibleSpawnLocations.RemoveAt(choice);
spawnNumber--;
}
}
spawnNumber = 2;
} }
private void checkLocation(Vector3 spawnposition)
private bool checkLocation(Vector3 spawnposition)
{ {
bool duplicate = checkDuplicatePosition(spawnposition); bool duplicate = checkDuplicatePosition(spawnposition);
if (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(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z));
Debug.Log("Fail one");
}
spawnBlock(spawnposition);
return true;
} }
else 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");
Debug.Log("Fail to spawn - duplicate positioning");
return false;
} }
} }
@ -86,30 +92,32 @@ public class blockSpawn : MonoBehaviour
return false; 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) private void spawnBlock(Vector3 spawnposition)
{ {
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; 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; block.transform.position = spawnposition;
//Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
spawnedLocations.Add(spawnposition); spawnedLocations.Add(spawnposition);
} }
public void assignLogicBlock(GameObject block, float value)
{
if(value > 0)
{
listtoUse = WeakLogicList;
}
else if (value < 0 && value > (-10))
{
listtoUse = NormalLogicList;
}
else if (value <= (-10))
{
listtoUse = StrongLogicList;
}
int number = (int)Random.Range(0, listtoUse.Length-1);
block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = listtoUse[number].element;
block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = listtoUse[number].Count;
}
} }

Loading…
Cancel
Save