Ranked done, Shop done and many fixes

This commit is contained in:
Sewmina
2022-09-20 00:16:35 +05:30
parent 4194d2decc
commit f4e5b60bbf
166 changed files with 15171 additions and 1586 deletions

View File

@@ -6,56 +6,120 @@ using LightReflectiveMirror;
public class AutoConnect : MonoBehaviour
{
public bool isClient;
public static bool isRankedServer;
public static string serverName;
public static int serverPort;
public bool isRanked;
public LightReflectiveMirrorTransport lrm;
public kcp2k.KcpTransport transport;
void Start()
{
if(isRanked){
if (isRanked)
{
if(!isRankedServer){
lrm.serverListUpdated.AddListener(OnServerListUpdated);
StartCoroutine(refreshList());
}else{
StartCoroutine(startHost());
// if(!isRankedServer){
// lrm.serverListUpdated.AddListener(OnServerListUpdated);
// StartCoroutine(refreshList());
// }else{
// StartCoroutine(startHost());
// }
string[] args = System.Environment.GetCommandLineArgs();
string input = "";
for (int i = 0; i < args.Length; i++)
{
// Debug.Log("ARG " + i + ": " + args[i]);
if (args[i] == "-port")
{
input = args[i + 1];
}
}
ushort port = 7777;
try
{
port = ushort.Parse(input);
}
catch
{
}
}else{
if(isClient){
if(!isClient){
Debug.Log("Setting port to " + port);
transport.Port = port;
NetworkManager.singleton.StartServer();
}else{
NetworkManager.singleton.networkAddress = RegionManager.selectedServer.ip;
transport.Port = (ushort)serverPort;
NetworkManager.singleton.StartClient();
}
}
else
{
if (isClient)
{
FindObjectOfType<NetworkManager>().networkAddress = RegionManager.selectedServer.ip;
FindObjectOfType<NetworkManager>().StartClient();
}
}
}
IEnumerator startHost(){
while(!lrm.isConnectedToRelay){
yield return new WaitForSeconds(1);
}
lrm.serverName = serverName;
lrm.isPublicServer=true;
NetworkManager.singleton.StartHost();
}
IEnumerator refreshList(){
while(!lrm.isConnectedToRelay){
yield return new WaitForSeconds(1);
}
while(!NetworkManager.singleton.isNetworkActive){
yield return new WaitForSeconds(1);
lrm.RequestServerList();
}
}
void OnServerListUpdated(){
Debug.Log("Got new server list of " + lrm.relayServerList.Count);
foreach(Room room in lrm.relayServerList){
if(room.serverName == serverName){
Debug.Log("Found server for me! Joining now");
NetworkManager.singleton.networkAddress = room.serverId;
NetworkManager.singleton.StartClient();
float t;
void Update(){
if(isRanked && !isClient){
//timer
t += Time.deltaTime;
if(t > 30 && FindObjectsOfType<SpaceshipController>().Length ==0){
//No players joined, or they left, gotta close my self
t=0;
StartCoroutine(CloseRoom());
}
}
}
IEnumerator CloseRoom(){
WWWForm form = new WWWForm();
form.AddField("port", serverPort);
WWW req = new WWW(DBmanager.phpRoot+"clear_ranked_room.php",form);
yield return req;
Application.Quit();
}
// IEnumerator startHost()
// {
// while (!lrm.isConnectedToRelay)
// {
// yield return new WaitForSeconds(1);
// }
// lrm.serverName = serverName;
// lrm.isPublicServer = true;
// NetworkManager.singleton.StartHost();
// }
// IEnumerator refreshList()
// {
// while (!lrm.isConnectedToRelay)
// {
// yield return new WaitForSeconds(1);
// }
// while (!NetworkManager.singleton.isNetworkActive)
// {
// yield return new WaitForSeconds(1);
// lrm.RequestServerList();
// }
// }
// void OnServerListUpdated()
// {
// Debug.Log("Got new server list of " + lrm.relayServerList.Count);
// foreach (Room room in lrm.relayServerList)
// {
// if (room.serverName == serverName)
// {
// Debug.Log("Found server for me! Joining now");
// NetworkManager.singleton.networkAddress = room.serverId;
// NetworkManager.singleton.StartClient();
// }
// }
// }
}

View File

@@ -36,7 +36,7 @@ public class Leaderboard : MonoBehaviour
{
for (int i = 0; i <= players.Length - 2; i++)
{
if ( (isRanked ? players[i].moonsCollected : players[i].Scores) > (isRanked? players[i+1].moonsCollected : players[i + 1].Scores))
if ( (!isRanked ? players[i].moonsCollected : players[i].Scores) > (!isRanked? players[i+1].moonsCollected : players[i + 1].Scores))
{
temp = players[i + 1];
players[i + 1] = players[i];
@@ -51,7 +51,7 @@ public class Leaderboard : MonoBehaviour
SpaceshipController thisPlayer = players[players.Length-i-1];
leaderboardItems[i].gameObject.SetActive(true);
leaderboardItems[i].text = (i+1) + ". " +thisPlayer.pname;
if(isRanked){
if(!isRanked){
leaderboardItems[i].transform.GetChild(0).GetComponent<Text>().text = thisPlayer.Scores.ToString();
}else{
leaderboardItems[i].transform.GetChild(0).GetComponent<Text>().text = (((float)Mathf.Clamp(thisPlayer.moonsCollected,0,30)/ 30f)*100f).ToString("n1") + " %";

View File

@@ -14,7 +14,6 @@ public class MatchMaker : MonoBehaviour
public TMP_Text timerTxt;
public Button btn_cancel;
float timer;
public string serverName;
void Start()
{
if(!DBmanager.LoggedIn){
@@ -47,8 +46,6 @@ public class MatchMaker : MonoBehaviour
}
}
bool isServer =false;
IEnumerator FetchMatchmake(){
WWWForm form = new WWWForm();
@@ -58,19 +55,31 @@ public class MatchMaker : MonoBehaviour
WWW req = new WWW(DBmanager.phpRoot+"matchmake.php", form);
yield return req;
Debug.Log(req.text);
if(req.text.Contains(DBmanager.username) && !loadedScene){
//Room created!
isServer= req.text.Substring(0,DBmanager.username.Length) == DBmanager.username;
serverName=req.text;
AutoConnect.serverName = serverName;
AutoConnect.isRankedServer = isServer;
// AutoConnect.isRanked = true;
Debug.Log($"Room created! [{req.text}] am I the server? : " + isServer);
// if(req.text.Contains(DBmanager.username) && !loadedScene){
// int port
// //Room created!
// // isServer= req.text.Substring(0,DBmanager.username.Length) == DBmanager.username;
// // serverName=req.text;
// // AutoConnect.serverName = serverName;
// // AutoConnect.isRankedServer = isServer;
// // AutoConnect.isRanked = true;
// Debug.Log($"Room created! [{req.text}] am I the server? : ");
LoadingScreen.instance.LoadLevel("MinigameRanked");
// LoadingScreen.instance.LoadLevel("MinigameRanked");
// }else{
// //Waiting
// }
try{
int port = int.Parse(req.text);
if(port > 5000){
Debug.Log("Got the port, Lets go!");
AutoConnect.serverPort = port;
LoadingScreen.instance.LoadLevel("MinigameRanked");
}
}catch{
}else{
//Waiting
}
}

View File

@@ -11,7 +11,12 @@ public class MinigameManager : NetworkBehaviour
public static MinigameManager instance;
public bool isRanked;
public bool RankedGameStarted=false;
[SyncVar(hook =nameof(OnWinnerChanged))]
public int winnerId=-1;
[SyncVar]
public double startedTime = 0;
public GameObject waitingScreen;
public RankedGameSummary rankedSummary;
public float mapRadius;
public int maxMoons, maxStars = 100;
public Transform pickupItemsParent;
@@ -38,7 +43,13 @@ public class MinigameManager : NetworkBehaviour
// if(!DBmanager.LoggedIn){SceneManager.LoadScene(0);} //Signed out, no game for u
if(isRanked){
RankedGameStarted= (FindObjectsOfType<SpaceshipController>().Length >=2);
// RankedGameStarted= (FindObjectsOfType<SpaceshipController>().Length >=2);
if(!RankedGameStarted && !isServer){
SpaceshipController[] players = FindObjectsOfType<SpaceshipController>();
if(players.Length >= 2){
RankedGameStarted=true;
}
}
waitingScreen.SetActive(!RankedGameStarted);
}
@@ -46,7 +57,51 @@ public class MinigameManager : NetworkBehaviour
HandlePickupSpawn();
KillOutOfBoundsPlayers();
if(isRanked){RankedMechanics();}
}
void RankedMechanics(){
SpaceshipController[] players = FindObjectsOfType<SpaceshipController>();
if(players.Length < 2){
//Not enough players
}else{
if(!RankedGameStarted){
startedTime=NetworkTime.time;
}
RankedGameStarted=true;
if(players[0].moonsCollected >= 30){
//player 1 has won
winnerId = (int)players[0].netId;
// players[0].WonRanked();
// players[1].LostRanked();
}else if(players[1].moonsCollected >= 30){
//player 2 has won
winnerId = (int)players[1].netId;
// players[0].WonRanked();
// players[1].LostRanked();
}
}
if(RankedGameStarted && players.Length < 2){
//Forfeited!
winnerId = (int)players[0].netId;
// players[0].WonRanked();
}
}
void OnWinnerChanged(int oldVal, int newVal){
if(newVal<= 0){return;}
Debug.Log($"{newVal} id won!");
SpaceshipController localPlayer = SceneData.localPlayer.GetComponent<SpaceshipController>();
if(newVal == localPlayer.netId){
//We won
localPlayer.CmdWonRanked();
Debug.Log("Its Me!, I won!");
}else{
Debug.Log("Its not me, I lost!");
localPlayer.CmdLostRanked();
}
}
void KillOutOfBoundsPlayers()

View File

@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class RankedGameSummary : MonoBehaviour
{
public TMP_Text txtGameOver;
public TMP_Text txtTrophies;
public void ShowWin(){
txtGameOver.text = "You Won!";
txtTrophies.text= "+30";
txtTrophies.color = Color.green;
}
public void ShowLoss(){
txtGameOver.text = "You Lost!";
txtTrophies.text= "-15";
txtTrophies.color = Color.red;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 812537ae82c3b8c56ada62732b76a181
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -13,6 +13,7 @@ public class SceneDataHolder : MonoBehaviour
public GameObject metalEarnings;
public TMP_Text survivalTimeTxt;
public TMP_Text mostTimeTxt;
public TMP_Text gameOverTxt;
public EventTrigger boostBtn;
public TMP_Text timerTxt;
void Awake()
@@ -32,8 +33,12 @@ public class SceneDataHolder : MonoBehaviour
xpEarnings.SetActive(xpEarned > 0); metalEarnings.SetActive(metalEarned > 0);
xpEarnings.GetComponentInChildren<TMP_Text>().text = xpEarned.ToString();
metalEarnings.GetComponentInChildren<TMP_Text>().text = metalEarned.ToString();
survivalTimeTxt.text = SceneData.SecondsToText(survivalTime);
mostTimeTxt.text = SceneData.SecondsToText(DBmanager.MostTime);
if(!MinigameManager.instance.isRanked){
survivalTimeTxt.text = SceneData.SecondsToText(survivalTime);
mostTimeTxt.text = SceneData.SecondsToText(DBmanager.MostTime);
}else{
// MinigameManager.instance.rankedSummary.ShowLoss();
}
}
}

View File

@@ -3,20 +3,21 @@ using UnityEngine;
using Mirror;
using System.Linq;
using UnityEngine.UI;
using System.Collections;
public class SpaceshipController : NetworkBehaviour
{
public SpriteRenderer playerImg;
public SkinsData skins;
[SyncVar(hook=nameof(OnSkinChanged))]
[SyncVar(hook = nameof(OnSkinChanged))]
public string skinName = null;
[SyncVar(hook=nameof(OnPnameChanged))]
[SyncVar(hook = nameof(OnPnameChanged))]
public string pname;
[SyncVar(hook=nameof(OnScoresChanged))]
[SyncVar(hook = nameof(OnScoresChanged))]
public int Scores;
[SyncVar(hook=nameof(OnKillsChanged))]
[SyncVar(hook = nameof(OnKillsChanged))]
public int Kills;
[SyncVar(hook=nameof(OnTrailTimeChanged))]
[SyncVar(hook = nameof(OnTrailTimeChanged))]
public float trailTime;
[SyncVar]
public int moonsCollected;
@@ -25,8 +26,8 @@ public class SpaceshipController : NetworkBehaviour
public bool dead;
[SyncVar]
public float speed;
[SyncVar(hook=nameof(OnScaleChanged))]
public float scaleMultiplier=1;
[SyncVar(hook = nameof(OnScaleChanged))]
public float scaleMultiplier = 1;
public Text pnameTxt;
public Transform body;
public TrailMgr trailMgr;
@@ -39,87 +40,108 @@ public class SpaceshipController : NetworkBehaviour
[Header("Client Prediction")]
public SortedDictionary<int, StatePayload> serverStateBuffer = new SortedDictionary<int, StatePayload>();
public Vector3 DetourError = new Vector3(0,0.2f,0);
public Vector3 DetourError = new Vector3(0, 0.2f, 0);
public Vector3 Detour = Vector3.zero;
public Quaternion RotationDetour = Quaternion.identity;
public float DetourCorrectionFactor = 0.5f;
public float timeDelayErrorFix = 1f;
public bool showDebugHUD = false;
public float distanceFromCenter= 0;
public float distanceFromCenter = 0;
[Command]
void CmdSetPname(string value){
void CmdSetPname(string value)
{
pname = value;
}
void OnPnameChanged(string oldName, string newName){
void OnPnameChanged(string oldName, string newName)
{
pnameTxt.text = newName;
}
[Command]
void CmdSetSkin(string newSkin){
void CmdSetSkin(string newSkin)
{
skinName = newSkin;
}
void OnSkinChanged(string oldSkin, string newSkin){
void OnSkinChanged(string oldSkin, string newSkin)
{
ChangeSkin(newSkin);
}
void ChangeSkin(string name){
Sprite newSprite = SkinManagerHelper.getSkinSprite(name,skins);
void ChangeSkin(string name)
{
Sprite newSprite = SkinManagerHelper.getSkinSprite(name, skins);
playerImg.sprite = newSprite;
}
void OnScoresChanged(int oldScores, int newScores){
Debug.Log($"Add scores { newScores - oldScores}, (total: {newScores})");
void OnScoresChanged(int oldScores, int newScores)
{
Debug.Log($"Add scores {newScores - oldScores}, (total: {newScores})");
}
void OnTrailTimeChanged(float oldValue, float newValue){
void OnTrailTimeChanged(float oldValue, float newValue)
{
trailMgr.trail.time = newValue;
}
void OnBoostDown(){
if(isLocalPlayer){
if(isServer){
boosting=true;
}else{
void OnBoostDown()
{
if (isLocalPlayer)
{
if (isServer)
{
boosting = true;
}
else
{
CmdSetBoosting(true);
}
}
}
void OnBoostUp(){
if(isLocalPlayer){
if(isServer){
boosting=false;
}else{
void OnBoostUp()
{
if (isLocalPlayer)
{
if (isServer)
{
boosting = false;
}
else
{
CmdSetBoosting(false);
}
}
}
void OnScaleChanged(float oldScale, float newScale){
if(isLocalPlayer){
SceneData.holder.boostBtn.gameObject.SetActive(newScale>1);
void OnScaleChanged(float oldScale, float newScale)
{
if (isLocalPlayer)
{
SceneData.holder.boostBtn.gameObject.SetActive(newScale > 1);
}
}
[Command]
void CmdSetBoosting(bool value){
boosting=value;
void CmdSetBoosting(bool value)
{
boosting = value;
}
[SyncVar]
double startedTime = 0;
void ResetStats(){
void ResetStats()
{
startedXp = DBmanager.Xp;
startedMetal = DBmanager.Metal;
}
void Start()
{
scaleMultiplier=1;
scaleMultiplier = 1;
if (isLocalPlayer)
{
ResetStats();
@@ -130,17 +152,25 @@ public class SpaceshipController : NetworkBehaviour
SceneData.OnBoostDown.AddListener(OnBoostDown);
SceneData.OnBoostUp.AddListener(OnBoostUp);
//Set username
if(isServer){pname=myName;}else{
if (isServer) { pname = myName; }
else
{
CmdSetPname(myName);
}
//Set Skin
if(!DBmanager.SkinsPurchased.Contains(SkinShopManager.GetEquipedSkin())){
if (!DBmanager.SkinsPurchased.Contains(SkinShopManager.GetEquipedSkin()))
{
//False skin purchase
}else{
if(isServer){
}
else
{
if (isServer)
{
skinName = SkinShopManager.GetEquipedSkin();
}else{
}
else
{
CmdSetSkin(SkinShopManager.GetEquipedSkin());
}
}
@@ -162,27 +192,29 @@ public class SpaceshipController : NetworkBehaviour
int lastClientUpdateTime = 0;
void FixedUpdate()
{
if(MinigameManager.instance.isRanked && !MinigameManager.instance.RankedGameStarted){return;}
if (MinigameManager.instance.isRanked && !MinigameManager.instance.RankedGameStarted) { return; }
distanceFromCenter = Vector3.Distance(transform.position, Vector3.zero);
pnameTxt.rectTransform.rotation = Quaternion.Euler(Vector3.zero);
//Update size of trail and spaceship
transform.localScale = Vector3.Lerp(transform.localScale,new Vector3(scaleMultiplier,scaleMultiplier,scaleMultiplier),0.1f);
trailMgr.trail.startWidth = Mathf.Lerp(trailMgr.trail.startWidth,scaleMultiplier,0.1f);
transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(scaleMultiplier, scaleMultiplier, scaleMultiplier), 0.1f);
trailMgr.trail.startWidth = Mathf.Lerp(trailMgr.trail.startWidth, scaleMultiplier, 0.1f);
if(dead){return;}
if (dead) { return; }
if (isLocalPlayer)
{
joyInput = joystick.input;
//Cheats => TODO: Remove this
if(Input.GetKeyDown(KeyCode.F)){
if (Input.GetKeyDown(KeyCode.F))
{
CmdCheatKills();
}
// Debug.Log(startedTime-NetworkTime.time);
double survivalTime = (startedTime ==0) ? 0 :NetworkTime.time - startedTime;
if(DBmanager.MostTime < (int)survivalTime){
double survivalTime = (startedTime == 0) ? 0 : NetworkTime.time - ((MinigameManager.instance.isRanked)? MinigameManager.instance.startedTime :startedTime);
if (DBmanager.MostTime < (int)survivalTime)
{
DBmanager.SetMostTime((int)survivalTime);
}
SceneData.SetTimerTxt(survivalTime);
@@ -197,15 +229,19 @@ public class SpaceshipController : NetworkBehaviour
}
}
if(isServer){
if (isServer)
{
//boost check
if(boosting && scaleMultiplier > 1){
speed= movingSpeed*2;
scaleMultiplier-=Time.deltaTime;
if(scaleMultiplier<1){scaleMultiplier=1;} //Clamp in case gets lower
}else{
speed=movingSpeed;
if (boosting && scaleMultiplier > 1)
{
speed = movingSpeed * 2;
scaleMultiplier -= Time.deltaTime;
if (scaleMultiplier < 1) { scaleMultiplier = 1; } //Clamp in case gets lower
}
else
{
speed = movingSpeed;
}
}
///Diff = rot1 . rot2
@@ -221,15 +257,16 @@ public class SpaceshipController : NetworkBehaviour
{
Vector3 newPosition = body.position + Detour;
Quaternion newRotation = body.rotation * RotationDetour;
if(Detour.magnitude > 0.5f){
trailMgr.trail.emitting =false;
if (Detour.magnitude > 0.5f)
{
trailMgr.trail.emitting = false;
}
body.position = Vector3.Lerp(body.position, newPosition, (Mathf.Abs(Detour.magnitude) > 0.2f) ? DetourCorrectionFactor * 2 * Detour.magnitude : DetourCorrectionFactor);
body.position = Vector3.Lerp(body.position, newPosition, (Mathf.Abs(Detour.magnitude) > 0.2f) ? DetourCorrectionFactor * 2 * Detour.magnitude : DetourCorrectionFactor);
Detour = newPosition - body.position;
body.rotation = Quaternion.Lerp(body.rotation, newRotation, DetourCorrectionFactor * ((joystick.touchDown) ? 0.1f : 1));
RotationDetour = Quaternion.Inverse(transform.rotation) * newRotation;
trailMgr.trail.emitting=true;
trailMgr.trail.emitting = true;
}
}
@@ -258,7 +295,9 @@ public class SpaceshipController : NetworkBehaviour
}
RpcUpdatePosition(joyInput, transform.position, transform.rotation, timeInMillis);
RpcUpdateTrail(trailMgr.positions);
}else{
}
else
{
transform.position = Vector3.Lerp(transform.position, targetPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 0.1f);
}
@@ -266,7 +305,7 @@ public class SpaceshipController : NetworkBehaviour
Vector3 targetPosition;
Quaternion targetRotation;
void Turn(Vector2 input)
{
@@ -298,7 +337,7 @@ public class SpaceshipController : NetworkBehaviour
payloadToCompare = lastServerState;
if (lastServerState.Time < time)
{
// Debug.Log("Server doesn't have that data yet\nYou asked for " + time + " best I can do is " + lastServerState.Time);
// Debug.Log("Server doesn't have that data yet\nYou asked for " + time + " best I can do is " + lastServerState.Time);
}
else
{
@@ -327,7 +366,7 @@ public class SpaceshipController : NetworkBehaviour
}
else
{
// Debug.Log($"RB : {positionDiff.magnitude} [{timeDiff}ms]");
// Debug.Log($"RB : {positionDiff.magnitude} [{timeDiff}ms]");
RpcRubberBand(joyInput, serverStateBuffer.Values.Last().Position, serverStateBuffer.Values.Last().Rotation, trailMgr.positions, timeInMillis);
}
@@ -336,21 +375,21 @@ public class SpaceshipController : NetworkBehaviour
[ClientRpc]
void RpcUpdatePosition(Vector2 input, Vector3 position, Quaternion rotation, double sentTime)
{
if(isLocalPlayer || isServer){return;}
if (isLocalPlayer || isServer) { return; }
double delay = (timeInMillis - sentTime) * timeDelayErrorFix;
int numberOfFrames = (int)((float)(delay/1000f) *50f);
int numberOfFrames = (int)((float)(delay / 1000f) * 50f);
Quaternion newRotation = rotation;
Vector3 direction = (rotation * Vector3.forward).normalized;
Vector3 newPosition = position + ((direction * speed) * numberOfFrames);
for (int i = 0; i < numberOfFrames; i++)
{
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
}
targetPosition = newPosition - DetourError;
targetRotation = newRotation;
}
double lastRubberBandTime = 0;
@@ -363,12 +402,12 @@ public class SpaceshipController : NetworkBehaviour
if (sentTime < lastRubberBandTime) { Debug.Log("Old rubber band rpc, ignoree..."); return; }
//Lag comprehension
double delay = (timeInMillis - sentTime) * timeDelayErrorFix;
int numberOfFrames = (int)((float)(delay/1000f) * 50f);
int numberOfFrames = (int)((float)(delay / 1000f) * 50f);
Quaternion newRotation = rotation;
Vector3 direction = (rotation * Vector3.forward).normalized;
Vector3 newPosition = position + ((direction * speed) * numberOfFrames);
for (int i = 0; i < numberOfFrames; i++)
{
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
@@ -402,12 +441,13 @@ public class SpaceshipController : NetworkBehaviour
RotationDetour = Quaternion.Inverse(transform.rotation) * newRotation;
lastRubberBandTime = sentTime;
// Debug.Log($"Rubber banded (Detour of pos:{Detour}, rotation: {RotationDetour}) you to {transform.position}, @ {sentTime} (delay: {delay}");
// Debug.Log($"Rubber banded (Detour of pos:{Detour}, rotation: {RotationDetour}) you to {transform.position}, @ {sentTime} (delay: {delay}");
}
}
[ClientRpc]
void RpcUpdateTrail(Vector3[] positions){
void RpcUpdateTrail(Vector3[] positions)
{
//trailMgr.trail.SetPositions(positions);
}
@@ -438,15 +478,18 @@ public class SpaceshipController : NetworkBehaviour
{
body = transform;
}
if(trailMgr==null){
if (trailMgr == null)
{
trailMgr = GetComponent<TrailMgr>();
}
}
public void TrailCollided(Collider2D hit){
if(!isServer){
public void TrailCollided(Collider2D hit)
{
if (!isServer)
{
Debug.Log("Got hit : " + hit.name);
// Debug.Log("This cannot run on client, That's illegal!"); // <-- What this log says
return;
@@ -454,55 +497,64 @@ public class SpaceshipController : NetworkBehaviour
SpaceshipController deadPlayer = hit.GetComponent<SpaceshipController>();
Debug.Log("got hit by player? : " + deadPlayer != null);
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone | KILLCODE
if (deadPlayer != null && !deadPlayer.dead && (NetworkTime.time- deadPlayer.startedTime) > 5)
{ // <-- okay we killed someone | KILLCODE
deadPlayer.Die(pname);
Debug.Log($"{pname} killed {deadPlayer.pname}");
OnKill();
OnKill();
}
}
void OnKill(){
void OnKill()
{
Kills++;
Scores+= 10; //TODO: Need to change Scores on kills?
scaleMultiplier+=0.05f;
OnScaleChanged(scaleMultiplier,scaleMultiplier);
Scores += 10; //TODO: Need to change Scores on kills?
scaleMultiplier += 0.05f;
OnScaleChanged(scaleMultiplier, scaleMultiplier);
IncreaseTrail(trailIncrementRate);
RpcOnKill();
}
[ClientRpc]
void RpcOnKill(){
void RpcOnKill()
{
}
void OnKillsChanged(int oldVal, int newVal){
if(isLocalPlayer){
void OnKillsChanged(int oldVal, int newVal)
{
if (isLocalPlayer)
{
Debug.Log("Show kills for " + newVal);
KillText.Show(newVal);
}
}
void IncreaseTrail(float rate){
trailTime = trailMgr.trail.time+ rate;
void IncreaseTrail(float rate)
{
trailTime = trailMgr.trail.time + rate;
trailMgr.trail.time = trailTime;
Debug.Log("Increasing trail of" + pname);
}
[Command]
void CmdCheatKills(){
void CmdCheatKills()
{
OnKill();
}
public void CollectPickup(PickupItem.PickupType type){
if(!isServer){Debug.Log("Server function ran on client. That's illegal!");} // <-- What this log says
switch(type){
public void CollectPickup(PickupItem.PickupType type)
{
if (!isServer) { Debug.Log("Server function ran on client. That's illegal!"); } // <-- What this log says
switch (type)
{
case PickupItem.PickupType.Moon:
IncreaseTrail(trailIncrementRate);
moonsCollected++;
break;
case PickupItem.PickupType.Star:
IncreaseTrail(trailIncrementRate/2f);
IncreaseTrail(trailIncrementRate / 2f);
break;
}
@@ -510,24 +562,27 @@ public class SpaceshipController : NetworkBehaviour
}
[ClientRpc]
void RpcCollectPickup(PickupItem.PickupType type){
if(isLocalPlayer){
MinigameManager.instance.GainMetals((type==PickupItem.PickupType.Star) ? 10 : 30);
void RpcCollectPickup(PickupItem.PickupType type)
{
if (isLocalPlayer)
{
MinigameManager.instance.GainMetals((type == PickupItem.PickupType.Star) ? 2 : 5);
}
}
public void Die(string killer){
public void Die(string killer)
{
MinigameManager.instance.SpawnLeftoverPickups(transform.position, Scores);
Debug.Log($"Sending death signal to {pname} by {killer}");
//Handle Respawning
OnScaleChanged(scaleMultiplier,1);
scaleMultiplier=1;
dead=true;
Scores=0;
Kills=0;
startedTime=NetworkTime.time;
OnScaleChanged(scaleMultiplier, 1);
scaleMultiplier = 1;
dead = true;
Scores = 0;
Kills = 0;
startedTime = NetworkTime.time;
trailTime = 1;
trailMgr.trail.time = trailTime;
RpcDie(killer);
@@ -538,59 +593,153 @@ public class SpaceshipController : NetworkBehaviour
private int startedXp;
private int startedMetal;
bool RewardedRankedMetal =false;
[ClientRpc]
public void RpcDie(string killer){
public void RpcDie(string killer)
{
Debug.Log($"{killer} killed {pname} : isItMe? -> {isLocalPlayer}");
KillfeedMgr.instance.AddNewEntry($"<b>{pname}</b> was killed by <b>{killer}</b>");
gameObject.SetActive(false);
if(isLocalPlayer){
if (isLocalPlayer)
{
//TODO: Death message goes here
SceneData.holder.ShowDeadscreen(DBmanager.Xp - startedXp, DBmanager.Metal - startedMetal, NetworkTime.time - startedTime);
if (MinigameManager.instance.isRanked)
{
// MinigameManager.instance.rankedSummary.ShowLoss();
// StartCoroutine(ShowRankedResults());
if(MinigameManager.instance.winnerId<=0){
Debug.LogError("Winner ID not synced yet");
}
if (MinigameManager.instance.winnerId != netId)
{
//LOSER!
MinigameManager.instance.rankedSummary.ShowLoss();
}
else
{
MinigameManager.instance.rankedSummary.ShowWin();
}
}
}
}
public void Respawn(Vector3 respawnPoint){
IEnumerator ShowRankedResults()
{
while (MinigameManager.instance.winnerId <= 0)
{
yield return new WaitForFixedUpdate();
}
}
public void Respawn(Vector3 respawnPoint)
{
Debug.Log("Respawning " + pname);
if(isServer){
if (isServer)
{
respawn(respawnPoint);
}else{
}
else
{
CmdRespawn(respawnPoint);
}
}
[Command]
void CmdRespawn(Vector3 respawnPoint){
void CmdRespawn(Vector3 respawnPoint)
{
respawn(respawnPoint);
}
public void respawn(Vector3 respawnPoint){
dead=false;
trailMgr.trail.emitting =false;
public void respawn(Vector3 respawnPoint)
{
dead = false;
trailMgr.trail.emitting = false;
trailMgr.trail.Clear();
RpcRespawn(respawnPoint);
transform.position = respawnPoint;
trailMgr.trail.emitting=true;
trailMgr.trail.emitting = true;
gameObject.SetActive(true);
}
[ClientRpc]
public void RpcRespawn(Vector3 respawnPoint){
GetComponent<NetworkTrail>().enableValidation=false;
public void RpcRespawn(Vector3 respawnPoint)
{
GetComponent<NetworkTrail>().enableValidation = false;
trailMgr.trail.Clear();
trailMgr.positions = new Vector3[0];
trailMgr.trail.emitting = false;
transform.position = respawnPoint;
trailMgr.trail.emitting=true;
GetComponent<NetworkTrail>().enableValidation=true;
trailMgr.trail.emitting = true;
GetComponent<NetworkTrail>().enableValidation = true;
gameObject.SetActive(true);
if(isLocalPlayer){
if (isLocalPlayer)
{
SceneData.holder.deadScreen.SetActive(false);
ResetStats();
}
}
[Command]
public void CmdWonRanked(){
WonRanked();
}
[Command]
public void CmdLostRanked(){
LostRanked();
}
public void WonRanked()
{
if (!isServer)
{
return;
}
RpcWonRanked();
Die("server");
}
[ClientRpc]
void RpcWonRanked()
{
if (isLocalPlayer)
{
DBmanager.SetMetal(DBmanager.Metal+500);
MinigameManager.instance.rankedSummary.ShowWin();
//Add trophies
DBmanager.SetTrophies(DBmanager.Trophies + 30);
}
}
public void LostRanked()
{
if (!isServer)
{
return;
}
RpcLostRanked();
Die("server");
}
void RpcLostRanked()
{
if (isLocalPlayer)
{
DBmanager.SetMetal(DBmanager.Metal+250);
MinigameManager.instance.rankedSummary.ShowLoss();
DBmanager.SetTrophies(Mathf.Clamp(DBmanager.Trophies - 15, 0, int.MaxValue));
}
}
}