public NetworkConnectionError StartServer(bool addToServerList) { NetworkConnectionError error; Network.InitializeSecurity(); error = Network.InitializeServer(settings.maxPlayers - 1, settings.port, !Network.HavePublicAddress()); //Get player type PlayerType myType = PlayerType.Anon; if (GameSettings.user.token != "") { myType = PlayerTypeHandler.GetPlayerType(GameSettings.user.playerName); } networkView.RPC("AddPlayerToList", RPCMode.All, Network.player, GameSettings.user.playerName, (int)myType); networkView.RPC("SetHost", RPCMode.AllBuffered, Network.player); //Set host for all players joining if (addToServerList) { isOnServerList = true; RegisterServer("In lobby"); } SendPublicMsg("Welcome to Sanicball!"); return(error); }
void LoadPlayerTypes(string data) { //Parse and add all players to the specialUsers list if (string.IsNullOrEmpty(data)) //Oh shit, the connection failed { isStarting = false; GJAPI.Data.GetCallback -= LoadPlayerTypes; StartOfflineMode(); return; } var specialUsers = new Dictionary <string, PlayerType>(); string[] pairs = data.Split(';'); foreach (string s in pairs) { string[] nameTypePair = s.Split('='); string nameStr = nameTypePair[0]; string typeStr = nameTypePair[1]; int typeInt = 0; if (int.TryParse(typeStr, out typeInt)) { typeInt += 2; //Add 2 to type to match with PlayerType enum specialUsers.Add(nameStr, (PlayerType)typeInt); } } PlayerTypeHandler.SetSpecialUsers(specialUsers); isStarting = false; GJAPI.Data.GetCallback -= LoadPlayerTypes; CheckIfSignedIn(); }
void SetupPlayer(NetworkPlayer player, string name, bool isAnon) { if (!Network.isServer) { return; } //Check if this player's version has been accepted if (connectionTimeout.ContainsKey(player)) { //This only happens when an older version joins a newer version. Therefore, Kick cannot be used. Slightly obsolete code. Network.CloseConnection(player, true); SendServerMsg("Player joined with wrong version and was kicked."); connectionTimeout.Remove(player); } //Set stuff on the newly connected player networkView.RPC("ChangeLevel", player, Application.loadedLevel); SendPublicMsg(name + " has joined the game."); //Add other players to new player's player list and do stuff with the new players foreach (ServerPlayer sp in client.GetPlayerList()) { networkView.RPC("AddPlayerToList", player, sp.player, sp.name, (int)sp.type); networkView.RPC("SetReady", player, sp.player, sp.ready); networkView.RPC("SetCharacter", player, sp.player, sp.character); networkView.RPC("SetSpectating", player, sp.player, sp.spectating); } //Grab new player's type PlayerType pType = PlayerType.Anon; if (!isAnon) { pType = PlayerTypeHandler.GetPlayerType(name); } //Add new player to everyone's player list networkView.RPC("AddPlayerToList", RPCMode.All, player, name, (int)pType); if (!client.IsInLobby) //Means the game is in progress. //networkView.RPC("SetSpectating",RPCMode.All,player,true); { SendPrivateMsg("You'll be able to race once the next round starts.", player); RaceController rc = GameObject.FindObjectOfType <RaceController>(); networkView.RPC("JoinRaceInProgress", player, rc.settings.ToString()); } else //Means we're in the lobby. { networkView.RPC("SetInitLobbyOnLevelLoad", player); //Sets the new player's character, forcing all players to spawn him into the lobby networkView.RPC("SetCharacter", RPCMode.All, player, 0); } Debug.Log("Player " + name + " has been successfully set up on clients."); }
void Start() { GameSettings.Init(); PlayerTypeHandler.Init(); options.UpdateVarsGeneral(); options.UpdateVarsProfile(); validKeyCodes = (KeyCode[])System.Enum.GetValues(typeof(KeyCode)); }
void OnGUI() { if (!hidden) { GUI.skin = skin; GUIStyle smallButton = GUI.skin.GetStyle("SmallButton"); Rect playerListRect = new Rect(Screen.width / 2 - 480, Screen.height / 2 - 320, 960, 640); GUI.Box(playerListRect, ""); GUILayout.BeginArea(playerListRect); List <ServerPlayer> playerList = client.GetPlayerList(); GUILayout.Label(playerList.Count + " player(s)"); playerListScroll = GUILayout.BeginScrollView(playerListScroll); if (client != null) { int i = 0; foreach (ServerPlayer sp in playerList) { GUIStyle labelStyle = GUI.skin.label; GUIStyle buttonStyle = smallButton; if (i % 2 == 0) { labelStyle = GUI.skin.GetStyle("LabelOdd"); buttonStyle = GUI.skin.GetStyle("SmallButtonOdd"); } string readystring = sp.ready ? "Ready" : "Not ready"; GUILayout.BeginHorizontal(); GUILayout.Label(sp.name, labelStyle, GUILayout.ExpandWidth(true)); GUIStyle typeStyle = new GUIStyle(labelStyle); typeStyle.normal.textColor = PlayerTypeHandler.GetPlayerColor(sp.type); GUILayout.Label(sp.type.ToString(), typeStyle, GUILayout.Width(200)); GUILayout.Label(readystring, labelStyle, GUILayout.Width(125)); if (Network.isServer && sp.player != Network.player) { if (GUILayout.Button("Kick", buttonStyle, GUILayout.Width(62))) { FindObjectOfType <Server>().KickPlayer(sp.player, "You have been kicked by the host."); } } else { GUILayout.Label("", labelStyle, GUILayout.Width(62)); } GUILayout.EndHorizontal(); i++; } } GUILayout.EndScrollView(); GUILayout.EndArea(); } }
public void SendChatMessage(NetworkPlayer sender, string message) { ServerPlayer player = GetComponent <Client>().FindServerPlayer(sender); string name = player.name; if (player.spectating) { name = "[Spectating] " + name; } if (GetComponent <Client>().IsHost(sender)) { name = "[Host] " + name; } AddMessage(name, message, PlayerTypeHandler.GetPlayerColor(player.type)); }
public Racer SpawnLobbyBall(ServerPlayer sp, bool isLocal) { Character c = Global.characters[sp.character]; //Get character for player Object o; if (sp.player == Network.player) { o = ballPlayerLobbyPrefab; } else { o = ballRemoteLobbyPrefab; } Racer r = (Racer)Instantiate(o, new Vector3(0, 8, -4), Quaternion.Euler(0, 90, 25)); r.name = o.name + "-" + sp.name; //Set character traits r.racerName = sp.name; r.renderer.material = c.material; r.mapIconMaterial = c.minimapIcon; r.GetComponent <TrailRenderer>().material = c.trail; BallControl bc = r.GetComponent <BallControl>(); if (c.alternativeMesh != null) { r.GetComponent <MeshFilter>().mesh = c.alternativeMesh; } if (bc != null) { bc.stats = c.stats; } r.transform.localScale = new Vector3(c.ballSize, c.ballSize, c.ballSize); if (o == ballRemoteLobbyPrefab) { GUIText label = r.transform.FindChild("ObjectLabel").guiText; label.text = sp.name; label.color = PlayerTypeHandler.GetPlayerColor(sp.type); } return(r); //Do more stuff //r.RaceController = raceController; //r.totalLaps = (int)settings.laps; }
public void DrawGUI() { GUIStyle smallButton = GUI.skin.GetStyle("smallButton"); if (page == 0) { if (GUILayout.Button("General")) { page = 1; } if (GUILayout.Button("Keybinds")) { page = 2; } if (GUILayout.Button("Profile")) { page = 3; } if (GUILayout.Button("Back", smallButton)) { menu.SetPage(MenuPage.None); } } if (page == 1) //General settings { enableTrails = GUILayout.Toggle(enableTrails, "Fancy ball trails"); fullscreen = GUILayout.Toggle(fullscreen, "Fullscreen"); vsync = GUILayout.Toggle(vsync, "Vsync"); Resolution currentRes = Screen.resolutions[(int)resolution]; GUILayout.Label("Resolution: " + currentRes.width + "x" + currentRes.height); resolution = GUILayout.HorizontalSlider(resolution, 0, Screen.resolutions.Length - 1); //GUILayout.Label ("Field of View: "+fov); //fov = (int)GUILayout.HorizontalSlider(fov,50,80); //AA buttons GUILayout.Label("Anti-aliasing: x" + aa); GUILayout.BeginHorizontal(); if (GUILayout.Button("Off", smallButton)) { aa = 0; } if (GUILayout.Button("x2", smallButton)) { aa = 2; } if (GUILayout.Button("x4", smallButton)) { aa = 4; } if (GUILayout.Button("x8", smallButton)) { aa = 8; } GUILayout.EndHorizontal(); //Dynamic shadows shadows = GUILayout.Toggle(shadows, "Dynamic Shedews"); //Camera speed for mouse / keyboard seperately GUILayout.Label("Camera speed (Mouse): " + (float)decimal.Round((decimal)sensitivityMouse, 1)); sensitivityMouse = GUILayout.HorizontalSlider(sensitivityMouse, 0.1f, 10); GUILayout.Label("Camera speed (Keyboard): " + (float)decimal.Round((decimal)sensitivityKeyboard, 1)); sensitivityKeyboard = GUILayout.HorizontalSlider(sensitivityKeyboard, 0.1f, 30); //sound volume GUILayout.Label("Sound volume: " + (float)decimal.Round((decimal)volume, 2) * 100 + "%"); volume = GUILayout.HorizontalSlider(volume, 0, 1); //Music toggle music = GUILayout.Toggle(music, "Music"); //sanic speed song sanicSpeedSong = GUILayout.Toggle(sanicSpeedSong, "Alt. song when going fast"); if (GUILayout.Button("Apply")) { Apply(); } if (GUILayout.Button("Back", smallButton)) { page = 0; UpdateVarsGeneral(); } } if (page == 2) //Keybind changer { foreach (KeyValuePair <string, KeybindInfo> bind in GameSettings.keybinds.GetAllBinds()) { GUILayout.BeginHorizontal(); GUILayout.Label(bind.Value.name, GUILayout.MaxWidth(200)); GUILayout.FlexibleSpace(); GUIStyle keycodeStyle = new GUIStyle(smallButton); keycodeStyle.alignment = TextAnchor.MiddleRight; if (GUILayout.Button(GameSettings.keybinds.GetKeyCodeName(bind.Value.keyCode), keycodeStyle, GUILayout.MaxWidth(200))) { keybindToChange = bind.Key; } GUILayout.EndHorizontal(); } if (GUILayout.Button("Reset keybinds", smallButton)) { GameSettings.keybinds = new Keybinds(); } if (string.IsNullOrEmpty(keybindToChange)) { if (GUILayout.Button("Apply")) { Apply(); } if (GUILayout.Button("Back", smallButton)) { page = 0; GameSettings.keybinds.LoadFromPrefs(); } } else { KeybindInfo info = GameSettings.keybinds.Find(keybindToChange); GUILayout.Label("Press a key to use for '" + info.name + "'. Escape to cancel."); } } if (page == 3) //Profile { if (GameSettings.user.UseGJ) //Logged in menu { GUILayout.Label("Signed in to Game Jolt as " + GameSettings.user.playerName); GUILayout.Label("'Remember me' is " + (GameSettings.user.rememberInfo ? "ON" : "OFF")); GUILayout.Label("Player status: " + PlayerTypeHandler.GetPlayerType(GameSettings.user.playerName)); if (GUILayout.Button("Log out")) { loginToken = GameSettings.user.token; loginName = GameSettings.user.playerName; GameSettings.user.token = ""; GameSettings.user.Save(); } } else //Anon menu { GUILayout.Label("Playing anonymously as '" + GameSettings.user.playerName + "'"); GUILayout.Label("'Remember me' is " + (GameSettings.user.rememberInfo ? "ON" : "OFF")); GUILayout.Label("Change your username:"******"Change", smallButton)) { GameSettings.user.playerName = anonName; } if (!GameSettings.user.offlineMode) { if (GUILayout.Button("Log in with Game Jolt", GUI.skin.GetStyle("smallbuttongreen"))) { page = 4; } } } if (GUILayout.Button("Back", smallButton)) { page = 0; UpdateVarsProfile(); } } if (page == 4) //Log in { GUILayout.Label("GJ username: "******"Focus"); loginName = GUILayout.TextField(loginName, 128); GUILayout.Label("User token: "); loginToken = GUILayout.PasswordField(loginToken, (char)0x25CF, 128); rememberMe = GUILayout.Toggle(rememberMe, "Remember me"); if ((GUILayout.Button("Log in") || (Event.current.isKey && Event.current.keyCode == KeyCode.Return)) && !isVerifying) { status = ""; if (loginName.Trim().Length <= 0) //Has the user typed something in the username field? { status = "You need to type an username."; } else if (loginToken.Trim().Length <= 0) //Has the user typed something in the token field? { status = "You need to type in your token."; } else //Log in with the Game Jolt API { status = "@spinner/Logging in..."; isVerifying = true; GJAPI.Users.Verify(loginName, loginToken); GJAPI.Users.VerifyCallback += LogInCallback; } } if (status.StartsWith("@spinner/")) { Spinner.Draw(status.Replace("@spinner/", "")); } else { GUILayout.Label(status); } if (GUILayout.Button("Back", smallButton) && !isVerifying) { page = 3; } } }
void CreateRacers(RaceController raceController) { Racer[] racers = new Racer[client.GetPlayerList().Count + (int)settings.aiBallCount]; int position = 0; int row = 0; foreach (ServerPlayer sp in client.GetPlayerList()) { if (sp.spectating) { continue; //Don't spawn spectators } Character c = Global.characters[sp.character]; //Get character for player Object o; if (sp.player == Network.player) { o = playerBallPrefab; //Is mlg mode? if (c.attributes.Contains("sponsored by mlg")) { Instantiate(mlgModeObject); } } else { o = remoteBallPrefab; } Racer r = (Racer)Instantiate(o, raceController.spawnPoint.GetSpawnPoint(position, c.ballSize / 2), raceController.spawnPoint.transform.rotation * Quaternion.Euler(0, -90, 0)); r.name = o.name + "-" + sp.name; //Set character traits r.racerName = sp.name; r.renderer.material = c.material; r.mapIconMaterial = c.minimapIcon; r.GetComponent <TrailRenderer>().material = c.trail; if (c.alternativeMesh != null) { r.GetComponent <MeshFilter>().mesh = c.alternativeMesh; } BallControl bc = r.GetComponent <BallControl>(); if (bc != null) { bc.stats = c.stats; } r.transform.localScale = new Vector3(c.ballSize, c.ballSize, c.ballSize); if (o == remoteBallPrefab) { GUIText label = r.transform.FindChild("ObjectLabel").guiText; label.text = sp.name; label.color = PlayerTypeHandler.GetPlayerColor(sp.type); } //Do more stuff r.totalLaps = (int)settings.laps; //Add racer to list sp.racer = r; racers[position] = r; position++; } //Create AI balls int aiPos = 0; Racer[] balllist = new Racer[(int)settings.aiBallCount]; for (int i = 0; i < settings.aiBallCount; i++) { //TO BE CHANGED (COPYPASTE IS BAD) //Calc position to put racer at Vector3 dir; if (position % 11 == 10) { row++; } if (position % 2 == 0) { dir = Vector3.right * ((position % 10) / 2 + 1) * 2; } else { dir = Vector3.left * ((position % 10) / 2) * 2; } dir += (Vector3.back * 1.4f) * row; Character c; if (aiPos < settings.aiCharacters.Count) { c = Global.characters[(int)settings.aiCharacters[aiPos]]; //Get character for player } else { c = Global.characters[1]; //Default to knackles } Object o; if (Network.isServer) { o = aiBallPrefab; } else { o = remoteBallPrefab; } Racer r = (Racer)Instantiate(o, raceController.spawnPoint.GetSpawnPoint(position, c.ballSize / 2), raceController.spawnPoint.transform.rotation * Quaternion.Euler(0, -90, 0)); r.name = o.name + "-" + c.name; //Set character traits r.racerName = c.name; r.renderer.material = c.material; r.mapIconMaterial = c.minimapIcon; r.GetComponent <TrailRenderer>().material = c.trail; BallControl bc = r.GetComponent <BallControl>(); if (bc != null) { bc.stats = c.stats; } r.transform.localScale = new Vector3(c.ballSize, c.ballSize, c.ballSize); //Do more stuff r.totalLaps = (int)settings.laps; //Add racer to list balllist[aiPos] = r; //Set aiball for client racers[position] = r; position++; aiPos++; } client.SetAIBalls(balllist); raceController.SetRacers(racers); }