Files
lingua-engine/samples/PhysicsDemo/GamePlugins/physics-demo-game/PhysicsDemoGamePlugin.cs
T
EmilandClaude Sonnet 5 2d0168104d M4: PhysicsDemo — the "one small game, end to end"
samples/PhysicsDemo ties physics, audio, input, and rendering together
through nothing but the kernel's own vocabulary — no plugin here
references another plugin's implementation, only Contracts. Press Space
to drop a box; engine.physics simulates it falling onto a static ground;
CubeRenderer (new — engine.render's QuadRenderer drew flat cards, no good
for a physics demo where a BoxCollider needs to actually look like a box)
draws it; physics-demo-game watches each spawned box's own Y velocity via
IPhysicsService and plays a bounce sound via IAudioService the moment a
real fall settles. A looping ambient track plays throughout via
AudioSource's own PlayOnAwake. project.json deliberately excludes
engine.editor — this is the shippable configuration M4's "done when"
actually asks for, not the dev one.

"Landed" isn't a Box3D contact event — the native shim never exposed one
(nothing but scalars crosses that boundary, see lingua_physics.c). Watching
velocity every frame is the honest, right-sized alternative for a demo
this size, not a shortcut around missing infrastructure.

Verified two ways. First, real Space-key input isn't simulable here (no
xdotool/ydotool under this Wayland session) — so PhysicsDemoGame.Tests
drives the actual PhysicsDemoGamePlugin.Configure/Tick through the real
Schedule with a controllable fake IEngineInput (and fake IPhysicsService/
IAudioService, since engine.physics/engine.audio's own correctness is
already covered elsewhere): spawn-on-press with edge detection, the
Rigidbody/BoxCollider/CubeRenderer combo the spawned box actually gets,
and — the case most likely to be subtly wrong — a box that never actually
falls doesn't false-positive as "landed," only one that fell past
FallingThreshold and then settled does, exactly once. All 5 passed
immediately. Second, a real windowed run: a box placed in scene.json above
the ground visibly falls and settles onto it on screen after a real
few-second wait — screenshotting by --screenshot-after-frames alone turned
out not to prove this (VSync is off, so frames race by far faster than
real physics time passes; enough elapsed frames isn't enough elapsed
seconds), an interactive run with a real sleep before the screenshot
command is what actually shows it.

Full suite: 92 tests. Remaining for M4: the Linux + Windows build pipeline.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
2026-09-02 17:47:15 +03:00

120 lines
4.0 KiB
C#

using System.Numerics;
using Engine.Audio.Contracts;
using Engine.Input.Contracts;
using Engine.Kernel.Plugins;
using Engine.Kernel.Scheduling;
using Engine.Kernel.World;
using Engine.Physics.Contracts;
using Engine.Render.Contracts;
using Silk.NET.Input;
namespace PhysicsDemoGame;
/// <summary>
/// M4's "one small game, end to end": pressing Space drops a new box from
/// above the ground; engine.physics simulates it falling and settling;
/// engine.render draws it as a real cube (CubeRenderer, added specifically
/// for this); this plugin plays a bounce sound (engine.audio) the moment
/// each box's fall actually stops. Physics, audio, input, and rendering
/// all cooperate here through nothing but the kernel's own vocabulary —
/// this plugin never references engine.physics/audio/render's
/// implementation assemblies, only their Contracts.
///
/// "Landed" isn't a Box3D contact event — the native shim doesn't expose
/// one (see native/physics-native/lingua_physics.c's own doc comment on
/// why nothing but scalars crosses that boundary; a contact callback would
/// need to carry structured per-contact data across it, exactly what's
/// kept out). Honest scope for a demo this size: watch each spawned box's
/// own Y velocity via IPhysicsService every frame, and call it landed the
/// first time a real fall (velocity past FallingThreshold at some point)
/// settles back near zero.
/// </summary>
public sealed class PhysicsDemoGamePlugin : IPlugin
{
private const float SpawnHeight = 6f;
private const float FallingThreshold = -1f;
private const float RestThreshold = 0.3f;
private readonly Random _random = new();
private readonly List<GameObject> _spawned = [];
private readonly HashSet<GameObject> _hasFallen = [];
private readonly HashSet<GameObject> _landed = [];
private IEngineInput? _input;
private IPhysicsService? _physics;
private IAudioService? _audio;
private GameObject? _bounceSound;
private bool _spacePressedLastFrame;
public void Configure(IPluginContext ctx)
{
_input = ctx.Services.Require<IEngineInput>();
_physics = ctx.Services.Require<IPhysicsService>();
_audio = ctx.Services.Require<IAudioService>();
ctx.Schedule.Add(Stage.Update, Tick)
.Writes<Rigidbody>()
.Writes<BoxCollider>()
.Writes<CubeRenderer>();
ctx.Log.Info("physics demo ready — press Space to drop a box");
}
public void Shutdown(IPluginContext ctx)
{
ctx.Schedule.RemoveAllFrom("physics-demo-game");
_spawned.Clear();
_hasFallen.Clear();
_landed.Clear();
_bounceSound = null;
_input = null;
_physics = null;
_audio = null;
}
private void Tick(IWorld world)
{
_bounceSound ??= world.Roots.FirstOrDefault(go => go.Name == "Bounce");
var spacePressed = _input!.IsKeyDown(Key.Space);
if (spacePressed && !_spacePressedLastFrame)
Spawn(world);
_spacePressedLastFrame = spacePressed;
foreach (var box in _spawned)
{
if (_landed.Contains(box))
continue;
var velocityY = _physics!.GetLinearVelocity(box).Y;
if (velocityY < FallingThreshold)
{
_hasFallen.Add(box);
continue;
}
if (_hasFallen.Contains(box) && MathF.Abs(velocityY) < RestThreshold)
{
_landed.Add(box);
if (_bounceSound is not null)
_audio!.Play(_bounceSound);
}
}
}
private void Spawn(IWorld world)
{
var go = world.CreateGameObject($"Box{_spawned.Count}");
go.Transform = Transform.Identity;
go.Transform.LocalPosition = new Vector3(
(float)(_random.NextDouble() * 4 - 2), SpawnHeight, (float)(_random.NextDouble() * 4 - 2));
go.AddComponent<Rigidbody>().Type = BodyType.Dynamic;
go.AddComponent<BoxCollider>();
go.AddComponent<CubeRenderer>();
_spawned.Add(go);
}
}