Skip to content

Commit

Permalink
nightly v1.8.0.0 (1/8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipodtouch0218 committed Jan 9, 2024
1 parent b0ee14b commit b355755
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Assets/Scripts/BuildInfo.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
public static class BuildInfo{public static string BUILD_TIME = "1/8/2024 5:02:13 AM";}
public static class BuildInfo{public static string BUILD_TIME = "1/8/2024 11:29:05 PM";}
56 changes: 41 additions & 15 deletions Assets/Scripts/Camera/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NSMB.Entities.Player;
using NSMB.Game;
using NSMB.Utils;
using NSMB.Extensions;

//[OrderBefore(typeof(Powerup))]
public class CameraController : NetworkBehaviour {
Expand All @@ -17,8 +18,9 @@ public class CameraController : NetworkBehaviour {
public static float ScreenShake {
get => _screenShake;
set {
if (CurrentController && !CurrentController.controller.IsOnGround)
if (CurrentController && !CurrentController.controller.IsOnGround) {
return;
}

_screenShake = value;
}
Expand Down Expand Up @@ -51,26 +53,40 @@ public bool IsControllingCamera {
//---Private Variables
private readonly List<SecondaryCameraPositioner> secondaryPositioners = new();
private PropertyReader<Vector3> currentPositionReader;
private Vector2 previousCurrentPosition;

public void OnValidate() {
if (!controller) controller = GetComponentInParent<PlayerController>();

currentPositionReader = GetPropertyReader<Vector3>(nameof(CurrentPosition));
this.SetIfNull(ref controller, UnityExtensions.GetComponentType.Parent);
}

public void Awake() {
TargetCamera = Camera.main;
TargetCamera.GetComponentsInChildren(secondaryPositioners);
}

public override void Spawned() {
currentPositionReader = GetPropertyReader<Vector3>(nameof(CurrentPosition));
Object.RenderTimeframe = !controller.IsProxy ? RenderTimeframe.Auto : RenderTimeframe.Remote;
}

public override void Render() {
if (!IsControllingCamera)
if (!IsControllingCamera) {
return;
}

Vector3 newPosition;

if (TryGetSnapshotsBuffers(out var from, out var to, out float alpha)) {
(var fromVector, var toVector) = currentPositionReader.Read(from, to);
Vector2 fromVector, toVector;

if (Object.RenderTimeframe == RenderTimeframe.Remote) {
// Weird interpolation stuff taken from EntityMover
fromVector = previousCurrentPosition;
toVector = CurrentPosition;
previousCurrentPosition = CurrentPosition;
} else {
(fromVector, toVector) = currentPositionReader.Read(from, to);
}

Utils.UnwrapLocations(fromVector, toVector, out var fromVectorRelative, out var toVectorRelative);
Vector2 lerped = Vector2.Lerp(fromVectorRelative, toVectorRelative, alpha);
Expand All @@ -83,8 +99,9 @@ public override void Render() {
}

Vector3 shakeOffset = Vector3.zero;
if ((_screenShake -= Time.deltaTime) > 0)
if ((_screenShake -= Time.deltaTime) > 0) {
shakeOffset = new Vector3((Random.value - 0.5f) * _screenShake, (Random.value - 0.5f) * _screenShake);
}

SetPosition(newPosition + shakeOffset);
}
Expand All @@ -100,12 +117,14 @@ public void Recenter(Vector2 pos) {
}

private void SetPosition(Vector3 position) {
if (!IsControllingCamera)
if (!IsControllingCamera) {
return;
}

TargetCamera.transform.position = position;
if (BackgroundLoop.Instance)
if (BackgroundLoop.Instance) {
BackgroundLoop.Instance.Reposition();
}

secondaryPositioners.RemoveAll(scp => !scp);
secondaryPositioners.ForEach(scp => scp.UpdatePosition());
Expand All @@ -115,21 +134,25 @@ private Vector3 CalculateNewPosition(float delta) {
float minY = GameManager.Instance.cameraMinY, heightY = GameManager.Instance.cameraHeightY;
float minX = GameManager.Instance.cameraMinX, maxX = GameManager.Instance.cameraMaxX;

if (!controller.IsDead && !controller.IsRespawning)
if (!controller.IsDead && !controller.IsRespawning) {
PlayerPos = AntiJitter(controller.transform.position);
}

float vOrtho = TargetCamera.orthographicSize;
float xOrtho = vOrtho * TargetCamera.aspect;
Vector3 newCameraPosition = CurrentPosition;

// Lagging camera movements
if (controller.IsOnGround)
if (controller.IsOnGround) {
LastFloorHeight = PlayerPos.y;
}

bool validFloor = controller.IsOnGround || LastFloorHeight < PlayerPos.y;

// Floor height
if (validFloor)
if (validFloor) {
newCameraPosition.y = Mathf.Max(newCameraPosition.y, LastFloorHeight + floorOffset);
}

// Smoothing
Vector3 smoothDamp = SmoothDampVel;
Expand Down Expand Up @@ -170,13 +193,15 @@ private Vector3 CalculateNewPosition(float delta) {
right = newCameraPosition.x > PlayerPos.x;
}

if (xDifference > 0.25f)
if (xDifference > 0.25f) {
newCameraPosition.x += (0.25f - xDifference - 0.01f) * (right ? 1 : -1);
}

// Clamping to within level bounds
float maxY = heightY == 0 ? (minY + vOrtho) : (minY + heightY - vOrtho);
if (newCameraPosition.y > maxY)
if (newCameraPosition.y > maxY) {
SmoothDampVel = Vector3.zero;
}

newCameraPosition.x = Mathf.Clamp(newCameraPosition.x, minX + xOrtho, maxX - xOrtho);
newCameraPosition.y = Mathf.Clamp(newCameraPosition.y, minY + vOrtho, maxY);
Expand All @@ -197,8 +222,9 @@ private static Vector2 AntiJitter(Vector3 vec) {
#if UNITY_EDITOR
private static Vector3 HalfRight = Vector3.right * 0.5f;
public void OnDrawGizmos() {
if (!controller || !controller.Object)
if (!controller || !controller.Object) {
return;
}

Gizmos.color = Color.red;
Gizmos.DrawWireCube(new(PlayerPos.x, LastFloorHeight), HalfRight);
Expand Down
21 changes: 17 additions & 4 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public override void Spawned() {
public override void Render() {
base.Render();

if (!SessionData.Instance) {
return;
}

if (GameState == Enums.GameState.Playing) {
HandleMusic();
}
Expand Down Expand Up @@ -355,7 +359,7 @@ public void PauseEndMatch() {

public void PauseQuitGame() {
sfx.PlayOneShot(Enums.Sounds.UI_Decide);
_ = NetworkHandler.ConnectToRegion();
NetworkHandler.Instance.runner.Shutdown(false, ShutdownReason.Ok);
}

public void PauseOpenOptions() {
Expand Down Expand Up @@ -681,7 +685,6 @@ private IEnumerator EndGame(int winningTeam) {

GameState = Enums.GameState.Ended;
IsMusicEnabled = false;
PlaySounds = false;
endSoundPlayed = true;

// End "WaitForGameEnd" objects
Expand All @@ -694,6 +697,8 @@ private IEnumerator EndGame(int winningTeam) {

yield return new WaitForSecondsRealtime(1);

PlaySounds = false;

TranslationManager tm = GlobalController.Instance.translationManager;
bool draw = winningTeam == -1;
string resultText;
Expand Down Expand Up @@ -784,6 +789,10 @@ private IEnumerator EndGame(int winningTeam) {
}

private void HandleMusic() {
if (!SessionData.Instance) {
return;
}

bool invincible = false;
bool mega = false;
bool speedup = false;
Expand Down Expand Up @@ -879,13 +888,17 @@ private bool AttemptSpawnBigStar() {
}

private void ResetAvailableStarSpawns() {
AvailableStarSpawns.RawSet(unchecked((ulong) ~0L));
AvailableStarSpawns.RawSet(unchecked(~0UL));
}

/// <summary>
/// Sets the game timestamps for Discord RPC
/// </summary>
private void SetGameTimestamps() {
public void SetGameTimestamps() {
if (!SessionData.Instance) {
return;
}

double now = DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
float secondsSinceStart = Runner.SimulationTime - GameStartTime;
gameStartTimestamp = now - secondsSinceStart;
Expand Down
28 changes: 14 additions & 14 deletions Assets/Scripts/Networking/AuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ public async static Task<AuthenticationValues> Authenticate(string userid, strin

IsAuthenticating = true;

string request = URL + "?";
string requestUrl = URL + "?";
if (userid != null) {
request += "&userid=" + userid;
requestUrl += "&userid=" + userid;
}
if (token != null) {
request += "&token=" + token;
requestUrl += "&token=" + token;
}
byte args = 0;
Utils.BitSet(ref args, 0, !Settings.Instance.generalUseNicknameColor);
request += "&args=" + args;
requestUrl += "&args=" + args;

UnityWebRequest client = UnityWebRequest.Get(request);
UnityWebRequest webRequest = UnityWebRequest.Get(requestUrl);
webRequest.certificateHandler = new MvLCertificateHandler();
webRequest.disposeCertificateHandlerOnDispose = true;
webRequest.disposeDownloadHandlerOnDispose = true;
webRequest.disposeUploadHandlerOnDispose = true;
webRequest.timeout = 10;

client.certificateHandler = new MvLCertificateHandler();
client.disposeCertificateHandlerOnDispose = true;
client.disposeDownloadHandlerOnDispose = true;
client.disposeUploadHandlerOnDispose = true;
await webRequest.SendWebRequest();

await client.SendWebRequest();

if (client.result != UnityWebRequest.Result.Success) {
if (webRequest.result != UnityWebRequest.Result.Success) {
IsAuthenticating = false;
return null;
}
Expand All @@ -47,9 +47,9 @@ public async static Task<AuthenticationValues> Authenticate(string userid, strin
AuthType = CustomAuthenticationType.Custom,
UserId = userid,
};
values.AddAuthParameter("data", client.downloadHandler.text.Trim().Replace("\"", ""));
values.AddAuthParameter("data", webRequest.downloadHandler.text.Trim().Replace("\"", ""));

client.Dispose();
webRequest.Dispose();

IsAuthenticating = false;
return values;
Expand Down
21 changes: 14 additions & 7 deletions Assets/Scripts/Networking/Fusion/NetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void INetworkRunnerCallbacks.OnDisconnectedFromServer(NetworkRunner runner, NetD

ReturnToMainMenu(() => {
OnDisconnectedFromServer?.Invoke(runner, reason);
RecreateInstance();
//RecreateInstance();
});
}

Expand Down Expand Up @@ -304,8 +304,9 @@ void INetworkRunnerCallbacks.OnPlayerLeft(NetworkRunner runner, PlayerRef player
if (data) {
Debug.Log($"[Network] {data.GetNickname()} ({data.GetUserIdString()}) left the room");
ChatManager.Instance.AddSystemMessage("ui.inroom.chat.player.quit", "playername", data.GetNickname());
if (Runner.IsServer) {
if (runner.IsServer) {
runner.Despawn(data.Object);
runner.PushHostMigrationSnapshot();
}
playerDatas.Remove(data);
}
Expand Down Expand Up @@ -420,11 +421,6 @@ public static async Task<StartGameResult> ConnectToRegion(string region = "") {
// Exit if we're already in a room
if (Runner && (Runner.SessionInfo.IsValid || Runner.LobbyInfo.IsValid) && !Runner.IsShutdown) {
await Runner.Shutdown();
} else {
if (Runner) {
DestroyImmediate(Runner);
}
Instance.runner = Instance.gameObject.AddComponent<NetworkRunner>();
}

if (string.IsNullOrEmpty(region)) {
Expand Down Expand Up @@ -650,6 +646,17 @@ private static void HostMigrationResume(NetworkRunner runner) {
continue;
}

// Don't respawn invalid PlayerDatas
if (pd.UserId == default) {
continue;
}

// Don't respawn duplicate PlayerDatas (idk why they even exist???)
// Like we despawn them on player leave and they just pop back up...
if (Instance.playerDatas.Any(other => other.UserId == pd.UserId)) {
continue;
}

// Oh, and immediately assign our own. We're greedy :)
// (not doing it breaks stuff cuz EnterRoom is called first...)
PlayerRef? player = null;
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Networking/Fusion/SessionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NSMB.Extensions;
using NSMB.UI.MainMenu;
using NSMB.Utils;
using NSMB.Game;

public class SessionData : NetworkBehaviour {

Expand Down Expand Up @@ -102,6 +103,10 @@ public override void Spawned() {
if (MainMenuManager.Instance) {
MainMenuManager.Instance.roomSettingsCallbacks.UpdateAllSettings(Instance, false);
}

if (GameManager.Instance) {
GameManager.Instance.SetGameTimestamps();
}
}

public override void Render() {
Expand Down
Loading

0 comments on commit b355755

Please sign in to comment.