Update engine subproject commit to f382fc98eadc4a489689e51e88748b616661a560-dirty; adjust plant genetics for optimal temperature and hardiness traits, enhance terrain scatter probabilities, and improve UI localization for connection status. Refactor multiplayer scene to implement exponential backoff for reconnections and streamline game initialization in web client.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.JSInterop;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace LittleSim.Web.Pages
|
||||
{
|
||||
@@ -10,7 +10,12 @@ namespace LittleSim.Web.Pages
|
||||
[Inject]
|
||||
private NavigationManager Navigation { get; set; } = null!;
|
||||
|
||||
private Game? _game;
|
||||
private LittleSimWebGame? _game;
|
||||
private string _serverAddress = "";
|
||||
private Uri? _pendingServer; // запрошен Connect до создания игры — подключим в первом тике
|
||||
private ConnectionStatus _lastStatus = ConnectionStatus.Idle;
|
||||
private int _lastCount = -1;
|
||||
private int _frame;
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
@@ -18,25 +23,92 @@ namespace LittleSim.Web.Pages
|
||||
|
||||
if (firstRender)
|
||||
{
|
||||
_serverAddress = DefaultServerAddress();
|
||||
StateHasChanged();
|
||||
JsRuntime.InvokeAsync<object>("initRenderJS", DotNetObjectReference.Create(this));
|
||||
}
|
||||
}
|
||||
|
||||
// Гонится из requestAnimationFrame (index.html). Игру создаём лениво в первом тике —
|
||||
// мира она не создаёт, лишь ждёт адрес сервера из формы.
|
||||
[JSInvokable]
|
||||
public void TickDotNet()
|
||||
{
|
||||
if (_game == null)
|
||||
{
|
||||
_game = new LittleSimWebGame(ResolveServerUri());
|
||||
_game = new LittleSimWebGame();
|
||||
_game.Run();
|
||||
}
|
||||
|
||||
_game.Tick();
|
||||
|
||||
if (_pendingServer is not null)
|
||||
{
|
||||
_game.Connect(_pendingServer);
|
||||
_pendingServer = null;
|
||||
}
|
||||
|
||||
// Перерисовываем оверлей только при смене статуса (или изредка — чтобы освежить счётчик),
|
||||
// а не каждый кадр rAF.
|
||||
var status = _game.Status;
|
||||
var count = _game.EntityCount;
|
||||
if (
|
||||
status != _lastStatus
|
||||
|| (status == ConnectionStatus.Connected && count != _lastCount && _frame % 15 == 0)
|
||||
)
|
||||
{
|
||||
_lastStatus = status;
|
||||
_lastCount = count;
|
||||
InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
_frame++;
|
||||
}
|
||||
|
||||
// Адрес сервера: ?server=ws://host:port в URL страницы; по умолчанию —
|
||||
// хост самой страницы на порту LittleSim.Server.
|
||||
private Uri ResolveServerUri()
|
||||
private void OnAddressKey(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Enter")
|
||||
{
|
||||
Connect();
|
||||
}
|
||||
}
|
||||
|
||||
private void Connect()
|
||||
{
|
||||
if (!Uri.TryCreate(_serverAddress?.Trim(), UriKind.Absolute, out var uri))
|
||||
{
|
||||
return; // адрес не похож на ws://… — игнорируем
|
||||
}
|
||||
|
||||
if (_game is not null)
|
||||
{
|
||||
_game.Connect(uri);
|
||||
}
|
||||
else
|
||||
{
|
||||
_pendingServer = uri;
|
||||
}
|
||||
}
|
||||
|
||||
private string StatusText =>
|
||||
_lastStatus switch
|
||||
{
|
||||
ConnectionStatus.Connecting => "подключение…",
|
||||
ConnectionStatus.Connected => $"подключено · {_lastCount} жит.",
|
||||
ConnectionStatus.Reconnecting => "переподключение…",
|
||||
_ => "не подключено",
|
||||
};
|
||||
|
||||
private string StatusClass =>
|
||||
_lastStatus switch
|
||||
{
|
||||
ConnectionStatus.Connected => "ok",
|
||||
ConnectionStatus.Connecting or ConnectionStatus.Reconnecting => "warn",
|
||||
_ => "idle",
|
||||
};
|
||||
|
||||
// Адрес по умолчанию: ?server=ws://host:port из URL страницы, иначе хост страницы на порту сервера.
|
||||
private string DefaultServerAddress()
|
||||
{
|
||||
var page = new Uri(Navigation.Uri);
|
||||
var query = page.Query.TrimStart('?');
|
||||
@@ -45,7 +117,7 @@ namespace LittleSim.Web.Pages
|
||||
var separator = pair.IndexOf('=');
|
||||
if (separator > 0 && pair[..separator] == "server")
|
||||
{
|
||||
return new Uri(Uri.UnescapeDataString(pair[(separator + 1)..]));
|
||||
return Uri.UnescapeDataString(pair[(separator + 1)..]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +126,7 @@ namespace LittleSim.Web.Pages
|
||||
Scheme = "ws",
|
||||
Host = page.Host,
|
||||
Port = WebNetSchema.DefaultPort,
|
||||
}.Uri;
|
||||
}.Uri.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user