using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
public class Health : MonoBehaviour
{
public int health;
public bool isLocalPlayer;
// This is just to make it easier to read in the editor and will not effect the game
[Header("UI")]
public TextMeshProUGUI healthText;
// This may look non important as it looks short but without this line of text the whole damage system would not be synced with the server.
[PunRPC]
// This Funtion is called when another script needs to tell the player that they took damage, setting it up this way means it will be easier to have diffrent damage inputs from diffrent weapons.
public void TakeDamage(int _damage)
{
// Health is set so that any intenger values (whole numbers) is recived it is taken away from the players health.
health -= _damage;
// This line of code is here to recive input from another script Called WeaponAK which controlls the damage being sent when active.
healthText.text = _damage.ToString();
// Basicly when the player Health is or less than 0 the game will destroy the players object and calls a respawn funtion to the server for that player.
if (health <= 0)
{
// This If statement will check will be made on the client side to see if they have died and if true will ley the server know.
if (isLocalPlayer)
{
// This will Call a script that the server will hand when respawning a player.
RoomManager.instance.RespoawnPlayer();
}
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Linq;
using Photon.Pun;
using TMPro;
using Photon.Pun.UtilityScripts;
using Photon.Realtime;
public class Leaderboard : MonoBehaviour
{
public GameObject playerHolder;
[Header("Options")]
public float refreshRate = 1f;
[Header("UI")]
public GameObject[] slots;
[Space]
public TextMeshProUGUI[] scoreText;
public TextMeshProUGUI[] nameText;
private void Start()
{
InvokeRepeating(nameof(Refresh), 1f, refreshRate);
}
public void Refresh()
{
foreach (var slot in slots)
{
slot.SetActive(false);
}
// this is will grabs photon's list of players in the current room the client is playing in
var sortedPlayerList =
(from player in PhotonNetwork.PlayerList orderby player.GetScore() descending select player).ToList();
int i = 0;
foreach (var player in sortedPlayerList)
{
slots[i].SetActive(true);
if (player.NickName == "")
player.NickName = "Anonymous";
nameText[i].text = player.NickName;
scoreText[i].text = player.GetScore().ToString();
i++;
}
}
private void Update()
{
playerHolder.SetActive(Input.GetKey(KeyCode.Tab));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
public class PlayerSetup : MonoBehaviour
{
public MovementNoAim movement;
public GrapplingNoAIm grapple;
public Swing swing;
public PlayerWeaponController weapon;
public GameObject camera;
public string nickname;
public TextMeshPro nicknameText;
// This is to check that the client is one gaining control.
public void IslocalPlayer()
{
movement.enabled = true;
grapple.enabled = true;
swing.enabled = true;
weapon.enabled = true;
camera.SetActive(true);
}
// This makes theis funtion callable by the server.
[PunRPC]
public void SetNickname(string _name)
{
nickname = _name;
nicknameText.text = nickname;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class RoomManager : MonoBehaviourPunCallbacks
{
public static RoomManager instance;
public GameObject player;
[Space]
public Transform[] spawnPoints;
[Space]
public GameObject roomCam;
[Space]
public GameObject nameUI;
public GameObject connectingUI;
private string nickname = "Anonymous";
private void Awake()
{
instance = this;
}
public void ChangeNickname(string _name)
{
nickname = _name;
}
// This is the same as the start() but now it starts connecting to the server once the player has entered a nickname and pressed a button
public void JoinRoomButtonPressed()
{
Debug.Log("Connecting...");
// The Setting for the Photon plugin will be used this (should be set on best locaton)
PhotonNetwork.ConnectUsingSettings();
nameUI.SetActive(false);
connectingUI.SetActive(true);
}
// Start is called before the first frame update
void Start()
{
// Everything In here has now been moved to JoinRoomButtonPressed()
}
// This call is only made when the photon plugin connects to the server (which can been seen on https://dashboard.photonengine.com/)
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Connected to Server");
// This command will tell the Photon plugin to join a lobby. Since this has been left blank with no other info the Photon network will connect the client to the default Lobby
PhotonNetwork.JoinLobby();
}
// This is called when the client is connected to the server and the Photon plugin has sucessfully connected the to Lobby
public override void OnJoinedLobby()
{
base.OnJoinedLobby();
// This means that he client will either tell the server to create a Room called "test" or join one that already exsits
PhotonNetwork.JoinOrCreateRoom("test", null, null);
Debug.Log("Client Connected and in the lobby!");
}
// This is called when the client is connected to the Lobby and the Photon plugin has sucessfully connected the to Room
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
Debug.Log("Client has sucessfuly connected to a room!");
roomCam.SetActive(false);
RespoawnPlayer();
}
public void RespoawnPlayer()
{
// This randomises the Spawn points for players that are joining the session.
Transform spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];
// This will add a playable pawn for the client to use (clarifacation Playable pawn means that theres an object that the player can control).
GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
// This makes all imput on the client side not be sent to server.
_player.GetComponent<PlayerSetup>().IslocalPlayer();
// This will make sure that it should only effect the client reciveing wise.
_player.GetComponent<Health>().isLocalPlayer = true;
// This sets the players nickname to the server.
_player.GetComponent<PhotonView>().RPC("SetNickname", RpcTarget.AllBuffered, nickname);
PhotonNetwork.LocalPlayer.NickName = nickname;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.Audio;
public class WeaponAK : MonoBehaviour
{
public int damage;
public Camera camera;
public float fireRate;
public AudioSource audioSource;
public AudioClip gunSound;
private float nextFire;
// Update is called once per frame
void Update()
{
// This if statement makes sure that if the gun is not ready to fire it will count down from the game time, This is here to prevent guns becomming into a portable lazer gun (in a nutshell).
if (nextFire > 0)
{
nextFire -= Time.deltaTime;
}
// This IF statement checks to see if the players firebutton is down while also checking if the guns is avalible to fire and if so will reset the nextFire timer is reset back to cooldown-
// and call the funtion Fire.
if (Input.GetButton("Fire1") && nextFire <= 0)
{
nextFire = 1 / fireRate;
fire();
}
}
// Once this Funtion is called it will begin to make some checks to see if the player hit an damageable object (e.g. Another Player).
// To do this it will first draw a staight line from the clients camera and where they are looking and draw a line with rhe lengh of 100 units = 100 meters (Acording to Google!!!).
// Then the sctipt will check to see if if the line has hit a player and if this true then the script will grab the object/player then check to see if the object has an componant called Health.
// This will then send an call out to the PhotonView Componant and tells it to activate the Funtion TakeDamage which is found in a players Health script and makes the apropiate changes-
// which are explained inside of the Health script.
void fire()
{
Ray ray = new Ray(camera.transform.position, camera.transform.forward);
RaycastHit hit;
// This is makes a noise to make sure the gun is being fired, the other reson that this line is here is to make it easier to get the message across in a video that the gun is being fired.
audioSource.PlayOneShot(gunSound, 0.5f);
if(Physics.Raycast(ray.origin, ray.direction, out hit, 100f))
{
if (hit.transform.gameObject.GetComponent<Health>())
{
hit.transform.gameObject.GetComponent<PhotonView>().RPC("TakeDamage", RpcTarget.All, damage);
}
}
}
}