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.

187 lines
6.2 KiB
C#

1 year ago
using MoreMountains.Feedbacks;
using MoreMountains.Tools;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MoreMountains.CorgiEngine
{
/// <summary>
/// Add this class on a character and it'll be able to dash just like the regular dash, and apply damage to everything its DamageOnTouch zone touches
/// </summary>
public class CharacterDoubleDash : CharacterDamageDash
{
[Header("Damage Dash Bounce")]
[Header("Bounciness Tech")]
/// the length of the raycast used to detect bounces, should be proportionate to the size and speed of your projectile
[Tooltip("the length of the raycast used to detect bounces, should be proportionate to the size and speed of your projectile")]
public float BounceRaycastLength = 100f;
/// the layers you want this projectile to bounce on
[Tooltip("the layers you want this to bounce on")]
public LayerMask BounceLayers;
/// a feedback to trigger at every bounce
[Tooltip("a feedback to trigger at every bounce")]
public MMFeedbacks BounceFeedback;
[SerializeField]
private GameObject arrowHolder;
[SerializeField]
private int mouseTreshhold = 2;
[SerializeField]
private float dashTimeSec = 0.1f;
private float startDashTime;
protected Vector3 _positionLastFrame;
protected Vector3 _raycastDirection;
protected Vector3 _reflectedDirection;
private Vector2 startMousePosition = new Vector2();
/// <summary>
/// On initialization, we disable our damage on touch object
/// </summary>
protected override void Initialization()
{
base.Initialization();
startMousePosition = Input.mousePosition;
}
public override void ProcessAbility()
{
base.ProcessAbility();
}
protected override void HandleInput()
{
if (Input.GetMouseButtonDown(0))
{
startMousePosition = Input.mousePosition;
arrowHolder.SetActive(true);
}
if (Input.GetMouseButtonUp(0))
{
arrowHolder.SetActive(false);
Vector2 newpose = Input.mousePosition;
Vector2 aimVectro = newpose - startMousePosition;
if (aimVectro.magnitude < mouseTreshhold) return;
Aim.SetAim(aimVectro.normalized);
startDashTime = Time.time;
MMDebug.RayCast(_character.transform.position, aimVectro.normalized, BounceRaycastLength, BounceLayers, MMColors.DarkOrange, true);
updatePosition();
StartDash();
}
float newpose2 = Vector2.Angle(startMousePosition,(Vector2)Input.mousePosition);
arrowHolder.transform.right = ((Vector2)Input.mousePosition - startMousePosition).normalized;
}
protected override void InitializeAnimatorParameters()
{
}
protected override IEnumerator Dash()
{
if (!AbilityAuthorized
|| (_condition.CurrentState != CharacterStates.CharacterConditions.Normal))
{
yield break;
}
_character._health.Invulnerable = true;
// we keep dashing until we've reached our target distance or until we get interrupted
while ( _shouldKeepDashing
&& !_controller.State.TouchingLevelBounds
&& _movement.CurrentState == CharacterStates.MovementStates.Dashing)
{
if (Time.time - startDashTime > dashTimeSec)
{
_shouldKeepDashing = false;
_controller.SetForce(Vector2.zero);
}
else
{
_controller.GravityActive(false);
_controller.SetForce(_dashDirection * DashForce);
}
yield return null;
}
StopDash();
}
protected override void UpdateCellBar()
{
if (Application.isPlaying)
{
if ((GUIManager.Instance != null) && (_character.CharacterType == Character.CharacterTypes.Player))
{
GUIManager.Instance.UpdateDoubleDashBar((EnergyCellLeft + EnergyRestoreTimer/EnergyCellRestoreTime), 0f, EnergyCellAmount);
}
}
}
public override void InitiateDash()
{
base.InitiateDash();
}
public override void StopDash()
{
base.StopDash();
_character._health.Invulnerable = false;
_shouldKeepDashing = false;
_controller.SetForce(Vector2.zero);
}
public virtual void OnTriggerEnter2D(Collider2D collider)
{
if (!AbilityAuthorized) return;
if(_movement.CurrentState == CharacterStates.MovementStates.Dashing) Colliding(collider.gameObject);
}
protected virtual void Colliding(GameObject collider)
{
if (!BounceLayers.MMContains(collider.layer))
{
return;
}
StopDash();
EnergyCellLeft++;
collider.gameObject.MMGetComponentNoAlloc<Health>().Damage(10,_character.gameObject,1,1);
// EnergyRestoreTimer = 0f;
UpdateCellBar();
}
/// <summary>
/// Applies a bounce in 2D
/// </summary>
/// <param name="hit"></param>
protected virtual void Bounce(RaycastHit2D hit)
{
BounceFeedback?.PlayFeedbacks();
_reflectedDirection = Vector3.Reflect(_raycastDirection, hit.normal);
float angle = Vector3.Angle(_raycastDirection, _reflectedDirection);
_dashDirection = _reflectedDirection.normalized;
Aim.SetAim(_dashDirection);
updatePosition();
// this.transform.right = _spawnerIsFacingRight ? _reflectedDirection.normalized : -_reflectedDirection.normalized;
}
protected virtual void updatePosition()
{
_positionLastFrame = _character.transform.position;
}
}
}