diff --git a/Basic/2DSpaceShooter/Assets/Scenes/network.unity b/Basic/2DSpaceShooter/Assets/Scenes/network.unity index ee25ef781..2c87c0c47 100644 --- a/Basic/2DSpaceShooter/Assets/Scenes/network.unity +++ b/Basic/2DSpaceShooter/Assets/Scenes/network.unity @@ -750,6 +750,7 @@ GameObject: - component: {fileID: 872897446} - component: {fileID: 872897444} - component: {fileID: 872897445} + - component: {fileID: 872897447} m_Layer: 0 m_Name: NetworkManager m_TagString: Untagged @@ -834,7 +835,7 @@ MonoBehaviour: MaxReceiveEventsPerTickRate: 500 EventTickrate: 64 ClientConnectionBufferTimeout: 10 - ConnectionApproval: 0 + ConnectionApproval: 1 ConnectionData: EnableTimeResync: 0 TimeResyncInterval: 30 @@ -856,6 +857,23 @@ MonoBehaviour: type: {class: NullableBoolSerializable, ns: MLAPI.Configuration, asm: Unity.Multiplayer.MLAPI.Runtime} data: Value: 14735065859013011121 +--- !u!114 &872897447 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 872897443} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e23a6243f7715c44ea5d997e13ccbaee, type: 3} + m_Name: + m_EditorClassIdentifier: + m_SpawnMethod: 0 + m_SpawnPositions: + - {x: 18, y: 12, z: 0} + - {x: 33, y: -32, z: 0} + - {x: 9, y: -25, z: 0} --- !u!4 &872897449 Transform: m_ObjectHideFlags: 0 diff --git a/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs b/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs new file mode 100644 index 000000000..98f05a6a9 --- /dev/null +++ b/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using MLAPI; +using UnityEngine; +using Random = UnityEngine.Random; + +[RequireComponent(typeof(NetworkManager))] +public class RandomPositionPlayerSpawner: MonoBehaviour +{ + NetworkManager m_NetworkManager; + + int m_RoundRobinIndex = 0; + + [SerializeField] + SpawnMethod m_SpawnMethod; + + [SerializeField] + List m_SpawnPositions = new List() { Vector3.zero }; + + /// + /// Get a spawn position for a spawned object based on the spawn method. + /// + /// ?The spawn position. + /// + public Vector3 GetNextSpawnPosition() + { + switch (m_SpawnMethod) + { + case SpawnMethod.Random: + var index = Random.Range(0, m_SpawnPositions.Count); + return m_SpawnPositions[index]; + case SpawnMethod.RoundRobin: + m_RoundRobinIndex = (m_RoundRobinIndex + 1) % m_SpawnPositions.Count; + return m_SpawnPositions[m_RoundRobinIndex]; + default: + throw new NotImplementedException(); + } + } + + private void Awake() + { + var networkManager = gameObject.GetComponent(); + networkManager.ConnectionApprovalCallback += ConnectionApprovalWithRandomSpawnPos; + } + + void ConnectionApprovalWithRandomSpawnPos(byte[] payload, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback) + { + callback(true, null, true, GetNextSpawnPosition(), Quaternion.identity); + } +} + +enum SpawnMethod +{ + Random = 0, + RoundRobin = 1, +} diff --git a/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs.meta b/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs.meta new file mode 100644 index 000000000..5f25cb277 --- /dev/null +++ b/Basic/2DSpaceShooter/Assets/Scripts/RandomPositionPlayerSpawner.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e23a6243f7715c44ea5d997e13ccbaee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index cc5ece1b8..1ef631b66 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -124,7 +124,7 @@ public void TakeDamage(int amount) m_Deaths += 1; Health.Value = 100; - transform.position = Vector3.zero; + transform.position = NetworkManager.GetComponent().GetNextSpawnPosition(); GetComponent().velocity = Vector3.zero; GetComponent().angularVelocity = 0; }