|
|
|
@ -3,61 +3,83 @@ using System.Collections.Generic;
|
|
|
|
|
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
|
|
|
|
using Unity.Netcode;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(SphereCollider))]
|
|
|
|
|
public class SlowZonePrefab : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("Slow Zone Settings")]
|
|
|
|
|
public Ability Ability;
|
|
|
|
|
public GameObject visualEffectPrefab; // Prefab for visualizing the area
|
|
|
|
|
|
|
|
|
|
private HashSet<ServerCharacter> affectedPlayers = new HashSet<ServerCharacter>();
|
|
|
|
|
private SphereCollider zoneCollider;
|
|
|
|
|
private GameObject visualEffectInstance;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
// Start the process to slow down players when the zone is spawned
|
|
|
|
|
StartCoroutine(ApplySlowEffect());
|
|
|
|
|
}
|
|
|
|
|
zoneCollider = GetComponent<SphereCollider>();
|
|
|
|
|
|
|
|
|
|
private IEnumerator ApplySlowEffect()
|
|
|
|
|
{
|
|
|
|
|
// Run the logic once immediately
|
|
|
|
|
ApplySlowToPlayers();
|
|
|
|
|
// Spawn the visual effect
|
|
|
|
|
if (visualEffectPrefab)
|
|
|
|
|
{
|
|
|
|
|
visualEffectInstance = Instantiate(visualEffectPrefab, transform.position, Quaternion.identity);
|
|
|
|
|
visualEffectInstance.transform.localScale = Vector3.one * Ability.abilityRadius * 2f; // Adjust scale to match the radius
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optionally, repeat the check while the zone exists
|
|
|
|
|
yield return new WaitForSeconds(Ability.abilityDuration);
|
|
|
|
|
// Automatically destroy after the ability duration
|
|
|
|
|
StartCoroutine(DelayedDestroy());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Destroy the zone after applying the slow effect
|
|
|
|
|
DespawnZone();
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
|
{
|
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
|
{
|
|
|
|
|
if (!player.IsCrow && affectedPlayers.Add(player)) // Only affect non-crow players
|
|
|
|
|
{
|
|
|
|
|
player.Movement.SetSpeedModifier(Ability.abilityMagnitude);
|
|
|
|
|
Debug.Log($"{player.name} entered the slow zone. Speed reduced.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplySlowToPlayers()
|
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
|
{
|
|
|
|
|
// Find all colliders in the zone
|
|
|
|
|
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Ability.abilityRadius);
|
|
|
|
|
foreach (var collider in hitColliders)
|
|
|
|
|
if (other.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
|
{
|
|
|
|
|
if (collider.TryGetComponent<ServerCharacter>(out var player))
|
|
|
|
|
if (affectedPlayers.Remove(player))
|
|
|
|
|
{
|
|
|
|
|
// Check if the player is NOT the crow
|
|
|
|
|
if (!player.IsCrow)
|
|
|
|
|
{
|
|
|
|
|
// Apply slow effect
|
|
|
|
|
StartCoroutine(SlowPlayer(player));
|
|
|
|
|
Debug.Log($"{player.name} is slowed down!");
|
|
|
|
|
}
|
|
|
|
|
player.Movement.ResetSpeedModifier();
|
|
|
|
|
Debug.Log($"{player.name} exited the slow zone. Speed restored.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator SlowPlayer(ServerCharacter player)
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
// Halve the player's movement speed
|
|
|
|
|
player.Movement.SetSpeedModifier(Ability.abilityMagnitude);
|
|
|
|
|
// Restore speed for all players when the zone is destroyed
|
|
|
|
|
foreach (var player in affectedPlayers)
|
|
|
|
|
{
|
|
|
|
|
player.Movement.ResetSpeedModifier();
|
|
|
|
|
Debug.Log($"{player.name}'s speed restored due to slow zone destruction.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
affectedPlayers.Clear();
|
|
|
|
|
|
|
|
|
|
// Wait for the slow duration
|
|
|
|
|
// Destroy the visual effect
|
|
|
|
|
if (visualEffectInstance)
|
|
|
|
|
{
|
|
|
|
|
Destroy(visualEffectInstance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator DelayedDestroy()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(Ability.abilityDuration);
|
|
|
|
|
|
|
|
|
|
// Restore the original speed
|
|
|
|
|
player.Movement.ResetSpeedModifier();
|
|
|
|
|
Debug.Log($"{player.name}'s speed is restored.");
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
if (IsServer)
|
|
|
|
|
{
|
|
|
|
|
DespawnZone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DespawnZone()
|
|
|
|
@ -76,11 +98,4 @@ public class SlowZonePrefab : NetworkBehaviour
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Debug visualization for the slow zone in the editor
|
|
|
|
|
private void OnDrawGizmos()
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.cyan;
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, Ability.abilityRadius);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|