Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more control over player spawning #626

Merged
merged 3 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Assets/Mirage/Runtime/PlayerSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mirage
{

/// <summary>
/// Spawns a player as soon as the connection is authenticated
/// Spawns a player as soon as the connection is authenticated
/// </summary>
public class PlayerSpawner : MonoBehaviour
{
Expand All @@ -26,6 +26,11 @@ public class PlayerSpawner : MonoBehaviour
[FormerlySerializedAs("playerPrefab")]
public NetworkIdentity PlayerPrefab;

/// <summary>
/// Opertionally delay the spawn.
Copy link
Contributor

@phodoval phodoval Feb 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't delay it, it will prevent it completely no? Maybe a mention/comment somewhere to call that new public method when this is turned off. Otherwise looks good.

paulpach marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public bool AutoSpawn = true;

// Start is called before the first frame update
public virtual void Start()
{
Expand Down Expand Up @@ -89,8 +94,13 @@ private void OnServerAuthenticated(INetworkConnection connection)
/// <param name="conn">Connection to the server.</param>
private void OnClientSceneChanged(string sceneName, SceneOperation sceneOperation)
{
if(sceneOperation == SceneOperation.Normal)
Client.Send(new AddPlayerMessage());
if (AutoSpawn && sceneOperation == SceneOperation.Normal)
RequestServerSpawnPlayer();
}

public virtual void RequestServerSpawnPlayer()
{
Client.Send(new AddPlayerMessage());
}

void OnServerAddPlayerInternal(INetworkConnection conn, AddPlayerMessage msg)
Expand Down
61 changes: 61 additions & 0 deletions Assets/Tests/Runtime/Host/PlayerSpawnerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using NUnit.Framework;
using UnityEngine;

namespace Mirage.Tests.Host
{
public class PlayerSpawnerTest : HostSetup<MockComponent>
{
AssetBundle bundle;
GameObject player;
PlayerSpawner spawner;

public override void ExtraSetup()
{
bundle = AssetBundle.LoadFromFile("Assets/Tests/Runtime/TestScene/testscene");

spawner = networkManagerGo.AddComponent<PlayerSpawner>();

spawner.Client = client;
spawner.Server = server;
spawner.SceneManager = sceneManager;
spawner.ClientObjectManager = clientObjectManager;
spawner.ServerObjectManager = serverObjectManager;

player = new GameObject();
NetworkIdentity identity = player.AddComponent<NetworkIdentity>();
spawner.PlayerPrefab = identity;

spawner.AutoSpawn = false;

spawner.Start();
}

public override void ExtraTearDown()
{
bundle.Unload(true);
Object.Destroy(player);
}

[Test]
public void DontAutoSpawnTest()
{
bool invokeAddPlayerMessage = false;
server.LocalConnection.RegisterHandler<AddPlayerMessage>(msg => invokeAddPlayerMessage = true);

sceneManager.ChangeServerScene("Assets/Mirror/Tests/Runtime/testScene.unity");

Assert.That(invokeAddPlayerMessage, Is.False);
}

[Test]
public void ManualSpawnTest()
{
bool invokeAddPlayerMessage = false;
server.LocalConnection.RegisterHandler<AddPlayerMessage>(msg => invokeAddPlayerMessage = true);

spawner.RequestServerSpawnPlayer();

Assert.That(invokeAddPlayerMessage, Is.False);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Runtime/Host/PlayerSpawnerTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.