Skip to content

Commit

Permalink
Replay duration (#3135)
Browse files Browse the repository at this point in the history
* add FlxReplay getDuration

* style

* add 1 to frame duration

* add docs

* add unit test
  • Loading branch information
Geokureli authored May 9, 2024
1 parent 27c47e5 commit ac6cc5e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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

0 comments on commit ac6cc5e

Please sign in to comment.