@@ -4,7 +4,7 @@ import flixel.FlxG;
44import flixel .input .keyboard .FlxKey ;
55import flixel .FlxBasic ;
66
7- typedef CheatCallback = Void -> Void ;
7+ typedef CheatCallback = Cheat -> Void ;
88
99typedef Cheat = {
1010 var code : Array <FlxKey >;
@@ -23,11 +23,13 @@ class KonamiTracker extends FlxBasic {
2323 var inputBuffer : Array <FlxKey > = [];
2424 var maxLength : Int = 0 ;
2525
26- public function new (? cheatTable : KeyIndexedArray <Array <FlxKey >, CheatCallback >) {
26+ public function new (? cheatTable : KeyIndexedArray <Array <FlxKey >, CheatCallback >, ? KonamiCallback : CheatCallback ) {
2727 super ();
2828 cheats = [];
29- addCheat (KONAMI_CODE_1 , function () {});
30- addCheat (KONAMI_CODE_2 , function () {});
29+ if (KonamiCallback != null ) {
30+ addCheat (KONAMI_CODE_1 , KonamiCallback );
31+ addCheat (KONAMI_CODE_2 , KonamiCallback );
32+ }
3133 if (cheatTable != null ) {
3234 for (entry in cheatTable ) {
3335 addCheat (entry .key , entry .value );
@@ -42,7 +44,7 @@ class KonamiTracker extends FlxBasic {
4244
4345 override public function update (elapsed : Float ): Void {
4446 super .update (elapsed );
45- var pressed : FlxKey = getPressedKey ();
47+ var pressed : Null < FlxKey > = getPressedKey ();
4648 if (pressed != null ) {
4749 inputBuffer .push (pressed );
4850 if (inputBuffer .length > maxLength )
@@ -55,7 +57,7 @@ class KonamiTracker extends FlxBasic {
5557 var matched = false ;
5658 for (cheat in cheats ) {
5759 if (matches (cheat .code )) {
58- cheat .callback ();
60+ cheat .callback (cheat );
5961 inputBuffer = [];
6062 matched = true ;
6163 break ;
@@ -91,17 +93,21 @@ class KonamiTracker extends FlxBasic {
9193 return true ;
9294 }
9395
94- public var allowAllKeys : Bool = false ;
96+ public var allowAllKeys : Bool = true ;
9597
9698 function getPressedKey (): Null <FlxKey > {
97- if (allowAllKeys ) {
98- // Check all keys if allowed
99- for (key in FlxKey .createAll ()) {
100- if (FlxG .keys .justPressed .check (key )) return key ;
101- }
102- } else {
103- for (key in [FlxKey .UP , FlxKey .DOWN , FlxKey .LEFT , FlxKey .RIGHT , FlxKey .A , FlxKey .B , FlxKey .C , FlxKey .X , FlxKey .Y , FlxKey .Z , FlxKey .ENTER , FlxKey .SPACE ]) {
104- if (FlxG .keys .justPressed .check (key )) return key ;
99+ var keys = allowAllKeys
100+ ? [for (key in FlxKey .toStringMap .keys ()) key ]
101+ : [
102+ FlxKey .UP , FlxKey .DOWN , FlxKey .LEFT , FlxKey .RIGHT ,
103+ FlxKey .A , FlxKey .B , FlxKey .C , FlxKey .X , FlxKey .Y ,
104+ FlxKey .Z , FlxKey .ENTER , FlxKey .SPACE
105+ ];
106+ for (key in keys ) {
107+ // Use Reflect to access the justPressed property dynamically
108+ var justPressed = Reflect .field (FlxG .keys , " justPressed" );
109+ if (justPressed != null && Reflect .callMethod (FlxG .keys , justPressed , [key ])) {
110+ return key ;
105111 }
106112 }
107113 return null ;
0 commit comments