using SHADE; using SHADE_Scripting; using SHADE_Scripting.Audio; using System; using System.Collections.Generic; using static PlayerController; using static Item; public class PickAndThrow : Script { public Vector3 throwForce = new Vector3(10.0f, 8.0f, 10.0f); public Vector3 cameraArmOffSet = new Vector3(0.0f, 0.25f, 0.0f); public GameObject item { get; set; } 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; private Item itemScript; private Transform raccoonHoldLocation; private ThirdPersonCamera tpc; private float lastXDir; private float lastZDir; private bool inRange = false; public bool throwItem = false; private Vector3 prevTargetOffSet; public GameObject silhouettePlayer; public Renderable silhouettePlayerRend; public GameObject silhouetteBag; public Renderable silhouetteBagRend; [Tooltip("Lenght of ray")] public float rayDistance = 1; [Tooltip("Height of ray")] public float rayHeight = 0.1f; [Tooltip("FOV when you aim")] public float aimingFOV = 50; [Tooltip("Default FOV")] public float defaultFOV = 45; protected override void awake() { pc = GetScript(); if(!pc) Debug.LogError("PLAYER CONTROLLER EMPTY"); raccoonHoldLocation = GetComponentInChildren(); if (!raccoonHoldLocation) Debug.LogError("CHILD EMPTY"); tpc = GetScriptInChildren(); if(!tpc) Debug.LogError("TPC EMPTY"); if(!silhouettePlayer) Debug.LogError("silhouettePlayer EMPTY"); else silhouettePlayerRend = silhouettePlayer.GetComponent(); if (!silhouetteBag) Debug.LogError("silhouetteBag EMPTY"); else silhouetteBagRend = silhouettePlayer.GetComponent(); AudioHandler.audioClipHandlers["SFXThrow"] = Audio.CreateAudioClip("event:/Raccoon/raccoon_throw"); timer = delayTimer; } protected override void update() { if (GameManager.Instance.GamePause) { return; } if (silhouettePlayerRend && silhouetteBagRend) { //wait for daniel //0.00075f; /* Vector3 dis = Camera.GetMainCamera().Position - GameObject.GetComponent().LocalPosition; float disSqr = dis.GetSqrMagnitude(); Debug.Log($"DIS :{disSqr}"); float ratio = System.Math.Clamp(1 - (disSqr / (1 + disSqr)), 0, 1.0f); silhouettePlayerRend.Material.SetProperty("data.offset", ratio * 0.00075f); silhouetteBagRend.Material.SetProperty("data.offset", ratio * 0.00075f);*/ } if (timer <= delayTimer) timer += Time.DeltaTimeF; CalculateDir(); CastRay(); if (pc && itemRidigBody && itemTransform && itemCollider) { 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)) { pc.isAiming = true; pc.camArm.ArmLength = aimingLength; prevTargetOffSet = pc.camArm.TargetOffset; pc.camArm.TargetOffset = cameraArmOffSet; pc.cam.FOV = aimingFOV; } if (Input.GetMouseButtonUp(Input.MouseCode.LeftButton) && pc.isAiming) { AudioHandler.audioClipHandlers["SFXThrow"].Play(); itemRidigBody.IsGravityEnabled = true; itemCollider.GetCollisionShape(0).IsTrigger = false; pc.isAiming = false; pc.camArm.TargetOffset = prevTargetOffSet; pc.cam.FOV = defaultFOV; if (tpc) pc.camArm.ArmLength = tpc.armLength; pc.holdItem = false; inRange = false; throwItem = true; timer = 0.0f; } if (Input.GetMouseButtonDown(Input.MouseCode.RightButton) && !pc.isAiming) { pc.holdItem = false; inRange = false; itemRidigBody.IsGravityEnabled = true; itemCollider.GetCollisionShape(0).IsTrigger = false; ResetItemObject(); } if (Input.GetMouseButtonDown(Input.MouseCode.RightButton) && pc.isAiming) { pc.isAiming = false; pc.cam.FOV = defaultFOV; pc.camArm.TargetOffset = prevTargetOffSet; if (tpc) pc.camArm.ArmLength = tpc.armLength; } } else if (!pc.holdItem) { itemRidigBody.IsGravityEnabled = true; itemCollider.GetCollisionShape(0).IsTrigger = false; } } 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(); } } } protected override void fixedUpdate() { if (GameManager.Instance.GamePause) { return; } if (throwItem && itemRidigBody && pc) { if (itemScript) { Vector3 vec = new Vector3(throwForce.x * lastXDir, throwForce.y + (throwForce.y * GetPitchRatioRange()), 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; itemScript = null; item = new GameObject(); } private void RetrieveItemComponets() { //get the transform of the given item if (item.GetScript() && !itemTransform && !itemRidigBody) { itemRidigBody = item.GetComponent(); if (!itemRidigBody) Debug.Log("Item rb EMPTY"); else { itemRidigBody.IsGravityEnabled = false; } itemTransform = item.GetComponent(); if (!itemTransform) Debug.Log("Item transform EMPTY"); else { itemTransform.LocalEulerAngles = Vector3.Zero; } itemCollider = item.GetComponent(); if (!itemCollider) Debug.Log("Item collider EMPTY"); else { itemCollider.GetCollisionShape(0).IsTrigger = true; } itemScript = item.GetScript(); if(!itemScript) Debug.Log("Item script EMPTY"); } } private void CalculateDir() { if (pc && pc.cam) { Vector3 camerAixs = pc.cam.GetForward(); camerAixs.y = 0; camerAixs.Normalise(); lastXDir = camerAixs.x; lastZDir = camerAixs.z; } } private void CastRay() { if (pc != null) { Vector3 dirNor = pc.tranform.Forward; Vector3 playerRayPos = pc.tranform.GlobalPosition; playerRayPos.y += rayHeight; dirNor.Normalise(); List rayList1 = Physics.Raycast(new Ray(playerRayPos, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(22.5f))), rayDistance, false, (ushort)65535); List rayList2 = Physics.Raycast(new Ray(playerRayPos, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(-22.5f))), rayDistance, false, (ushort)65535); List rayList3 = Physics.Raycast(new Ray(playerRayPos, dirNor), rayDistance * 0.75f, false, (ushort)65535); if (rayList1.Count > 0) { RaycastHit ray1 = rayList1[0]; inRange = CheckForItem(ray1); return; } else if (rayList2.Count > 0) { RaycastHit ray2 = rayList2[0]; inRange = CheckForItem(ray2); return; } else if (rayList3.Count > 0) { RaycastHit ray3 = rayList3[0]; inRange = CheckForItem(ray3); return; } else { inRange = false; } } } private bool CheckForItem(RaycastHit ray) { if (ray.Hit) { if (ray.Other.Value.GetScript() && !pc.holdItem) { item = ray.Other.Value; return true; } else return false; } return false; } private float GetPitchRatioRange() { return (pc.camArm.Pitch - tpc.pitchUpperClamp) / (tpc.pitchLowerClamp - tpc.pitchUpperClamp); } }