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

235 lines
6.4 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;
2023-01-03 10:14:39 +08:00
using System.Collections.Generic;
2022-11-02 00:25:43 +08:00
using static PlayerController;
2022-11-23 20:26:53 +08:00
using static Item;
2022-11-02 00:25:43 +08:00
public class PickAndThrow : Script
{
2022-11-04 17:35:09 +08:00
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
public Vector3 cameraArmOffSet = new Vector3(0.0f, 0.25f, 0.0f);
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 Transform itemTransform;
private RigidBody itemRidigBody;
private Collider itemCollider;
2022-11-23 20:26:53 +08:00
private Item itemScript;
private Transform raccoonHoldLocation;
2022-11-21 00:12:09 +08:00
private ThirdPersonCamera tpc;
private float lastXDir;
private float lastZDir;
private bool inRange = false;
public bool throwItem = false;
[Tooltip("Lenght of ray")]
public float rayDistance = 1;
2022-11-21 00:12:09 +08:00
2022-11-02 00:25:43 +08:00
protected override void awake()
{
pc = GetScript<PlayerController>();
2022-11-21 00:12:09 +08:00
if(!pc)
Debug.LogError("PLAYER CONTROLLER EMPTY");
2022-11-21 00:12:09 +08:00
raccoonHoldLocation = GetComponentInChildren<Transform>();
2022-11-21 21:01:44 +08:00
if (!raccoonHoldLocation)
Debug.LogError("CHILD EMPTY");
2022-11-21 00:12:09 +08:00
tpc = GetScriptInChildren<ThirdPersonCamera>();
if(!tpc)
Debug.LogError("TPC EMPTY");
2022-11-21 00:12:09 +08:00
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();
CastRay();
if (pc && itemRidigBody && itemTransform && itemCollider)
2022-11-02 00:25:43 +08:00
{
if (pc.holdItem)
{
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
itemTransform.LocalRotation = pc.tranform.LocalRotation;
itemRidigBody.LinearVelocity = Vector3.Zero;
itemRidigBody.AngularVelocity = Vector3.Zero;
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
2022-11-21 00:12:09 +08:00
pc.isAiming = true;
pc.camArm.ArmLength = aimingLength;
pc.camArm.TargetOffset = cameraArmOffSet;
2022-11-21 00:12:09 +08:00
}
if (Input.GetMouseButtonUp(Input.MouseCode.LeftButton) && pc.isAiming)
{
2022-11-23 00:44:27 +08:00
Audio.PlaySFXOnce2D("event:/Raccoon/raccoon_throw");
itemRidigBody.IsGravityEnabled = true;
itemCollider.GetCollisionShape(0).IsTrigger = false;
2022-11-21 00:12:09 +08:00
pc.isAiming = false;
pc.camArm.TargetOffset = Vector3.Zero;
if (tpc)
2022-11-21 00:12:09 +08:00
pc.camArm.ArmLength = tpc.armLength;
pc.holdItem = false;
inRange = false;
throwItem = true;
2022-11-21 00:12:09 +08:00
timer = 0.0f;
}
2022-11-23 20:26:53 +08:00
if (Input.GetMouseButtonDown(Input.MouseCode.RightButton) && !pc.isAiming)
{
pc.holdItem = false;
inRange = false;
itemRidigBody.IsGravityEnabled = true;
itemCollider.GetCollisionShape(0).IsTrigger = false;
ResetItemObject();
}
2022-11-23 20:26:53 +08:00
if (Input.GetMouseButtonDown(Input.MouseCode.RightButton) && pc.isAiming)
{
pc.isAiming = false;
pc.camArm.TargetOffset = Vector3.Zero;
2022-11-23 20:26:53 +08:00
if (tpc)
pc.camArm.ArmLength = tpc.armLength;
}
}
else if (!pc.holdItem)
{
itemRidigBody.IsGravityEnabled = true;
itemCollider.GetCollisionShape(0).IsTrigger = false;
}
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
}
protected override void fixedUpdate()
{
if (throwItem && itemRidigBody && pc)
{
if (itemScript)
{
Vector3 vec = new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir);
if (itemScript.currCategory == ItemCategory.LIGHT)
itemRidigBody.AddForce(vec * 0.2f);
if (itemScript.currCategory == ItemCategory.MEDIUM)
itemRidigBody.AddForce(vec * 0.75f);
if (itemScript.currCategory == ItemCategory.HEAVY)
itemRidigBody.AddForce(vec);
}
itemRidigBody.LinearVelocity += pc.rb.LinearVelocity;
throwItem = false;
ResetItemObject();
}
}
private void ResetItemObject()
{
itemRidigBody = null;
itemTransform = null;
itemCollider = null;
2022-11-23 20:26:53 +08:00
itemScript = null;
item = new GameObject();
}
private void RetrieveItemComponets()
{
//get the transform of the given item
if (item.GetScript<Item>() && !itemTransform && !itemRidigBody)
{
itemRidigBody = item.GetComponent<RigidBody>();
if (!itemRidigBody)
Debug.Log("Item rb EMPTY");
else
{
itemRidigBody.IsGravityEnabled = false;
}
itemTransform = item.GetComponent<Transform>();
if (!itemTransform)
Debug.Log("Item transform EMPTY");
else
{
itemTransform.LocalEulerAngles = Vector3.Zero;
}
itemCollider = item.GetComponent<Collider>();
if (!itemCollider)
Debug.Log("Item collider EMPTY");
else
{
itemCollider.GetCollisionShape(0).IsTrigger = true;
}
2022-11-23 20:26:53 +08:00
itemScript = item.GetScript<Item>();
if(!itemScript)
Debug.Log("Item script EMPTY");
}
}
2022-11-13 21:56:28 +08:00
private void CalculateDir()
{
if (pc && pc.cam)
2022-11-13 21:56:28 +08:00
{
Vector3 camerAixs = pc.cam.GetForward();
2022-11-13 21:56:28 +08:00
camerAixs.y = 0;
camerAixs.Normalise();
lastXDir = camerAixs.x;
lastZDir = camerAixs.z;
}
}
private void CastRay()
2022-11-21 00:12:09 +08:00
{
if (pc != null)
{
Vector3 dirNor = pc.tranform.Forward;
Vector3 playerRayPos = pc.tranform.GlobalPosition;
playerRayPos.y += 0.05f;
dirNor.Normalise();
2023-01-03 10:14:39 +08:00
List<RaycastHit> rayList1 = Physics.Raycast(new Ray(playerRayPos, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(22.5f))), rayDistance, false);
List<RaycastHit> rayList2 = Physics.Raycast(new Ray(playerRayPos, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(-22.5f))), rayDistance, false);
List<RaycastHit> rayList3 = Physics.Raycast(new Ray(playerRayPos, dirNor), rayDistance * 0.75f, false);
RaycastHit ray1 = rayList1[0];
RaycastHit ray2 = rayList2[0];
RaycastHit ray3 = rayList3[0];
inRange = CheckForItem(ray1) || CheckForItem(ray2) || CheckForItem(ray3);
}
2022-11-21 00:12:09 +08:00
}
private bool CheckForItem(RaycastHit ray)
{
if (ray.Hit)
{
if (ray.Other.Value.GetScript<Item>() && !pc.holdItem)
{
item = ray.Other.Value;
return true;
}
else
return false;
}
return false;
}
2022-11-02 00:25:43 +08:00
}