Skip to content

Commit

Permalink
Add glitch effects while in Thargoid-controlled space
Browse files Browse the repository at this point in the history
  • Loading branch information
poveden committed Dec 6, 2022
1 parent 5419453 commit 6180b7f
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fix discovery of Epic- and Oculus-based installation folders
- Fix crash when game bindings are reloaded while modifier keys are being scanned
- Fix crash when entering Thargoid-controlled space

### Added

- Add discovery for Horizons 3.8 to 4.0 side update installation folder
- Add glitch effects while in Thargoid-controlled space

## [1.17.0](https://github.com/poveden/EliteChroma/compare/v1.16.3...v1.17.0) — 2022-09-03

Expand Down
140 changes: 140 additions & 0 deletions src/EliteChroma.Core/Layers/GlitchLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Diagnostics.CodeAnalysis;
using ChromaWrapper;
using ChromaWrapper.Sdk;
using EliteChroma.Core.Chroma;
using EliteFiles.Status;

namespace EliteChroma.Core.Layers
{
[SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "Simple randomness for glitch effect.")]
internal sealed class GlitchLayer : LayerBase
{
private readonly Random _rnd = new Random();

private enum DeviceGlitch
{
None = 0,
Simple,
Full,
}

public override int Order => 700;

protected override void OnRender(ChromaCanvas canvas)
{
if (Game.Status.LegalState == LegalState.Thargoid)
{
_ = StartAnimation();
}
else
{
_ = StopAnimation();
return;
}

RenderShiftGlitch(canvas.Keyboard.Key);
RenderColorGlitch(canvas.Mouse.Color);
RenderColorGlitch(canvas.Mousepad.Color);
RenderShiftGlitch(canvas.Keypad.Color);
RenderColorGlitch(canvas.Headset.Color);
RenderColorGlitch(canvas.ChromaLink.Color);
}

private void RenderShiftGlitch(IKeyGrid grid)
{
DeviceGlitch glitch = GetDeviceGlitch();
if (glitch == DeviceGlitch.None)
{
return;
}

for (int y = 0; y < grid.Rows; y++)
{
for (int x = 0; x < grid.Columns; x++)
{
if (glitch == DeviceGlitch.Full || IsLedGlitchy())
{
int x2 = Math.Clamp(x + GetGlitchKeyShift(), 0, grid.Columns - 1);
(grid[y, x2], grid[y, x]) = (grid[y, x], grid[y, x2]);
}
}
}
}

private void RenderShiftGlitch(ILedGrid grid)
{
DeviceGlitch glitch = GetDeviceGlitch();
if (glitch == DeviceGlitch.None)
{
return;
}

for (int y = 0; y < grid.Rows; y++)
{
for (int x = 0; x < grid.Columns; x++)
{
if (glitch == DeviceGlitch.Full || IsLedGlitchy())
{
int x2 = Math.Clamp(x + GetGlitchKeyShift(), 0, grid.Columns - 1);
(grid[y, x2], grid[y, x]) = (grid[y, x], grid[y, x2]);
}
}
}
}

private void RenderColorGlitch(ILedArray leds)
{
DeviceGlitch glitch = GetDeviceGlitch();
if (glitch == DeviceGlitch.None)
{
return;
}

for (int i = 0; i < leds.Count; i++)
{
if (glitch == DeviceGlitch.Full || IsLedGlitchy())
{
leds[i] = GetColorGlitch(leds[i]);
}
}
}

private DeviceGlitch GetDeviceGlitch()
{
const double DeviceProbability = 0.05;
const double AllLedsProbability = DeviceProbability * 0.1;

return _rnd.NextDouble() switch
{
<= AllLedsProbability => DeviceGlitch.Full,
<= DeviceProbability => DeviceGlitch.Simple,
_ => DeviceGlitch.None,
};
}

private bool IsLedGlitchy()
{
const double LedProbability = 0.1;

return _rnd.NextDouble() <= LedProbability;
}

private int GetGlitchKeyShift()
{
const int MinShift = -2;
const int MaxShift = 3;

return _rnd.Next(MinShift, MaxShift);
}

private ChromaColor GetColorGlitch(ChromaColor color)
{
int flags = _rnd.Next(1, 7);
byte r = (flags & 1) != 0 ? color.R : (byte)0;
byte g = (flags & 2) != 0 ? color.G : (byte)0;
byte b = (flags & 4) != 0 ? color.B : (byte)0;

return ChromaColor.FromRgb(r, g, b);
}
}
}
3 changes: 3 additions & 0 deletions src/EliteFiles/Status/LegalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ public enum LegalState

/// <summary>Warrant.</summary>
Warrant,

/// <summary>Thargoid.</summary>
Thargoid,
}
}

0 comments on commit 6180b7f

Please sign in to comment.