RiftMayhem/Assets/Scripts/Networking/NetworkedChanneling.cs
Pedro Gomes 86deb651ba Build Manager & new abilities update
- Build Manager system to allow swapping, saving and loading build setups for characters
- Necromancer new channeled ability: Soulfire
- Mage new melee ability: Cold Slash
- Mage new summon ability: Mirror Image
- Added summon ability support to spawn more than one minion per cast
2024-12-27 23:23:10 +00:00

175 lines
4.6 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class NetworkedChanneling : MonoBehaviour
{
[Header("Visuals")]
[SerializeField] protected GameObject visuals;
[Header("Physics:")]
[SerializeField] protected LayerMask abilityHitLayer;
[Header("Set by Code")]
public PhotonView photonView;
public PhotonView owner;
public Taggable ownerTag;
public ChanneledAbility ability;
public float duration;
public bool canHitSelf;
public bool followUser;
public bool allowAiming;
public float radius;
public bool channeling = false;
private float endTime;
protected PhotonView possibleTarget;
protected Taggable target;
protected List<Taggable> targets = new List<Taggable>();
protected Collider[] hits;
public BoxCollider OptionalBoxOverlap;
ProjectileSpawnLocationController aimController;
public UnityEvent<PhotonView, Taggable, List<Taggable>> onTickHappened = new UnityEvent<PhotonView, Taggable, List<Taggable>>();
protected void OnTickPerformed()
{
onTickHappened.Invoke(owner, ownerTag, targets);
}
protected virtual void Awake()
{
photonView = GetComponent<PhotonView>();
}
public virtual void Init(ref Coroutine channelingCoroutine)
{
if (photonView.IsMine)
{
photonView.RPC(nameof(RPC_RemoteInit), RpcTarget.Others, AbilityIndexer.Instance.Abilities.IndexOf(ability));
channeling = true;
aimController = owner.GetComponentInChildren<ProjectileSpawnLocationController>();
channelingCoroutine = StartCoroutine(ExecuteChanneling());
if (followUser)
StartCoroutine(FollowUser());
StartCoroutine(SelfDestruct());
}
}
IEnumerator FollowUser()
{
while (Time.time < endTime)
{
this.transform.position = owner.transform.position;
if (allowAiming)
{
this.transform.forward = aimController.GetLookat();
}
yield return new WaitForEndOfFrame();
}
visuals.SetActive(false);
}
[PunRPC]
protected virtual void RPC_RemoteInit(int abilityIndex)
{
ability = (ChanneledAbility)AbilityIndexer.Instance.Abilities[abilityIndex];
}
[PunRPC]
private void RPC_DisableVisuals()
{
visuals.SetActive(false);
}
IEnumerator ExecuteChanneling()
{
visuals.SetActive(true);
endTime = Time.time + duration;
while (Time.time < endTime)
{
yield return new WaitForSeconds(0.5f);
CheckSurroundings();
}
}
public void DisableVisuals()
{
if (photonView.IsMine)
photonView.RPC(nameof(RPC_DisableVisuals), RpcTarget.All);
}
protected virtual void CheckSurroundings()
{
if (!channeling) return;
if (OptionalBoxOverlap != null)
{
hits = Physics.OverlapBox(OptionalBoxOverlap.transform.position, OptionalBoxOverlap.size / 2f, OptionalBoxOverlap.transform.rotation, abilityHitLayer);
}
else
{
hits = Physics.OverlapSphere(this.transform.position, radius, abilityHitLayer);
}
targets.Clear();
foreach (Collider collider in hits)
{
Debug.Log("hit collider " + collider.name);
possibleTarget = collider.GetComponentInParent<PhotonView>();
if (possibleTarget != null)
if (possibleTarget == owner && !canHitSelf) continue;
target = collider.GetComponentInParent<Taggable>();
Debug.Log("hit collider, Got taggable: " + target.name);
if (target == null) continue;
//Debug.Log("hit collider, targetTag: " + target.targetTag.name);
foreach (TargetTag tag in ability.targettingTags)
{
Debug.Log("hit collider, ability.Tags: " + tag);
}
if (!target.IsValidTarget(ability.targettingTags)) continue;
Debug.Log("hit collider, added target: " + target.name);
targets.Add(target);
}
foreach (BaseEffect effect in ability.abilityEffects)
{
effect.ApplyEffect(ownerTag, targets);
}
OnTickPerformed();
}
protected IEnumerator SelfDestruct()
{
yield return new WaitForSeconds(duration + 1f);
PhotonNetwork.Destroy(photonView);
}
protected void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, radius);
}
}