using System;
using Amphora;
namespace Konis.Testbed
{
///
/// The frames of an expanding ring, drawn in code so the testbed also shows a flipbook that has no PNG.
///
public static class ShockwaveRings
{
public const int FrameCount = 8;
public const int Side = 48;
private static double SmoothStep(double edge0, double edge1, double value)
{
double fraction = Math.Max(0.0, Math.Min(1.0, (value - edge0) / (edge1 - edge0)));
return fraction * fraction * (3.0 - 2.0 * fraction);
}
private static byte AlphaAt(int frame, double across, double down)
{
double progress = (frame + 1.0) / FrameCount;
double radius = 0.15 + 0.8 * progress;
double halfWidth = 0.14 * (1.0 - 0.6 * progress);
double distance = Math.Abs(Math.Sqrt(across * across + down * down) - radius);
double alpha = (1.0 - SmoothStep(0.0, halfWidth, distance)) * (1.0 - progress * 0.7);
return (byte)(alpha * 255.0 + 0.5);
}
///
/// Returns white rings in a row of frames, each wider and fainter than the one before.
///
public static PixelSheetDef Build()
{
int width = Side * FrameCount;
var rgba = new byte[width * Side * PixelSheetDef.BytesPerPixel];
double middle = (Side - 1) / 2.0;
for (int y = 0; y < Side; y++)
{
for (int x = 0; x < width; x++)
{
int frame = x / Side;
double across = (x % Side - middle) / middle;
double down = (y - middle) / middle;
int index = (y * width + x) * PixelSheetDef.BytesPerPixel;
rgba[index + 0] = 255;
rgba[index + 1] = 255;
rgba[index + 2] = 255;
rgba[index + 3] = AlphaAt(frame, across, down);
}
}
return new PixelSheetDef
{
Name = nameof(Sheet),
Width = width,
Height = Side,
SpriteWidth = Side,
SpriteHeight = Side,
Rgba = new ByteView(rgba),
};
}
///
/// The rings as a sheet of one row.
///
public static readonly PixelSheetDef Sheet = Build();
///
/// The rings as sprites, in the order they play.
///
public static readonly SheetSpriteDef[] Frames =
SheetSpriteDef.Run(Sheet, row: 0, firstColumn: 0, count: FrameCount);
}
}