SHADE_Y3/Assets/Scripts/Gameplay/Player/SC_PickAndThrow.cs

180 lines
4.5 KiB
C#

using SHADE;
using SHADE_Scripting;
using System;
using static PlayerController;
public class PickAndThrow : Script
{
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
public GameObject item { get; set; }
public float delayTimer = 1.0f;
public float aimingLength = 1.5f;
private float timer;
private PlayerController pc;
private Camera cam;
private Transform itemTransform;
private RigidBody itemRidibody;
private Transform raccoonHoldLocation;
private Transform playerTran;
private ThirdPersonCamera tpc;
private float lastXDir;
private float lastZDir;
private bool inRange = false;
protected override void awake()
{
playerTran = GetComponent<Transform>();
if (!playerTran)
Debug.Log("PLAYERTRANSFORM EMPTY"); ;
pc = GetScript<PlayerController>();
if(!pc)
Debug.Log("PLAYER CONTROLLER EMPTY");
raccoonHoldLocation = GetComponentInChildren<Transform>();
if (!raccoonHoldLocation)
Debug.Log("CHILD EMPTY");
tpc = GetScriptInChildren<ThirdPersonCamera>();
if(!tpc)
Debug.Log("TPC EMPTY");
timer = delayTimer;
}
protected override void update()
{
if(timer <= delayTimer)
timer += Time.DeltaTimeF;
CalculateDir();
if (pc && itemRidibody && itemTransform)
{
if (pc.holdItem)
{
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
itemTransform.LocalRotation = playerTran.LocalRotation;
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
Debug.Log("AIMING");
pc.isAiming = true;
pc.camArm.ArmLength = aimingLength;
}
if (Input.GetMouseButtonUp(Input.MouseCode.LeftButton) && pc.isAiming)
{
Audio.PlayBGMOnce2D("event:/Raccoon/raccoon_throw");
pc.isAiming = false;
if(tpc)
pc.camArm.ArmLength = tpc.armLength;
pc.holdItem = false;
inRange = false;
itemRidibody.IsGravityEnabled = true;
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
ResetItemObject();
timer = 0.0f;
}
if (Input.GetMouseButtonDown(Input.MouseCode.RightButton))
{
pc.holdItem = false;
inRange = false;
itemRidibody.IsGravityEnabled = true;
ResetItemObject();
}
}
else if (!pc.holdItem)
itemRidibody.IsGravityEnabled = true;
}
if (timer > delayTimer && pc && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
if (pc.currentState == RaccoonStates.WALKING || pc.currentState == RaccoonStates.IDLE)
{
pc.holdItem = true;
RetrieveItemComponets();
}
}
}
private void ResetItemObject()
{
itemRidibody = null;
itemTransform = null;
item = new GameObject();
}
private void RetrieveItemComponets()
{
//get the transform of the given item
if (item.GetScript<Item>() && itemTransform == null && itemRidibody == null)
{
itemRidibody = item.GetComponent<RigidBody>();
if (itemRidibody == null)
Debug.Log("Item rb EMPTY");
else
{
itemRidibody.IsGravityEnabled = false;
itemRidibody.LinearVelocity = Vector3.Zero;
itemRidibody.AngularVelocity = Vector3.Zero;
}
itemTransform = item.GetComponent<Transform>();
if (itemTransform == null)
Debug.Log("Item transform EMPTY");
else
{
itemTransform.LocalEulerAngles = Vector3.Zero;
}
}
}
private void CalculateDir()
{
if (cam == null)
cam = GetComponentInChildren<Camera>();
else if (cam)
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
lastXDir = camerAixs.x;
lastZDir = camerAixs.z;
}
}
private void DelayCheck()
{
timer += Time.DeltaTimeF;
}
protected override void onCollisionEnter(CollisionInfo info)
{
}
protected override void onTriggerEnter(CollisionInfo info)
{
if (info.GameObject.GetScript<Item>() && !pc.holdItem)
{
item = info.GameObject;
inRange = true;
}
}
protected override void onTriggerStay(CollisionInfo info)
{
//Debug.Log("STAY");
}
protected override void onTriggerExit(CollisionInfo info)
{
//Debug.Log("EXIT");
if (info.GameObject.GetScript<Item>() != null && !pc.holdItem)
{
inRange = false;
}
}
}