mmorpg/Assets/Script/playerNetwork.cs
2024-01-30 19:56:12 +05:30

216 lines
5.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
using Assets.HeroEditor4D.Common.Scripts.Enums;
using Mirror;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
public class playerNetwork : NetworkBehaviour
{
[SyncVar(hook =nameof(OnHealthChanged))] public int health = 100;
public Character4D character;
[SyncVar(hook =nameof(OnDirectionChanged))]
public Vector2 directionNetwork;
[SyncVar(hook =nameof(OnAnimChanged))]
public int animIntNetwork;
[SyncVar]
public int enemyKillCount;
public TMP_Text txtEnemyKillCount;
public GameObject canvas;
public healthBar healthBar;
public Inventory inventory;
void Start(){
if(!isLocalPlayer){
canvas.SetActive(false);
}
}
void OnDirectionChanged(Vector2 oldVal, Vector2 newVal){
character.SetDirection(newVal);
}
void OnAnimChanged(int oldVal, int newVal){
character.AnimationManager.SetState((CharacterState)newVal);
}
void Update(){
if(isLocalPlayer){
if(isServer){
SetAnimationData(character.Direction, character.Animator.GetInteger("State"));
}else{
CmdUpdateAnim(character.Direction, character.Animator.GetInteger("State"));
}
healthBar.SetHealth(health);
txtEnemyKillCount.text = enemyKillCount.ToString();
}
}
[Command]
void CmdUpdateAnim(Vector2 direction, int animState){
SetAnimationData(direction, animState);
}
void SetAnimationData(Vector2 direction, int animState){
directionNetwork = direction;
animIntNetwork = animState;
if(!isLocalPlayer){
character.AnimationManager.SetState((CharacterState)animState);
character.SetDirection(direction);
}
}
void OnHealthChanged(int oldVal, int newVal){
if(!isLocalPlayer){return;}
healthBar.SetHealth(newVal);
}
public void SetHealth(int newvalue){
if(isServer){
health = newvalue;
healthBar.SetHealth(newvalue);
}else{
CmdSetHealth(newvalue);
}
}
[Command]
void CmdSetHealth(int newValue){
health = newValue;
}
public void TakeDamage(int attackDamage){
serverTakeDmg(attackDamage);
// if(isLocalPlayer){
// takedmg(attackDamage);
// }else if(isServer){
// RpcTakeDamage(attackDamage);
// }
}
void serverTakeDmg(int damage){
health -= damage;
if(health <=0){
RpcDeath();
death();
}
}
public void OnEnemyKilled(){
enemyKillCount++;
}
[ClientRpc]
public void RpcDeath(){
death();
}
public void death(){
Debug.Log("Death called");
character.AnimationManager.Die();
StartCoroutine(CouroutineWaitDeath());
// throw new System.Exception();
}
IEnumerator CouroutineWaitDeath (){
yield return new WaitForSecondsRealtime(3);
playerRespawn();
}
public void OnAttack(){
if(isLocalPlayer){
PlayAttackAnim();
if(isServer){
RpcPlayAttackAnim();
}else{
CmdPlayAttackAnim();
}
}
}
[Command]
void CmdPlayAttackAnim(){
PlayAttackAnim();
RpcPlayAttackAnim();
}
[ClientRpc]
void RpcPlayAttackAnim(){
if(isLocalPlayer){return;}
PlayAttackAnim();
}
void PlayAttackAnim(){
switch (character.WeaponType)
{
case WeaponType.Melee1H:
case WeaponType.Paired:
character.AnimationManager.Slash(twoHanded: false);
break;
case WeaponType.Melee2H:
character.AnimationManager.Slash(twoHanded: true);
break;
case WeaponType.Bow:
character.AnimationManager.ShotBow();
break;
}
}
public void playerRespawn(){
Debug.Log("Respawning");
health=100;
healthBar.SetHealth(health);
character.AnimationManager.SetState(CharacterState.Idle);
Transform newSpawnLocationPlayer = GameManager.instance.spawnPointsPlayer.GetChild(Random.Range(0,GameManager.instance.spawnPointsPlayer.childCount));
transform.position = newSpawnLocationPlayer.position;
}
//Pickup
public void PickupObject(pickup item){
if(!isServer){ Debug.LogError("Cant call command on client, 403"); return; }
if(isLocalPlayer){
pickupObject(item.lootType);
}else{
RpcPickupObject(item.lootType);
}
}
[ClientRpc]
void RpcPickupObject(string type){
if(isLocalPlayer){
pickupObject(type);
}
}
void pickupObject(string type){
inventory.AddItem(type);
}
public void DropPickup(string type){
if(isServer){
GameManager.instance.SpawnPickup(type,transform.position + new Vector3(1.5f,1f));
}else{
CmdDropPickup(type);
}
}
[Command]
void CmdDropPickup(string type){
GameManager.instance.SpawnPickup(type,transform.position + new Vector3(4,0));
}
}