@@ -20,59 +20,70 @@ export const TANK_MOVE_HANDLER = (tank: TankModel, world: World, isStuck: boolea
20
20
isStuck : isStuck
21
21
} ;
22
22
if ( activeKey ) {
23
- let x = move . location . x ;
24
- let y = move . location . y ;
25
- let r = move . rotation ;
26
- let initialDirection = r ;
27
- let correctionAxis = 'x' ;
28
- switch ( activeKey ) {
29
- case 'ArrowUp' :
30
- y -= TANK_MOVE_STEP ;
31
- r = 0 ;
32
- break ;
33
- case 'ArrowRight' :
34
- x += TANK_MOVE_STEP ;
35
- r = 90 ;
36
- correctionAxis = 'y' ;
37
- break ;
38
- case 'ArrowDown' :
39
- y += TANK_MOVE_STEP ;
40
- r = 180 ;
41
- break ;
42
- case 'ArrowLeft' :
43
- x -= TANK_MOVE_STEP ;
44
- r = 270 ;
45
- correctionAxis = 'y' ;
46
- break ;
47
- default :
48
- }
49
-
50
- // move correction (stick to grid)
51
- if ( initialDirection !== r ) {
52
- if ( correctionAxis === 'x' ) {
53
- x = 3 + ( Math . round ( x / OBSTACLE_WIDTH ) * OBSTACLE_WIDTH ) ;
54
- } else {
55
- y = 3 + ( Math . round ( y / OBSTACLE_HEIGHT ) * OBSTACLE_HEIGHT ) ;
56
- }
57
- }
23
+ // perform step
24
+ const predict = TANK_STEP_HANDLER ( move , activeKey ) ;
58
25
59
26
// intersection check
60
27
if ( world . isIntersecting (
61
- Object . assign ( { } , tank , { location : { x : x , y : y } } ) ,
28
+ Object . assign ( { } , tank , { location : predict . location } ) ,
62
29
Collision . BLOCK_MOVE
63
30
) . length === 0 ) {
64
- move . location = { x : x , y : y } ;
65
- move . rotation = r ;
31
+ move = predict ;
66
32
move . isStuck = false ;
67
33
world . updateObject ( tank . id , move . location ) ;
68
34
} else {
69
35
move . isStuck = true ;
70
36
// just rotate in case of intersection
71
37
// but prevent rotation of stucked AI as it looks like glitch
72
38
if ( tank . actor !== TankActor . AI || ! move . isStuck ) {
73
- move . rotation = r ;
39
+ move . rotation = predict . rotation ;
74
40
}
75
41
}
76
42
}
77
43
return move ;
44
+ } ;
45
+
46
+ /**
47
+ * Tank step handler.
48
+ *
49
+ * @param move - tank move
50
+ * @param activeKey - active key
51
+ * @param step - step to perform
52
+ */
53
+ export const TANK_STEP_HANDLER = ( move : TankMove , activeKey : string , step : number = TANK_MOVE_STEP ) => {
54
+ let x = move . location . x ;
55
+ let y = move . location . y ;
56
+ let r = move . rotation ;
57
+ let initialDirection = r ;
58
+ let correctionAxis = 'x' ;
59
+ switch ( activeKey ) {
60
+ case 'ArrowUp' :
61
+ y -= step ;
62
+ r = 0 ;
63
+ break ;
64
+ case 'ArrowRight' :
65
+ x += step ;
66
+ r = 90 ;
67
+ correctionAxis = 'y' ;
68
+ break ;
69
+ case 'ArrowDown' :
70
+ y += step ;
71
+ r = 180 ;
72
+ break ;
73
+ case 'ArrowLeft' :
74
+ x -= step ;
75
+ r = 270 ;
76
+ correctionAxis = 'y' ;
77
+ break ;
78
+ default :
79
+ }
80
+ // move correction (stick to grid)
81
+ if ( initialDirection !== r ) {
82
+ if ( correctionAxis === 'x' ) {
83
+ x = 3 + ( Math . round ( x / OBSTACLE_WIDTH ) * OBSTACLE_WIDTH ) ;
84
+ } else {
85
+ y = 3 + ( Math . round ( y / OBSTACLE_HEIGHT ) * OBSTACLE_HEIGHT ) ;
86
+ }
87
+ }
88
+ return Object . assign ( { } , move , { location : { x : x , y : y } , rotation : r } ) ;
78
89
} ;
0 commit comments