Basic Networking done

This commit is contained in:
2022-01-26 19:10:12 +05:30
parent d852b2bb2c
commit 891318680d
1111 changed files with 105855 additions and 780 deletions

View File

@@ -0,0 +1,51 @@
using UnityEngine;
namespace Mirror.Examples.Benchmark
{
public class BenchmarkNetworkManager : NetworkManager
{
[Header("Spawns")]
public GameObject spawnPrefab;
public int spawnAmount = 5000;
public float interleave = 1;
void SpawnAll()
{
// calculate sqrt so we can spawn N * N = Amount
float sqrt = Mathf.Sqrt(spawnAmount);
// calculate spawn xz start positions
// based on spawnAmount * distance
float offset = -sqrt / 2 * interleave;
// spawn exactly the amount, not one more.
int spawned = 0;
for (int spawnX = 0; spawnX < sqrt; ++spawnX)
{
for (int spawnZ = 0; spawnZ < sqrt; ++spawnZ)
{
// spawn exactly the amount, not any more
// (our sqrt method isn't 100% precise)
if (spawned < spawnAmount)
{
// instantiate & position
GameObject go = Instantiate(spawnPrefab);
float x = offset + spawnX * interleave;
float z = offset + spawnZ * interleave;
go.transform.position = new Vector3(x, 0, z);
// spawn
NetworkServer.Spawn(go);
++spawned;
}
}
}
}
public override void OnStartServer()
{
base.OnStartServer();
SpawnAll();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f0f6e2c4566084948a433ee5285d3fb7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using UnityEngine;
namespace Mirror.Examples.Benchmark
{
public class MonsterMovement : NetworkBehaviour
{
public float speed = 1;
public float movementProbability = 0.5f;
public float movementDistance = 20;
bool moving;
Vector3 start;
Vector3 destination;
public override void OnStartServer()
{
start = transform.position;
}
[ServerCallback]
void Update()
{
if (moving)
{
if (Vector3.Distance(transform.position, destination) <= 0.01f)
{
moving = false;
}
else
{
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
}
}
else
{
float r = Random.value;
if (r < movementProbability * Time.deltaTime)
{
Vector2 circlePos = Random.insideUnitCircle;
Vector3 dir = new Vector3(circlePos.x, 0, circlePos.y);
Vector3 dest = transform.position + dir * movementDistance;
// within move dist around start?
// (don't want to wander off)
if (Vector3.Distance(start, dest) <= movementDistance)
{
destination = dest;
moving = true;
}
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9cddc2e496c474e538a494465be0192a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using UnityEngine;
namespace Mirror.Examples.Benchmark
{
public class PlayerMovement : NetworkBehaviour
{
public float speed = 5;
void Update()
{
if (!isLocalPlayer) return;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(h, 0, v);
transform.position += dir.normalized * (Time.deltaTime * speed);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c482338c8cc6d4a3cba81934c0151972
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: