1
1
import { Controller } from '@hotwired/stimulus' ;
2
2
3
-
4
3
export default class extends Controller {
5
- static targets = [ "game" , "ship" , "comet" , "message" , "timer" , "audioBackground" , "startPlaceholder" , "endPlaceholder" ] ;
4
+ static targets = [
5
+ 'game' ,
6
+ 'ship' ,
7
+ 'comet' ,
8
+ 'message' ,
9
+ 'timer' ,
10
+ 'audioBackground' ,
11
+ 'startPlaceholder' ,
12
+ 'endPlaceholder' ,
13
+ ] ;
6
14
7
15
connect ( ) {
8
16
this . svg = this . gameTarget ;
@@ -13,22 +21,22 @@ export default class extends Controller {
13
21
this . cometWidth = 60 ;
14
22
this . cometHeight = 60 ;
15
23
this . cometInterval = 1000 ;
16
- document . addEventListener ( " keydown" , this . keyDownHandler . bind ( this ) ) ;
17
- document . addEventListener ( " keyup" , this . keyUpHandler . bind ( this ) ) ;
24
+ document . addEventListener ( ' keydown' , this . keyDownHandler . bind ( this ) ) ;
25
+ document . addEventListener ( ' keyup' , this . keyUpHandler . bind ( this ) ) ;
18
26
this . audioBackgroundTarget . loop = true ;
19
27
}
20
28
21
29
disconnect ( ) {
22
- document . removeEventListener ( " keydown" , this . keyDownHandler . bind ( this ) ) ;
23
- document . removeEventListener ( " keyup" , this . keyUpHandler . bind ( this ) ) ;
30
+ document . removeEventListener ( ' keydown' , this . keyDownHandler . bind ( this ) ) ;
31
+ document . removeEventListener ( ' keyup' , this . keyUpHandler . bind ( this ) ) ;
24
32
}
25
33
26
34
fresh ( ) {
27
35
while ( this . svg . firstChild ) {
28
36
this . svg . removeChild ( this . svg . firstChild ) ;
29
37
}
30
38
31
- this . timer = 0
39
+ this . timer = 0 ;
32
40
this . cometSpeed = this . baseCometSpeed ;
33
41
this . shipX = ( this . svg . width . baseVal . value - this . shipWidth ) / 2 ;
34
42
this . shipY = this . svg . height . baseVal . value - this . shipHeight - 10 ;
@@ -41,28 +49,28 @@ export default class extends Controller {
41
49
}
42
50
43
51
keyDownHandler ( event ) {
44
- if ( event . key === " ArrowLeft" ) {
52
+ if ( event . key === ' ArrowLeft' ) {
45
53
this . leftPressed = true ;
46
- } else if ( event . key === " ArrowRight" ) {
54
+ } else if ( event . key === ' ArrowRight' ) {
47
55
this . rightPressed = true ;
48
56
}
49
57
}
50
58
51
59
keyUpHandler ( event ) {
52
- if ( event . key === " ArrowLeft" ) {
60
+ if ( event . key === ' ArrowLeft' ) {
53
61
this . leftPressed = false ;
54
- } else if ( event . key === " ArrowRight" ) {
62
+ } else if ( event . key === ' ArrowRight' ) {
55
63
this . rightPressed = false ;
56
64
}
57
65
}
58
66
59
67
createShipElement ( ) {
60
68
const shipElement = this . shipTarget . cloneNode ( true ) ;
61
- shipElement . setAttribute ( "x" , this . shipX ) ;
62
- shipElement . setAttribute ( "y" , this . shipY ) ;
63
- shipElement . setAttribute ( " width" , this . shipWidth ) ;
64
- shipElement . setAttribute ( " height" , this . shipHeight ) ;
65
- shipElement . setAttribute ( " fill" , " #FFFFFF" ) ;
69
+ shipElement . setAttribute ( 'x' , this . shipX ) ;
70
+ shipElement . setAttribute ( 'y' , this . shipY ) ;
71
+ shipElement . setAttribute ( ' width' , this . shipWidth ) ;
72
+ shipElement . setAttribute ( ' height' , this . shipHeight ) ;
73
+ shipElement . setAttribute ( ' fill' , ' #FFFFFF' ) ;
66
74
this . svg . appendChild ( shipElement ) ;
67
75
return shipElement ;
68
76
}
@@ -73,22 +81,22 @@ export default class extends Controller {
73
81
} else if ( this . rightPressed && this . shipX < this . svg . width . baseVal . value - this . shipWidth ) {
74
82
this . shipX += this . shipSpeed ;
75
83
}
76
- this . shipElement . setAttribute ( "x" , this . shipX ) ;
84
+ this . shipElement . setAttribute ( 'x' , this . shipX ) ;
77
85
}
78
86
79
87
createCometElement ( x , y ) {
80
88
const cometElement = this . cometTarget . cloneNode ( true ) ;
81
- cometElement . setAttribute ( "x" , x ) ;
82
- cometElement . setAttribute ( "y" , y ) ;
83
- cometElement . setAttribute ( " width" , this . cometWidth ) ;
84
- cometElement . setAttribute ( " height" , this . cometHeight ) ;
85
- cometElement . setAttribute ( " fill" , " #FF0000" ) ;
89
+ cometElement . setAttribute ( 'x' , x ) ;
90
+ cometElement . setAttribute ( 'y' , y ) ;
91
+ cometElement . setAttribute ( ' width' , this . cometWidth ) ;
92
+ cometElement . setAttribute ( ' height' , this . cometHeight ) ;
93
+ cometElement . setAttribute ( ' fill' , ' #FF0000' ) ;
86
94
this . svg . appendChild ( cometElement ) ;
87
95
return cometElement ;
88
96
}
89
97
90
98
moveComets ( ) {
91
- this . comets . forEach ( comet => {
99
+ this . comets . forEach ( ( comet ) => {
92
100
comet . y += this . cometSpeed ;
93
101
comet . x = comet . element . getAttribute ( 'x' ) ;
94
102
comet . x -= this . cometSpeed ;
@@ -97,12 +105,12 @@ export default class extends Controller {
97
105
const distance = Math . sqrt ( dx * dx + dy * dy ) ;
98
106
const vx = dx / distance ;
99
107
const vy = dy / distance ;
100
- if ( distance > 100 ) {
108
+ if ( distance > 100 ) {
101
109
comet . x += vx ;
102
110
comet . y += vy ;
103
111
}
104
- comet . element . setAttribute ( "x" , comet . x ) ;
105
- comet . element . setAttribute ( "y" , comet . y ) ;
112
+ comet . element . setAttribute ( 'x' , comet . x ) ;
113
+ comet . element . setAttribute ( 'y' , comet . y ) ;
106
114
if ( comet . y > this . svg . height . baseVal . value ) {
107
115
this . svg . removeChild ( comet . element ) ;
108
116
this . comets . splice ( this . comets . indexOf ( comet ) , 1 ) ;
@@ -111,20 +119,21 @@ export default class extends Controller {
111
119
}
112
120
113
121
createComet ( ) {
114
- let x = Math . random ( ) * this . svg . width . baseVal . value + ( this . svg . width . baseVal . value * 0.5 ) ;
122
+ let x = Math . random ( ) * this . svg . width . baseVal . value + this . svg . width . baseVal . value * 0.5 ;
115
123
if ( this . shipX > this . svg . width . baseVal . value / 3 ) {
116
- x += ( this . shipX * 0.5 ) ;
124
+ x += this . shipX * 0.5 ;
117
125
}
118
126
const y = 0 ;
119
127
const cometElement = this . createCometElement ( x , y ) ;
120
- this . comets . push ( { element : cometElement , y : y } ) ;
128
+ this . comets . push ( { element : cometElement , y : y } ) ;
121
129
}
122
130
123
131
collisionDetection ( fuzziness ) {
124
132
const shipRect = this . shipElement . getBoundingClientRect ( ) ;
125
- this . comets . forEach ( comet => {
133
+ this . comets . forEach ( ( comet ) => {
126
134
const cometRect = comet . element . getBoundingClientRect ( ) ;
127
- const fuzzinessFactor = fuzziness * Math . min ( shipRect . width , shipRect . height , cometRect . width , cometRect . height ) ;
135
+ const fuzzinessFactor =
136
+ fuzziness * Math . min ( shipRect . width , shipRect . height , cometRect . width , cometRect . height ) ;
128
137
if (
129
138
shipRect . left + fuzzinessFactor < cometRect . right &&
130
139
shipRect . right - fuzzinessFactor > cometRect . left &&
@@ -140,15 +149,14 @@ export default class extends Controller {
140
149
} ) ;
141
150
}
142
151
143
- stats ( ) {
152
+ stats ( ) {
144
153
this . timer = this . timer + 0.5 ;
145
- let value = ( this . timer / 100 ) . toFixed ( 2 )
154
+ let value = ( this . timer / 100 ) . toFixed ( 2 ) ;
146
155
147
156
this . timerTarget . innerText = value ;
148
157
this . cometSpeed = this . baseCometSpeed + value * 0.1 ;
149
158
150
-
151
- if ( value > 20 ) {
159
+ if ( value > 20 ) {
152
160
clearInterval ( this . intervalComet ) ;
153
161
clearInterval ( this . intervalGame ) ;
154
162
@@ -159,12 +167,12 @@ export default class extends Controller {
159
167
draw ( ) {
160
168
this . moveShip ( ) ;
161
169
this . moveComets ( ) ;
162
- this . collisionDetection ( 0.4 ) ; // Погрешность 40%
170
+ this . collisionDetection ( 0.4 ) ; // Погрешность 40%
163
171
this . stats ( ) ;
164
172
}
165
173
166
174
start ( ) {
167
- if ( this . audioBackgroundTarget . currentTime === 0 ) {
175
+ if ( this . audioBackgroundTarget . currentTime === 0 ) {
168
176
this . audioBackgroundTarget . play ( ) ;
169
177
}
170
178
0 commit comments