Skip to content

Commit

Permalink
Improved example program
Browse files Browse the repository at this point in the history
  • Loading branch information
Somfic committed Aug 7, 2023
1 parent d3ba7d7 commit fbda11d
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
using EliteAPI;
using EliteAPI.Abstractions.Bindings.Models;
using EliteAPI.Bindings;
using EliteAPI.Events;
using EliteAPI.Events.Status.FCMaterials;
using EliteAPI.Events.Status.Ship.Events;
using FcMaterialsEvent = EliteAPI.Events.Status.FCMaterials.FcMaterialsEvent;

// Create an instance of the API
// Create an instance of the Api
var api = EliteDangerousApi.Create();

// Subscribe to the lights event
api.Events.On<LightsStatusEvent>(lights =>
Console.WriteLine($"Lights are {(lights.Value ? "on" : "off")}"));

api.Bindings.OnBindings(_ =>
// Subscribe to the DockingRequested event
api.Events.On<DockingRequestedEvent>((e, context) =>
{
Console.WriteLine(api.Bindings[KeyBinding.ShipSpotLightToggle].Primary?.Key);
// Return if the event was raised while catching up with the journal
if (context.IsRaisedDuringCatchup)
return;
Console.WriteLine($"Requested docking at {e.StationName}");
});

api.Events.On<BalanceStatusEvent>(e => Console.WriteLine(e.Value));

api.Events.On<FcMaterialsEvent>(e =>
// Subscribe to all events
api.Events.OnAny(e =>
{
Console.WriteLine("FC Materials:");
var eventName = e.Event;
});

// Handle gear changes in the OnGearChange method
api.Events.On<GearStatusEvent>(OnGearChange);

// Start the API
if (!await api.StartAsync())
{
Console.WriteLine("Failed to start EliteAPI");
return;
}
await api.StartAsync();

Console.WriteLine("EliteAPI started");
// Get the binding for the ship's lights
var lightsBinding = api.Bindings[KeyBinding.ShipSpotLightToggle].Primary?.Key ?? "not set";
Console.WriteLine($"Lights binding: {lightsBinding}");

// Wait for the in-game Shutdown event (this method is blocking and will halt the main thread)
await Task.Delay(-1);

// Run until the game stops
api.Events.WaitFor<ShutdownEvent>();
Console.WriteLine("Game closed, shutting down");
await api.StopAsync();

// This method will be called when the gear changes
void OnGearChange(GearStatusEvent gear)
{
Console.WriteLine($"Gear is {(gear.Value ? "down" : "up")}");
}

0 comments on commit fbda11d

Please sign in to comment.