@@ -4,6 +4,7 @@ import { EntityWorld, spikeTriangles } from "./entities/EntityWorld";
44import { RefillEntity , RefillType } from "./entities/types" ;
55import { TILE_JUMP_THROUGH , tileAt } from "./grid" ;
66import { parseLevel } from "./level" ;
7+ import { PlayerControls } from "./input/PlayerControls" ;
78import { addFloat , approach , maxFloat , stepTimer , subFloat , toFloat } from "./player/math" ;
89import { Player } from "./player/Player" ;
910import { InputState , PlayerEffect } from "./player/types" ;
@@ -48,8 +49,7 @@ export class GameScene extends Phaser.Scene {
4849 private tileDepths ! : Int32Array ;
4950
5051 private keys ! : Record < string , Phaser . Input . Keyboard . Key > ;
51- private pendingJumpEdges : Array < "down" | "up" > = [ ] ;
52- private pendingDashPresses = 0 ;
52+ private controls ! : PlayerControls ;
5353
5454 private accumulator = 0 ;
5555 private readonly fixedDt = toFloat ( 1 / 60 ) ;
@@ -116,15 +116,12 @@ export class GameScene extends Phaser.Scene {
116116 right : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . RIGHT ) ,
117117 up : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . UP ) ,
118118 down : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . DOWN ) ,
119- a : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . A ) ,
120- d : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . D ) ,
121- w : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . W ) ,
122- s : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . S ) ,
123119 grab : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . Z ) ,
124120 dash : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . X ) ,
125121 jump : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . C ) ,
126122 restart : kb . addKey ( Phaser . Input . Keyboard . KeyCodes . R ) ,
127123 } ;
124+ this . controls = new PlayerControls ( ) ;
128125 this . keys . jump . on ( "down" , this . onJumpDown , this ) ;
129126 this . keys . jump . on ( "up" , this . onJumpUp , this ) ;
130127 this . keys . dash . on ( "down" , this . onDashDown , this ) ;
@@ -144,7 +141,7 @@ export class GameScene extends Phaser.Scene {
144141 . text (
145142 VIEWPORT . width - 8 ,
146143 VIEWPORT . height - 8 ,
147- "Move: arrows / WASD \nC jump X dash Z grab R reset" ,
144+ "Move: arrow keys \nC jump X dash Z grab R reset" ,
148145 {
149146 fontFamily : "monospace" ,
150147 fontSize : "9px" ,
@@ -166,7 +163,7 @@ export class GameScene extends Phaser.Scene {
166163 this . player . hardRespawn ( this . spawnX , this . spawnY ) ;
167164 this . world . resetTransientState ( ) ;
168165 this . syncRefillViews ( ) ;
169- this . clearInputEdgeQueues ( ) ;
166+ this . controls . clearTransientState ( ) ;
170167 this . forceCameraSnap ( ) ;
171168 this . cameras . main . fadeIn ( 80 , 10 , 10 , 20 ) ;
172169 this . accumulator = 0 ;
@@ -205,7 +202,7 @@ export class GameScene extends Phaser.Scene {
205202 this . player . hardRespawn ( this . spawnX , this . spawnY ) ;
206203 this . world . resetTransientState ( ) ;
207204 this . syncRefillViews ( ) ;
208- this . clearInputEdgeQueues ( ) ;
205+ this . controls . clearTransientState ( ) ;
209206 this . forceCameraSnap ( ) ;
210207 if ( spiked ) {
211208 this . cameras . main . flash ( 180 , 0 , 0 , 0 , false ) ;
@@ -297,7 +294,7 @@ export class GameScene extends Phaser.Scene {
297294 this . keys . jump . off ( "up" , this . onJumpUp , this ) ;
298295 this . keys . dash . off ( "down" , this . onDashDown , this ) ;
299296 }
300- this . clearInputEdgeQueues ( ) ;
297+ this . controls ?. reset ( ) ;
301298 this . playerView ?. destroy ( ) ;
302299 this . refillEmitter ?. destroy ( ) ;
303300 for ( const refill of this . refills ) {
@@ -308,51 +305,29 @@ export class GameScene extends Phaser.Scene {
308305 }
309306
310307 private gatherStepInput ( ) : InputState {
311- let x = 0 ;
312- let y = 0 ;
313- if ( this . keys . left . isDown || this . keys . a . isDown ) x -= 1 ;
314- if ( this . keys . right . isDown || this . keys . d . isDown ) x += 1 ;
315- if ( this . keys . up . isDown || this . keys . w . isDown ) y -= 1 ;
316- if ( this . keys . down . isDown || this . keys . s . isDown ) y += 1 ;
317-
318- const jump = this . keys . jump . isDown ;
319- const jumpEdge = this . pendingJumpEdges . shift ( ) ;
320- const jumpPressed = jumpEdge === "down" ;
321- const jumpReleased = jumpEdge === "up" ;
322- const dash = this . keys . dash . isDown ;
323- const dashPressed = this . pendingDashPresses > 0 ;
324- if ( dashPressed ) this . pendingDashPresses -- ;
325- const grab = this . keys . grab . isDown ;
326-
327- return {
328- x,
329- y,
330- jump,
331- jumpPressed,
332- jumpReleased,
333- dash,
334- dashPressed,
335- grab,
336- } ;
308+ this . controls . setCheck ( "leftArrow" , this . keys . left . isDown ) ;
309+ this . controls . setCheck ( "rightArrow" , this . keys . right . isDown ) ;
310+ this . controls . setCheck ( "upArrow" , this . keys . up . isDown ) ;
311+ this . controls . setCheck ( "downArrow" , this . keys . down . isDown ) ;
312+ this . controls . setCheck ( "jump" , this . keys . jump . isDown ) ;
313+ this . controls . setCheck ( "dash" , this . keys . dash . isDown ) ;
314+ this . controls . setCheck ( "grab" , this . keys . grab . isDown ) ;
315+
316+ return this . controls . update ( this . fixedDt ) ;
337317 }
338318
339319 private onJumpDown ( event : KeyboardEvent ) : void {
340320 if ( event . repeat ) return ;
341- this . pendingJumpEdges . push ( "down ") ;
321+ this . controls . queuePress ( "jump ") ;
342322 }
343323
344324 private onJumpUp ( ) : void {
345- this . pendingJumpEdges . push ( "up ") ;
325+ this . controls . queueRelease ( "jump ") ;
346326 }
347327
348328 private onDashDown ( event : KeyboardEvent ) : void {
349329 if ( event . repeat ) return ;
350- this . pendingDashPresses ++ ;
351- }
352-
353- private clearInputEdgeQueues ( ) : void {
354- this . pendingJumpEdges . length = 0 ;
355- this . pendingDashPresses = 0 ;
330+ this . controls . queuePress ( "dash" ) ;
356331 }
357332
358333 private updateCamera ( snapshot : ReturnType < Player [ "getSnapshot" ] > , dt : number ) : void {
0 commit comments