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

180 lines
4.5 KiB
C#
Raw Normal View History

2022-11-02 00:25:43 +08:00
using SHADE;
2022-11-21 00:12:09 +08:00
using SHADE_Scripting;
2022-11-02 00:25:43 +08:00
using System;
using static PlayerController;
public class PickAndThrow : Script
{
2022-11-04 17:35:09 +08:00
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
2022-11-13 21:56:28 +08:00
public GameObject item { get; set; }
2022-11-21 00:12:09 +08:00
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;
2022-11-21 00:12:09 +08:00
private Transform playerTran;
private ThirdPersonCamera tpc;
private float lastXDir;
private float lastZDir;
private bool inRange = false;
2022-11-21 00:12:09 +08:00
2022-11-02 00:25:43 +08:00
protected override void awake()
{
2022-11-21 00:12:09 +08:00
playerTran = GetComponent<Transform>();
2022-11-21 21:01:44 +08:00
if (!playerTran)
2022-11-21 00:12:09 +08:00
Debug.Log("PLAYERTRANSFORM EMPTY"); ;
2022-11-02 00:25:43 +08:00
pc = GetScript<PlayerController>();
2022-11-21 00:12:09 +08:00
if(!pc)
Debug.Log("PLAYER CONTROLLER EMPTY");
raccoonHoldLocation = GetComponentInChildren<Transform>();
2022-11-21 21:01:44 +08:00
if (!raccoonHoldLocation)
Debug.Log("CHILD EMPTY");
2022-11-21 00:12:09 +08:00
tpc = GetScriptInChildren<ThirdPersonCamera>();
if(!tpc)
Debug.Log("TPC EMPTY");
timer = delayTimer;
2022-11-02 00:25:43 +08:00
}
protected override void update()
{
2022-11-21 00:12:09 +08:00
if(timer <= delayTimer)
timer += Time.DeltaTimeF;
2022-11-13 21:56:28 +08:00
CalculateDir();
if (pc && itemRidibody && itemTransform)
2022-11-02 00:25:43 +08:00
{
if (pc.holdItem)
{
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
2022-11-21 00:12:09 +08:00
itemTransform.LocalRotation = playerTran.LocalRotation;
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
2022-11-21 00:12:09 +08:00
Debug.Log("AIMING");
pc.isAiming = true;
pc.camArm.ArmLength = aimingLength;
}
if (Input.GetMouseButtonUp(Input.MouseCode.LeftButton) && pc.isAiming)
{
2022-11-21 21:01:44 +08:00
Audio.PlayBGMOnce2D("event:/Raccoon/raccoon_throw");
2022-11-21 00:12:09 +08:00
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();
2022-11-21 00:12:09 +08:00
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;
2022-11-02 00:25:43 +08:00
}
2022-11-13 21:56:28 +08:00
2022-11-21 00:12:09 +08:00
if (timer > delayTimer && pc && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
2022-11-13 21:56:28 +08:00
{
2022-11-21 00:12:09 +08:00
if (pc.currentState == RaccoonStates.WALKING || pc.currentState == RaccoonStates.IDLE)
{
pc.holdItem = true;
RetrieveItemComponets();
}
2022-11-13 21:56:28 +08:00
}
2022-11-02 00:25:43 +08:00
}
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;
}
}
}
2022-11-13 21:56:28 +08:00
private void CalculateDir()
{
if (cam == null)
cam = GetComponentInChildren<Camera>();
else if (cam)
2022-11-13 21:56:28 +08:00
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
lastXDir = camerAixs.x;
lastZDir = camerAixs.z;
}
}
2022-11-21 00:12:09 +08:00
private void DelayCheck()
{
timer += Time.DeltaTimeF;
}
2022-11-02 00:25:43 +08:00
protected override void onCollisionEnter(CollisionInfo info)
{
}
protected override void onTriggerEnter(CollisionInfo info)
{
2022-11-21 00:12:09 +08:00
if (info.GameObject.GetScript<Item>() && !pc.holdItem)
2022-11-02 00:25:43 +08:00
{
item = info.GameObject;
inRange = true;
2022-11-02 00:25:43 +08:00
}
}
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;
}
}
2022-11-02 00:25:43 +08:00
}