RustToDust Phase 2: Procedural brush terrain, 4-corner Ymir support, and Godot 4.7.2 Mono client
This commit is contained in:
parent
32b040a59b
commit
215b5ea834
20 changed files with 3486 additions and 56 deletions
256
client/Main.cs
Normal file
256
client/Main.cs
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
using System;
|
||||
using Godot;
|
||||
using RustToDust.Core;
|
||||
using RustToDust.Ymir;
|
||||
|
||||
namespace RustToDust.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Root Godot node for Rust to Dust.
|
||||
/// Godot physics is Dummy; <see cref="ArenaSession"/> owns 100% of vehicle, thermal, spectator, and terrain physics at 60 Hz.
|
||||
/// </summary>
|
||||
public partial class Main : Node3D
|
||||
{
|
||||
private TerrainRenderer _terrain = null!;
|
||||
private ArenaRenderer _arena = null!;
|
||||
private Camera3D _camera = null!;
|
||||
private Hud _hud = null!;
|
||||
private ArenaSession? _session;
|
||||
|
||||
private PlayerInput? _scriptedInput;
|
||||
private float _cameraDistance = 30f;
|
||||
private Vector3 _cameraSmoothedTarget = Vector3.Zero;
|
||||
private float _elapsedSeconds;
|
||||
private bool _interactQueued;
|
||||
private int _seed = 42;
|
||||
|
||||
public ArenaSession Session => _session ?? throw new InvalidOperationException($"No active ArenaSession ({Failure ?? "uninitialized"})");
|
||||
public TerrainRenderer Terrain => _terrain;
|
||||
public ArenaRenderer Arena => _arena;
|
||||
public Hud Hud => _hud;
|
||||
public string? Failure { get; private set; }
|
||||
public int Seed => _seed;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
SetupLightingAndSky();
|
||||
|
||||
_terrain = new TerrainRenderer { Name = "Terrain" };
|
||||
AddChild(_terrain);
|
||||
|
||||
_arena = new ArenaRenderer { Name = "Arena" };
|
||||
AddChild(_arena);
|
||||
|
||||
_camera = new Camera3D
|
||||
{
|
||||
Name = "TacticalCamera",
|
||||
Current = true,
|
||||
Fov = 48f,
|
||||
Far = 520f
|
||||
};
|
||||
AddChild(_camera);
|
||||
|
||||
_hud = new Hud(() => _session, OnEquipCargo) { Name = "Hud" };
|
||||
AddChild(_hud);
|
||||
|
||||
Reset(_seed);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
_session?.Dispose();
|
||||
_session = null;
|
||||
}
|
||||
|
||||
public void Reset(int seed)
|
||||
{
|
||||
_seed = seed;
|
||||
_session?.Dispose();
|
||||
_session = null;
|
||||
Failure = null;
|
||||
|
||||
try
|
||||
{
|
||||
_session = new ArenaSession(seed);
|
||||
var p = _session.State.Player;
|
||||
_cameraSmoothedTarget = new Vector3(p.X, p.Y, p.Z);
|
||||
_terrain.InvalidateAll();
|
||||
_terrain.Sync(_session.State.Terrain);
|
||||
_arena.Sync(_session, _elapsedSeconds);
|
||||
_hud.Sync(_session);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Failure = ex.Message;
|
||||
GD.PushError("ArenaSession startup failed: " + ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetScriptedInput(PlayerInput? input) => _scriptedInput = input;
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventKey key && key.Pressed && !key.Echo)
|
||||
{
|
||||
switch (key.Keycode)
|
||||
{
|
||||
case Key.E:
|
||||
_interactQueued = true;
|
||||
break;
|
||||
case Key.Tab:
|
||||
_hud.ToggleGarage();
|
||||
break;
|
||||
case Key.R:
|
||||
Reset(_seed + 1);
|
||||
break;
|
||||
case Key.Escape:
|
||||
Input.MouseMode = Input.MouseMode == Input.MouseModeEnum.Confined
|
||||
? Input.MouseModeEnum.Visible
|
||||
: Input.MouseModeEnum.Confined;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (@event is InputEventMouseButton mb && mb.Pressed)
|
||||
{
|
||||
if (mb.ButtonIndex == MouseButton.WheelUp)
|
||||
{
|
||||
_cameraDistance = Math.Clamp(_cameraDistance - 2.5f, 16f, 58f);
|
||||
}
|
||||
else if (mb.ButtonIndex == MouseButton.WheelDown)
|
||||
{
|
||||
_cameraDistance = Math.Clamp(_cameraDistance + 2.5f, 16f, 58f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (_session is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_elapsedSeconds += (float)delta;
|
||||
var input = _scriptedInput ?? GatherPlayerInput();
|
||||
_interactQueued = false;
|
||||
|
||||
_session.Step(input);
|
||||
_terrain.Sync(_session.State.Terrain);
|
||||
_arena.Sync(_session, _elapsedSeconds);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_session is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateCamera((float)delta);
|
||||
_hud.Sync(_session);
|
||||
}
|
||||
|
||||
private PlayerInput GatherPlayerInput()
|
||||
{
|
||||
float throttle = 0f;
|
||||
if (Input.IsKeyPressed(Key.W) || Input.IsKeyPressed(Key.Up)) throttle += 1f;
|
||||
if (Input.IsKeyPressed(Key.S) || Input.IsKeyPressed(Key.Down)) throttle -= 1f;
|
||||
|
||||
float steer = 0f;
|
||||
if (Input.IsKeyPressed(Key.D) || Input.IsKeyPressed(Key.Right)) steer += 1f;
|
||||
if (Input.IsKeyPressed(Key.A) || Input.IsKeyPressed(Key.Left)) steer -= 1f;
|
||||
|
||||
bool brake = Input.IsKeyPressed(Key.Space);
|
||||
Vector2 mousePos = GetViewport().GetMousePosition();
|
||||
bool pointerOnHud = _hud.IsPointerOverInteractiveHud(mousePos);
|
||||
bool fire = !pointerOnHud && Input.IsMouseButtonPressed(MouseButton.Left);
|
||||
|
||||
var (aimX, aimZ) = ComputeMouseTerrainAim(mousePos);
|
||||
return new PlayerInput(
|
||||
Throttle: throttle,
|
||||
Steer: steer,
|
||||
AimWorldX: aimX,
|
||||
AimWorldZ: aimZ,
|
||||
Fire: fire,
|
||||
InteractPickup: _interactQueued,
|
||||
Brake: brake);
|
||||
}
|
||||
|
||||
private (float AimX, float AimZ) ComputeMouseTerrainAim(Vector2 mousePos)
|
||||
{
|
||||
var player = Session.State.Player;
|
||||
Vector3 rayOrigin = _camera.ProjectRayOrigin(mousePos);
|
||||
Vector3 rayDir = _camera.ProjectRayNormal(mousePos);
|
||||
|
||||
if (Session.State.Terrain.Raycast(
|
||||
rayOrigin.X, rayOrigin.Y, rayOrigin.Z,
|
||||
rayDir.X, rayDir.Y, rayDir.Z,
|
||||
maxDistance: 320f,
|
||||
out float hx, out _, out float hz, out _))
|
||||
{
|
||||
return (hx, hz);
|
||||
}
|
||||
|
||||
if (MathF.Abs(rayDir.Y) > 1e-4f)
|
||||
{
|
||||
float t = (player.Y - rayOrigin.Y) / rayDir.Y;
|
||||
if (t > 0f)
|
||||
{
|
||||
Vector3 p = rayOrigin + rayDir * t;
|
||||
return (p.X, p.Z);
|
||||
}
|
||||
}
|
||||
|
||||
return (player.X + MathF.Sin(player.Yaw) * 25f, player.Z + MathF.Cos(player.Yaw) * 25f);
|
||||
}
|
||||
|
||||
private void UpdateCamera(float dt)
|
||||
{
|
||||
var p = Session.State.Player;
|
||||
var target = new Vector3(p.X, p.Y + 1.0f, p.Z);
|
||||
float blend = 1f - MathF.Exp(-dt * 9f);
|
||||
_cameraSmoothedTarget = _cameraSmoothedTarget.Lerp(target, blend);
|
||||
|
||||
// Angled tactical 3rd-person desert chase view
|
||||
Vector3 offset = new Vector3(0f, _cameraDistance * 0.78f, -_cameraDistance * 0.72f);
|
||||
_camera.GlobalPosition = _cameraSmoothedTarget + offset;
|
||||
_camera.LookAt(_cameraSmoothedTarget, Vector3.Up);
|
||||
}
|
||||
|
||||
private void OnEquipCargo(int cargoIndex, string hardpointId)
|
||||
{
|
||||
_session?.TryEquipCargo(cargoIndex, hardpointId);
|
||||
}
|
||||
|
||||
private void SetupLightingAndSky()
|
||||
{
|
||||
var sun = new DirectionalLight3D
|
||||
{
|
||||
Name = "DesertSun",
|
||||
LightColor = new Color(1.0f, 0.92f, 0.78f),
|
||||
LightEnergy = 1.45f,
|
||||
ShadowEnabled = true,
|
||||
RotationDegrees = new Vector3(-48f, 32f, 0f)
|
||||
};
|
||||
AddChild(sun);
|
||||
|
||||
var skyMat = new ProceduralSkyMaterial
|
||||
{
|
||||
SkyTopColor = new Color(0.24f, 0.34f, 0.44f),
|
||||
SkyHorizonColor = new Color(0.76f, 0.56f, 0.37f),
|
||||
GroundBottomColor = new Color(0.32f, 0.22f, 0.14f),
|
||||
GroundHorizonColor = new Color(0.70f, 0.50f, 0.33f)
|
||||
};
|
||||
var env = new Godot.Environment
|
||||
{
|
||||
BackgroundMode = Godot.Environment.BGMode.Sky,
|
||||
Sky = new Sky { SkyMaterial = skyMat },
|
||||
AmbientLightSource = Godot.Environment.AmbientSource.Sky,
|
||||
AmbientLightEnergy = 0.55f,
|
||||
FogEnabled = true,
|
||||
FogLightColor = new Color(0.74f, 0.55f, 0.38f),
|
||||
FogDensity = 0.0022f
|
||||
};
|
||||
AddChild(new WorldEnvironment { Name = "WorldEnv", Environment = env });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue