-
-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(HexSpatialHash): Renamed example scripts
- Loading branch information
1 parent
e217341
commit a7d367e
Showing
6 changed files
with
117 additions
and
113 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Assets/Mirror/Examples/HexSpatialHash/Scripts/Hex3DNetworkManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Mirror.Examples.Hex3D | ||
{ | ||
[AddComponentMenu("")] | ||
public class Hex3DNetworkManager : NetworkManager | ||
{ | ||
// Overrides the base singleton so we don't have to cast to this type everywhere. | ||
public static new Hex3DNetworkManager singleton => (Hex3DNetworkManager)NetworkManager.singleton; | ||
|
||
[Header("Spawns")] | ||
public GameObject spawnPrefab; | ||
|
||
[Range(1, 8000)] | ||
public ushort spawnPrefabsCount = 1000; | ||
|
||
[Range(1, 10)] | ||
public byte spawnPrefabSpacing = 3; | ||
|
||
public override void OnValidate() | ||
{ | ||
if (Application.isPlaying) return; | ||
base.OnValidate(); | ||
|
||
// Adjust spawnPrefabsCount to have an even cube root | ||
ushort cubeRoot = (ushort)Mathf.Pow(spawnPrefabsCount, 1f / 3f); | ||
spawnPrefabsCount = (ushort)(Mathf.Pow(cubeRoot, 3f)); | ||
} | ||
|
||
public override void OnStartClient() | ||
{ | ||
NetworkClient.RegisterPrefab(spawnPrefab); | ||
} | ||
|
||
public override void OnStartServer() | ||
{ | ||
// instantiate an empty GameObject | ||
GameObject Spawns = new GameObject("Spawns"); | ||
Transform SpawnsTransform = Spawns.transform; | ||
|
||
int spawned = 0; | ||
|
||
// Spawn prefabs in a cube grid centered around origin (0,0,0) | ||
float cubeRoot = Mathf.Pow(spawnPrefabsCount, 1f / 3f); | ||
int gridSize = Mathf.RoundToInt(cubeRoot); | ||
|
||
// Calculate the starting position to center the grid | ||
float startX = -(gridSize - 1) * spawnPrefabSpacing * 0.5f; | ||
float startY = -(gridSize - 1) * spawnPrefabSpacing * 0.5f; | ||
float startZ = -(gridSize - 1) * spawnPrefabSpacing * 0.5f; | ||
|
||
//Debug.Log($"Start Positions: X={startX}, Y={startY}, Z={startZ}, gridSize={gridSize}"); | ||
|
||
for (int x = 0; x < gridSize; ++x) | ||
for (int y = 0; y < gridSize; ++y) | ||
for (int z = 0; z < gridSize; ++z) | ||
if (spawned < spawnPrefabsCount) | ||
{ | ||
float x1 = startX + x * spawnPrefabSpacing; | ||
float y1 = startY + y * spawnPrefabSpacing; | ||
float z1 = startZ + z * spawnPrefabSpacing; | ||
Vector3 position = new Vector3(x1, y1, z1); | ||
|
||
NetworkServer.Spawn(Instantiate(spawnPrefab, position, Quaternion.identity, SpawnsTransform)); | ||
++spawned; | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
47 changes: 47 additions & 0 deletions
47
Assets/Mirror/Examples/HexSpatialHash/Scripts/Hex3DPlayer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using UnityEngine; | ||
using Mirror; | ||
|
||
namespace Mirror.Examples.Hex3D | ||
{ | ||
[AddComponentMenu("")] | ||
public class Hex3DPlayer : NetworkBehaviour | ||
{ | ||
[Range(1, 20)] | ||
public float speed = 10; | ||
|
||
void Update() | ||
{ | ||
if (!isLocalPlayer) return; | ||
|
||
float h = Input.GetAxis("Horizontal"); | ||
float v = Input.GetAxis("Vertical"); | ||
|
||
// if left shift is held, apply v to y instead of z | ||
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) | ||
{ | ||
Vector3 dir = new Vector3(h, v, 0); | ||
transform.position += dir.normalized * (Time.deltaTime * speed); | ||
} | ||
else | ||
{ | ||
Vector3 dir = new Vector3(h, 0, v); | ||
transform.position += dir.normalized * (Time.deltaTime * speed); | ||
} | ||
|
||
if (Input.GetKey(KeyCode.Q)) | ||
transform.Rotate(Vector3.up, -90 * Time.deltaTime); | ||
if (Input.GetKey(KeyCode.E)) | ||
transform.Rotate(Vector3.up, 90 * Time.deltaTime); | ||
} | ||
|
||
void OnGUI() | ||
{ | ||
if (isLocalPlayer) | ||
{ | ||
GUILayout.BeginArea(new Rect(10, Screen.height - 50, 300, 300)); | ||
GUILayout.Label("Use WASD+QE to move and rotate\nHold Shift with W/S to move up/down"); | ||
GUILayout.EndArea(); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
69 changes: 0 additions & 69 deletions
69
Assets/Mirror/Examples/HexSpatialHash/Scripts/HexNetworkManager.cs
This file was deleted.
Oops, something went wrong.
44 changes: 0 additions & 44 deletions
44
Assets/Mirror/Examples/HexSpatialHash/Scripts/HexPlayer.cs
This file was deleted.
Oops, something went wrong.