Files
Netick.Unity/Samples~/Bomberman/Scripts/Bomb.cs
Karrar 1b0d33d8f0 Read the description for detailed changes.
- Added `Sandbox.Players`: a synchronized list of `NetworkPlayerId` structs representing connected players.
- Added `Sandbox.Events.OnPlayerJoined` and `Sandbox.Events.OnPlayerLeft` callbacks, synchronized across all clients.
- Changed internal interpolation of quaternions to use `Slerp` instead of `Lerp`.
- Fixed a potential crash caused by undefined behavior when reaching `NetickConfig.MaxPlayers` and destroying network objects.
- Fixed an issue where sandbox-loaded scenes were not being unloaded during shutdown.
- Fixed a bug preventing Prediction Error Correction from functioning correctly.
2025-06-27 05:47:00 +03:00

72 lines
1.8 KiB
C#

using UnityEngine;
using Netick;
using Netick.Unity;
namespace Netick.Samples.Bomberman
{
public class Bomb : NetworkBehaviour
{
public GameObject ExplosionPrefab;
public BombermanController Bomber;
public float ExplosionDelay = 3.0f;
private readonly Vector3[] _directionsAroundBomb = new Vector3[4] { Vector3.right, Vector3.left, Vector3.up, Vector3.down };
private static RaycastHit[] _hits = new RaycastHit[20];
public override void NetworkStart()
{
Bomber?.SpawnedBombs.Add(this);
GetComponent<Renderer>().enabled = true;
}
public override void NetworkDestroy()
{
Bomber?.SpawnedBombs.Remove(this);
// spawn explosion.
if (ExplosionPrefab != null)
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
}
public override void NetworkFixedUpdate()
{
if (Sandbox.TickToTime(Sandbox.Tick - Object.SpawnTick) >= ExplosionDelay)
Explode();
}
private void Explode()
{
// hide bomb after delay.
GetComponent<Renderer>().enabled = false;
// dealing damage is done on the server only.
if (IsServer)
{
DamageTargetsAroundBomb(transform.position);
Sandbox.Destroy(Object);
}
}
private void DamageTargetsAroundBomb(Vector3 pos)
{
// find all objects around the bomb position.
foreach (var dir in _directionsAroundBomb)
{
var hitsCount = Sandbox.Physics.Raycast(pos, dir, _hits, 1f);
for (int i = 0; i < hitsCount; i++)
{
var target = _hits[i].collider.gameObject;
var block = target.GetComponent<Block>();
var bomber = target.GetComponent<BombermanController>();
if (block != null)
block.Visible = false;
bomber?.Die();
}
}
}
}
}