Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Invaders hit fx not replicated on player & enemy bullets #113

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions Basic/Invaders/Assets/Scripts/EnemyBullet.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Unity.Mathematics;
using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Assertions;

public class EnemyBullet : MonoBehaviour
public class EnemyBullet : NetworkBehaviour
{
private const float k_YBoundary = -4.0f;

Expand All @@ -16,55 +16,74 @@ public class EnemyBullet : MonoBehaviour
[SerializeField]
ParticleSystem m_ShieldExplosionParticle;

void Awake()
{
enabled = false;
}

public override void OnNetworkSpawn()
{
enabled = IsServer;
}

private void Start()
{
Assert.IsTrue(InvadersGame.Singleton);
Assert.IsTrue(NetworkManager.Singleton);

if(InvadersGame.Singleton)
InvadersGame.Singleton.isGameOver.OnValueChanged += OnGameOver;
}

private void Update()
{
if (!NetworkManager.Singleton.IsServer) return;
if (!IsServer) return;
LPLafontaineB marked this conversation as resolved.
Show resolved Hide resolved
transform.Translate(0, -m_TravelSpeed * Time.deltaTime, 0);

if (transform.position.y < k_YBoundary) Destroy(gameObject);
}

private void OnDestroy()
public override void OnDestroy()
{
base.OnDestroy();
if (InvadersGame.Singleton) InvadersGame.Singleton.isGameOver.OnValueChanged -= OnGameOver;
LPLafontaineB marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnTriggerEnter2D(Collider2D collider)
{
if (!NetworkManager.Singleton.IsServer)
// several OnTriggerEnter2D calls may be invoked in the same frame (for different Colliders), so we check if
// we're spawned to make sure we don't trigger hits for already despawned bullets
if (!IsServer || !IsSpawned)
return;

var hitPlayer = collider.gameObject.GetComponent<PlayerControl>();
if (hitPlayer != null)
{
hitPlayer.HitByBullet();
Destroy(gameObject);
NetworkObject.Despawn();
return;
}

var hitShield = collider.gameObject.GetComponent<Shield>();
if (hitShield != null)
{
SpawnExplosionVFXClientRpc(transform.position, Quaternion.identity);

Destroy(hitShield.gameObject);
Destroy(gameObject);
Instantiate(m_ShieldExplosionParticle, transform.position, quaternion.identity);
NetworkObject.Despawn();
}
}

[ClientRpc]
void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation)
{
Instantiate(m_ShieldExplosionParticle, spawnPosition, spawnRotation);
}

private void OnGameOver(bool oldValue, bool newValue)
{
enabled = false;

// On game over destroy the bullets
if (NetworkManager.Singleton.IsServer) Destroy(gameObject);
if (IsServer) NetworkObject.Despawn();
LPLafontaineB marked this conversation as resolved.
Show resolved Hide resolved
}
}
49 changes: 33 additions & 16 deletions Basic/Invaders/Assets/Scripts/PlayerBullet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Unity.Mathematics;
using System;
using Unity.Netcode;
using UnityEngine;

public class PlayerBullet : MonoBehaviour
public class PlayerBullet : NetworkBehaviour
{
private const float k_YBoundary = 15.0f;
public PlayerControl owner;
Expand All @@ -15,46 +15,63 @@ public class PlayerBullet : MonoBehaviour
[SerializeField]
ParticleSystem m_EnemyExplosionParticle;

void Awake()
{
enabled = false;
}

public override void OnNetworkSpawn()
{
enabled = IsServer;
}

private void Update()
{
if (!NetworkManager.Singleton.IsServer) return;
if (!IsServer) return;

transform.Translate(0, m_TravelSpeed * Time.deltaTime, 0);

if (transform.position.y > k_YBoundary)
if (NetworkManager.Singleton.IsServer)
Destroy(gameObject);
{
NetworkObject.Despawn();
}
}

private void OnTriggerEnter2D(Collider2D collider)
{
if (!NetworkManager.Singleton.IsServer)
// several OnTriggerEnter2D calls may be invoked in the same frame (for different Colliders), so we check if
// we're spawned to make sure we don't trigger hits for already despawned bullets
if (!IsServer || !IsSpawned)
return;

var hitEnemy = collider.gameObject.GetComponent<EnemyAgent>();
if (hitEnemy != null && owner != null)
{
owner.IncreasePlayerScore(hitEnemy.score);

if (NetworkManager.Singleton.IsServer)
{
// Only the server can despawn a NetworkObject
hitEnemy.NetworkObject.Despawn();
}
// Only the server can despawn a NetworkObject
hitEnemy.NetworkObject.Despawn();

SpawnExplosionVFXClientRPC(transform.position, Quaternion.identity);

Destroy(gameObject);
NetworkObject.Despawn();

// this instantiates at the position of the bullet, there is an offset in the Y axis on the
// particle systems in the prefab so it looks like it spawns in the middle of the enemy
Instantiate(m_EnemyExplosionParticle, transform.position, quaternion.identity);
return;
}

var hitShield = collider.gameObject.GetComponent<Shield>();
if (hitShield != null)
{
Destroy(hitShield.gameObject);
Destroy(gameObject);
NetworkObject.Despawn();
}
}

[ClientRpc]
void SpawnExplosionVFXClientRPC(Vector3 spawnPosition, Quaternion spawnRotation)
{
// this instantiates at the position of the bullet, there is an offset in the Y axis on the
// particle systems in the prefab so it looks like it spawns in the middle of the enemy
Instantiate(m_EnemyExplosionParticle, spawnPosition, spawnRotation);
}
}