using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.Pool;
public class Body : MonoBehaviour
{
private ObjectPool
m_Pool;
public Transform m_Pelvis;
public int m_DespawnTimer;
public bool m_JDActive;
public bool m_RandomActuations;
private float m_Noise;
private float m_NoiseX;
private float m_NoiseY;
private float m_NoiseZ;
[Header("Body Parts")]
public Transform m_Head;
public Transform m_Spine;
public Transform m_Left_Thigh;
public Transform m_Left_Leg;
public Transform m_Left_Foot;
public Transform m_Right_Thigh;
public Transform m_Right_Leg;
public Transform m_Right_Foot;
public Transform m_Left_Arm;
public Transform m_Left_Forearm;
public Transform m_Right_Arm;
public Transform m_Right_Forearm;
[Range(-1, 1)]
public float t;
private JointDriveController m_JDController;
private void OnEnable()
{
if(m_JDController == null && m_JDActive) {
m_JDController = this.GetComponent();
m_JDController.SetupBodyPart(m_Head);
m_JDController.SetupBodyPart(m_Spine);
m_JDController.SetupBodyPart(m_Left_Thigh);
m_JDController.SetupBodyPart(m_Left_Leg);
m_JDController.SetupBodyPart(m_Left_Foot);
m_JDController.SetupBodyPart(m_Right_Thigh);
m_JDController.SetupBodyPart(m_Right_Leg);
m_JDController.SetupBodyPart(m_Right_Foot);
m_JDController.SetupBodyPart(m_Left_Arm);
m_JDController.SetupBodyPart(m_Left_Forearm);
m_JDController.SetupBodyPart(m_Right_Arm);
m_JDController.SetupBodyPart(m_Right_Forearm);
}
m_NoiseX = Random.Range(-200, 200);
m_NoiseY = Random.Range(-200, 200);
m_NoiseZ = Random.Range(-200, 200);
if(!m_RandomActuations)
StartCoroutine(DespawnTimer());
//StartCoroutine(DespawnTimer());
}
public void SetPool(ObjectPool pool)
{
m_Pool = pool;
}
IEnumerator DespawnTimer()
{
yield return new WaitForSecondsRealtime(m_DespawnTimer);
m_Pool.Release(this);
}
private void FixedUpdate(){
if(m_RandomActuations)
RandomActutations();
}
public void RandomActutations(){
var bpDict = m_JDController.bodyPartsDict;
m_Noise += Time.fixedDeltaTime * 2;
var counter = 0;
foreach (var bodyPart in bpDict.Values){
m_NoiseX = Random.Range(-2, 2);
m_NoiseY = Random.Range(-2, 2);
m_NoiseZ = Random.Range(-2, 2);
bodyPart.SetJointTargetRotation(m_NoiseX, m_NoiseY, m_NoiseZ);
//bodyPart.SetJointStrength(1);
counter++;
}
}
}