984 lines
31 KiB
C#
984 lines
31 KiB
C#
using Photon.Pun;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using UnityEngine.Animations.Rigging;
|
|
using UnityEngine.UI;
|
|
using static UnityEngine.Rendering.DebugUI;
|
|
|
|
public class PlayerController : MonoBehaviourPunCallbacks
|
|
{
|
|
public CharacterController controller;
|
|
InventoryManager inventoryManager;
|
|
public PlayerNetwork playerNetwork;
|
|
public Animator anim;
|
|
public RuntimeAnimatorController defaultController;
|
|
private Transform cam;
|
|
|
|
public Transform camFollowTarget;
|
|
|
|
[Header("Movement")]
|
|
public float speed = 6f;
|
|
public bool isGrounded => !anim.GetBool("jumping");
|
|
|
|
public float sprintingMultiplier = 1.5f;
|
|
public float stamina;
|
|
public float maxStamina = 15;
|
|
public float staminaRechargeRate = 2;
|
|
public bool jumped;
|
|
public float jumpSpeed = 10f;
|
|
public GameObject umbrella;
|
|
float turnSmoothVelocity;
|
|
public float turnSmoothTime = 0.1f;
|
|
//public GameObject blobShadow;
|
|
[HideInInspector]
|
|
public Vector3 moveDirection;
|
|
public Vector3 movingVector;
|
|
public Vector3 crouchCamOffset;
|
|
|
|
[Header("Shooting")]
|
|
public float fireRate = 0.1f;
|
|
public bool aiming;
|
|
public float aimedFOV = 20;
|
|
public float normalFOV = 50;
|
|
public bool shooting;
|
|
public float aimingSpeedMultiplier = 0.5f;
|
|
public Transform aimSpine;
|
|
public MeleeListener meleeListener;
|
|
|
|
[SerializeField] private int m_activeWeaponSlot = -1;
|
|
|
|
/// <summary>
|
|
/// Active Weapon in hand
|
|
/// -1 = nothin
|
|
/// 0 = primary
|
|
/// 1 = secondary
|
|
/// 2 = handgun
|
|
/// 3 = throwable
|
|
/// </summary>
|
|
public int activeWeaponSlot
|
|
{
|
|
get
|
|
{
|
|
return m_activeWeaponSlot;
|
|
}
|
|
set
|
|
{
|
|
m_activeWeaponSlot = value;
|
|
UIManager.SetWeaponSlot(value);
|
|
if (value >= 0)
|
|
{
|
|
playerNetwork.SetActiveWeapon(WeaponTypeInHand);
|
|
UpdateShootingData(WeaponTypeInHand);
|
|
}
|
|
else
|
|
{
|
|
playerNetwork.SetActiveWeapon(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
public Rig RifleRig;
|
|
public Transform leftHandIK, m_leftHandIK;
|
|
public Rig AimRig;
|
|
public Transform lookAtTarget;
|
|
|
|
[Header("Throwables")]
|
|
public float throwingSpeed;
|
|
public float throwingSpeedMin, throwingSpeedMax;
|
|
public Transform throwableSpawnPoint;
|
|
public LineRenderer NadeTrajectoryRenderer;
|
|
public float NadeTrajectoryResolution = 0.5f;
|
|
public float NadeTrajectoryLength = 250;
|
|
Transform _lookAtTarget;
|
|
|
|
|
|
// -------------------------------------------------------------- VARS END -------------------------------------------------------------------
|
|
|
|
|
|
void Awake()
|
|
{
|
|
inventoryManager = GetComponent<InventoryManager>();
|
|
playerNetwork = GetComponent<PlayerNetwork>();
|
|
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
//Auto assign
|
|
if (anim == null) anim = GetComponent<Animator>();
|
|
if (controller == null) controller = GetComponent<CharacterController>();
|
|
if(meleeListener == null) meleeListener = GetComponentInChildren<MeleeListener>();
|
|
}
|
|
PlayerFX effects;
|
|
|
|
void Start()
|
|
{
|
|
if (!photonView.IsMine)
|
|
{
|
|
Destroy(this);
|
|
return;
|
|
}
|
|
_lookAtTarget = Camera.main.transform.GetChild(0);
|
|
CameraFollow.instance.target = camFollowTarget;
|
|
GameManager.localPlayer = this;
|
|
|
|
Application.targetFrameRate = 60;
|
|
cam = Camera.main.transform;
|
|
effects = GetComponent<PlayerFX>();
|
|
// SetupCameras();
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
|
|
stamina = maxStamina;
|
|
|
|
Debug.Log(GameManager.desiredGameMode);
|
|
//Setup Gamemode
|
|
if (GameManager.desiredGameMode == GameModes.Deathmatch.ToString())
|
|
{
|
|
Debug.Log("Preparing Deathmatch");
|
|
InventoryManager.instance.GiveDefaultLoots(GameManager.instance.DeathmatchDefaultInventory);
|
|
|
|
}
|
|
|
|
SettingsManager.instance.OnSettingsChanged.AddListener(OnSettingsChanged);
|
|
}
|
|
float movingSpeedMultiplier = 1;
|
|
void OnSettingsChanged()
|
|
{
|
|
movingSpeedMultiplier = SettingsManager.instance.moveSpeedSlider.value;
|
|
}
|
|
|
|
public bool reloading => anim.GetCurrentAnimatorClipInfo(1)[0].clip.name == "Reloading";
|
|
bool overShoulder = false;
|
|
|
|
bool weaponInHand => activeWeaponSlot == 0 ? (inventoryManager.FirstWeapon != null) : (activeWeaponSlot == 1 ? inventoryManager.SecondWeapon != null : (activeWeaponSlot == 2 ? inventoryManager.PrimaryWeapon != null : false));
|
|
// Update is called once per frame
|
|
bool throwInit = false;
|
|
|
|
void OnMouse0()
|
|
{
|
|
if(activeWeaponSlot >-1 && WeaponTypeInHand==null){return;}
|
|
if (WeaponTypeInHand != null && WeaponTypeInHand.isThrowable)
|
|
{
|
|
ToggleNadeTrajectory(true);
|
|
throwInit = true;
|
|
anim.SetBool("throw_start", true);
|
|
anim.SetBool("throw_end", false);
|
|
return;
|
|
}
|
|
shooting = true;
|
|
if (!aiming) { aimToggle(true); }
|
|
}
|
|
|
|
public bool isActive = true;
|
|
|
|
void HandleInput()
|
|
{
|
|
if(Input.mouseScrollDelta.y >0){
|
|
if(activeWeaponSlot >= 0){
|
|
SetActiveWeapon(activeWeaponSlot-1);
|
|
}else{
|
|
SetActiveWeapon(3);
|
|
}
|
|
}else if(Input.mouseScrollDelta.y <0){
|
|
if(activeWeaponSlot < 3){
|
|
SetActiveWeapon(activeWeaponSlot+1);
|
|
}else{
|
|
SetActiveWeapon(-1);
|
|
}
|
|
}
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
overShoulder = true;
|
|
aimToggle(true);
|
|
}
|
|
if (Input.GetMouseButtonUp(1))
|
|
{
|
|
overShoulder = false;
|
|
aimToggle(false);
|
|
|
|
}
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
OnMouse0();
|
|
}
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
if (throwInit && WeaponTypeInHand.isThrowable)
|
|
{
|
|
anim.SetBool("throw_start", false);
|
|
anim.SetBool("throw_end", true);
|
|
ToggleNadeTrajectory(false);
|
|
StartCoroutine(CoroutineThrowNade());
|
|
}
|
|
shooting = false;
|
|
if (!Input.GetMouseButton(1))
|
|
{
|
|
aimToggle(false);
|
|
}
|
|
throwInit = false;
|
|
|
|
readyToShoot = 2f;
|
|
}
|
|
#region INPUTS
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.C))
|
|
{
|
|
toggleCrouch();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
{
|
|
jump();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.R))
|
|
{
|
|
reload();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.G))
|
|
{
|
|
if (inventoryManager.EquippedThrowable != null)
|
|
{
|
|
if (activeWeaponSlot == 3)
|
|
{
|
|
inventoryManager.CycleThrowable();
|
|
}
|
|
activeWeaponSlot = 3;
|
|
|
|
UpdateWeapon();
|
|
}
|
|
}
|
|
|
|
// if(Input.GetKeyDown(KeyCode.G)){
|
|
// if(inventoryManager.EquippedThrowable != null){
|
|
// ToggleNadeTrajectory(true);
|
|
// }
|
|
// }
|
|
// if (Input.GetKeyUp(KeyCode.G))
|
|
// {
|
|
// ToggleNadeTrajectory(false);
|
|
// throwNade();
|
|
// }
|
|
|
|
if (Input.GetKeyDown(KeyCode.LeftShift))
|
|
{
|
|
sprinting = true;
|
|
}
|
|
if (Input.GetKeyUp(KeyCode.LeftShift))
|
|
{
|
|
sprinting = false;
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.X))
|
|
{
|
|
SetActiveWeapon(-1);
|
|
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
{
|
|
SetActiveWeapon(0);
|
|
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
{
|
|
SetActiveWeapon(1);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha3))
|
|
{
|
|
SetActiveWeapon(2);
|
|
}
|
|
// if(Input.GetKeyDown(KeyCode.Alpha4)){
|
|
// inventoryManager.CycleThrowable();
|
|
// UpdateWeapon();
|
|
// }
|
|
#endregion
|
|
}
|
|
|
|
public void SetActiveWeapon(int val){
|
|
activeWeaponSlot = val;
|
|
UpdateWeapon();
|
|
AutoReload();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!isActive) { return; }
|
|
|
|
if (!UIManager.isPaused) { HandleInput(); }
|
|
|
|
CameraFollow.instance.isAiming = (overShoulder && weaponInHand && (isGrounded || airTime > airTimeForAim));
|
|
lookAtTarget.position = Camera.main.transform.position + (Camera.main.transform.forward * 1000);
|
|
leftHandIK.SetLocalPositionAndRotation(m_leftHandIK.localPosition, m_leftHandIK.localRotation);
|
|
if (NadeTrajectoryRenderer.enabled)
|
|
{
|
|
UpdateNadeTrajectory();
|
|
}
|
|
|
|
shoot();
|
|
UpdateAnim();
|
|
UIManager.SetStamina(stamina, maxStamina);
|
|
if(punchCooldown >0){
|
|
punchCooldown -= Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!isActive) { return; }
|
|
move();
|
|
}
|
|
|
|
|
|
void UpdateShootingData(LootItemScriptableObject data)
|
|
{
|
|
if (data == null) { return; }
|
|
fireRate = data.fireRate;
|
|
}
|
|
|
|
public void OnHit(float damage)
|
|
{
|
|
|
|
}
|
|
|
|
public void OnJumpBoost(float boost)
|
|
{
|
|
if (photonView.IsMine)
|
|
{
|
|
moveDirection.y += boost;
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------------- NADES -------------------------------------------------------------------
|
|
void ToggleNadeTrajectory(bool val)
|
|
{
|
|
NadeTrajectoryRenderer.enabled = val;
|
|
if (val) { throwingSpeed = throwingSpeedMin; }
|
|
}
|
|
|
|
void UpdateNadeTrajectory()
|
|
{
|
|
|
|
if (throwingSpeed < throwingSpeedMax) { throwingSpeed += Time.deltaTime * 1.5f; }
|
|
int i = 0;
|
|
NadeTrajectoryRenderer.positionCount = Mathf.CeilToInt(NadeTrajectoryLength / NadeTrajectoryResolution);
|
|
Vector3 startPosition = throwableSpawnPoint.position;
|
|
Vector3 startVelocity = ThrowableStartVelocity;
|
|
NadeTrajectoryRenderer.SetPosition(i, startPosition);
|
|
for (float time = 0; time < NadeTrajectoryLength; time += NadeTrajectoryResolution)
|
|
{
|
|
if (i >= NadeTrajectoryRenderer.positionCount - 1) { break; }
|
|
i++;
|
|
|
|
Vector3 point = startPosition + time * startVelocity;
|
|
point.y = startPosition.y + startVelocity.y * time + (Physics.gravity.y / 2f * time * time);
|
|
NadeTrajectoryRenderer.SetPosition(i, point);
|
|
|
|
Vector3 lastPoint = NadeTrajectoryRenderer.GetPosition(i - 1);
|
|
if (Physics.Linecast(lastPoint, point))
|
|
{
|
|
NadeTrajectoryRenderer.positionCount = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Vector3 ThrowableStartVelocity => Camera.main.transform.forward * throwingSpeed + new Vector3(0, throwingSpeed / 2f, 0);
|
|
void throwNade()
|
|
{
|
|
if (inventoryManager.EquippedThrowable == null) { return; }
|
|
if (inventoryManager.Stock.GetValueOrDefault(inventoryManager.EquippedThrowable, 0) <= 0) { return; }
|
|
|
|
StartCoroutine(CoroutineThrowNade());
|
|
playerNetwork.OnNadeThrow();
|
|
}
|
|
bool throwing = false;
|
|
IEnumerator CoroutineThrowNade()
|
|
{
|
|
throwing = true;
|
|
int m_activeWeaponSlot = activeWeaponSlot;
|
|
activeWeaponSlot = -1;
|
|
UpdateWeapon();
|
|
anim.SetTrigger("Throw");
|
|
|
|
yield return new WaitForSeconds(0.2f);
|
|
GameObject nade = PhotonNetwork.Instantiate(inventoryManager.EquippedThrowable.spawnableName, throwableSpawnPoint.position, Quaternion.identity);
|
|
Destroy(nade.GetComponent<PickupItem>());
|
|
nade.GetComponent<Rigidbody>().velocity = ThrowableStartVelocity;
|
|
|
|
GetComponent<PlayerNetwork>().StartBomb(photonView.ViewID, nade, inventoryManager.EquippedThrowable);
|
|
|
|
inventoryManager.UseThrowable();
|
|
yield return new WaitForSeconds(0.6f);
|
|
|
|
activeWeaponSlot = m_activeWeaponSlot;
|
|
UpdateWeapon();
|
|
throwing = false;
|
|
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------- SHOOT -------------------------------------------------------------------
|
|
|
|
float st = 0;
|
|
public float airTime;
|
|
public float airTimeForGliding = 0.7f;
|
|
public float airTimeForAim = 1;
|
|
void shoot()
|
|
{
|
|
st += Time.deltaTime;
|
|
if (shooting && !jumped)
|
|
{
|
|
if (st > fireRate)
|
|
{
|
|
st = 0;
|
|
FireShot();
|
|
if (!WeaponTypeInHand.isAutomatic) { shooting = false; }
|
|
}
|
|
}
|
|
|
|
if (aiming)
|
|
{
|
|
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f));
|
|
RaycastHit hit = new RaycastHit();
|
|
string enemyName = "";
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
if (hit.collider.tag == "Player")
|
|
{
|
|
HitBox hitbox = hit.collider.GetComponent<HitBox>();
|
|
if (hitbox != null)
|
|
{
|
|
enemyName = hitbox.player.username == "" ? "Dev0" : hitbox.player.username;
|
|
}
|
|
}
|
|
}
|
|
|
|
UIManager.instance.EnemyPopupText.text = enemyName;
|
|
}
|
|
else
|
|
{
|
|
UIManager.instance.EnemyPopupText.text = "";
|
|
}
|
|
}
|
|
public float recoil = 0.05f;
|
|
float readyToShoot = 2;
|
|
|
|
void FireShot()
|
|
{
|
|
if(activeWeaponSlot == -1){
|
|
//PUNCH
|
|
Punch();
|
|
return;
|
|
}
|
|
|
|
if (inventoryManager.loadedRounds1 <= 0 && activeWeaponSlot == 0) { Debug.Log("Reload to shoot"); AutoReload(); return; }// Abort if no rounds in weapon 1
|
|
if (inventoryManager.loadedRounds2 <= 0 && activeWeaponSlot == 1) { Debug.Log("Reload to shoot"); AutoReload(); return; }// Abort ifn no rounds in weapon 2
|
|
if (inventoryManager.loadedRoundsPrimary <= 0 && activeWeaponSlot == 2) { Debug.Log("Reload to shoot"); AutoReload(); return; } // Abort ifn no rounds in pitsol
|
|
|
|
StartCoroutine(CoroutineFireShot());
|
|
}
|
|
float punchCooldown =0;
|
|
void Punch(){
|
|
if(punchCooldown > 0){return;}
|
|
// if(anim.GetCurrentAnimatorClipInfo(3).Length >0){
|
|
// if(anim.GetCurrentAnimatorClipInfo(3)[0].clip.name == "Punching"){Debug.Log("Already punching");return;}
|
|
// }
|
|
Debug.Log("Punch");
|
|
// anim.CrossFadeInFixedTime("Punch",0.1f);
|
|
anim.SetTrigger("Punch");
|
|
photonView.RPC("RpcSetTrigger", RpcTarget.All, "Punch");
|
|
|
|
meleeListener.OnMeleeEntered = OnPunchDelivered;
|
|
punchCooldown = 0.35f;
|
|
}
|
|
|
|
[PunRPC]
|
|
void RpcSetTrigger(string triggerName){
|
|
if(!photonView.IsMine){
|
|
anim.SetTrigger(triggerName);
|
|
}
|
|
}
|
|
|
|
void OnPunchDelivered(Collider collider){
|
|
Debug.Log("Punched " + collider.name);
|
|
if(collider.GetComponent<HitBox>()!=null){
|
|
collider.GetComponent<HitBox>().OnPunched(photonView.ViewID, meleeListener.transform.position, transform);
|
|
}
|
|
}
|
|
public LayerMask shootingLayerMask;
|
|
IEnumerator CoroutineFireShot()
|
|
{
|
|
bool aiming = anim.GetCurrentAnimatorClipInfo(1)[0].clip.name.Contains("Aiming");
|
|
if (!aiming)
|
|
{
|
|
Debug.Log("Not aiming");
|
|
anim.CrossFadeInFixedTime("Aiming", 0f);
|
|
aimToggle(true);
|
|
yield return new WaitForSeconds(0.05f);
|
|
}
|
|
CameraFollow.instance.y -= recoil;
|
|
CameraFollow.instance.x += Random.Range(-recoil / 2f, recoil / 2f);
|
|
|
|
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f));
|
|
RaycastHit hit = new RaycastHit();
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
effects.FireFX(hit);
|
|
}
|
|
else
|
|
{
|
|
effects.FireFX(null);
|
|
}
|
|
|
|
if (activeWeaponSlot == 0)
|
|
{
|
|
inventoryManager.loadedRounds1--;
|
|
// inventoryManager.Stock[WeaponTypeInHand.ammo]--;
|
|
}
|
|
else if (activeWeaponSlot == 1)
|
|
{
|
|
inventoryManager.loadedRounds2--;
|
|
// inventoryManager.Stock[WeaponTypeInHand.ammo]--;
|
|
}
|
|
else if (activeWeaponSlot == 2)
|
|
{
|
|
inventoryManager.loadedRoundsPrimary--;
|
|
}
|
|
|
|
UIManager.UpdateInventory(inventoryManager);
|
|
}
|
|
|
|
void aimToggle(bool value)
|
|
{
|
|
if (!weaponInHand) { return; }
|
|
if (reloading) { return; }
|
|
//Add the conditions
|
|
aiming = value;
|
|
|
|
UIManager.SetCroshair(value);
|
|
UpdateAnim();
|
|
|
|
|
|
UpdateWeapon();
|
|
}
|
|
|
|
bool CanReload(LootItemScriptableObject weapon, int loadedRounds)
|
|
{
|
|
if (loadedRounds >= weapon.roundsPerClip) { return false; } //Already loaded
|
|
if (inventoryManager.getStock(weapon.ammo) <= 0) { return false; }//No ammo in inventory
|
|
|
|
return true;
|
|
}
|
|
|
|
public void reload()
|
|
{
|
|
if (inventoryManager.Stock.GetValueOrDefault(WeaponTypeInHand.ammo, 0) <= 0) { Debug.Log("No ammo to reload"); return; }
|
|
if (activeWeaponSlot == 0 && !CanReload(inventoryManager.FirstWeapon, inventoryManager.loadedRounds1)) { Debug.Log("No extra ammo to reload"); return; }
|
|
if (activeWeaponSlot == 1 && !CanReload(inventoryManager.SecondWeapon, inventoryManager.loadedRounds2)) { Debug.Log("No extra ammo to reload"); return; }
|
|
if (activeWeaponSlot == 2 && !CanReload(inventoryManager.PrimaryWeapon, inventoryManager.loadedRoundsPrimary)) { Debug.Log("No extra ammo to reload"); return; }
|
|
|
|
if (reloading) { return; }
|
|
StartCoroutine(toggleReload());
|
|
}
|
|
|
|
IEnumerator toggleReload()
|
|
{
|
|
aimToggle(false);
|
|
RifleRig.weight = 0;
|
|
anim.SetLayerWeight(1, 1);
|
|
|
|
anim.SetBool("reloading", true);
|
|
yield return new WaitForSeconds(0.2f);
|
|
anim.SetBool("reloading", false);
|
|
while (reloading)
|
|
{
|
|
RifleRig.weight = 0;
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
ReloadBullets();
|
|
|
|
Debug.Log(anim.GetCurrentAnimatorClipInfo(1)[0].clip.name);
|
|
RifleRig.weight = 1;
|
|
UpdateWeapon();
|
|
//Reload done
|
|
}
|
|
|
|
void AutoReload()
|
|
{
|
|
if (WeaponTypeInHand.ammo != null)
|
|
{
|
|
int roundsInHand = 0;
|
|
switch (activeWeaponSlot)
|
|
{
|
|
case 0:
|
|
roundsInHand = inventoryManager.loadedRounds1; break;
|
|
case 1:
|
|
roundsInHand = inventoryManager.loadedRounds2; break;
|
|
case 2:
|
|
roundsInHand = inventoryManager.loadedRoundsPrimary; break;
|
|
}
|
|
|
|
if (inventoryManager.getStock(WeaponTypeInHand.ammo) > 0 && roundsInHand <= 0)
|
|
{
|
|
reload();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ReloadBullets()
|
|
{
|
|
if (activeWeaponSlot == 0)
|
|
{
|
|
int emptyBullets = inventoryManager.FirstWeapon.roundsPerClip - inventoryManager.loadedRounds1;
|
|
int refillingBullets = Mathf.Clamp(emptyBullets, 0, inventoryManager.Stock.GetValueOrDefault(inventoryManager.FirstWeapon.ammo, 0));
|
|
|
|
inventoryManager.loadedRounds1 += refillingBullets;
|
|
inventoryManager.Stock[inventoryManager.FirstWeapon.ammo] -= refillingBullets;
|
|
}
|
|
else if (activeWeaponSlot == 1)
|
|
{
|
|
int emptyBullets = inventoryManager.SecondWeapon.roundsPerClip - inventoryManager.loadedRounds2;
|
|
int refillingBullets = Mathf.Clamp(emptyBullets, 0, inventoryManager.Stock.GetValueOrDefault(inventoryManager.SecondWeapon.ammo, 0));
|
|
|
|
inventoryManager.loadedRounds2 += refillingBullets;
|
|
inventoryManager.Stock[inventoryManager.SecondWeapon.ammo] -= refillingBullets;
|
|
}
|
|
else if (activeWeaponSlot == 2)
|
|
{
|
|
int emptyBullets = inventoryManager.PrimaryWeapon.roundsPerClip - inventoryManager.loadedRoundsPrimary;
|
|
int refillingBullets = Mathf.Clamp(emptyBullets, 0, inventoryManager.Stock.GetValueOrDefault(inventoryManager.PrimaryWeapon.ammo, 0));
|
|
|
|
inventoryManager.loadedRoundsPrimary += refillingBullets;
|
|
inventoryManager.Stock[inventoryManager.PrimaryWeapon.ammo] -= refillingBullets;
|
|
}
|
|
|
|
UIManager.UpdateInventory(inventoryManager);
|
|
}
|
|
|
|
// -------------------------------------------------------------- PICKUPS -------------------------------------------------------------------
|
|
|
|
LootItemScriptableObject WeaponTypeInHand => (activeWeaponSlot == 0) ? inventoryManager.FirstWeapon : ((activeWeaponSlot == 1) ? inventoryManager.SecondWeapon : ((activeWeaponSlot == 2) ? inventoryManager.PrimaryWeapon : (activeWeaponSlot == 3 && inventoryManager.EquippedThrowable != null ? inventoryManager.EquippedThrowable : null)));
|
|
|
|
public void UpdateWeapon()
|
|
{
|
|
LootItemScriptableObject weaponType = WeaponTypeInHand;
|
|
Debug.Log(weaponType);
|
|
bool isNade = false;
|
|
if (weaponType != null)
|
|
{
|
|
if (weaponType.isThrowable) { isNade = true; }
|
|
}
|
|
if (isNade)
|
|
{
|
|
foreach (PlayerWeaponEntry weapon in playerNetwork.weapons)
|
|
{
|
|
weapon.obj.SetActive(false);
|
|
}
|
|
|
|
foreach (LootObjectEntry nade in playerNetwork.nades)
|
|
{
|
|
nade.obj.SetActive(nade.data == weaponType);
|
|
}
|
|
|
|
anim.runtimeAnimatorController = defaultController;
|
|
anim.SetLayerWeight(1, 0);
|
|
RifleRig.weight = 0;
|
|
return;
|
|
}
|
|
|
|
foreach (LootObjectEntry nade in playerNetwork.nades)
|
|
{
|
|
nade.obj.SetActive(false);
|
|
}
|
|
|
|
PlayerWeaponEntry objectEntry = playerNetwork.weapons[0];
|
|
foreach (PlayerWeaponEntry obj in playerNetwork.weapons)
|
|
{
|
|
obj.obj.SetActive(weaponType == obj.data);
|
|
obj.muzzleFlash.SetActive(false);
|
|
if (weaponType == obj.data)
|
|
{
|
|
objectEntry = obj;
|
|
}
|
|
}
|
|
if (weaponType != null)
|
|
{
|
|
if (weaponType.overrideController == null)
|
|
{
|
|
anim.runtimeAnimatorController = defaultController;
|
|
}
|
|
else
|
|
{
|
|
anim.runtimeAnimatorController = weaponType.overrideController;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
m_leftHandIK.SetLocalPositionAndRotation(objectEntry.leftHandIk.localPosition, objectEntry.leftHandIk.localRotation);
|
|
|
|
UpdateRigWeights();
|
|
|
|
}
|
|
|
|
void UpdateRigWeights()
|
|
{
|
|
bool m_aiming = anim.GetCurrentAnimatorClipInfo(1)[0].clip.name.Contains("Aim");
|
|
float weight = WeaponTypeInHand == null ? 0 : 1;
|
|
if (weight == 1 && WeaponTypeInHand.isThrowable)
|
|
{
|
|
weight = 0;
|
|
}
|
|
// Debug.Log(weight);
|
|
anim.SetLayerWeight(1, activeWeaponSlot == 2 && !m_aiming ? 0 : weight);
|
|
RifleRig.weight = activeWeaponSlot == 2 ? 0 : weight;
|
|
}
|
|
|
|
public void OnWeaponPickup()
|
|
{
|
|
StartCoroutine(pickupWeapon());
|
|
}
|
|
|
|
IEnumerator pickupWeapon()
|
|
{
|
|
pickingLoot = true;
|
|
anim.SetTrigger("Pickup");
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
// RifleGO.SetActive(true);
|
|
UpdateWeapon();
|
|
if (anim.GetCurrentAnimatorClipInfoCount(2) <= 0) { Debug.Log("No pickup animation"); }
|
|
else
|
|
{
|
|
while (anim.GetCurrentAnimatorClipInfo(2)[0].clip.name == "Pickup")
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
}
|
|
}
|
|
pickingLoot = false;
|
|
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------- MOVEMENTS -------------------------------------------------------------------
|
|
|
|
bool crouched;
|
|
public void toggleCrouch()
|
|
{
|
|
crouched = !crouched;
|
|
UpdateAnim();
|
|
}
|
|
|
|
public void jump()
|
|
{
|
|
if (crouched)
|
|
{
|
|
crouched = false;
|
|
return;
|
|
}
|
|
jumped = true;
|
|
|
|
UpdateAnim();
|
|
}
|
|
bool changingGun, pickingLoot;
|
|
Vector3 direction;
|
|
bool sprinting = false;
|
|
float _speed = 0;
|
|
int aimRotationOffset = 0;//50
|
|
|
|
void move()
|
|
{
|
|
controller.Move(moveDirection * Time.deltaTime);
|
|
|
|
if (!controller.isGrounded)
|
|
{
|
|
moveDirection.y -= 9.8f * Time.deltaTime;
|
|
if (airTime > airTimeForGliding)
|
|
{
|
|
moveDirection.y = -3f;
|
|
if (activeWeaponSlot >= 0)
|
|
{
|
|
activeWeaponSlot = -1;
|
|
UpdateWeapon();
|
|
}
|
|
}
|
|
anim.SetFloat("Gliding", airTime > airTimeForGliding ? 1 : 0);
|
|
aimToggle(false);
|
|
}
|
|
umbrella.SetActive(airTime > airTimeForGliding);
|
|
|
|
if (jumped)
|
|
{
|
|
if (controller.isGrounded)
|
|
{
|
|
moveDirection.y = -1f;
|
|
#region obsolete
|
|
/*if (sensingObject != null && sensingObject.tag == "Wall")
|
|
{
|
|
controller.enabled = false;
|
|
anim.CrossFadeInFixedTime("WallClimb", 0.1f);
|
|
cineCam.LookAt = rootMotionCamFollow;
|
|
cineCam.Follow = rootMotionCamFollow;
|
|
transform.position = PlayerControllerHelper.playerPositionNearWall(sensingObject, transform.position, 0.2f) + new Vector3(0, heightDiffer);//+ new Vector3(0, 1.5f, 0)
|
|
transform.eulerAngles = PlayerControllerHelper.getWallRotation(sensingObject, transform.position);
|
|
// Debug.Log(transform.eulerAngles);
|
|
}
|
|
else
|
|
{
|
|
moveDirection.y = jumpSpeed;
|
|
}*/
|
|
#endregion
|
|
moveDirection.y = jumpSpeed;
|
|
|
|
}
|
|
|
|
jumped = false;
|
|
}
|
|
|
|
bool slowdown = (aiming || shooting || reloading || changingGun || crouched || pickingLoot);
|
|
bool speedup = (sprinting && !aiming);
|
|
bool m_aiming = anim.GetCurrentAnimatorClipInfo(1)[0].clip.name.Contains("Aim");
|
|
|
|
//
|
|
float horizontal = 0;
|
|
float vertical = 0;
|
|
|
|
horizontal = Input.GetAxis("Horizontal");
|
|
vertical = Input.GetAxis("Vertical");
|
|
if (vertical < 1 || horizontal != 0)
|
|
{
|
|
speedup = false;
|
|
}
|
|
anim.SetFloat("move_x", horizontal);
|
|
anim.SetFloat("move_y", vertical);
|
|
|
|
direction = new Vector3(horizontal, 0f, vertical);
|
|
if (direction.magnitude > 1) { direction = direction.normalized; }
|
|
|
|
if (controller.isGrounded)
|
|
{
|
|
airTime = 0;
|
|
if (anim.GetCurrentAnimatorStateInfo(0).IsName("fallToLand")) { return; }
|
|
|
|
if (aiming)
|
|
{
|
|
anim.SetFloat("Horizontal", horizontal);
|
|
anim.SetFloat("Vertical", vertical);
|
|
}
|
|
else
|
|
{
|
|
anim.SetFloat("Horizontal", 0);
|
|
anim.SetFloat("Vertical", 1);
|
|
}
|
|
if (direction.magnitude >= 0.1f)
|
|
{
|
|
if (stamina > 0 && speedup)
|
|
{
|
|
stamina -= Time.deltaTime;
|
|
}
|
|
else if (!speedup)
|
|
{
|
|
RechargeStamina();
|
|
}
|
|
// float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
|
|
float targetAngle = cam.eulerAngles.y;
|
|
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
|
|
if (aiming && !crouched)
|
|
{
|
|
angle = cam.eulerAngles.y + aimRotationOffset;
|
|
}
|
|
|
|
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
|
|
|
// Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
|
|
Vector3 moveDir = transform.forward * vertical + transform.right * horizontal;
|
|
float m_speed = speed * movingSpeedMultiplier * (slowdown ? aimingSpeedMultiplier : 1) * (speedup && stamina > 0 ? sprintingMultiplier : 1);
|
|
if (pickingLoot || throwing) { m_speed = 0; }
|
|
_speed = Mathf.Lerp(_speed, m_speed, 0.1f);
|
|
movingVector = moveDir.normalized * ((direction.magnitude < 0.3f) ? 0.3f : direction.magnitude) * _speed * Time.deltaTime;
|
|
anim.SetBool("moving", true);
|
|
// Debug.Log("Moving Vector : " + movingVector);\
|
|
float curMovingSpeed = new Vector3(movingVector.x, 0, movingVector.z).magnitude / movingSpeedMultiplier;
|
|
anim.SetFloat("movingSpeed", curMovingSpeed);
|
|
CameraFollow.instance.currentTargetSpeed = aiming ? 0 : curMovingSpeed;
|
|
}
|
|
else
|
|
{
|
|
RechargeStamina();
|
|
if (true) //climbing wall
|
|
{
|
|
float targetAngle = cam.eulerAngles.y;
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime * 5);
|
|
Vector3 prevRot = transform.eulerAngles;
|
|
|
|
if (m_aiming && !crouched)
|
|
{
|
|
angle = cam.eulerAngles.y + aimRotationOffset;
|
|
}
|
|
transform.rotation = Quaternion.Euler(0, angle, 0);
|
|
movingVector = Vector3.zero;
|
|
anim.SetBool("moving", false);
|
|
anim.SetFloat("rotatingSpeed", Mathf.Lerp(anim.GetFloat("rotatingSpeed"), Mathf.Clamp((transform.eulerAngles.y - prevRot.y) / 1f, -0.9f, 0.9f), 1f));
|
|
}
|
|
}
|
|
anim.SetBool("jumping", false);
|
|
}
|
|
else
|
|
{
|
|
airTime += Time.deltaTime;
|
|
|
|
float targetAngle = cam.eulerAngles.y;
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime * 5);
|
|
if (m_aiming)
|
|
{
|
|
angle = cam.eulerAngles.y;
|
|
}
|
|
transform.rotation = Quaternion.Euler(0, angle, 0);
|
|
anim.SetBool("jumping", true);
|
|
|
|
Vector3 moveDir = transform.forward * vertical + transform.right * horizontal;
|
|
movingVector = moveDir.normalized * ((direction.magnitude < 0.3f) ? 0.3f : direction.magnitude) * _speed * Time.deltaTime;
|
|
|
|
}
|
|
controller.Move(movingVector);
|
|
}
|
|
|
|
void RechargeStamina()
|
|
{
|
|
if (stamina < maxStamina)
|
|
{
|
|
stamina += Time.deltaTime * staminaRechargeRate;
|
|
}
|
|
}
|
|
|
|
public void UpdateAnim()
|
|
{
|
|
anim.SetBool("aiming", aiming);
|
|
anim.SetBool("crouched", crouched);
|
|
|
|
|
|
bool m_aiming = anim.GetCurrentAnimatorClipInfo(1)[0].clip.name.Contains("Aim");
|
|
AimRig.weight = m_aiming ? 1 : 0;
|
|
UpdateRigWeights();
|
|
}
|
|
|
|
|
|
public void OnDeath()
|
|
{
|
|
isActive = false;
|
|
activeWeaponSlot = -1;
|
|
UpdateWeapon();
|
|
}
|
|
}
|