Skip to content

Commit

Permalink
fix(HexSpatialHash): Renamed example scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGadget1024 committed Feb 26, 2025
1 parent e217341 commit a7d367e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 113 deletions.
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;
}
}
}
}
47 changes: 47 additions & 0 deletions Assets/Mirror/Examples/HexSpatialHash/Scripts/Hex3DPlayer.cs
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();
}
}
}
}
69 changes: 0 additions & 69 deletions Assets/Mirror/Examples/HexSpatialHash/Scripts/HexNetworkManager.cs

This file was deleted.

44 changes: 0 additions & 44 deletions Assets/Mirror/Examples/HexSpatialHash/Scripts/HexPlayer.cs

This file was deleted.

0 comments on commit a7d367e

Please sign in to comment.