Advertisement
GreenMap

Untitled

Mar 15th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8.  
  9. public class GameManager : MonoBehaviourPunCallbacks, IPunObservable
  10. {
  11.     public Text PlayerHitPointsText;
  12.     public Text EnemyHitPointsText;
  13.     private int PlayerHitpoints;
  14.     private int EnemyHitpoints;
  15.     public GameObject PlayerPrefab;
  16.     // Use this for initialization
  17.     void Start () {
  18.         PlayerHitpoints = 3;
  19.         Vector3 pos = new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
  20.         PhotonNetwork.Instantiate(PlayerPrefab.name, pos, Quaternion.identity);
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void Update () {
  25.         PlayerHitPointsText.text = "Health: " + PlayerHitpoints;
  26.         EnemyHitPointsText.text = "Enemy health: " + EnemyHitpoints;
  27.         if(PlayerHitpoints == 0 && PhotonNetwork.NetworkClientState == ClientState.Joined)
  28.         {
  29.             PhotonNetwork.LeaveRoom();
  30.         }
  31.     }
  32.     public override void OnLeftRoom()
  33.     {
  34.         SceneManager.LoadScene(0);
  35.     }
  36.     public override void OnPlayerEnteredRoom(Player newPlayer)
  37.     {
  38.         Debug.LogFormat("Player {0} entered", newPlayer.NickName);
  39.     }
  40.     public override void OnPlayerLeftRoom(Player otherPlayer)
  41.     {
  42.         Debug.LogFormat("Player {0} left", otherPlayer.NickName);
  43.     }
  44.     public void Leave()
  45.     {
  46.         PhotonNetwork.LeaveRoom();
  47.     }
  48.     public void PlayerHitPointsReduce()
  49.     {
  50.         PlayerHitpoints -= 1;
  51.     }
  52.  
  53.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  54.     {
  55.         if (stream.IsWriting)
  56.         {
  57.             stream.SendNext(PlayerHitpoints);
  58.         }
  59.         else if(stream.IsReading)
  60.         {
  61.             EnemyHitpoints = (int)stream.ReceiveNext();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement