You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

169 lines
5.5 KiB

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
namespace Moloch
{
public class PullingState : BaseState
{
private LocomotionSM m_SM;
private GameObject m_SelectedTarget;
private bool m_Owned;
private bool m_Reached;
private float m_WaitAnimation;
private bool m_isPulling;
public PullingState(LocomotionSM machine) : base("Pulling", machine) { m_SM = machine; }
public override void Enter()
{
base.Enter();
m_WaitAnimation = Random.Range(2f, 5f);
m_Reached = false;
m_Owned = false;
m_SelectedTarget = null;
m_isPulling = false;
m_SM.m_NavMeshAgent.SetSpeed(Random.Range(0.5f, 1.5f));
m_SM.m_NavMeshAgent.SetAcceleration(Random.Range(0.5f, 1f));
}
public override void UpdateLogic()
{
base.UpdateLogic();
// Search for target. If nothing found, move to center
if(m_SelectedTarget == null)
{
AttemptFindTarget();
}
else
{
if (!m_Owned)
{
CheckIfOwned();
}
else
{
if(!m_Reached)
{
WalkToTarget();
}
if(m_Reached)
{
// Grab object
if(m_WaitAnimation >= 0.1)
{
m_WaitAnimation -= Time.fixedDeltaTime;
Debug.Log($"Waiting for animation {m_WaitAnimation}");
}
else
{
// Grab Object
if(!m_isPulling) {
m_SM.m_Animator.SetBool("isCrouchBack", true);
m_SM.m_NavMeshAgent.RandomWalk();
m_isPulling=true;
}
else
{
if (!m_SM.m_NavMeshAgent.HasReachedWaypoint())
{
m_SM.m_NavMeshAgent.RotationOff();
m_SM.m_IKManager.SetTarget(m_SelectedTarget);
m_SM.m_IKManager.ConnectJoint();
m_SM.m_NavMeshAgent.StartMoving();
m_SM.m_Animator.SetFloat("Velocity", m_SM.m_NavMeshAgent.m_Agent.velocity.magnitude / 2.5f);
}
else
{
m_SM.m_IKManager.ReleaseJoint();
m_SM.SwitchState(m_SM.m_idleState);
}
}
}
}
}
}
}
private void AttemptFindTarget()
{
// Search for it.
if (m_SM.m_PhysicsOverlapManager.FindNearestTarget() == null)
{
// If not found, move to center
Debug.Log($"Move to center");
m_SM.m_NavMeshAgent.m_Agent.SetDestination(new Vector3(0,0,0));
m_SM.m_NavMeshAgent.StartMoving();
m_SM.m_Animator.SetFloat("Velocity", m_SM.m_NavMeshAgent.m_Agent.velocity.magnitude / 2.5f);
// If the agent has reached the center, but nothing is found => exit state
if (m_SM.m_NavMeshAgent.HasReachedWaypoint())
{
m_SM.SwitchState(m_SM.m_wanderState);
}
}
else
{
// Else, assign target.
m_SelectedTarget = m_SM.m_PhysicsOverlapManager.FindNearestTarget().gameObject;
}
}
private bool CheckIfOwned()
{
if (CorpseManagerSingleton.Instance.AddCorpseOwned(m_SelectedTarget))
{
m_Owned = true;
return true;
}
else
{
m_SelectedTarget = null;
return false;
}
}
private void WalkToTarget()
{
// Move to target location
m_SM.m_NavMeshAgent.SetWaypoint(m_SelectedTarget);
m_SM.m_NavMeshAgent.MoveToWaypoint();
if (!m_SM.m_NavMeshAgent.HasReachedWaypoint())
{
m_SM.m_Animator.SetFloat("Velocity", m_SM.m_NavMeshAgent.m_Agent.velocity.magnitude / 2.5f);
Debug.Log($"Moving to target {m_SelectedTarget.name}");
}
else
{
m_SM.m_Animator.SetFloat("Velocity", 0);
m_SM.m_NavMeshAgent.StopMoving();
m_SM.m_Animator.SetBool("isCrouching", true);
m_Reached = true;
}
}
public override void Exit()
{
base.Exit();
m_SM.m_Animator.SetFloat("Velocity", 0);
m_SM.m_NavMeshAgent.StopMoving();
m_SM.m_Animator.SetBool("isCrouchBack", false);
m_SM.m_Animator.SetBool("isCrouching", false);
}
}
}