Commit cb5cf6e8 by Philipp Adolf

Randomly spawn players on map

Place Start_Position objects on the map, players are spawned at a randomly choosen one.
parent 50c74286
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
<Levels> <Levels>
<Developer StartLevel="0" /> <Developer StartLevel="0" />
<Level> <Level>
<Item prefab="Player One" x="-5" /> <Item prefab="Start_Position" x="5" />
<Item prefab="Player Two" x="5" /> <Item prefab="Start_Position" x="-5" />
<Item prefab="Start_Position" />
<Item prefab="ground" y="-1.5" scalex="22.78" /> <Item prefab="ground" y="-1.5" scalex="22.78" />
<Item prefab="ground" x="-7.5" y="-0.38" rot="90" scalex="30" /> <Item prefab="ground" x="-7.5" y="-0.38" rot="90" scalex="30" />
<Item prefab="ground" x="7.5" y="-0.38" rot="90" scalex="30" /> <Item prefab="ground" x="7.5" y="-0.38" rot="90" scalex="30" />
......
fileFormatVersion: 2
guid: d0e95f1c653856b409b235f9de3f6493
timeCreated: 1433514922
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
...@@ -6,6 +6,7 @@ using UnityEngine.UI; ...@@ -6,6 +6,7 @@ using UnityEngine.UI;
public class GameMaster : MonoBehaviour { public class GameMaster : MonoBehaviour {
DeserializedLevelsLoader levelLoader; DeserializedLevelsLoader levelLoader;
public PlayerControl[] playerPrefabs;
public GameObject gameOverPanel; public GameObject gameOverPanel;
public Text rankingText; public Text rankingText;
...@@ -13,6 +14,50 @@ public class GameMaster : MonoBehaviour { ...@@ -13,6 +14,50 @@ public class GameMaster : MonoBehaviour {
void Start () { void Start () {
levelLoader = new DeserializedLevelsLoader(); levelLoader = new DeserializedLevelsLoader();
levelLoader.loadLevel(); levelLoader.loadLevel();
deleteAllPlayers();
spawnPlayers(2);
}
private void deleteAllPlayers()
{
foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player"))
Object.Destroy(player, 0f);
}
private void spawnPlayers(int count)
{
Debug.Log("spawning " + count + " players");
GameObject[] startPositions = GameObject.FindGameObjectsWithTag("Start_Position");
if (count > startPositions.Length)
{
count = startPositions.Length;
Debug.LogError("map is capped at " + count + " players");
}
shuffle(startPositions);
for (int i = 0; i < count; i++)
{
PlayerControl player = (PlayerControl) Object.Instantiate(
playerPrefabs[i % playerPrefabs.Length],
startPositions[i].transform.position,
Quaternion.identity);
player.init(i + 1);
}
}
private static void shuffle<T>(T[] array)
{
int i = array.Length;
while (i > 1)
{
int k = Random.Range(0, i--);
T t = array[k];
array[k] = array[i];
array[i] = t;
}
} }
public void gameOver(List<string> ranking) public void gameOver(List<string> ranking)
......
...@@ -60,8 +60,6 @@ public class PlayerControl : MonoBehaviour ...@@ -60,8 +60,6 @@ public class PlayerControl : MonoBehaviour
void Start() void Start()
{ {
body2D = GetComponent<Rigidbody2D>(); body2D = GetComponent<Rigidbody2D>();
init(playerNumber);
} }
public void init(int playerNumber) public void init(int playerNumber)
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment