Files
GladiatorIdle/Mono/Arena/HubScene.Journey.cs
2026-11-09 16:13:28 +01:00

311 lines
20 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
/// <summary>
/// Reise-Ansicht (Vollbild): der eigene Gladiator marschiert als 3D-Figur durch die Region –
/// endloser Weg in Perspektive, Bäume, Büsche und Felsen am Rand, Meilensteine, das Regionsbild
/// weit hinten (langsamer, Parallaxe). Tag und Nacht laufen mit der Reisezeit (90 s je Tag),
/// nachts trägt er eine Fackel. Tagebuchzeilen „Tag N: …“ erscheinen nach dem Fortschritt.
/// </summary>
internal sealed partial class HubScene
{
/// Boden, Weg und Bewuchs je Region.
sealed record Land(string Ground, Vector3 GroundTint, string Path, Vector3 PathTint, string[] Props, Vector3 Haze);
static readonly Dictionary<string, Land> Lands = new()
{
["gallia"] = new("tex_grass", new(1f, 1f, 0.95f), "tex_sand", new(0.82f, 0.66f, 0.48f), new[] { "prop_oak", "prop_bush", "prop_rock", "prop_cypress" }, new(0.78f, 0.80f, 0.74f)),
["germania"] = new("tex_grass", new(0.78f, 0.9f, 0.78f), "tex_sand", new(0.66f, 0.54f, 0.42f), new[] { "prop_pine", "prop_pine", "prop_oak", "prop_bush", "prop_rock" }, new(0.62f, 0.68f, 0.66f)),
["britannia"] = new("tex_grass", new(0.88f, 1f, 0.88f), "tex_sand", new(0.72f, 0.62f, 0.52f), new[] { "prop_oak", "prop_bush", "prop_rock", "prop_rock" }, new(0.72f, 0.76f, 0.78f)),
["hispania"] = new("tex_dirt", new(1f, 0.9f, 0.8f), "tex_desert", new(0.95f, 0.85f, 0.7f), new[] { "prop_cypress", "prop_bush", "prop_rock" }, new(0.86f, 0.76f, 0.62f)),
["dacia"] = new("tex_snow", new(0.95f, 0.97f, 1f), "tex_snow", new(0.72f, 0.7f, 0.7f), new[] { "prop_pine", "prop_pine", "prop_rock" }, new(0.80f, 0.84f, 0.90f)),
["aegyptus"] = new("tex_desert", new(1f, 0.95f, 0.85f), "tex_desert", new(0.85f, 0.75f, 0.6f), new[] { "prop_palm", "prop_rock", "prop_palm" }, new(0.90f, 0.80f, 0.62f)),
};
const float WALK_SPEED = 230, CELL = 520;
readonly Camera _jCam = new();
readonly Particles _jfx = new();
readonly DuelFighter _jDummy = new() { IsPlayer = true, X = 0, Facing = 1, MaxHp = 1, Hp = 1 };
FighterView? _jHero;
BasicEffect? _jFx, _jPlane;
AlphaTestEffect? _jPropFx;
Mesh3D? _jGround, _jPath, _jStone;
string? _jRegion;
float _jX;
int _jLine = -1; float _jLineT;
/// Testlauf: Tageszeit (0 Morgen, 0,25 Mittag, 0,5 Abend, 0,75 Nacht) statt aus der Reisezeit.
public float? DevDayPhase;
// feste Puffer (keine neuen Objekte je Bild)
readonly VertexPositionColorTexture[] _jBack = new VertexPositionColorTexture[6];
readonly Dictionary<string, (VertexPositionColorTexture[] V, int N)> _jProps = new();
void OpenJourney()
{
if (_game.Away == null) return;
_overlay = Overlay.Journey;
_jX = 0; _jLine = -1;
string hero = Art.Hero(_game.Class, _game.Look.Preset);
_jHero ??= Assets.Has(hero) ? new FighterView(hero, 0.5f, _game.Look.Color) : new FighterView(Art.Fighter(_game.Class), 0.5f);
_jCam.FollowX = 300; _jCam.FollowZoom = 1.0f;
_jCam.Center = new Vector2(300, Camera.H / 2 + 40);
SoundManager.PlayDrawer();
}
void UpdateJourney(float dt)
{
if (_game.Away is not { } away) { _overlay = Overlay.Map; return; }
bool calm = AppSettings.ReduceMotion;
_jX += WALK_SPEED * dt;
_jHero!.Update(dt, dt, _jDummy, _jX, calm);
_jCam.FollowX = _jX + 260;
_jCam.Update(dt, _jX, _jX, calm);
_jfx.Calm = calm;
_jfx.Update(dt);
if (_jRegion != away.Region) BuildJourney(away.Region);
_jLineT += dt;
}
void BuildJourney(string region)
{
_jRegion = region;
var land = Lands[region];
float floor = Stage.FloorY;
// Boden: Gitter um den Ursprung, verschoben in ganzen Kachelbreiten (Textur bleibt an der Welt)
Mesh3D Grid(string tex, Vector3 tint, float z0, float z1, float y, int nz)
{
var m = new Mesh3D { Tex = Assets.Sprite(tex, "jpg"), Wrap = true, Tint = tint };
const int NX = 30;
float x0 = -4200, x1 = 4200;
for (int i = 0; i < NX; i++)
for (int k = 0; k < nz; k++)
{
float xa = x0 + (x1 - x0) * i / NX, xb = x0 + (x1 - x0) * (i + 1) / NX;
float za = z0 + (z1 - z0) * k / nz, zb = z0 + (z1 - z0) * (k + 1) / nz;
Vector2 T(float x, float z) => new(x / 420f, z / 420f);
m.Quad(new(xa, y, zb), new(xb, y, zb), new(xb, y, za), new(xa, y, za), -Vector3.UnitY, Color.White,
T(xa, zb), T(xb, zb), T(xb, za), T(xa, za));
}
m.Freeze();
return m;
}
_jGround = Grid(land.Ground, land.GroundTint, -3000, 900, floor, 10);
_jPath = Grid(land.Path, land.PathTint, -230, 150, floor - 0.6f, 2);
// Meilenstein
_jStone = new Mesh3D { Tex = Assets.Pixel };
_jStone.Box(new Vector3(0, floor - 45, 0), new Vector3(34, 90, 30), new Color(170, 164, 150));
_jStone.Box(new Vector3(0, floor - 95, 0), new Vector3(40, 12, 36), new Color(150, 144, 132));
_jStone.Freeze();
_jProps.Clear();
}
/// Licht nach Tageszeit (0 Morgen … 0,25 Mittag … 0,5 Abend … 0,75 Nacht … 1 Morgen).
static (Vector3 SunDir, Vector3 Sun, Vector3 Amb, Vector3 Fog, Vector3 Sky, float Night) DayLight(float p)
{
(float P, Vector3 Dir, Vector3 Sun, Vector3 Amb, Vector3 Fog, Vector3 Sky)[] keys =
{
(0.00f, new(0.8f, 0.35f, -0.5f), new(1.0f, 0.70f, 0.50f), new(0.36f, 0.30f, 0.32f), new(0.88f, 0.70f, 0.60f), new(1.0f, 0.82f, 0.74f)),
(0.25f, new(0.25f, 0.92f, -0.3f), new(1.0f, 0.96f, 0.88f), new(0.48f, 0.46f, 0.44f), new(0.82f, 0.82f, 0.80f), new(1.0f, 1.0f, 1.0f)),
(0.50f, new(-0.8f, 0.35f, -0.5f), new(1.0f, 0.56f, 0.34f), new(0.36f, 0.28f, 0.28f), new(0.82f, 0.52f, 0.40f), new(1.0f, 0.72f, 0.58f)),
(0.62f, new(-0.3f, 0.8f, -0.4f), new(0.30f, 0.36f, 0.60f), new(0.15f, 0.17f, 0.28f), new(0.12f, 0.14f, 0.24f), new(0.26f, 0.30f, 0.48f)),
(0.90f, new(-0.3f, 0.8f, -0.4f), new(0.30f, 0.36f, 0.60f), new(0.15f, 0.17f, 0.28f), new(0.12f, 0.14f, 0.24f), new(0.26f, 0.30f, 0.48f)),
(1.00f, new(0.8f, 0.35f, -0.5f), new(1.0f, 0.70f, 0.50f), new(0.36f, 0.30f, 0.32f), new(0.88f, 0.70f, 0.60f), new(1.0f, 0.82f, 0.74f)),
};
int i = 0;
while (i < keys.Length - 2 && p > keys[i + 1].P) i++;
var a = keys[i]; var b = keys[i + 1];
float k = MathHelper.Clamp((p - a.P) / (b.P - a.P), 0, 1);
k = k * k * (3 - 2 * k);
float night = p < 0.55f ? 0 : p < 0.64f ? (p - 0.55f) / 0.09f : p < 0.9f ? 1 : MathF.Max(0, 1 - (p - 0.9f) / 0.08f);
return (Vector3.Normalize(Vector3.Lerp(a.Dir, b.Dir, k)), Vector3.Lerp(a.Sun, b.Sun, k), Vector3.Lerp(a.Amb, b.Amb, k),
Vector3.Lerp(a.Fog, b.Fog, k), Vector3.Lerp(a.Sky, b.Sky, k), night);
}
void DrawJourney(GraphicsDevice gd, SpriteBatch sb, BasicEffect fx, float scale, Vector2 offset)
{
if (_game.Away is not { } away || _jRegion == null || _jHero == null) return;
var land = Lands[away.Region];
var reg = RegionDef.Get(away.Region);
double total = Adventures.Durations[away.Duration], elapsed = total - away.Left;
float phase = DevDayPhase ?? (float)((elapsed / 90 + 0.12) % 1);
var (sunDir, sun, amb, fogDay, sky, night) = DayLight(phase);
var fog = fogDay * Vector3.Lerp(Vector3.One, land.Haze, 0.5f + 0.5f * (1 - night)) ;
bool calm = AppSettings.ReduceMotion;
int vw = gd.Viewport.Width, vh = gd.Viewport.Height;
var v3 = _jCam.View3(); var p3 = _jCam.Proj3(scale, offset, vw, vh);
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
_jFx ??= new BasicEffect(gd) { TextureEnabled = true, VertexColorEnabled = true, PreferPerPixelLighting = true };
_jPlane ??= new BasicEffect(gd) { TextureEnabled = true, VertexColorEnabled = true, LightingEnabled = false };
_jPropFx ??= new AlphaTestEffect(gd) { VertexColorEnabled = true, ReferenceAlpha = 110, AlphaFunction = CompareFunction.Greater };
_jPlane.World = Matrix.Identity; _jPlane.View = v3; _jPlane.Projection = p3;
gd.Clear(ClearOptions.Target | ClearOptions.DepthBuffer | ClearOptions.Stencil, new Color(fog), 1, 0);
float camX = _jCam.Center.X;
// fernes Regionsbild (gespiegelt wiederholt, langsamer als der Weg: Parallaxe)
if (Picture(reg.Art) is Texture2D pic)
{
const float Z = -5200, W = 7000;
float h = W * pic.Height / pic.Width, bottom = Stage.FloorY + 900, top = bottom - h * 1.15f;
float xl = camX - 9000, xr = camX + 9000, shift = camX * 0.8f;
var c = new Color(sky * (0.92f - 0.55f * night));
float u0 = (xl - shift) / W, u1 = (xr - shift) / W;
_jBack[0] = new(new Vector3(xl, top, Z), c, new Vector2(u0, 0)); _jBack[1] = new(new Vector3(xr, top, Z), c, new Vector2(u1, 0));
_jBack[2] = new(new Vector3(xr, bottom, Z), c, new Vector2(u1, 1)); _jBack[3] = _jBack[0]; _jBack[4] = _jBack[2];
_jBack[5] = new(new Vector3(xl, bottom, Z), c, new Vector2(u0, 1));
_jFx.World = Matrix.Identity; _jFx.View = v3; _jFx.Projection = p3;
_jFx.LightingEnabled = false; _jFx.FogEnabled = true; _jFx.FogColor = fog; _jFx.FogStart = 4000; _jFx.FogEnd = 16000;
_jFx.DiffuseColor = Vector3.One; _jFx.Texture = pic;
gd.DepthStencilState = DepthStencilState.None; gd.BlendState = BlendState.Opaque; gd.RasterizerState = RasterizerState.CullNone;
foreach (var pass in _jFx.CurrentTechnique.Passes)
{
pass.Apply();
gd.SamplerStates[0] = MirrorSampler;
gd.DrawUserPrimitives(PrimitiveType.TriangleList, _jBack, 0, 2);
}
}
// Boden und Weg (in ganzen Kachelbreiten mitgeführt)
float snap = MathF.Floor(camX / 420f) * 420f;
_jFx.LightingEnabled = true; _jFx.FogStart = 1600; _jFx.FogEnd = 6200;
_jFx.AmbientLightColor = amb; _jFx.DirectionalLight0.Enabled = true; _jFx.DirectionalLight0.Direction = sunDir;
_jFx.DirectionalLight0.DiffuseColor = sun; _jFx.DirectionalLight0.SpecularColor = Vector3.Zero;
_jFx.DirectionalLight1.Enabled = false; _jFx.DirectionalLight2.Enabled = false;
gd.DepthStencilState = DepthStencilState.Default;
foreach (var m in new[] { _jGround!, _jPath! })
{
_jFx.World = Matrix.CreateTranslation(snap, 0, 0);
_jFx.Texture = Assets.Mipmapped(m.Tex!); _jFx.DiffuseColor = m.Tint;
foreach (var pass in _jFx.CurrentTechnique.Passes)
{
pass.Apply();
gd.SamplerStates[0] = SamplerState.AnisotropicWrap;
gd.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, m.VA, 0, m.VA!.Length, m.IA, 0, m.IA!.Length / 3);
}
}
// Meilensteine alle 4 Felder
_jFx.Texture = Assets.Pixel; _jFx.DiffuseColor = Vector3.One;
int c0 = (int)MathF.Floor((camX - 4200) / CELL), c1 = (int)MathF.Ceiling((camX + 4200) / CELL);
for (int cell = c0; cell <= c1; cell++)
if (cell % 4 == 0)
{
_jFx.World = Matrix.CreateTranslation(cell * CELL, 0, -170);
foreach (var pass in _jFx.CurrentTechnique.Passes)
{
pass.Apply();
gd.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _jStone!.VA, 0, _jStone.VA!.Length, _jStone.IA, 0, _jStone.IA!.Length / 3);
}
}
// Bäume, Büsche, Felsen: je Feld fest ausgewürfelt
foreach (var key in land.Props) if (!_jProps.ContainsKey(key)) _jProps[key] = (new VertexPositionColorTexture[6 * 400], 0);
var counts = new Dictionary<string, int>(_jProps.Count);
foreach (var k in _jProps.Keys) counts[k] = 0;
var light = new Color(Vector3.Clamp(amb + sun * 0.55f, Vector3.Zero, Vector3.One));
for (int cell = c0; cell <= c1; cell++)
{
var rng = new Random(cell * 7919 + away.Region.Length * 104729);
int n = 2 + rng.Next(3);
for (int i = 0; i < n; i++)
{
string kind = land.Props[rng.Next(land.Props.Length)];
if (!_jProps.TryGetValue(kind, out var buf) || Assets.Sprite(kind) is not Texture2D tex) continue;
bool small = kind is "prop_bush" or "prop_rock";
float x = cell * CELL + (float)rng.NextDouble() * CELL;
float zr = (float)rng.NextDouble();
// nah am Weg nur Kleines, weiter hinten auch Bäume; selten etwas ganz vorn
float z = zr < 0.45f ? -240 - 520 * (float)rng.NextDouble() : -800 - 2000 * (float)rng.NextDouble();
if (!small && z > -380) continue; // Bäume nicht direkt am Weg
float size = (small ? 170 : 720) * (0.75f + 0.55f * (float)rng.NextDouble());
int k = counts[kind];
if (k + 6 > buf.V.Length) continue;
float half = size / 2, bottom = Stage.FloorY + size * 0.04f, top = bottom - size;
bool flip = rng.Next(2) == 0;
float ua = flip ? 1 : 0, ub = 1 - ua;
var v = buf.V;
v[k] = new(new Vector3(x - half, top, z), light, new Vector2(ua, 0)); v[k + 1] = new(new Vector3(x + half, top, z), light, new Vector2(ub, 0));
v[k + 2] = new(new Vector3(x + half, bottom, z), light, new Vector2(ub, 1)); v[k + 3] = v[k]; v[k + 4] = v[k + 2];
v[k + 5] = new(new Vector3(x - half, bottom, z), light, new Vector2(ua, 1));
counts[kind] = k + 6;
}
}
_jPropFx.World = Matrix.Identity; _jPropFx.View = v3; _jPropFx.Projection = p3;
_jPropFx.FogEnabled = true; _jPropFx.FogColor = fog; _jPropFx.FogStart = 1600; _jPropFx.FogEnd = 6200;
foreach (var (kind, cnt) in counts)
{
if (cnt == 0 || Assets.Sprite(kind) is not Texture2D tex) continue;
_jPropFx.Texture = Assets.Mipmapped(tex);
foreach (var pass in _jPropFx.CurrentTechnique.Passes)
{
pass.Apply();
gd.SamplerStates[0] = SamplerState.AnisotropicClamp;
gd.DrawUserPrimitives(PrimitiveType.TriangleList, _jProps[kind].V, 0, cnt / 3);
}
}
// Gladiator mit Schatten; nachts mit Fackel
var look = new ArenaLook("wood", sunDir, sun, amb, fog, 1600, 6200, Vector3.One, Color.White, 0, false, 0, 1);
_jHero.UseCamera3D(v3, p3);
_jHero.SetLighting(look);
Stage3D.SoftShadows(gd, sb, sunDir, 0.42f * (1 - 0.6f * night), _jHero);
var torch = _jHero.Chest + new Vector2(70, -40);
if (night > 0.01f)
{
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone, _jPlane);
float fl = 0.85f + 0.15f * MathF.Sin((float)_t * 19) * MathF.Sin((float)_t * 7.7f);
D.Glow(sb, new Vector2(_jHero.X, Stage.FloorY), new Vector2(1300 * fl, 300), new Color(255, 140, 50) * (0.35f * night));
D.Glow(sb, torch, new Vector2(700 * fl), new Color(255, 150, 60) * (0.35f * night));
sb.End();
if (!calm && Random.Shared.NextDouble() < 0.4 * night) _jfx.Sparks(torch, 1, new Color(255, 180, 80), -MathF.PI / 2, 0.7f, 140);
}
Stage3D.BeginFigures(gd);
_jHero.DrawBody(gd, fx, _jDummy, calm);
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone, _jPlane);
if (night > 0.01f)
{
float fl = 0.8f + 0.2f * MathF.Sin((float)_t * 23);
D.Glow(sb, torch, new Vector2(90 * fl, 130 * fl), new Color(255, 170, 70) * night);
D.Glow(sb, torch - new Vector2(0, 14), new Vector2(46, 70 * fl), new Color(255, 236, 170) * night);
}
_jfx.DrawAdd(sb);
sb.End();
// Anzeige: oben Region und Rest, Mitte unten Tagebuch, unten Knöpfe
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
for (int i = 0; i < 16; i++) D.Rect(sb, new RectangleF(0, i * 14, Camera.W, 14), Color.Black * (0.6f * (1 - i / 16f)));
D.Glow(sb, new Vector2(Camera.W / 2, Camera.H + 120), new Vector2(3000, 520), Color.Black * 0.6f);
int day = 1 + (int)(elapsed / 90);
D.Text(sb, Assets.Serif, 40, L.t(reg.Key).ToUpperInvariant(), new Vector2(Camera.W / 2, 56), D.Gold, Align.Center);
D.Text(sb, Assets.SansBold, 20, $"{string.Format(L.t("exp.day"), day)} · {string.Format(L.t("exp.left"), NumberFormat.Duration(away.Left))}", // i18n-ok
new Vector2(Camera.W / 2, 100), D.Sand, Align.Center);
float k2 = (float)(elapsed / total);
D.Bar(sb, new RectangleF(Camera.W / 2 - 300, 122, 600, 10), k2, k2, D.Gold);
// Tagebuch: Zeilen nach Fortschritt, fest ausgewählt je Reise
int lines = 2 + 2 * away.Duration;
int idx = Math.Min(lines - 1, (int)(k2 * lines));
if (idx != _jLine) { _jLine = idx; _jLineT = 0; }
var order = new int[Adventures.LOG_LINES];
for (int i = 0; i < order.Length; i++) order[i] = i + 1;
new Random(away.Region.GetHashCode() ^ (away.Duration * 7919) ^ (int)(away.Chance * 1000)).Shuffle(order);
int lineDay = day;
float la = MathHelper.Clamp(_jLineT / 1.2f, 0, 1);
string diary = $"{string.Format(L.t("exp.day"), lineDay)}: {L.t("expedition.log." + order[idx % order.Length])}"; // i18n-ok
D.Text(sb, Assets.Serif, 28, diary, new Vector2(Camera.W / 2 + 2, 172), Color.Black * (0.7f * la), Align.Center);
D.Text(sb, Assets.Serif, 28, diary, new Vector2(Camera.W / 2, 170), new Color(250, 236, 205) * la, Align.Center);
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - 330, Camera.H - 120, 320, 60), L.t("exp.map"), size: 22)) _overlay = Overlay.Map;
if (_ui.Button(sb, new RectangleF(Camera.W / 2 + 10, Camera.H - 120, 320, 60), L.t("exp.recall"), size: 18, accent: D.Red))
{ _game.Recall(); SoundManager.PlayBoo(); _overlay = Overlay.Map; }
sb.End();
}
static readonly SamplerState MirrorSampler = new() { AddressU = TextureAddressMode.Mirror, AddressV = TextureAddressMode.Clamp, Filter = TextureFilter.Linear };
}
}