in GameLiftExampleUnityProject/Assets/Scripts/Server/Server.cs [112:175]
private void ProcessMessages()
{
// Go through any messages we received to process
foreach (SimpleMessage msg in messagesToProcess)
{
// Spawn player
if (msg.messageType == MessageType.Spawn)
{
Debug.Log("Player spawned: " + msg.float1 + "," + msg.float2 + "," + msg.float3);
NetworkPlayer player = new NetworkPlayer(msg.clientId);
this.players.Add(player);
player.Spawn(msg, this.playerPrefab);
player.SetPlayerId(msg.clientId);
// Send all existing player positions to the newly joined
for (int i = 0; i < this.players.Count-1; i++)
{
var otherPlayer = this.players[i];
// Send state
var positionMessage = otherPlayer.GetPositionMessage(overrideChangedCheck: true);
if (positionMessage != null)
{
positionMessage.clientId = otherPlayer.GetPlayerId();
this.server.SendMessage(player.GetPlayerId(), positionMessage);
}
}
}
// Set player input
if (msg.messageType == MessageType.PlayerInput)
{
// Only handle input if the player exists
if (this.PlayerExists(msg.clientId))
{
Debug.Log("Player moved: " + msg.float1 + "," + msg.float2 + " ID: " + msg.clientId);
if (this.PlayerExists(msg.clientId))
{
var player = this.GetPlayer(msg.clientId);
player.SetInput(msg);
}
else
{
Debug.Log("PLAYER MOVED BUT IS NOT SPAWNED! SPAWN TO RANDOM POS");
Vector3 spawnPos = new Vector3(UnityEngine.Random.Range(-5, 5), 1, UnityEngine.Random.Range(-5, 5));
var quat = Quaternion.identity;
SimpleMessage tmpMsg = new SimpleMessage(MessageType.Spawn);
tmpMsg.SetFloats(spawnPos.x, spawnPos.y, spawnPos.z, quat.x, quat.y, quat.z, quat.w);
tmpMsg.clientId = msg.clientId;
NetworkPlayer player = new NetworkPlayer(msg.clientId);
this.players.Add(player);
player.Spawn(tmpMsg, this.playerPrefab);
player.SetPlayerId(msg.clientId);
}
}
else
{
Debug.Log("Player doesn't exists anymore, don't take in input: " + msg.clientId);
}
}
}
messagesToProcess.Clear();
}