Add StickGame Assets
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 731898d70776e2341a64ea1f9494299a, type: 3}
|
||||
m_Name: GridCondition
|
||||
m_EditorClassIdentifier:
|
||||
NetworkObject: {fileID: 0}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc503f7541ebd424c94541e6a767efee
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using FishNet.Connection;
|
||||
using FishNet.Object;
|
||||
using FishNet.Observing;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Component.Observing
|
||||
{
|
||||
/// <summary>
|
||||
/// When this observer condition is placed on an object, a client must be within the specified grid accuracy to view the object.
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "FishNet/Observers/Grid Condition", fileName = "New Grid Condition")]
|
||||
public class GridCondition : ObserverCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns if the object which this condition resides should be visible to connection.
|
||||
/// </summary>
|
||||
/// <param name="connection">Connection which the condition is being checked for.</param>
|
||||
/// <param name="currentlyAdded">True if the connection currently has visibility of this object.</param>
|
||||
/// <param name="notProcessed">True if the condition was not processed. This can be used to skip processing for performance. While output as true this condition result assumes the previous ConditionMet value.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool ConditionMet(NetworkConnection connection, bool currentlyAdded, out bool notProcessed)
|
||||
{
|
||||
//If here then checks are being processed.
|
||||
notProcessed = false;
|
||||
|
||||
return connection.HashGridEntry.NearbyEntries.Contains(base.NetworkObject.HashGridEntry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How a condition is handled.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override ObserverConditionType GetConditionType() => ObserverConditionType.Timed;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Clones referenced ObserverCondition. This must be populated with your conditions settings.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override ObserverCondition Clone()
|
||||
{
|
||||
GridCondition copy = ScriptableObject.CreateInstance<GridCondition>();
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 731898d70776e2341a64ea1f9494299a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,260 @@
|
||||
using FishNet.Managing;
|
||||
using FishNet.Object;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FishNet.Component.Observing
|
||||
{
|
||||
public class GridEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// Position on the grid.
|
||||
/// </summary>
|
||||
public Vector2Int Position;
|
||||
/// <summary>
|
||||
/// This grid entry as well those neighboring it.
|
||||
/// </summary>
|
||||
public HashSet<GridEntry> NearbyEntries;
|
||||
|
||||
public GridEntry() { }
|
||||
public GridEntry(HashSet<GridEntry> nearby)
|
||||
{
|
||||
NearbyEntries = nearby;
|
||||
}
|
||||
|
||||
public void SetValues(Vector2Int position, HashSet<GridEntry> nearby)
|
||||
{
|
||||
Position = position;
|
||||
NearbyEntries = nearby;
|
||||
}
|
||||
public void SetValues(HashSet<GridEntry> nearby)
|
||||
{
|
||||
NearbyEntries = nearby;
|
||||
}
|
||||
public void SetValues(Vector2Int position)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Position = Vector2Int.zero;
|
||||
NearbyEntries.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public class HashGrid : MonoBehaviour
|
||||
{
|
||||
#region Types.
|
||||
public enum GridAxes : byte
|
||||
{
|
||||
XY = 0,
|
||||
YZ = 1,
|
||||
XZ = 2,
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal.
|
||||
/// <summary>
|
||||
/// Value for when grid position is not set.
|
||||
/// </summary>
|
||||
internal static Vector2Int UnsetGridPosition = (Vector2Int.one * int.MaxValue);
|
||||
/// <summary>
|
||||
/// An empty grid entry.
|
||||
/// </summary>
|
||||
internal static GridEntry EmptyGridEntry = new GridEntry(new HashSet<GridEntry>());
|
||||
#endregion
|
||||
|
||||
#region Serialized.
|
||||
/// <summary>
|
||||
/// Axes of world space to base the grid on.
|
||||
/// </summary>
|
||||
[Tooltip("Axes of world space to base the grid on.")]
|
||||
[SerializeField]
|
||||
private GridAxes _gridAxes = GridAxes.XY;
|
||||
/// <summary>
|
||||
/// Accuracy of the grid. Objects will be considered nearby if they are within this number of units. Lower values may be more expensive.
|
||||
/// </summary>
|
||||
[Tooltip("Accuracy of the grid. Objects will be considered nearby if they are within this number of units. Lower values may be more expensive.")]
|
||||
[Range(1, ushort.MaxValue)]
|
||||
[SerializeField]
|
||||
private ushort _accuracy = 10;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Half of accuracy.
|
||||
/// </summary>
|
||||
private int _halfAccuracy;
|
||||
/// <summary>
|
||||
/// Cache of List<GridEntry>.
|
||||
/// </summary>
|
||||
private Stack<HashSet<GridEntry>> _gridEntryHashSetCache = new Stack<HashSet<GridEntry>>();
|
||||
/// <summary>
|
||||
/// Cache of GridEntrys.
|
||||
/// </summary>
|
||||
private Stack<GridEntry> _gridEntryCache = new Stack<GridEntry>();
|
||||
/// <summary>
|
||||
/// All grid entries.
|
||||
/// </summary>
|
||||
private Dictionary<Vector2Int, GridEntry> _gridEntries = new Dictionary<Vector2Int, GridEntry>();
|
||||
/// <summary>
|
||||
/// NetworkManager this is used with.
|
||||
/// </summary>
|
||||
private NetworkManager _networkManager;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_networkManager = GetComponentInParent<NetworkManager>();
|
||||
|
||||
if (_networkManager == null)
|
||||
{
|
||||
NetworkManager.StaticLogError($"NetworkManager not found on object or within parent of {gameObject.name}. The {GetType().Name} must be placed on or beneath a NetworkManager.");
|
||||
return;
|
||||
}
|
||||
|
||||
//Make sure there is only one per networkmanager.
|
||||
if (!_networkManager.HasInstance<HashGrid>())
|
||||
{
|
||||
_halfAccuracy = Mathf.CeilToInt((float)_accuracy / 2f);
|
||||
_networkManager.RegisterInstance(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets out values to be used when creating a new GridEntry.
|
||||
/// </summary>
|
||||
private void OutputNewGridCollections(out GridEntry gridEntry, out HashSet<GridEntry> gridEntries)
|
||||
{
|
||||
const int cacheCount = 100;
|
||||
//Build caches if needed.
|
||||
if (_gridEntryHashSetCache.Count == 0)
|
||||
{
|
||||
for (int i = 0; i < cacheCount; i++)
|
||||
_gridEntryHashSetCache.Push(new HashSet<GridEntry>());
|
||||
}
|
||||
if (_gridEntryCache.Count == 0)
|
||||
{
|
||||
for (int i = 0; i < cacheCount; i++)
|
||||
_gridEntryCache.Push(new GridEntry());
|
||||
}
|
||||
|
||||
gridEntry = _gridEntryCache.Pop();
|
||||
gridEntries = _gridEntryHashSetCache.Pop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a GridEntry for position and inserts it into GridEntries.
|
||||
/// </summary>
|
||||
private GridEntry CreateGridEntry(Vector2Int position)
|
||||
{
|
||||
//Make this into a stack that populates a number of entries when empty. also populate with some in awake.
|
||||
GridEntry newEntry;
|
||||
HashSet<GridEntry> nearby;
|
||||
OutputNewGridCollections(out newEntry, out nearby);
|
||||
newEntry.SetValues(position, nearby);
|
||||
//Add to grid.
|
||||
_gridEntries[position] = newEntry;
|
||||
|
||||
//Get neighbors.
|
||||
int endX = (position.x + 1);
|
||||
int endY = (position.y + 1);
|
||||
int iterations = 0;
|
||||
for (int x = (position.x - 1); x <= endX; x++)
|
||||
{
|
||||
for (int y = (position.y - 1); y <= endY; y++)
|
||||
{
|
||||
iterations++;
|
||||
if (_gridEntries.TryGetValue(new Vector2Int(x, y), out GridEntry foundEntry))
|
||||
{
|
||||
nearby.Add(foundEntry);
|
||||
foundEntry.NearbyEntries.Add(newEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newEntry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets grid positions and neighbors for a NetworkObject.
|
||||
/// </summary>
|
||||
internal void GetNearbyHashGridPositions(NetworkObject nob, ref HashSet<Vector2Int> collection)
|
||||
{
|
||||
Vector2Int position = GetHashGridPosition(nob);
|
||||
//Get neighbors.
|
||||
int endX = (position.x + 1);
|
||||
int endY = (position.y + 1);
|
||||
for (int x = (position.x - 1); x < endX; x++)
|
||||
{
|
||||
for (int y = (position.y - 1); y < endY; y++)
|
||||
collection.Add(new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the grid position to use for a NetworkObjects current position.
|
||||
/// </summary>
|
||||
internal Vector2Int GetHashGridPosition(NetworkObject nob)
|
||||
{
|
||||
Vector3 position = nob.transform.position;
|
||||
float fX;
|
||||
float fY;
|
||||
if (_gridAxes == GridAxes.XY)
|
||||
{
|
||||
fX = position.x;
|
||||
fY = position.y;
|
||||
}
|
||||
else if (_gridAxes == GridAxes.XZ)
|
||||
{
|
||||
fX = position.x;
|
||||
fY = position.z;
|
||||
}
|
||||
else if (_gridAxes == GridAxes.YZ)
|
||||
{
|
||||
fX = position.y;
|
||||
fY = position.z;
|
||||
}
|
||||
else
|
||||
{
|
||||
_networkManager?.LogError($"GridAxes of {_gridAxes.ToString()} is not handled.");
|
||||
return default;
|
||||
}
|
||||
|
||||
return new Vector2Int(
|
||||
(int)fX / _halfAccuracy
|
||||
, (int)fY / _halfAccuracy
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a GridEntry for a NetworkObject, creating the entry if needed.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal GridEntry GetGridEntry(NetworkObject nob)
|
||||
{
|
||||
Vector2Int pos = GetHashGridPosition(nob);
|
||||
return GetGridEntry(pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a GridEntry for position, creating the entry if needed.
|
||||
/// </summary>
|
||||
internal GridEntry GetGridEntry(Vector2Int position)
|
||||
{
|
||||
GridEntry result;
|
||||
if (!_gridEntries.TryGetValue(position, out result))
|
||||
result = CreateGridEntry(position);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a9fc3aafb02eb74fb571c300f846bf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user