| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 | 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;    public GameObject deadRagdoll;    [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); });        player.Death.AddListener(() => { callDeath(); });        player.Respawn.AddListener(() => { callRespawn();});        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 void callDeath()    {        Debug.Log("Calling Death");        if (isServer)        {            death();            RpcDeath();        }        else        {            CmdDeath();        }    }    [Command]    void CmdDeath()    {        death();        RpcDeath();    }    [ClientRpc]    void RpcDeath()    {        death();    }    void death()    {     //   if (isLocalPlayer) { return; }        playerModelParent.gameObject.SetActive(false);        Instantiate(deadRagdoll, transform.position, transform.rotation);    }    public void callRespawn()    {        if (isServer)        {            Respawn();            RpcRespawn();        }        else        {            CmdRespawn();        }    }    [Command]    void CmdRespawn()    {        Respawn();        RpcRespawn();    }    [ClientRpc]    void RpcRespawn()    {        Respawn();    }    void Respawn()    {        if (isLocalPlayer) { return; }        playerModelParent.gameObject.SetActive(true);    }}public static class netPlayerStats{    public static netPlayer localPlayer;}
 |