Add StickGame Assets
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using FishNet.Managing.Scened;
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Demo.AdditiveScenes
|
||||
{
|
||||
public class LevelLoader : NetworkBehaviour
|
||||
{
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!base.IsServer)
|
||||
return;
|
||||
|
||||
Player player = GetPlayerOwnedObject(other);
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
/* Create a lookup handle using this objects scene.
|
||||
* This is one of many ways FishNet knows what scene to load
|
||||
* for the clients. */
|
||||
SceneLookupData lookupData = new SceneLookupData(gameObject.scene);
|
||||
SceneLoadData sld = new SceneLoadData(lookupData)
|
||||
{
|
||||
/* Set automatically unload to false
|
||||
* so the server does not unload this scene when
|
||||
* there are no more connections in it. */
|
||||
Options = new LoadOptions()
|
||||
{
|
||||
AutomaticallyUnload = false
|
||||
},
|
||||
/* Also move the client object to the new scene.
|
||||
* This step is not required but may be desirable. */
|
||||
MovedNetworkObjects = new NetworkObject[] { player.NetworkObject },
|
||||
//Load scenes as additive.
|
||||
ReplaceScenes = ReplaceOption.None,
|
||||
//Set the preferred active scene so the client changes active scenes.
|
||||
PreferredActiveScene = lookupData,
|
||||
};
|
||||
|
||||
base.SceneManager.LoadConnectionScenes(player.Owner, sld);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (!base.IsServer)
|
||||
return;
|
||||
|
||||
Player player = GetPlayerOwnedObject(other);
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
/* Create a lookup handle using this objects scene.
|
||||
* This is one of many ways FishNet knows what scene to load
|
||||
* for the clients. */
|
||||
SceneLookupData lookupData = new SceneLookupData(gameObject.scene);
|
||||
/* Tell server to keep unused when unloading. This will keep
|
||||
* the scene even if there are no connections.
|
||||
* This varies from AutomaticallyUnload slightly;
|
||||
* automatically unload will remove the scene on the server
|
||||
* if there are no more connections, such as if players
|
||||
* were to disconnect. But when manually telling a scene to
|
||||
* unload you must tell the server to keep it even if unused,
|
||||
* if that is your preference. */
|
||||
SceneUnloadData sud = new SceneUnloadData(lookupData)
|
||||
{
|
||||
Options = new UnloadOptions()
|
||||
{
|
||||
Mode = UnloadOptions.ServerUnloadMode.KeepUnused
|
||||
}
|
||||
};
|
||||
|
||||
base.SceneManager.UnloadConnectionScenes(player.Owner, sud);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Player script if the object is a player.
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
private Player GetPlayerOwnedObject(Collider other)
|
||||
{
|
||||
/* When an object exits this trigger unload the level for the client. */
|
||||
Player player = other.GetComponent<Player>();
|
||||
//Not the player object.
|
||||
if (player == null)
|
||||
return null;
|
||||
//No owner??
|
||||
if (!player.Owner.IsActive)
|
||||
return null;
|
||||
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02e5160f1a9b4d047a6cbdb3f094a86d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using FishNet.Connection;
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Demo.AdditiveScenes
|
||||
{
|
||||
public class Player : NetworkBehaviour
|
||||
{
|
||||
|
||||
[SerializeField]
|
||||
private Transform _ownerObjects;
|
||||
[SerializeField]
|
||||
private float _moveRate = 2f;
|
||||
|
||||
private List<Waypoint> _wayPoints = new List<Waypoint>();
|
||||
private int _goalIndex;
|
||||
private Vector3 _goalOffset;
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
_wayPoints = GameObject.FindObjectsOfType<Waypoint>().ToList();
|
||||
/* Stagger spawn position slightly depending on player count.
|
||||
* Also inverse direction so players cross each other when more
|
||||
* than one. This is just demo fanciness. */
|
||||
if (base.ServerManager.Clients.Count % 2 == 0)
|
||||
{
|
||||
_goalOffset = new Vector3(-0.5f, 0f, 0f);
|
||||
_wayPoints = _wayPoints.OrderBy(x => x.WaypointIndex).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
_goalOffset = new Vector3(0.5f, 0f, 0f);
|
||||
_wayPoints = _wayPoints.OrderByDescending(x => x.WaypointIndex).ToList();
|
||||
}
|
||||
|
||||
//Snap to current waypoint.
|
||||
transform.position = _wayPoints[0].transform.position + _goalOffset;
|
||||
//Set goal to next waypoint.
|
||||
_goalIndex = 1;
|
||||
}
|
||||
|
||||
public override void OnOwnershipClient(NetworkConnection prevOwner)
|
||||
{
|
||||
_ownerObjects.gameObject.SetActive(base.IsOwner);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//Not server or not setup.
|
||||
if (!base.IsServer)
|
||||
return;
|
||||
if (_wayPoints.Count == 0)
|
||||
return;
|
||||
if (_goalIndex >= _wayPoints.Count)
|
||||
return;
|
||||
|
||||
Vector3 posGoal = _wayPoints[_goalIndex].transform.position + _goalOffset;
|
||||
transform.position = Vector3.MoveTowards(transform.position, posGoal, _moveRate * Time.deltaTime);
|
||||
|
||||
Vector3 lookDirection = (posGoal - transform.position).normalized;
|
||||
//Rotate to goal if there is a look direction.
|
||||
if (lookDirection != Vector3.zero)
|
||||
{
|
||||
Quaternion rot = Quaternion.LookRotation((posGoal - transform.position).normalized, transform.up);
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, 270f * Time.deltaTime);
|
||||
}
|
||||
|
||||
//If at goal set next goal.
|
||||
if (transform.position == posGoal)
|
||||
{
|
||||
_goalIndex++;
|
||||
//Reset index to 0 if at last goal.
|
||||
if (_goalIndex >= _wayPoints.Count)
|
||||
_goalIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8541d1cca4da7043b84a0d563939dea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using FishNet.Managing.Scened;
|
||||
using FishNet.Object;
|
||||
using GameKit.Utilities.Types;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace FishNet.Demo.AdditiveScenes
|
||||
{
|
||||
|
||||
public class ServerScenePrewarmer : NetworkBehaviour
|
||||
{
|
||||
[SerializeField, Scene]
|
||||
private string[] _scenes = new string[0];
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
/* Load all the needed scenes at once.
|
||||
* This is not really required in most situations
|
||||
* but the server uses waypoints from each scene
|
||||
* to move the players. The waypoints won't exist unless
|
||||
* all scenes do. This can be seen in the Player
|
||||
* script. */
|
||||
|
||||
/* Load scenes using the FishNet SceneManager, with automaticallyUnload
|
||||
* as false. This will keep the scenes from unloading when a client
|
||||
* disconnects or leaves the scene. */
|
||||
foreach (string item in _scenes)
|
||||
{
|
||||
SceneLookupData lookupData = new SceneLookupData(item);
|
||||
SceneLoadData sld = new SceneLoadData(lookupData)
|
||||
{
|
||||
Options = new LoadOptions
|
||||
{
|
||||
AutomaticallyUnload = false
|
||||
},
|
||||
};
|
||||
|
||||
base.SceneManager.LoadConnectionScenes(sld);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b06d5bb8c7e0da04581df0b9454b81b7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Demo.AdditiveScenes
|
||||
{
|
||||
|
||||
public class Waypoint : MonoBehaviour
|
||||
{
|
||||
public byte WaypointIndex;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70028e1b1708627408637ab9a3cd6bd4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user