Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Last active October 6, 2021 07:46
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/7521689 to your computer and use it in GitHub Desktop.
Save cnsoft/7521689 to your computer and use it in GitHub Desktop.
Set Custom Propertites.
PhotonNetwork.isMessageQueueRunning = true;
isPaused = true;
resolutions = Screen.resolutions;
resolutionIndex = (resolutions.Length-1)/2;
QualityNames = QualitySettings.names;
playerList = true;
enableHelper = GameObject.FindWithTag("EnableHelper").gameObject;
//Setup team names ********************************
team_1.teamName = "Team A";
team_2.teamName = "Team B";
//Set player colors *********************************
team_1_Color = Color.cyan;
team_2_Color = Color.yellow;
//*********************************************
//Get round time
roundDuration = (int)PhotonNetwork.room.customProperties["RoundDuration"];
gameMode = (string)PhotonNetwork.room.customProperties["GameMode"];
//Setup all player properties
Hashtable setPlayerTeam = new Hashtable() {{"TeamName", "Spectators"}};
PhotonNetwork.player.SetCustomProperties(setPlayerTeam);
Hashtable setPlayerKills = new Hashtable() {{"Kills", 0}};
PhotonNetwork.player.SetCustomProperties(setPlayerKills);
Hashtable setPlayerDeaths = new Hashtable() {{"Deaths", 0}};
PhotonNetwork.player.SetCustomProperties(setPlayerDeaths);
//if WeaponSync master client, store reference tiem for others
if(PhotonNetwork.isMasterClient){
referenceTime = (float)PhotonNetwork.time;
//And store in room properties given reference time
Hashtable setReferenceTime = new Hashtable() {{"RefTime", referenceTime}};
PhotonNetwork.room.SetCustomProperties(setReferenceTime);
//Setup team scores
Hashtable setTeam1Score = new Hashtable() {{"Team1Score", 0}};
PhotonNetwork.room.SetCustomProperties(setTeam1Score);
Hashtable setTeam2Score = new Hashtable() {{"Team2Score", 0}};
PhotonNetwork.room.SetCustomProperties(setTeam2Score);
}else{
//Get saved reference time
referenceTime = (float)PhotonNetwork.room.customProperties["RefTime"];
}
//Join all spawn points in one list for DM mode
allSpawnPoints.Clear();
foreach(Transform point in team_1.spawnPoints){
allSpawnPoints.Add(point);
}
foreach(Transform point in team_2.spawnPoints){
allSpawnPoints.Add(point);
}
//异步加载地图.
IEnumerator LoadMap(string sceneName){
PhotonNetwork.isMessageQueueRunning = false;
fadeDir = 1;
yield return new WaitForSeconds(1);
Application.backgroundLoadingPriority = ThreadPriority.High;
AsyncOperation async = Application.LoadLevelAsync(sceneName);
yield return async;
Debug.Log("Loading complete");
}
//回调地图加载的方式 ::处理断连 离开房间.
void OnDisconnectedFromPhoton(){
print ("Disconnected from Photon");
//Something wrong with connection - go to main menu
isPaused = false;
roomCamera.SetActive(true);
StartCoroutine(LoadMap("MainMenu"));
}
void OnLeftRoom(){
isPaused = false;
roomCamera.SetActive(true);
StartCoroutine(LoadMap("MainMenu"));
}
//
//同步协程发射子弹..
//SHOTGUN type weapons sync **************************************************
void syncShotGun(float fractions){
this.StopAllCoroutines();
StartCoroutine(shotGunShot(fractions));
}
IEnumerator shotGunShot(float fractions){
for (int i = 0;i < (int)fractions; i++) {
Quaternion oldRotation = firePoint.rotation;
firePoint.rotation = Quaternion.Euler(Random.insideUnitSphere * 3) * firePoint.rotation;
Instantiate (bullet, firePoint.position, firePoint.rotation);
firePoint.rotation = oldRotation;
}
//Play fire sound
audio.clip = fireAudio;
audio.Play();
//Muzzle flash
if(muzzleFlash){
muzzleFlash.renderer.enabled = true;
yield return new WaitForSeconds(0.04f);
muzzleFlash.renderer.enabled = false;
}
}
//************************************************************************
//DODamage is called by HitBox.
//This is a dmaage our remote player received from Hit Boxes
public void TotalDamage(float damage){
if(disableDamage)
return;
fadeValue = 2;
photonView.RPC("DoDamage", PhotonTargets.All, damage, PhotonNetwork.player);
}
[RPC]
//This is damage sent fro remote player instance to our local
void DoDamage(float damage, PhotonPlayer player){
if(weKilled)
return;
if(currentHp > 0 && photonView.isMine){
this.StopAllCoroutines();
StartCoroutine(doCameraShake());
}
fadeValueB = 2;
currentHp -= damage;
//We got killed
if(currentHp < 0){
//Deactivate all child meshes
for(int i = 0; i < transform.childCount; i++){
transform.GetChild(i).gameObject.SetActive(false);
}
//Spawn ragdoll
GameObject temp;
temp = Instantiate(ragdoll, transform.position, transform.rotation) as GameObject;
if(!photonView.isMine){
temp.SendMessage("clearCamera");
if(PhotonNetwork.player == player){
//Send death notification message to script WhoKilledWho.cs
GameObject.FindWithTag("Network").SendMessage("AddKillNotification", gameObject.name, SendMessageOptions.DontRequireReceiver);
//Add 1 kill for our player
int totalKIlls = (int)PhotonNetwork.player.customProperties["Kills"];
totalKIlls ++;
Hashtable setPlayerKills = new Hashtable() {{"Kills", totalKIlls}};
PhotonNetwork.player.SetCustomProperties(setPlayerKills);
//Add team score
int teamScore = new int();
if((string)PhotonNetwork.player.customProperties["TeamName"] == rmm.team_1.teamName){
teamScore = (int)PhotonNetwork.room.customProperties["Team1Score"];
teamScore ++;
Hashtable setTeam1Score = new Hashtable() {{"Team1Score", teamScore}};
PhotonNetwork.room.SetCustomProperties(setTeam1Score);
}
if((string)PhotonNetwork.player.customProperties["TeamName"] == rmm.team_2.teamName){
teamScore = (int)PhotonNetwork.room.customProperties["Team2Score"];
teamScore ++;
Hashtable setTeam2Score = new Hashtable() {{"Team2Score", teamScore}};
PhotonNetwork.room.SetCustomProperties(setTeam2Score);
}
}
}else{
//print ("We got killed");
temp.SendMessage("RespawnAfter");
//We was killed, add 1 to deaths
int totalDeaths = (int)PhotonNetwork.player.customProperties["Deaths"];
totalDeaths ++;
Hashtable setPlayerDeaths = new Hashtable() {{"Deaths", totalDeaths}};
PhotonNetwork.player.SetCustomProperties(setPlayerDeaths);
//Destroy our player
StartCoroutine(DestroyPlayer(0.2f));
}
currentHp = 0;
weKilled = true;
}
}
//HitBox. (it is used to apply damage )
void ApplyDamage(float damage){
//So we receive bullet damage and also add other damage value we have configured in PlayerDamage.cs
playerDamage.TotalDamage(damage + maxDamage);
}
//Bullet.js
//We cant hit ourselfs
if(hit.transform.tag != "Player" && doDamage){
hit.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
//处理子弹碰撞到自己hitbox (不是player) 也销毁.
//Explosions.js
var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;
var explosionTimeout = 2.0;
var player1 = GameObject;
function Start () {
var explosionPosition = transform.position;
// Apply damage to close by objects first
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
// Calculate distance from the explosion position to the closest point on the collider
var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
// The hit points we apply fall decrease with distance from the explosion point
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
// Tell the rigidbody or any other script attached to the hit object how much damage is to be applied!
hit.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
}
// Apply explosion forzces to all rigidbodies
// This needs to be in two steps for ragdolls to work correctly.
// (Enemies are first turned into ragdolls with ApplyDamage then we apply forces to all the spawned body parts)
colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
}
// stop emitting particles
if (particleEmitter) {
particleEmitter.emit = true;
yield WaitForSeconds(0.5);
particleEmitter.emit = false;
}
Destroy (gameObject, explosionTimeout);
}
//爆炸的处理
@Gryffind96
Copy link

this very useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment