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.

123 lines
3.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.Generic;
using Fusion;
using UnityEngine;
using UnityEngine.Rendering;
namespace Projectiles
{
/// <summary>
/// Component handling all visual/hierarchy related tasks and effects (immortality, death).
/// </summary>
public class PlayerBody : ContextBehaviour
{
// PRIVATE MEMBERS
[Networked]
private int SelectedCharacterIndex { get; set; }
[SerializeField]
private List<GameObject> _characters;
[SerializeField]
private GameObject _root;
[SerializeField]
private GameObject _visual;
[SerializeField]
private GameObject _immortalityEffect;
// [SerializeField]
// private Transform _capTransform;
// [SerializeField]
// private Rigidbody _flyingCapPrefab;
// [SerializeField]
// private float _capImpulse = 10f;
[SerializeField]
private GameObject _deathEffectPrefab;
private PlayerAgent _agent;
private HitboxRoot _hitboxRoot;
public Animator _animator;
// ContextBehaviour INTERFACE
public override void Spawned()
{
if (Object.HasStateAuthority)
{
if (_characters != null && _characters.Count > 0)
{
SelectedCharacterIndex = UnityEngine.Random.Range(0, _characters.Count);
}
}
UpdateCharacterModel();
// Keep the existing logic:
_root.SetActive(_agent.Health.IsAlive);
_agent.Health.FatalHitTaken += OnFatalHit;
var renderers = _visual.GetComponentsInChildren<SkinnedMeshRenderer>();
for (int i = 0; i < renderers.Length; i++)
{
// This makes the local players model invisible to itself (only shadows), while others see it normally.
renderers[i].shadowCastingMode = HasInputAuthority ? ShadowCastingMode.ShadowsOnly : ShadowCastingMode.On;
}
}
private void UpdateCharacterModel()
{
for (int i = 0; i < _characters.Count; i++)
{
_characters[i].SetActive(i == SelectedCharacterIndex);
if (i == SelectedCharacterIndex)
{
_animator = _characters[i].GetComponent<Animator>();
}
}
}
public override void FixedUpdateNetwork()
{
// Disable hitbox detection when agent is dead
_hitboxRoot.HitboxRootActive = _agent.Health.IsAlive;
}
public override void Render()
{
_immortalityEffect.SetActive(_agent.Health.IsImmortal);
}
public override void Despawned(NetworkRunner runner, bool hasState)
{
_agent.Health.FatalHitTaken -= OnFatalHit;
}
// MONOBEHAVIOUR
protected void Awake()
{
_agent = GetComponent<PlayerAgent>();
_hitboxRoot = GetComponent<HitboxRoot>();
}
// PRIVATE METHODS
private void OnFatalHit(HitData hit)
{
_agent.KCC.SetActive(false);
_root.SetActive(false);
var deathEffect = Runner.InstantiateInRunnerScene(_deathEffectPrefab);
deathEffect.transform.position = transform.position + Vector3.up;
// var flyingCap = Runner.InstantiateInRunnerScene(_flyingCapPrefab);
// flyingCap.transform.SetPositionAndRotation(_capTransform.position, _capTransform.rotation);
//
// var direction = (hit.Direction + 2f * Vector3.up).normalized;
// flyingCap.AddForceAtPosition(direction * _capImpulse, flyingCap.transform.position - hit.Direction * 0.2f, ForceMode.Impulse);
if (Runner.Config.PeerMode == NetworkProjectConfig.PeerModes.Multiple)
{
// Runner.AddVisibilityNodes(flyingCap.gameObject);
Runner.AddVisibilityNodes(deathEffect.gameObject);
}
}
}
}