Funkin/source/funkin/play/notes/SustainTrail.hx

390 lines
11 KiB
Haxe
Raw Normal View History

2023-06-22 01:41:01 -04:00
package funkin.play.notes;
import funkin.play.notes.notestyle.NoteStyle;
2023-06-22 01:41:01 -04:00
import funkin.play.notes.NoteDirection;
import funkin.data.song.SongData.SongNoteData;
2023-06-22 01:41:01 -04:00
import flixel.util.FlxDirectionFlags;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.tile.FlxDrawTrianglesItem;
import flixel.math.FlxMath;
import funkin.ui.options.PreferencesMenu;
2023-06-22 01:41:01 -04:00
/**
* This is based heavily on the `FlxStrip` class. It uses `drawTriangles()` to clip a sustain note
* trail at a certain time.
* The whole `FlxGraphic` is used as a texture map. See the `NOTE_hold_assets.fla` file for specifics
* on how it should be constructed.
*
* @author MtH
*/
class SustainTrail extends FlxSprite
{
/**
* The triangles corresponding to the hold, followed by the endcap.
* `top left, top right, bottom left`
* `top left, bottom left, bottom right`
*/
static final TRIANGLE_VERTEX_INDICES:Array<Int> = [0, 1, 2, 1, 2, 3, 4, 5, 6, 5, 6, 7];
public var strumTime:Float = 0; // millis
public var noteDirection:NoteDirection = 0;
public var sustainLength(default, set):Float = 0; // millis
public var fullSustainLength:Float = 0;
2023-08-31 18:47:23 -04:00
public var noteData:Null<SongNoteData>;
2024-05-09 12:51:03 -04:00
public var parentStrumline:Strumline;
2023-06-22 01:41:01 -04:00
2023-07-05 22:11:58 -04:00
public var cover:NoteHoldCover = null;
2023-06-22 01:41:01 -04:00
/**
2023-06-22 19:28:39 -04:00
* Set to `true` if the user hit the note and is currently holding the sustain.
* Should display associated effects.
2023-06-22 01:41:01 -04:00
*/
2023-06-22 19:28:39 -04:00
public var hitNote:Bool = false;
/**
* Set to `true` if the user missed the note or released the sustain.
* Should make the trail transparent.
*/
public var missedNote:Bool = false;
2024-01-15 14:20:44 -05:00
/**
* Set to `true` after handling additional logic for missing notes.
*/
public var handledMiss:Bool = false;
2023-06-22 19:28:39 -04:00
// maybe BlendMode.MULTIPLY if missed somehow, drawTriangles does not support!
2023-06-22 01:41:01 -04:00
/**
* A `Vector` of floats where each pair of numbers is treated as a coordinate location (an x, y pair).
*/
public var vertices:DrawData<Float> = new DrawData<Float>();
/**
* A `Vector` of integers or indexes, where every three indexes define a triangle.
*/
public var indices:DrawData<Int> = new DrawData<Int>();
/**
* A `Vector` of normalized coordinates used to apply texture mapping.
*/
public var uvtData:DrawData<Float> = new DrawData<Float>();
private var processedGraphic:FlxGraphic;
private var zoom:Float = 1;
/**
* What part of the trail's end actually represents the end of the note.
* This can be used to have a little bit sticking out.
*/
public var endOffset:Float = 0.5; // 0.73 is roughly the bottom of the sprite in the normal graphic!
/**
* At what point the bottom for the trail's end should be clipped off.
* Used in cases where there's an extra bit of the graphic on the bottom to avoid antialiasing issues with overflow.
*/
public var bottomClip:Float = 0.9;
public var isPixel:Bool;
var graphicWidth:Float = 0;
var graphicHeight:Float = 0;
2023-06-22 01:41:01 -04:00
/**
* Normally you would take strumTime:Float, noteData:Int, sustainLength:Float, parentNote:Note (?)
* @param NoteData
* @param SustainLength Length in milliseconds.
* @param fileName
*/
public function new(noteDirection:NoteDirection, sustainLength:Float, noteStyle:NoteStyle)
2023-06-22 01:41:01 -04:00
{
2024-06-01 13:47:45 -04:00
super(0, 0);
// BASIC SETUP
this.sustainLength = sustainLength;
this.fullSustainLength = sustainLength;
this.noteDirection = noteDirection;
setupHoldNoteGraphic(noteStyle);
indices = new DrawData<Int>(12, true, TRIANGLE_VERTEX_INDICES);
this.active = true; // This NEEDS to be true for the note to be drawn!
}
/**
* Creates hold note graphic and applies correct zooming
* @param noteStyle The note style
*/
public function setupHoldNoteGraphic(noteStyle:NoteStyle):Void
{
loadGraphic(noteStyle.getHoldNoteAssetPath());
2023-06-22 01:41:01 -04:00
antialiasing = true;
this.isPixel = noteStyle.isHoldNotePixel();
if (isPixel)
2023-06-22 01:41:01 -04:00
{
endOffset = bottomClip = 1;
antialiasing = false;
}
2024-06-11 14:52:08 -04:00
else
{
endOffset = 0.5;
bottomClip = 0.9;
}
2023-06-22 01:41:01 -04:00
2024-06-01 13:47:45 -04:00
zoom = 1.0;
zoom *= noteStyle.fetchHoldNoteScale();
2023-06-22 01:41:01 -04:00
zoom *= 0.7;
// CALCULATE SIZE
graphicWidth = graphic.width / 8 * zoom; // amount of notes * 2
2024-05-09 12:51:03 -04:00
graphicHeight = sustainHeight(sustainLength, parentStrumline?.scrollSpeed ?? 1.0);
2023-06-22 01:41:01 -04:00
// instead of scrollSpeed, PlayState.SONG.speed
flipY = Preferences.downscroll;
2023-06-22 01:41:01 -04:00
// alpha = 0.6;
alpha = 1.0;
// calls updateColorTransform(), which initializes processedGraphic!
updateColorTransform();
updateClipping();
}
2024-05-09 12:51:03 -04:00
function getBaseScrollSpeed()
{
2024-05-09 12:51:03 -04:00
return (PlayState.instance?.currentChart?.scrollSpeed ?? 1.0);
}
var previousScrollSpeed:Float = 1;
override function update(elapsed)
{
super.update(elapsed);
if (previousScrollSpeed != (parentStrumline?.scrollSpeed ?? 1.0))
{
triggerRedraw();
}
previousScrollSpeed = parentStrumline?.scrollSpeed ?? 1.0;
2023-06-22 01:41:01 -04:00
}
/**
* Calculates height of a sustain note for a given length (milliseconds) and scroll speed.
* @param susLength The length of the sustain note in milliseconds.
* @param scroll The current scroll speed.
*/
public static inline function sustainHeight(susLength:Float, scroll:Float)
{
2024-06-18 09:11:00 -04:00
return (susLength * Constants.PIXELS_PER_MS * scroll);
2023-06-22 01:41:01 -04:00
}
function set_sustainLength(s:Float):Float
2023-06-22 01:41:01 -04:00
{
if (s < 0.0) s = 0.0;
if (sustainLength == s) return s;
this.sustainLength = s;
2024-05-09 12:51:03 -04:00
triggerRedraw();
return this.sustainLength;
}
function triggerRedraw()
{
2024-05-15 12:26:03 -04:00
graphicHeight = sustainHeight(sustainLength, parentStrumline?.scrollSpeed ?? 1.0);
2023-06-22 01:41:01 -04:00
updateClipping();
updateHitbox();
2023-06-22 01:41:01 -04:00
}
public override function updateHitbox():Void
{
width = graphicWidth;
height = graphicHeight;
offset.set(0, 0);
origin.set(width * 0.5, height * 0.5);
}
2023-06-22 01:41:01 -04:00
/**
* Sets up new vertex and UV data to clip the trail.
* If flipY is true, top and bottom bounds swap places.
* @param songTime The time to clip the note at, in milliseconds.
*/
public function updateClipping(songTime:Float = 0):Void
{
2024-06-01 13:47:45 -04:00
if (graphic == null)
{
return;
}
2024-05-09 12:51:03 -04:00
var clipHeight:Float = FlxMath.bound(sustainHeight(sustainLength - (songTime - strumTime), parentStrumline?.scrollSpeed ?? 1.0), 0, graphicHeight);
if (clipHeight <= 0.1)
2023-06-22 01:41:01 -04:00
{
visible = false;
return;
}
else
{
2023-06-22 01:41:01 -04:00
visible = true;
}
2023-06-22 01:41:01 -04:00
var bottomHeight:Float = graphic.height * zoom * endOffset;
var partHeight:Float = clipHeight - bottomHeight;
// ===HOLD VERTICES==
// Top left
vertices[0 * 2] = 0.0; // Inline with left side
vertices[0 * 2 + 1] = flipY ? clipHeight : graphicHeight - clipHeight;
2023-06-22 01:41:01 -04:00
// Top right
vertices[1 * 2] = graphicWidth;
2023-06-22 01:41:01 -04:00
vertices[1 * 2 + 1] = vertices[0 * 2 + 1]; // Inline with top left vertex
// Bottom left
vertices[2 * 2] = 0.0; // Inline with left side
vertices[2 * 2 + 1] = if (partHeight > 0)
{
// flipY makes the sustain render upside down.
flipY ? 0.0 + bottomHeight : vertices[1] + partHeight;
}
else
{
vertices[0 * 2 + 1]; // Inline with top left vertex (no partHeight available)
}
// Bottom right
vertices[3 * 2] = graphicWidth;
2023-06-22 01:41:01 -04:00
vertices[3 * 2 + 1] = vertices[2 * 2 + 1]; // Inline with bottom left vertex
// ===HOLD UVs===
// The UVs are a bit more complicated.
// UV coordinates are normalized, so they range from 0 to 1.
// We are expecting an image containing 8 horizontal segments, each representing a different colored hold note followed by its end cap.
uvtData[0 * 2] = 1 / 4 * (noteDirection % 4); // 0%/25%/50%/75% of the way through the image
uvtData[0 * 2 + 1] = (-partHeight) / graphic.height / zoom; // top bound
// Top left
// Top right
uvtData[1 * 2] = uvtData[0 * 2] + 1 / 8; // 12.5%/37.5%/62.5%/87.5% of the way through the image (1/8th past the top left)
uvtData[1 * 2 + 1] = uvtData[0 * 2 + 1]; // top bound
// Bottom left
uvtData[2 * 2] = uvtData[0 * 2]; // 0%/25%/50%/75% of the way through the image
uvtData[2 * 2 + 1] = 0.0; // bottom bound
// Bottom right
uvtData[3 * 2] = uvtData[1 * 2]; // 12.5%/37.5%/62.5%/87.5% of the way through the image (1/8th past the top left)
uvtData[3 * 2 + 1] = uvtData[2 * 2 + 1]; // bottom bound
// === END CAP VERTICES ===
// Top left
vertices[4 * 2] = vertices[2 * 2]; // Inline with bottom left vertex of hold
vertices[4 * 2 + 1] = vertices[2 * 2 + 1]; // Inline with bottom left vertex of hold
// Top right
vertices[5 * 2] = vertices[3 * 2]; // Inline with bottom right vertex of hold
vertices[5 * 2 + 1] = vertices[3 * 2 + 1]; // Inline with bottom right vertex of hold
// Bottom left
vertices[6 * 2] = vertices[2 * 2]; // Inline with left side
vertices[6 * 2 + 1] = flipY ? (graphic.height * (-bottomClip + endOffset) * zoom) : (graphicHeight + graphic.height * (bottomClip - endOffset) * zoom);
2023-06-22 01:41:01 -04:00
// Bottom right
vertices[7 * 2] = vertices[3 * 2]; // Inline with right side
vertices[7 * 2 + 1] = vertices[6 * 2 + 1]; // Inline with bottom of end cap
// === END CAP UVs ===
// Top left
uvtData[4 * 2] = uvtData[2 * 2] + 1 / 8; // 12.5%/37.5%/62.5%/87.5% of the way through the image (1/8th past the top left of hold)
uvtData[4 * 2 + 1] = if (partHeight > 0)
{
0;
}
else
{
(bottomHeight - clipHeight) / zoom / graphic.height;
};
// Top right
uvtData[5 * 2] = uvtData[4 * 2] + 1 / 8; // 25%/50%/75%/100% of the way through the image (1/8th past the top left of cap)
uvtData[5 * 2 + 1] = uvtData[4 * 2 + 1]; // top bound
// Bottom left
uvtData[6 * 2] = uvtData[4 * 2]; // 12.5%/37.5%/62.5%/87.5% of the way through the image (1/8th past the top left of hold)
uvtData[6 * 2 + 1] = bottomClip; // bottom bound
// Bottom right
uvtData[7 * 2] = uvtData[5 * 2]; // 25%/50%/75%/100% of the way through the image (1/8th past the top left of cap)
uvtData[7 * 2 + 1] = uvtData[6 * 2 + 1]; // bottom bound
}
@:access(flixel.FlxCamera)
override public function draw():Void
{
if (alpha == 0 || graphic == null || vertices == null) return;
for (camera in cameras)
{
if (!camera.visible || !camera.exists) continue;
// if (!isOnScreen(camera)) continue; // TODO: Update this code to make it work properly.
getScreenPosition(_point, camera).subtractPoint(offset);
camera.drawTriangles(processedGraphic, vertices, indices, uvtData, null, _point, blend, true, antialiasing);
}
#if FLX_DEBUG
if (FlxG.debugger.drawDebug) drawDebug();
#end
2023-06-22 01:41:01 -04:00
}
2023-06-22 19:28:39 -04:00
public override function kill():Void
{
super.kill();
strumTime = 0;
noteDirection = 0;
sustainLength = 0;
fullSustainLength = 0;
noteData = null;
hitNote = false;
missedNote = false;
}
public override function revive():Void
{
super.revive();
strumTime = 0;
noteDirection = 0;
sustainLength = 0;
fullSustainLength = 0;
noteData = null;
hitNote = false;
missedNote = false;
handledMiss = false;
}
2023-06-22 01:41:01 -04:00
override public function destroy():Void
{
vertices = null;
indices = null;
uvtData = null;
processedGraphic.destroy();
super.destroy();
}
override function updateColorTransform():Void
{
super.updateColorTransform();
if (processedGraphic != null) processedGraphic.destroy();
processedGraphic = FlxGraphic.fromGraphic(graphic, true);
processedGraphic.bitmap.colorTransform(processedGraphic.bitmap.rect, colorTransform);
}
}