You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/ExecutionerBox.cs

88 lines
2.9 KiB
C#

using UnityEngine;
using Unity.Netcode;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using System.Collections;
using Unity.BossRoom.Gameplay.GameplayObjects;
public class ExecutionerBox : NetworkBehaviour
{
private ServerCharacter _owner;
private Vector3 _endPos;
private float _travelTime;
private float _startTime;
private Vector3 _startPos;
private float _warningTime;
public override void OnNetworkSpawn()
{
var playerList = CrowManager.Instance.players;
foreach (ServerCharacter serverCharacter in playerList)
{
if (serverCharacter.NetworkObject.IsOwner) // Check if this player is the local player
{
var abilitySystem = serverCharacter.GetComponent<AbilitySystem>();
if (abilitySystem != null)
{
abilitySystem.InitiateGlobalCooldown(GameDataSource.Instance.TheExecutionerKey);
}
break; // Exit loop after finding the local player
}
}
}
public void Initialize(ServerCharacter owner, Vector3 startPos, Vector3 endPos, float travelTime, float warningTime)
{
_owner = owner;
_startPos = startPos;
_endPos = endPos;
_travelTime = travelTime;
_warningTime = warningTime;
transform.position = startPos;
transform.LookAt(endPos);
StartCoroutine(StartExecutionerSequence());
}
private IEnumerator StartExecutionerSequence()
{
// Show warning UI on all clients
// UIManager.Instance.ShowExecutionerWarning(_warningTime);
WarningPanel.Instance.ShowPanelForTime(_warningTime);
// Wait for the warning time before moving
yield return new WaitForSeconds(_warningTime);
_startTime = Time.time;
StartCoroutine(MoveExecutioner());
}
private IEnumerator MoveExecutioner()
{
float elapsedTime = 0f;
while (elapsedTime < _travelTime)
{
elapsedTime = Time.time - _startTime;
transform.position = Vector3.Lerp(_startPos, _endPos, elapsedTime / _travelTime);
yield return null;
}
NetworkObject.Despawn();
}
private void OnTriggerEnter(Collider other)
{
if (!IsServer) return;
if (other.TryGetComponent<ServerCharacter>(out var victim))
{
ulong ownerId = _owner.OwnerClientId;
// Handle score changes
ScoreManager.Instance.AddPlayerScore(ownerId, 10,Color.yellow); // Add to owner
ScoreManager.Instance.SubtractPlayerScore(victim.OwnerClientId, 10,new Color(1,0.647f,0,1)); // Subtract from victim
Debug.Log($"[ExecutionerBox] Scores updated. " +
$"Owner ({ownerId}) gained 10, " +
$"Victim ({victim.OwnerClientId}) lost 10");
}
}
}