1
+ import MissileMove from "../models/move/MissileMove" ;
2
+ import { MISSILE_HEIGHT , MISSILE_MOVE_STEP , MISSILE_WIDTH } from "../../constants" ;
3
+ import { Collision } from "../enums/Collision" ;
4
+ import MissileModel from "../models/components/MissileModel" ;
5
+ import World from "../classes/World" ;
6
+ import Point from "../models/Point" ;
7
+
8
+ /**
9
+ * Missile move handler.
10
+ */
11
+ export const MISSILE_MOVE_HANDLER = ( missile : MissileModel , world : World , direction : number , axis : string ) : MissileMove => {
12
+ let move : MissileMove = {
13
+ location : missile . location ,
14
+ hitObjects : [ ]
15
+ } ;
16
+
17
+ // calculate new coordinates and check if missile may move there
18
+ const step = direction * MISSILE_MOVE_STEP ;
19
+ const newCoords = MISSILE_NEXT_COORDINATES ( axis , step , missile . location ) ;
20
+ const hitObjects = world . isIntersecting ( {
21
+ id : missile . id ,
22
+ location : {
23
+ x : newCoords . x ,
24
+ y : newCoords . y
25
+ } ,
26
+ dimension : {
27
+ width : MISSILE_WIDTH ,
28
+ height : MISSILE_HEIGHT
29
+ }
30
+ } ,
31
+ Collision . BLOCK_SHOT
32
+ ) ;
33
+
34
+ // handle missile fell if it hits other object or continue moving
35
+ if ( hitObjects . length > 0 ) {
36
+ move . hitObjects = hitObjects ;
37
+ } else {
38
+ move . location = newCoords ;
39
+ world . updateObject ( missile . id , move . location ) ;
40
+ }
41
+ return move ;
42
+ } ;
43
+
44
+ /**
45
+ * Calculate missile next coordinates.
46
+ *
47
+ * @param axis - axis (x or y)
48
+ * @param step - how far missile will move
49
+ * @param location - initial position
50
+ */
51
+ export const MISSILE_NEXT_COORDINATES = ( axis : string , step : number , location : Point ) : Point => {
52
+ return {
53
+ x : axis === 'x' ? ( location . x + step ) : location . x ,
54
+ y : axis === 'y' ? ( location . y + step ) : location . y
55
+ } ;
56
+ } ;
57
+
58
+ /**
59
+ * Missile direction map.
60
+ */
61
+ export const MISSILE_DIRECTION_MAP : { [ index : number ] : string } = {
62
+ 0 : '-y' ,
63
+ 90 : '+x' ,
64
+ 180 : '+y' ,
65
+ 270 : '-x'
66
+ } ;
0 commit comments