Fixed an issue with calculating the normal on a box HitShape.

This commit is contained in:
Karrar
2024-10-08 15:36:58 +03:00
parent d6e0bc5597
commit 01d6efd4b4
23 changed files with 233 additions and 249 deletions

View File

@@ -5,99 +5,73 @@ using Netick.Unity;
namespace Netick.Samples.Bomberman
{
public class BombermanEventsHandler : NetworkEventsListener
public class BombermanEventsHandler : NetworkBehaviour
{
public List<BombermanController> Players = new(4);
public List<BombermanController> AlivePlayers = new(4);
public List<BombermanController> AlivePlayers = new List<BombermanController>();
public Vector3[] SpawnPositions = new Vector3[4]
{
new Vector3(11, 9, 0),
new Vector3(11, 1, 0),
new Vector3(1, 9, 0),
new Vector3(1, 1, 0)
};
private GameObject _playerPrefab;
private Queue<Vector3> _freePositions = new Queue<Vector3>(4);
// ******************* Netick Callbacks *******************
// This is called on the server and the clients when Netick has started.
public override void OnStartup(NetworkSandbox sandbox)
private GameObject _playerPrefab;
private Vector3[] _spawnPositions = new Vector3[4] { new Vector3(11, 9, 0), new Vector3(11, 1, 0), new Vector3(1, 9, 0), new Vector3(1, 1, 0) };
private Queue<Vector3> _freePositions = new(4);
public override void NetworkStart()
{
_playerPrefab = sandbox.GetPrefab("Bomberman Player");
sandbox.InitializePool(sandbox.GetPrefab("Bomb"), 5);
Sandbox.Events.OnInputRead += OnInput;
Sandbox.Events.OnConnectRequest += OnConnectRequest;
Sandbox.Events.OnPlayerConnected += OnPlayerConnected;
Sandbox.Events.OnPlayerDisconnected += OnPlayerDisconnected;
_playerPrefab = Sandbox.GetPrefab("Bomberman Player");
Sandbox.InitializePool(Sandbox.GetPrefab("Bomb"), 5);
Sandbox.InitializePool(_playerPrefab,4);
for (int i = 0; i < 4; i++)
_freePositions.Enqueue(_spawnPositions[i]);
if (IsServer)
RestartGame();
}
public void OnConnectRequest(NetworkSandbox sandbox, NetworkConnectionRequest request)
{
if (Sandbox.ConnectedPlayers.Count >= 4)
request.Refuse();
}
// This is called on the server when a playerObj has connected.
public void OnPlayerConnected(NetworkSandbox sandbox, NetworkPlayer player)
{
var playerObj = sandbox.NetworkInstantiate(_playerPrefab, _spawnPositions[Sandbox.ConnectedPlayers.Count], Quaternion.identity, player).GetComponent<BombermanController>();
player.PlayerObject = playerObj.gameObject;
AlivePlayers. Add(playerObj);
Players. Add(playerObj);
}
// This is called on the server when a client has disconnected.
public void OnPlayerDisconnected(NetworkSandbox sandbox, Netick.NetworkPlayer player, TransportDisconnectReason reason)
{
_freePositions.Enqueue(((GameObject)player.PlayerObject).GetComponent<BombermanController>().SpawnPos);
Players. Remove (((GameObject)player.PlayerObject).GetComponent<BombermanController>());
}
// This is called to read inputs.
public override void OnInput(NetworkSandbox sandbox)
public void OnInput(NetworkSandbox sandbox)
{
var input = sandbox.GetInput<BombermanInput>();
var input = sandbox.GetInput<BombermanInput>();
input.Movement = GetMovementDir();
input.PlantBomb |= Input.GetKeyDown(KeyCode.Space);
sandbox.SetInput(input);
}
// This is called on the server when a player has connected.
public override void OnPlayerConnected(NetworkSandbox sandbox, NetworkPlayer networkPlayer)
{
var player = sandbox.NetworkInstantiate(_playerPrefab, SpawnPositions[Sandbox.ConnectedPlayers.Count], Quaternion.identity, networkPlayer).GetComponent<BombermanController>();
networkPlayer.PlayerObject = player.gameObject;
AlivePlayers.Add(player);
}
// This is called on the server when a client has disconnected.
public override void OnClientDisconnected(NetworkSandbox sandbox, NetworkConnection client, TransportDisconnectReason reason)
{
_freePositions.Enqueue(((GameObject)client.PlayerObject).GetComponent<BombermanController>().SpawnPos);
}
public override void OnConnectRequest(NetworkSandbox sandbox, NetworkConnectionRequest request)
{
if (_freePositions.Count < 1)
request.Refuse();
}
// This is called on the server and the clients when the scene has been loaded.
public override void OnSceneLoaded(NetworkSandbox sandbox)
{
if (sandbox.IsClient)
return;
_freePositions.Clear();
for (int i = 0; i < 4; i++)
_freePositions.Enqueue(SpawnPositions[i]);
RestartGame();
}
// ******************* ********************
public void RestartGame()
{
DestroyLevel();
CreateNewLevel();
foreach (var player in Sandbox.ConnectedPlayers)
((GameObject)player.PlayerObject).GetComponent<BombermanController>().Respawn();
}
private void DestroyLevel()
{
var blocks = Sandbox.FindObjectsOfType<Block>();
var bombs = Sandbox.FindObjectsOfType<Bomb>();
foreach (var block in blocks)
// destroy level.
foreach (var block in Sandbox.FindObjectsOfType<Block>())
Sandbox.Destroy(block.Object);
foreach (var bomb in bombs)
foreach (var bomb in Sandbox.FindObjectsOfType<Bomb>())
Sandbox.Destroy(bomb.Object);
}
private void CreateNewLevel()
{
// create new level.
var blockPrefab = Sandbox.GetPrefab("DestroyableBlock");
var powerUpPrefab = Sandbox.GetPrefab("Power Up");
var numberOfBoosters = Random.Range(2, 4+1);
@@ -109,8 +83,8 @@ namespace Netick.Samples.Bomberman
{
for (int y = 1; y <= maxY; y++)
{
var spawn = Random.value > 0.5f;
var pos = new Vector3(x, y);
var spawn = Random.value > 0.5f;
var pos = new Vector3(x, y);
if (spawn && IsValidPos(pos))
{
@@ -122,8 +96,8 @@ namespace Netick.Samples.Bomberman
while (numberOfBoosters > 0)
{
var randomPos = new Vector3(Random.Range(1, 11+1), Random.Range(1, 9+1), 0);
var type = (Random.value > 0.5f) ? PowerUpType.Speed : PowerUpType.IncreaseBombs;
var randomPos = new Vector3(Random.Range(1, 11+1), Random.Range(1, 9+1), 0);
var type = (Random.value > 0.5f) ? PowerUpType.Speed : PowerUpType.IncreaseBombs;
if (!takenPositions.Contains(randomPos) && IsValidPos(randomPos))
{
@@ -132,24 +106,25 @@ namespace Netick.Samples.Bomberman
numberOfBoosters--;
}
}
// reset players.
foreach (var player in Players)
player.Respawn();
}
private bool IsValidPos(Vector3 pos)
{
// if the pos is the position of a static block, we ignore it
// if the pos is the position of a static block, we ignore it.
if ((pos.x >= 2 && pos.x <= 10) && (pos.y >= 2 && pos.y <= 8))
if (pos.x % 2 == 0 && pos.y % 2 == 0)
return false;
// if the pos is near the position of the spawn locations of the players, we ignore it
foreach (var loc in SpawnPositions)
// if the pos is near the position of the spawn locations of the players, we ignore it.
foreach (var loc in _spawnPositions)
{
if (pos == loc)
return false;
if (pos == loc + Vector3.up || pos == loc + Vector3.down)
return false;
if (pos == loc + Vector3.left || pos == loc + Vector3.right)
return false;
if (pos == loc) return false;
if (pos == loc + Vector3.up || pos == loc + Vector3.down) return false;
if (pos == loc + Vector3.left || pos == loc + Vector3.right) return false;
}
return true;
@@ -168,24 +143,19 @@ namespace Netick.Samples.Bomberman
else if (AlivePlayers.Count < 1)
RestartGame();
}
public void RespawnPlayer(BombermanController bomber)
{
if (!AlivePlayers.Contains(bomber))
AlivePlayers.Add(bomber);
}
private Vector2 GetMovementDir()
{
if (Input.GetKey(KeyCode.W))
return Vector2.up;
else if (Input.GetKey(KeyCode.D))
return Vector2.right;
else if (Input.GetKey(KeyCode.S))
return Vector2.down;
else if (Input.GetKey(KeyCode.A))
return Vector2.left;
else
return Vector2.zero;
if (Input.GetKey(KeyCode.W)) return Vector2.up;
else if (Input.GetKey(KeyCode.D)) return Vector2.right;
else if (Input.GetKey(KeyCode.S)) return Vector2.down;
else if (Input.GetKey(KeyCode.A)) return Vector2.left;
else return Vector2.zero;
}
}
}