diff --git a/Assets/Scripts/AIEntity.cs b/Assets/Scripts/AIEntity.cs index db0b678..2f58539 100644 --- a/Assets/Scripts/AIEntity.cs +++ b/Assets/Scripts/AIEntity.cs @@ -16,6 +16,67 @@ public class AIEntity : Entity } } + /*//Basically a copy of JumpingMovementState + class NonPhysicThrownState : BaseStateAI { + readonly Vector3 target; + + Vector3 startPosition; + float duration; + float startTime; + + public NonPhysicThrownState(AIEntity entity, Vector3 target) : base(entity) { + this.target = target; + } + + public override void EnterState() { + base.EnterState(); + + duration = entity.AIStats.ThrownDurationPerMeter * Vector3.Distance(entity.transform.position, target); + startPosition = entity.transform.position; + startTime = Time.time; + entity.rb.SetEnabled(false); + } + + public override void LeaveState() { + base.LeaveState(); + entity.rb.SetEnabled(true); + } + + public override BaseState? FixedUpdateState() { + float currentTime = Time.time - startTime; + if (currentTime >= duration) + return new FindTargetState(entity); + + entity.rb.MovePosition(Vector3.Lerp( + startPosition, + target, + entity.AIStats.ThrownCurve.Evaluate(currentTime / duration) + )); + + return null; + } + }*/ + + class ThrownState : BaseStateAI { + public ThrownState(AIEntity entity) : base(entity) {} + + public override void EnterState() { + base.EnterState(); + + entity.rb.SetEnabled(false); + } + + public override void LeaveState() { + base.LeaveState(); + entity.rb.SetEnabled(true); + } + + public override BaseState? FixedUpdateState() + => entity.rb.velocity.magnitude < entity.stats.MinVelocityWhenThrown + ? new FindTargetState(entity) + : null; + } + class SeekState : BaseStateAI{ public SeekState(AIEntity entity) : base(entity){ diff --git a/Assets/Scripts/AIStats.cs b/Assets/Scripts/AIStats.cs index e1d503a..2879e1b 100644 --- a/Assets/Scripts/AIStats.cs +++ b/Assets/Scripts/AIStats.cs @@ -3,4 +3,10 @@ using UnityEngine; [CreateAssetMenu] public class AIStats : ScriptableObject { public float closeEnough = 1f; + + [field: SerializeField] + public AnimationCurve ThrownCurve { get; } + + [field: SerializeField] + public float ThrownDurationPerMeter { get; } } \ No newline at end of file diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 72f9457..51b1034 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -39,7 +39,7 @@ public class Entity : MonoBehaviour protected virtual void FixedUpdate() { //TODO sqrMagnitude? - if (beingPushed && rb.velocity.magnitude < stats.MinVelocityWhenPushed) { + if (beingPushed && rb.velocity.magnitude < stats.MinVelocityWhenThrown) { rb.velocity = Vector2.zero; beingPushed = false; } diff --git a/Assets/Scripts/EntityStats.cs b/Assets/Scripts/EntityStats.cs index 654e200..6f841c8 100644 --- a/Assets/Scripts/EntityStats.cs +++ b/Assets/Scripts/EntityStats.cs @@ -2,5 +2,5 @@ public class EntityStats { [field: SerializeField] [field: Min(0f)] - public float MinVelocityWhenPushed { get; private set; } = 5f; + public float MinVelocityWhenThrown { get; private set; } = 5f; } \ No newline at end of file diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 21dc09f..0cb8f80 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -91,12 +91,6 @@ public class PlayerMovement : MonoBehaviour { safeZone = null; } - void SetRigidbodyEnabled(bool enabled) { - rb.velocity = Vector2.zero; - rb.angularVelocity = 0f; - rb.isKinematic = !enabled; - } - #endregion #region States @@ -171,7 +165,7 @@ public class PlayerMovement : MonoBehaviour { base.EnterState(); safeZone.EnterSafeZone(); - playerMovement.SetRigidbodyEnabled(false); + playerMovement.rb.SetEnabled(false); } protected override BaseState Transition() => new ImmobileMovementState(playerMovement); @@ -190,7 +184,7 @@ public class PlayerMovement : MonoBehaviour { base.EnterState(); safeZone.ExitSafeZone(); - playerMovement.SetRigidbodyEnabled(true); + playerMovement.rb.SetEnabled(true); } protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t); diff --git a/Assets/Scripts/Utils.cs b/Assets/Scripts/Utils.cs index 32f7c6b..f2fdfd3 100644 --- a/Assets/Scripts/Utils.cs +++ b/Assets/Scripts/Utils.cs @@ -1,4 +1,5 @@ -using UnityEngine.InputSystem; +using UnityEngine; +using UnityEngine.InputSystem; public static class Utils { public static bool WasPressedThisFrame(this InputAction.CallbackContext ctx, ref bool lastValue) { @@ -8,4 +9,10 @@ public static class Utils { return wasJustPressed; } + + public static void SetEnabled(this Rigidbody2D rb, bool enabled) { + rb.velocity = Vector2.zero; + rb.angularVelocity = 0f; + rb.isKinematic = !enabled; + } } \ No newline at end of file