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

@@ -7,32 +7,29 @@ namespace Netick.Samples.Bomberman
public class Bomb : NetworkBehaviour
{
public GameObject ExplosionPrefab;
public GameObject ExplosionPrefab;
public BombermanController Bomber;
public float ExplosionDelay = 3.0f;
public BombermanController Bomber;
public float ExplosionDelay = 3.0f;
private readonly Vector3[] _directionsAroundBomb = new Vector3[4] { Vector3.right, Vector3.left, Vector3.up, Vector3.down };
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
// spawn explosion.
if (ExplosionPrefab != null)
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
}
public override void NetworkReset()
{
GetComponent<Renderer>().enabled = true;
}
public override void NetworkFixedUpdate()
{
if (Sandbox.TickToTime(Sandbox.Tick - Object.SpawnTick) >= ExplosionDelay)
@@ -41,47 +38,35 @@ namespace Netick.Samples.Bomberman
private void Explode()
{
// hide bomb after delay
// hide bomb after delay.
GetComponent<Renderer>().enabled = false;
// dealing damage is done on the server only
// dealing damage is done on the server only.
if (IsServer)
{
DamageTargetsAroundBomb(transform.position);
// only the server can destroy the bomb or the client but only when the Id of the bomb is -1, meaning it was a spawn-predicted but never was confirmed by the server
if (IsServer || Id == -1)
Sandbox.Destroy(Object);
}
private void DamageTargetsAroundBomb(Vector3 pos)
{
// Find all objects around the bomb position
// Note: Causes GC
foreach (var dir in _directionsAroundBomb)
{
var hits = Physics.RaycastAll(pos, dir, 1f);
foreach (var hit in hits)
Damage(hit.collider.gameObject);
}
}
private void Damage(GameObject target)
private void DamageTargetsAroundBomb(Vector3 pos)
{
var obj = target.GetComponent<NetworkObject>();
var block = target.GetComponent<Block>();
var bomber = target.GetComponent<BombermanController>();
// find all objects around the bomb position.
foreach (var dir in _directionsAroundBomb)
{
var hitsCount = Sandbox.Physics.Raycast(pos, dir, _hits, 1f);
// make sure the object is not null and in the same sandbox as the bomb
if (obj == null || obj.Sandbox != Sandbox)
return;
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;
if (bomber != null)
bomber.Die();
if (block != null)
block.Visible = false;
bomber?.Die();
}
}
}
}
}