zombie_mp/Assets/Scripts/netPlayer.cs
2021-08-23 23:23:20 +05:30

539 lines
13 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HQFPSWeapons;
using Mirror;
using System;
using UnityEngine.Animations.Rigging;
public class netPlayer : NetworkBehaviour
{
public List<GameObject> currentZombies;
public Player player;
public GameObject playerModelParent;
public Animator anim;
[SyncVar]
public string pname;
[Header("Health")]
// public GameObject bloodPrefab;
public GameObject BloodAttach;
public GameObject[] BloodFX;
[HideInInspector]
public int effectIdx;
[Header("Weapons")]
public tpsGunData[] guns;
public int curGunIndex = -1;
public bool holdingRifle;
public Rig leftArm;
public Rig rightArm;
public Transform weaponAim;
public Transform leftHandTarget;
public Transform headTarget;
public Transform weaponHoldPose;
public Transform weaponAimPose;
[Header("WeaponAnimations")]
public Animator weaponAnim;
[Header("Aiming")]
public Transform aimHeightTarget;
public Transform aimHeightTDC;
public Transform aimHeightMid;
public Transform aimHeightBDC;
public bool aiming;
public Rig headRig;
public Rig chestRig;
void Start()
{
if (!isLocalPlayer) { return; } //stuff only for local player
netPlayerStats.localPlayer = this;
Collider[] colliders = gameObject.GetComponentsInChildren<Collider>();
Debug.Log($"Found ${colliders.Length} colliders and disabling them");
foreach (Collider collider in colliders)
{
collider.gameObject.layer = LayerMask.NameToLayer("netPlayer");
}
player = FindObjectOfType<Player>();
pname = PlayerPrefs.GetString("pname");
hideTpsCharacter();
// transform.GetChild(0).gameObject.SetActive(false);
if (!isServer)
{
CmdUpdateName(pname);
}
else
{
RpcUpdateName(pname);
}
//prepare listeners
player.EquippedItem.AddChangeListener((SaveableItem item) => { callChangeEquipment((item==null)? "":item.Name); });
player.UseContinuously.AddListener(() => { Debug.Log("Using Continously"); });
player.UseOnce.AddListener(() => { Debug.Log("Used once"); });
player.Aim.AddStartListener(() => { callToggleAim(true); });
player.Aim.AddStopListener(() => { callToggleAim(false); });
player.Run.AddStartListener(() => { callToggleAim(false); });
toggleAim(false);
}
#region initilaztionNetworkStuff
void hideTpsCharacter()
{
foreach (SkinnedMeshRenderer renderer in playerModelParent.GetComponentsInChildren<SkinnedMeshRenderer>())
{
renderer.enabled = false;
}
}
[Command]
void CmdUpdateName(string e)
{
pname = e;
RpcUpdateName(e);
}
[ClientRpc]
void RpcUpdateName(string e)
{
pname = e;
}
#endregion
// Update is called once per frame
bool jumping = false;
float armWeights = 0;
float bodyWeights = 0;
void FixedUpdate()
{
//updatePlayerDirection (Regardless of isLocalPlayer)
if (isLocalPlayer)
{//Stuff only for local player
float yAxis = player.LookDirection.Val.y;
if (yAxis == 0)
{
aimHeightTarget.position = aimHeightMid.position;
}else if(yAxis> 0)
{
aimHeightTarget.position = aimHeightMid.position + ((aimHeightTDC.position - aimHeightMid.position) * yAxis);
}else if(yAxis < 0)
{
aimHeightTarget.position = aimHeightMid.position + ((aimHeightBDC.position - aimHeightMid.position) * -yAxis);
}
anim.SetFloat("vX", Input.GetAxis("Horizontal"));
anim.SetFloat("vY", Input.GetAxis("Vertical"));
anim.SetBool("crouch", player.Crouch.Active);
anim.SetBool("aiming", player.Aim.Active);
// player.Aim.lis
transform.position = player.transform.position;
transform.rotation = player.transform.rotation;
// Debug.Log($"walk status : {player.Walk.Active} , running status: {player.Run.Active}");
int animCode = 0;
if (player.Run.Active)
{
animCode = 2;
} else if (player.Walk.Active)
{
animCode = 1;
}
if (player.Jump.Active)
{
animCode = 3;
}
CallChangeAnim(animCode);
} else {//update for other peoples
}
if (curGunIndex >= 0)
{
leftHandTarget.position = guns[curGunIndex].leftHandPosition.position;
leftHandTarget.rotation = guns[curGunIndex].leftHandPosition.rotation;
headTarget.position = guns[curGunIndex].headTarget.position;
if (aiming)
{
if (bodyWeights <= 1)
{
bodyWeights += 0.5f;
headRig.weight = bodyWeights;
chestRig.weight = bodyWeights;
}
weaponAim.position = Vector3.Lerp(weaponAim.position,guns[curGunIndex].aiming_rightHandPosition.position, 0.1f);
weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].aiming_rightHandPosition.rotation, 0.1f);
}
else
{
if (bodyWeights >= 0)
{
bodyWeights -= 0.05f;
headRig.weight = bodyWeights;
chestRig.weight = bodyWeights;
}
weaponAim.position = Vector3.Lerp(weaponAim.position, guns[curGunIndex].holding_rightHandPosition.position, 0.1f);
weaponAim.rotation = Quaternion.Lerp(weaponAim.rotation, guns[curGunIndex].holding_rightHandPosition.rotation, 0.1f);
}
}
else
{
}
}
public void callToggleWeaponEquipment(bool value)
{
if (isServer)
{
toggleWeaponEquipment(value);
RpcToggleWeaponEquipment(value);
}
else
{
CmdToggleWeaponEquipment(value);
}
}
[Command]
void CmdToggleWeaponEquipment(bool value)
{
toggleWeaponEquipment(value);
RpcToggleWeaponEquipment(value);
}
[ClientRpc]
void RpcToggleWeaponEquipment(bool value)
{
if (!isLocalPlayer) { return; }
toggleWeaponEquipment(value);
}
public void toggleWeaponEquipment(bool value)
{
if (value)
{
leftArm.weight = 1;
rightArm.weight = 1;
toggleAim(false);
weaponAim.gameObject.SetActive(true);
}
else
{
headRig.weight = 0;
chestRig.weight = 0;
leftArm.weight = 0;
rightArm.weight = 0;
weaponAim.gameObject.SetActive(false);
}
}
public void callToggleAim(bool value)
{
if (isServer)
{
toggleAim(value);
RpcToggleAim(value);
}
else
{
CmdToggleAim(value);
}
}
[Command]
void CmdToggleAim(bool value)
{
toggleAim(value);
RpcToggleAim(value);
}
[ClientRpc]
void RpcToggleAim(bool value)
{
toggleAim(value);
}
public void toggleAim(bool value)
{
aiming = value;
return;
if (value)
{
headRig.weight = 1;
chestRig.weight = 1;
weaponAim.position = guns[curGunIndex].aiming_rightHandPosition.position;
weaponAim.rotation = guns[curGunIndex].aiming_rightHandPosition.rotation;
}
else
{
headRig.weight = 0;
chestRig.weight = 0;
weaponAim.position = guns[curGunIndex].holding_rightHandPosition.position;
weaponAim.rotation = guns[curGunIndex].holding_rightHandPosition.rotation;
}
}
/*
Animation Codes
0 Idle Breathing
1 Walking
2 Running
3 Jumping
*/
void CallChangeAnim(int code)
{
switch (code)
{
case 0:
anim.SetBool("moving", false);
anim.SetBool("running", false);
anim.SetBool("jumping", false);
break;
case 1:
anim.SetBool("moving", true);
anim.SetBool("running", false);
anim.SetBool("jumping", false);
break;
case 2:
anim.SetBool("moving", true);
anim.SetBool("running", true);
anim.SetBool("jumping", false);
break;
case 3:
anim.SetBool("jumping", true);
break;
}
}
///Equipment change
///
public void callChangeEquipment(string name)
{
if (isServer)
{
RpcChangeEquipment(name);
ChangeEquipment(name);
}
else
{
CmdChangeEquipment(name);
}
}
[Command]
void CmdChangeEquipment(string name)
{
ChangeEquipment(name);
RpcChangeEquipment(name);
}
[ClientRpc]
void RpcChangeEquipment(string name)
{
ChangeEquipment(name);
}
void ChangeEquipment(string name)
{
Debug.Log("Change of equipments got called : " + name);
if (name == "")
{
toggleWeaponEquipment(false);
}
else
{
ItemData itemData = null;
ItemDatabase.Default.TryGetItem(name, out itemData);
if (itemData != null)
{
curGunIndex = getWeaponWithName(itemData.Name);
toggleWeaponEquipment(true);
Debug.Log("Found Item from database : " + itemData.Name+ " id:"+curGunIndex);
if (curGunIndex >= 0)
{
foreach (tpsGunData gun in guns)
{
gun.gameObject.SetActive(false);
}
if (!isLocalPlayer)
{
guns[curGunIndex].gameObject.SetActive(true);
}
}
}
else
{
Debug.Log("ITEM NOT FOUND FUCKING FIX THIS!");
}
}
}
///Shoot
///
public void callShoot(Vector3 hitPoint, Vector3 hitNormals)
{
callToggleAim(true);
if (isServer)
{
broadcastShot(hitPoint, hitNormals);
RpcBroadcastShot(hitPoint, hitNormals);
}
else
{
CmdBroadcastShot(hitPoint, hitNormals);
}
}
[Command]
void CmdBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
{
broadcastShot(hitPoint, hitNormals);
RpcBroadcastShot(hitPoint, hitNormals);
}
[ClientRpc]
void RpcBroadcastShot(Vector3 hitPoint, Vector3 hitNormals)
{
if (isServer) { return; }
broadcastShot(hitPoint, hitNormals);
}
void broadcastShot(Vector3 hitPoint, Vector3 hitNormals)
{
// SurfaceEffects effect = (SurfaceEffects)Enum.Parse(typeof(SurfaceEffects), effectType);
SurfaceManager.SpawnEffect(0, SurfaceEffects.BulletHit , 1f,hitPoint,Quaternion.LookRotation(hitNormals));
//shootingEffects
if (curGunIndex > 0)
{
guns[curGunIndex].triggerMuzzleFlash();
}
Debug.Log("Broadcasting shot");
// weaponAnim.CrossFade("shot",0.1f);
}
int getWeaponWithName(string name)
{
for(int i =0; i< guns.Length; i++)
{
tpsGunData gunData = guns[i];
if (gunData.weaponName == name)
{
return i;
}
}
return -1;
}
public void callDamage(float damage)
{
RpcDamage(damage);
}
[ClientRpc]
public void RpcDamage(float damage)
{
if (isLocalPlayer)
{
player.GetComponentInChildren<PlayerVitals>().Entity.ChangeHealth.Try(new HealthEventData(-damage));
Debug.Log("You are local player, damaging");
}
else
{
Debug.Log("Nah you aint the local player, no damage for you sweetie");
}
}
[Command(requiresAuthority =false)]
public void CmdDmg(float damage)
{
RpcDamage(damage);
Debug.Log("RPC damage");
}
public void hitboxDamage(HealthEventData damageData, shotType hitboxType)
{
float dmg = 0;
switch (hitboxType)
{
case shotType.headshot:
dmg = damageData.Delta * 5;
break;
case shotType.chestshot:
dmg = damageData.Delta;
break;
case shotType.legshot:
dmg = damageData.Delta / 2f;
break;
}
dmg = -dmg;
Debug.Log("Received damage from hitbox");
if (!isServer)
{
CmdDmg(dmg); Debug.Log("Command damage " + dmg.ToString());
}
else
{
RpcDamage(dmg);
}
}
}
public static class netPlayerStats
{
public static netPlayer localPlayer;
}