Started adding AIEntity's ThrownState

This commit is contained in:
Jason Durand 01 2022-04-02 15:50:30 -04:00
parent 25a10047ad
commit a7a29f4cce
6 changed files with 79 additions and 11 deletions

View File

@ -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{ class SeekState : BaseStateAI{
public SeekState(AIEntity entity) : base(entity){ public SeekState(AIEntity entity) : base(entity){

View File

@ -3,4 +3,10 @@ using UnityEngine;
[CreateAssetMenu] [CreateAssetMenu]
public class AIStats : ScriptableObject { public class AIStats : ScriptableObject {
public float closeEnough = 1f; public float closeEnough = 1f;
[field: SerializeField]
public AnimationCurve ThrownCurve { get; }
[field: SerializeField]
public float ThrownDurationPerMeter { get; }
} }

View File

@ -39,7 +39,7 @@ public class Entity : MonoBehaviour
protected virtual void FixedUpdate() { protected virtual void FixedUpdate() {
//TODO sqrMagnitude? //TODO sqrMagnitude?
if (beingPushed && rb.velocity.magnitude < stats.MinVelocityWhenPushed) { if (beingPushed && rb.velocity.magnitude < stats.MinVelocityWhenThrown) {
rb.velocity = Vector2.zero; rb.velocity = Vector2.zero;
beingPushed = false; beingPushed = false;
} }

View File

@ -2,5 +2,5 @@
public class EntityStats { public class EntityStats {
[field: SerializeField] [field: Min(0f)] [field: SerializeField] [field: Min(0f)]
public float MinVelocityWhenPushed { get; private set; } = 5f; public float MinVelocityWhenThrown { get; private set; } = 5f;
} }

View File

@ -91,12 +91,6 @@ public class PlayerMovement : MonoBehaviour {
safeZone = null; safeZone = null;
} }
void SetRigidbodyEnabled(bool enabled) {
rb.velocity = Vector2.zero;
rb.angularVelocity = 0f;
rb.isKinematic = !enabled;
}
#endregion #endregion
#region States #region States
@ -171,7 +165,7 @@ public class PlayerMovement : MonoBehaviour {
base.EnterState(); base.EnterState();
safeZone.EnterSafeZone(); safeZone.EnterSafeZone();
playerMovement.SetRigidbodyEnabled(false); playerMovement.rb.SetEnabled(false);
} }
protected override BaseState Transition() => new ImmobileMovementState(playerMovement); protected override BaseState Transition() => new ImmobileMovementState(playerMovement);
@ -190,7 +184,7 @@ public class PlayerMovement : MonoBehaviour {
base.EnterState(); base.EnterState();
safeZone.ExitSafeZone(); safeZone.ExitSafeZone();
playerMovement.SetRigidbodyEnabled(true); playerMovement.rb.SetEnabled(true);
} }
protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t); protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t);

View File

@ -1,4 +1,5 @@
using UnityEngine.InputSystem; using UnityEngine;
using UnityEngine.InputSystem;
public static class Utils { public static class Utils {
public static bool WasPressedThisFrame(this InputAction.CallbackContext ctx, ref bool lastValue) { public static bool WasPressedThisFrame(this InputAction.CallbackContext ctx, ref bool lastValue) {
@ -8,4 +9,10 @@ public static class Utils {
return wasJustPressed; return wasJustPressed;
} }
public static void SetEnabled(this Rigidbody2D rb, bool enabled) {
rb.velocity = Vector2.zero;
rb.angularVelocity = 0f;
rb.isKinematic = !enabled;
}
} }