143 lines
4.8 KiB
C#
143 lines
4.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
//Thomas09
|
|
|
|
public class MeleeController : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
private float aimVelocity;
|
|
private Weapon weapon;
|
|
private Animator anim;
|
|
private ParticleSystem swordSwingPS;
|
|
private Camera playerCamera;
|
|
private LayerMask whatCanBeHitBySword;
|
|
|
|
|
|
private int SWING = Animator.StringToHash("Swing");
|
|
private int SWING2 = Animator.StringToHash("Swing2");
|
|
|
|
private bool currentlySwinging;
|
|
|
|
private float timer = 0;
|
|
private bool canSwing = false;
|
|
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
weapon = OnDemandLoader.Load<Weapon>("config/" + int.Parse(this.gameObject.name));
|
|
anim = GetComponent<Animator>();
|
|
swordSwingPS = transform.Find("SM_Wep_Sword_01/Vfx_SwordTrails/Trails")?.GetComponent<ParticleSystem>();
|
|
playerCamera = Player.Instance.transform.Find("FirstPersonCamera_Holder").GetComponentInChildren<Camera>();
|
|
whatCanBeHitBySword = LayerMask.GetMask("Enemy");
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
//if sniper was last weapon used, and you switched to a none sniper, disable the sniper scope overlay.
|
|
CrosshairManager.Instance.SetSniperCrosshairAlpha(0.0f);
|
|
StartCoroutine(Player.Instance.EquipWeaponCoroutine(weapon));
|
|
Player.Instance.GetInputHandler().AimKeyPressed = false;
|
|
Player.Instance.GetInputHandler().ToggleAiming = false;
|
|
//Debug.Log(weapon.meleeHitFlag);
|
|
}
|
|
void Update()
|
|
{
|
|
//melee weapons should NOT be allowed to aim. So if the players FOV is different from the standard when you switch to a melee weapon, lerp it back down to its default value.
|
|
if(playerCamera.fieldOfView != weapon.normalFOV)
|
|
{
|
|
playerCamera.fieldOfView = Mathf.SmoothDamp(playerCamera.fieldOfView, weapon.normalFOV, ref aimVelocity, 0.1f);
|
|
}
|
|
|
|
if (Player.Instance.updatePlayer)
|
|
{
|
|
CrosshairManager.Instance.SetDynamicCrossHair(0, 0, 0, 0, Player.Instance, weapon.reticletype);
|
|
RunState();
|
|
if(!canSwing)
|
|
{
|
|
timer += Time.deltaTime;
|
|
if(timer >= weapon.SWING_COOLDOWN)
|
|
{
|
|
canSwing = true;
|
|
timer = 0;
|
|
ResetTriggers();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SwingState();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SwingState()
|
|
{
|
|
if (Player.Instance.GetInputHandler().FireKeyPressed)
|
|
{
|
|
Player.Instance.GetInputHandler().FireKeyPressed = false;
|
|
canSwing = false;
|
|
int swingState = Random.Range(0, 2);
|
|
switch (swingState)
|
|
{
|
|
case 0:
|
|
anim.SetTrigger(SWING);
|
|
break;
|
|
case 1:
|
|
anim.SetTrigger(SWING2);
|
|
break;
|
|
}
|
|
|
|
PacketManager.sendChatMessage("/meleeSwing");
|
|
}
|
|
}
|
|
private void RunState()
|
|
{
|
|
anim.SetBool("Run", Player.Instance.CanPlayRunningEvents());
|
|
}
|
|
|
|
private void HitEnemy() //used in animation event
|
|
{
|
|
RaycastHit hit;
|
|
int npcId = -1;
|
|
int hitFlag = -1;
|
|
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit,weapon.raycastLength, whatCanBeHitBySword))
|
|
{
|
|
if (hit.transform.root.TryGetComponent(out NPC npc))
|
|
{
|
|
PlayHitVFX();
|
|
hitFlag = Player.Instance.GetHitFlag(hit.transform.tag);
|
|
float calculatedDamage = Player.Instance.CalculateWeaponDamage(Player.Instance.transform.position, hit.transform.root.position, weapon.maxDamage, weapon.effectiveRange, hitFlag);
|
|
npcId = npc.GetID();
|
|
Player.Instance.ApplyDamage(hit, npcId, calculatedDamage, hitFlag);
|
|
}
|
|
}
|
|
//Debug.Log($"Hit NPC WITH SWORD! Hit flag: {hitFlag} ID: {npcId}");
|
|
PacketManager.sendMeleeAttack(hitFlag, npcId);
|
|
}
|
|
|
|
private void PlayHitVFX()
|
|
{
|
|
float randPitch = Random.Range(0.85f, 1.25f);
|
|
int randClip = Random.Range(0, 3);
|
|
if (randClip == 0) { AudioManager.Instance.playSound("Hit_Enemy", randPitch); }
|
|
else if (randClip == 1) { AudioManager.Instance.playSound("Hit_Enemy2", randPitch); }
|
|
else if (randClip == 2) { AudioManager.Instance.playSound("Hit_Enemy3", randPitch); }
|
|
}
|
|
|
|
private void PlaySwingSound() //used in animation event
|
|
{
|
|
string sound = ItemDef.GetInstance().getFireSound(int.Parse(gameObject.name));
|
|
AudioManager.Instance.playSound(sound);
|
|
if(swordSwingPS != null) swordSwingPS.Play();
|
|
}
|
|
|
|
private void ResetTriggers()
|
|
{
|
|
anim.ResetTrigger(SWING);
|
|
anim.ResetTrigger(SWING2);
|
|
}
|
|
|
|
}
|