Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replay duration #3135

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions flixel/input/keyboard/FlxKeyboard.hx
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ class FlxKeyboard extends FlxKeyManager<FlxKey, FlxKeyList>
* @param Record Array of data about key states.
*/
@:allow(flixel.system.replay.FlxReplay)
function playback(Record:Array<CodeValuePair>):Void
function playback(record:Array<CodeValuePair>):Void
{
var i:Int = 0;
var l:Int = Record.length;
var i = 0;
final len = record.length;

while (i < l)
while (i < len)
{
var o = Record[i++];
var o2 = getKey(o.code);
o2.current = o.value;
final keyRecord = record[i++];
final key = getKey(keyRecord.code);
key.current = keyRecord.value;
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions flixel/system/replay/FlxReplay.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FlxReplay

/**
* The number of frames in this recording.
* **Note:** This doesn't include empty records, unlike `getDuration()`
*/
public var frameCount:Int;

Expand Down Expand Up @@ -241,4 +242,21 @@ class FlxReplay
FlxArrayUtil.setLength(_frames, _capacity);
frameCount = 0;
}

/**
* The duration of this replay, in frames. **Note:** this is different from `frameCount`, which
* is the number of unique records, which doesn't count frames with no input
*
* @since 5.9.0
*/
public function getDuration()
{
if (_frames != null)
{
// Add 1 to the last frame index, because they are zero-based
return _frames[_frames.length - 1].frame + 1;
}

return 0;
}
}
9 changes: 9 additions & 0 deletions tests/unit/src/flixel/system/replay/FlxReplayTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ class FlxReplayTest extends FlxTest
{
return new FrameRecord().create(i, null, new MouseRecord(0, 0, mouseState, 0));
}

@Test // #3135
function testGetDuration()
{
var replay = new FlxReplay();
replay.load("987654321\n299km0,0,2,0\n");
// add 1 because frame indices are zero-based
Assert.areEqual(300, replay.getDuration());
}
}

class ReplayState extends FlxState
Expand Down
Loading