88 lines
3.5 KiB
C#
88 lines
3.5 KiB
C#
using System.Numerics;
|
|
using GamePort.Assets;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>First usable default shrink/jetpack device; special gadgets stay separate.</summary>
|
|
public sealed class SceneGadgets : IDisposable
|
|
{
|
|
private readonly LabSimulation player;
|
|
private readonly SceneInventory inventory;
|
|
private readonly OriginalScenePhysics physics;
|
|
private readonly OriginalCameraRay geometry;
|
|
private bool loop, busy;
|
|
public ShrinkRay Ray { get; } = new();
|
|
public IReadOnlyList<ShrinkSoldier> Soldiers { get; }
|
|
public int ShrinkHits { get; private set; }
|
|
public event Action<int, bool>? Sound;
|
|
public event Action? StopSound;
|
|
public event Action<int>? ScoreAwarded;
|
|
|
|
public SceneGadgets(SceneDirector director, SceneInventory inventory, LabSimulation player, Mesh world)
|
|
{
|
|
this.inventory = inventory; this.player = player;
|
|
//Static source registration game-filter is only recovered for the route.
|
|
geometry = OriginalCameraRay.FromRouteScene(world, player.LevelCode);
|
|
physics = new OriginalScenePhysics(world);
|
|
Soldiers = director.Actors.Where(a => a.Kind == "3SOL").Select(a => new ShrinkSoldier(a)).ToArray();
|
|
foreach (var soldier in Soldiers) soldier.Actor.Animation = "WALK";
|
|
player.InputActions += Input;
|
|
}
|
|
|
|
public void StepBodies(float seconds)
|
|
{
|
|
Ray.StepTexture(seconds);
|
|
foreach (var soldier in Soldiers) soldier.StepBody(physics, seconds);
|
|
}
|
|
|
|
public void StepTimers(float seconds)
|
|
{
|
|
foreach (var soldier in Soldiers) soldier.StepTimer(seconds);
|
|
}
|
|
|
|
private void Input(PlayerInput input)
|
|
{
|
|
if (!input.Fire) { busy = false; Stop(); return; }
|
|
if (busy || player.GadgetMode != -1) { Stop(); return; }
|
|
player.StopForPrimaryFire();
|
|
if (inventory.State(0,3) != 1)
|
|
{ Sound?.Invoke(117,false); busy = true; Stop(); return; }
|
|
//426937..426989: ordinary airborne FALL stops/hides the owned beam.
|
|
if (!player.Grounded && player.Pose == PlayerPose.Fall) { Stop(); return; }
|
|
player.SetShooting(player.Grounded);
|
|
if (!loop) { Sound?.Invoke(0,true); loop = true; }
|
|
const float dt = FixedStepClock.Step;
|
|
player.AdjustEnergy(-dt);
|
|
//Bridge modern signed float yaw to the source's nonnegative Euler domain.
|
|
var yaw = (player.Yaw % 360 + 360) % 360;
|
|
var hits = Ray.Fire(player.ActorRoot,yaw,dt,
|
|
Soldiers.Select(s => new ShrinkCandidate(s.Actor.Tag,s.Actor.Position,BodyEnabled:s.Actor.BodyEnabled,Shrunk:s.Shrunk)),
|
|
(start,end) => geometry.TraceGeometry(start,end));
|
|
foreach (var tag in hits)
|
|
{
|
|
var soldier = Soldiers.First(s => s.Actor.Tag == tag);
|
|
soldier.Shrink(); ShrinkHits++;
|
|
//Ordinary3SOL callback42AD81 awards10, then publishes marker94C.
|
|
//Other enemy classes/special LIBBYPLANT/C3DDINO callbacks are separate.
|
|
if (!tag.Equals("LIBBYPLANT",StringComparison.OrdinalIgnoreCase)
|
|
&& !tag.Equals("C3DDINO",StringComparison.OrdinalIgnoreCase))
|
|
{ ScoreAwarded?.Invoke(10); soldier.MarkScoredHit(); }
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Ray.Hide(); player.SetShooting(false);
|
|
if (loop) StopSound?.Invoke();
|
|
loop = false;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Stop(); busy = false; Ray.Reset(); ShrinkHits = 0;
|
|
foreach (var soldier in Soldiers) soldier.Reset();
|
|
}
|
|
|
|
public void Dispose() { player.InputActions -= Input; Stop(); }
|
|
}
|