using System;
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
namespace GDD.PUN
{
public class PunCharacterHealth : MonoBehaviourPun
{
protected CharacterSystem _characterSystem;
protected byte _punEventCode = 10;
public CharacterSystem CharacterSystem
{
get => _characterSystem;
}
protected virtual void Awake()
{
_characterSystem = GetComponent<CharacterSystem>();
}
protected virtual void OnEnable()
{
}
protected virtual void Start()
{
//SyncPlayerStats
if(!photonView.IsMine)
photonView.RPC("GetPlayerStatsToOtherPlayer", photonView.Owner, photonView.ViewID);
}
protected virtual void Update()
{
}
[PunRPC]
public virtual void OnInitializeOtherPlayer(object[] datas, int OwnerNetID)
{
if(OwnerNetID != photonView.ViewID || photonView.IsMine)
return;
//print($"{gameObject.name} | OnInitializeOtherPlayer || Shield = {(float)datas[2]}");
//print($"Shield = {(float)datas[2]}");
_characterSystem.SetHP((float)datas[0]);
_characterSystem.SetMaxHP((float)datas[1]);
_characterSystem.SetShield((float)datas[2]);
_characterSystem.SetUpdateEXP((int)datas[3]);
_characterSystem.SetEXP((int)datas[3]);
}
[PunRPC]
public virtual void GetPlayerStatsToOtherPlayer(object OwnerNetID)
{
if((int)OwnerNetID != photonView.ViewID)
return;
//print($"GetPlayerStatsToOtherPlayer : {gameObject.name} || ID : {OwnerNetID} = {photonView}");
float shield;
if (photonView.IsMine)
shield = GameManager.Instance.gameInstance.shield;
else
shield = _characterSystem.GetShield();
print($"Shieldddddddddd ::::: {shield}");
object[] datas = new object[]
{
_characterSystem.GetHP(),
_characterSystem.GetMaxHP(),
shield,
_characterSystem.GetUpdateEXP()
};
photonView.RPC("OnInitializeOtherPlayer", RpcTarget.All, datas, OwnerNetID);
}
public virtual void TakeDamage(float amount)
{
if (photonView != null)
photonView.RPC("PunRPCApplyHealth", photonView.Owner, -amount, photonView.ViewID);
else
print("photonView is NULL.");
}
public virtual void HealingPoint(float amount)
{
if (photonView != null)
photonView.RPC("PunRPCSetHealth", RpcTarget.All, amount, photonView.ViewID);
else
print("photonView is NULL.");
}
public virtual void ShieldPoint(float amount)
{
if (photonView != null)
photonView.RPC("PunRPCSetShield", RpcTarget.All, amount, photonView.ViewID);
else
print("photonView is NULL.");
}
[PunRPC]
public virtual void PunRPCApplyHealth(float amount, int OwnerNetID)
{
if(OwnerNetID != photonView.ViewID)
return;
//Debug.Log("Update @" + PhotonNetwork.LocalPlayer.ActorNumber + " Apply Health : " + amount + " form : " + OwnerNetID);
if (_characterSystem.GetShield() > 0 && amount < 0)
{
photonView.RPC("PunRPCSetShield", RpcTarget.All, amount, OwnerNetID);
float shield_remaining = _characterSystem.GetShield() + amount;
if(shield_remaining <= 0)
photonView.RPC("PunRPCSetHealth", RpcTarget.All, shield_remaining, OwnerNetID);
}
else
{
photonView.RPC("PunRPCSetHealth", RpcTarget.All, amount, OwnerNetID);
}
if (_characterSystem.GetHP() <= 0)
{
//Debug.Log("NetID : " + OwnerNetID.ToString() + " Killed " + photonView.ViewID);
photonView.RPC("PunResetCharacter", RpcTarget.All);
}
}
[PunRPC]
public virtual void PunRPCSetShield(float amount, int OwnerNetID)
{
print($"Shield {gameObject.name} is : {amount}");
if(OwnerNetID != photonView.ViewID)
return;
if (_characterSystem.GetShield() + amount > 0)
_characterSystem.SetShield(_characterSystem.GetShield() + amount);
else
_characterSystem.SetShield(0);
}
[PunRPC]
public virtual void PunRPCSetHealth(float amount, int OwnerNetID)
{
if(OwnerNetID != photonView.ViewID)
return;
//print($"HP Amount = {amount}");
//print($"Total HP = {_characterSystem.GetHP() + amount} || Char HP = {_characterSystem.GetHP()}");
if (_characterSystem.GetHP() + amount > 0)
_characterSystem.SetHP(_characterSystem.GetHP() + amount);
else
_characterSystem.SetHP(0);
print($"Health {gameObject.name} is : {_characterSystem.GetHP()}");
}
[PunRPC]
public virtual void PunResetCharacter()
{
Debug.Log("Reset ...");
}
public void SendRevive()
{
photonView.RPC("PunReviveCharacter", RpcTarget.All, photonView.ViewID);
}
[PunRPC]
public void PunReviveCharacter(int ViewID)
{
if(ViewID != photonView.ViewID)
return;
print($"Recipt : OnCharacterRevive = {ViewID} || {photonView.ViewID}");
_characterSystem.OnCharacterRevive();
}
protected virtual void OnDisable()
{
}
}
}
using Photon.Pun;
namespace GDD.PUN
{
public class PunPlayerHealth : PunCharacterHealth
{
public virtual void UpdateEXPAndLevelPoint()
{
int[] amount = new[]
{
_characterSystem.GetUpdateEXP(),
_characterSystem.GetMaxEXP(),
_characterSystem.GetLevel()
};
photonView.RPC("PunRPCSetEXPAndLevel", RpcTarget.Others, amount);
}
[PunRPC]
public virtual void PunRPCSetEXPAndLevel(int[] amount)
{
if(photonView.IsMine)
return;
_characterSystem.SetUpdateEXP(amount[0]);
_characterSystem.SetEXP(amount[0]);
_characterSystem.SetMaxEXP(amount[1]);
_characterSystem.SetLevel(amount[2]);
//print($"Level is : {amount[0]}");
}
}
}
namespace GDD.PUN
{
public class PunEnemyHealth : PunCharacterHealth
{
}
}
using UnityEngine;
using UnityEngine.Pool;
namespace GDD.ObjectPool
{
public class ObjectPoolBuilder : MonoBehaviour
{
[SerializeField]protected int _maxPoolSize = 10;
[SerializeField]protected int _stackDefaultCapacity = 5;
protected GameManager GM;
protected IObjectPool<GameObjectPool> _pool;
protected GameObject _gameObjectPool;
public int maxPoolSize
{
get => _maxPoolSize;
set => _maxPoolSize = value;
}
public int stackDefaultCapacity
{
get => _stackDefaultCapacity;
set => _stackDefaultCapacity = value;
}
public GameObject Set_GameObject
{
set => _gameObjectPool = value;
}
public IObjectPool<GameObjectPool> Pool
{
get
{
if (_pool == null)
_pool = new ObjectPool<GameObjectPool>(CreatePooledItem,
OnTakeFromPool,
OnReturnToPool,
OnDestroyPoolObject,
true,
stackDefaultCapacity,
maxPoolSize);
return _pool;
}
}
public virtual void Start()
{
GM = GameManager.Instance;
}
public virtual void Update()
{
}
public virtual GameObjectPool CreatePooledItem()
{
return null;
}
public virtual void OnReturnToPool(GameObjectPool gObject)
{
gObject.gameObject.SetActive(false);
}
public virtual void OnTakeFromPool(GameObjectPool gObject)
{
gObject.gameObject.SetActive(true);
}
public virtual void OnDestroyPoolObject(GameObjectPool bullet)
{
Destroy(bullet.gameObject);
}
public virtual GameObject OnSpawn()
{
GameObject bullet = Pool.Get().gameObject;
return bullet;
}
}
}
using System;
using GDD.ObjectPool;
using UnityEngine;
using UnityEngine.Pool;
namespace GDD
{
public class BulletObjectPool : ObjectPoolBuilder
{
public override void OnReturnToPool(GameObjectPool bullet)
{
Rigidbody rigidbody = bullet.GetComponent<Rigidbody>();
rigidbody.velocity = Vector3.zero;
base.OnReturnToPool(bullet);
}
public override GameObject OnSpawn()
{
GameObject bullet_gobj = base.OnSpawn().gameObject;
bullet_gobj.layer = LayerMask.NameToLayer("Bullet");
TakeDamage bullet_TD;
if (bullet_gobj.GetComponent<TakeDamage>() == null)
{
bullet_TD = bullet_gobj.gameObject.AddComponent<TakeDamage>();
}
else
{
bullet_TD = bullet_gobj.GetComponent<TakeDamage>();
}
bullet_TD.ownerLayer = transform;
return bullet_gobj;
}
}
}
using UnityEngine;
using UnityEngine.Pool;
namespace GDD.ObjectPool
{
public class GameObjectPool : MonoBehaviour
{
public IObjectPool<GameObjectPool> objectPool { get; set; }
public virtual void OnDisable()
{
}
public virtual void ReturnToPool()
{
if(gameObject.activeSelf)
objectPool.Release(this);
}
public virtual void ResetBullet(Transform spawnPoint)
{
transform.position = spawnPoint.position;
}
}
}
using System;
using GDD.ObjectPool;
using UnityEngine;
using UnityEngine.Pool;
namespace GDD
{
public class PlayerBullet : GameObjectPool
{
public IWeapon _weapon { get; set; }
}
}