using MoreMountains.Feedbacks; using MoreMountains.Tools; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MoreMountains.CorgiEngine { /// /// 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 /// public class CharacterDamageDashBounce : 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 projectile 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(); /// /// On initialization, we disable our damage on touch object /// 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.UpdateDemageDashBar((EnergyCellLeft + EnergyRestoreTimer/EnergyCellRestoreTime), 0f, EnergyCellAmount); } } } public override void InitiateDash() { base.InitiateDash(); } public override void StopDash() { base.StopDash(); _character._health.Invulnerable = false; } public virtual void OnTriggerEnter2D(Collider2D collider) { Debug.Log("Collide enter"); // if (!AbilityAuthorized) return; if (_movement.CurrentState == CharacterStates.MovementStates.Dashing) Colliding(collider.gameObject); } protected virtual void Colliding(GameObject collider) { Debug.Log("Collide " + collider.layer); if (!BounceLayers.MMContains(collider.layer)) { return; } _raycastDirection = (_character.transform.position - _positionLastFrame); Debug.Log("Collide2"); RaycastHit2D hit = MMDebug.RayCast(_character.transform.position, _dashDirection.normalized, BounceRaycastLength, BounceLayers, MMColors.DarkOrange, true); Bounce(hit); } /// /// Applies a bounce in 2D /// /// protected virtual void Bounce(RaycastHit2D hit) { BounceFeedback?.PlayFeedbacks(); Debug.Log("Before " + _dashDirection); _reflectedDirection = Vector3.Reflect(_raycastDirection, hit.normal); float angle = Vector3.Angle(_raycastDirection, _reflectedDirection); _dashDirection = _reflectedDirection.normalized; Debug.Log("After " + _dashDirection); Aim.SetAim(_dashDirection); updatePosition(); // this.transform.right = _spawnerIsFacingRight ? _reflectedDirection.normalized : -_reflectedDirection.normalized; } protected virtual void updatePosition() { _positionLastFrame = _character.transform.position; } } }