@@ -34,34 +34,46 @@ const jitterComponent = {
34
34
35
35
let xDirecton = 0
36
36
let yDirection = 0
37
+ let isSpacebar = false
37
38
38
39
const controlComponent = {
39
40
name : "control" ,
40
41
state : {
41
- moveSpeed : 2
42
+ moveSpeed : 2 ,
43
+ runSpeed : 5 ,
44
+ controlsID : null
42
45
} ,
43
46
onAttach ( ) {
44
47
const keys = { }
45
48
46
- setInterval ( ( ) => {
49
+ const id = setInterval ( ( ) => {
47
50
if ( keys [ "ArrowLeft" ] ) xDirecton = - 1
48
51
if ( keys [ "ArrowRight" ] ) xDirecton = 1
49
52
if ( keys [ "ArrowUp" ] ) yDirection = - 1
50
53
if ( keys [ "ArrowDown" ] ) yDirection = 1
54
+ if ( keys [ " " ] ) isSpacebar = true
51
55
} , 20 )
52
56
57
+ this . controlsID = id
58
+
53
59
document . addEventListener ( "keydown" , ( event ) => {
60
+ event . preventDefault ( )
54
61
keys [ event . key ] = true
55
62
} )
56
63
57
64
document . addEventListener ( "keyup" , ( event ) => {
65
+ event . preventDefault ( )
58
66
delete keys [ event . key ]
59
67
60
68
if ( ! keys [ "ArrowLeft" ] ) xDirecton = 0
61
69
if ( ! keys [ "ArrowRight" ] ) xDirecton = 0
62
70
if ( ! keys [ "ArrowUp" ] ) yDirection = 0
63
71
if ( ! keys [ "ArrowDown" ] ) yDirection = 0
72
+ if ( ! keys [ " " ] ) isSpacebar = false
64
73
} )
74
+ } ,
75
+ onRemove ( ) {
76
+
65
77
}
66
78
}
67
79
@@ -73,6 +85,60 @@ const growComponent = {
73
85
}
74
86
}
75
87
88
+ const collisionComponent = {
89
+ name : "onCollisionDeleteComponent" ,
90
+ }
91
+
92
+ const collisionProcessor = {
93
+ name : "onCollisionDeleteProcessor" ,
94
+ target : "onCollisionDeleteComponent" ,
95
+ update ( component , entities ) {
96
+ entities . forEach ( ( entity ) => {
97
+ const allEntities = ECS . getEntities ( )
98
+
99
+ for ( let targetEntity of allEntities ) {
100
+ const isPlayer = targetEntity . name === "Player"
101
+
102
+ if ( ! isPlayer ) {
103
+ continue
104
+ }
105
+
106
+
107
+ const targetPosition = ECS . entityHasComponent ( targetEntity , "position" )
108
+ const targetRenderer = ECS . entityHasComponent ( targetEntity , "box" ) || ECS . entityHasComponent ( targetEntity , "playerBox" )
109
+ const hasPosition = ECS . entityHasComponent ( entity , "position" )
110
+ const hasRenderer = ECS . entityHasComponent ( entity , "box" ) || ECS . entityHasComponent ( entity , "playerBox" )
111
+ const hasCollision = ECS . entityHasComponent ( entity , "onCollisionDeleteComponent" )
112
+ const isSelf = targetEntity === entity
113
+
114
+ if ( ! hasPosition || ! hasRenderer || isSelf || ! targetPosition || ! targetRenderer || ! hasCollision ) {
115
+ continue
116
+ }
117
+
118
+ const x = entity . state . x
119
+ const y = entity . state . y
120
+ const width = entity . state . width
121
+ const height = entity . state . height
122
+
123
+ const targetX = targetEntity . state . x
124
+ const targetY = targetEntity . state . y
125
+ const targetWidth = targetEntity . state . width
126
+ const targetHeight = targetEntity . state . height
127
+
128
+ const xCollision = ( x + width ) >= targetX && x <= ( targetX + targetWidth )
129
+ const yCollision = ( y + height ) >= targetY && y <= ( targetY + targetHeight )
130
+
131
+ if ( xCollision && yCollision ) {
132
+ ECS . removeComponentFromEntity ( entity , "onCollisionDeleteComponent" )
133
+ ECS . addComponentToEntity ( entity , "growEffect" )
134
+ ECS . addComponentToEntity ( entity , "jitter" )
135
+ ECS . addComponentToEntity ( entity , "flashColor" )
136
+ }
137
+ }
138
+ } )
139
+ }
140
+ }
141
+
76
142
const growProcesssor = {
77
143
name : "growProcessor" ,
78
144
target : "growEffect" ,
@@ -83,6 +149,7 @@ const growProcesssor = {
83
149
84
150
if ( entity . state . width > entity . state . maxGrowSize ) {
85
151
ECS . removeEntity ( entity )
152
+ score += 10
86
153
}
87
154
} )
88
155
}
@@ -95,6 +162,14 @@ const controlProcessor = {
95
162
entities . forEach ( entity => {
96
163
entity . state . x += xDirecton * entity . state . moveSpeed
97
164
entity . state . y += yDirection * entity . state . moveSpeed
165
+
166
+ if ( isSpacebar && xDirecton ) {
167
+ entity . state . x += xDirecton * entity . state . runSpeed
168
+ }
169
+
170
+ if ( isSpacebar && yDirection ) {
171
+ entity . state . y += yDirection * entity . state . runSpeed
172
+ }
98
173
} )
99
174
}
100
175
}
@@ -121,8 +196,10 @@ const boxRenderingProcessor = {
121
196
target : "box" ,
122
197
update ( component , entities ) {
123
198
entities . forEach ( entity => {
199
+ const hasFlashColor = ECS . entityHasComponent ( entity , "flashColor" )
200
+
124
201
ctx . translate ( entity . state . x , entity . state . y )
125
- ctx . fillStyle = "orange"
202
+ ctx . fillStyle = hasFlashColor ? entity . state . flashColorCurrent : "orange"
126
203
ctx . fillRect ( 0 , 0 , entity . state . width , entity . state . height )
127
204
ctx . translate ( - entity . state . x , - entity . state . y )
128
205
} )
@@ -142,23 +219,53 @@ const playerBoxRenderingProcessor = {
142
219
}
143
220
}
144
221
222
+ const flashColorComponent = {
223
+ name : "flashColor" ,
224
+ state : {
225
+ flashColorTimer : 0 ,
226
+ flashColorSpeed : 200 ,
227
+ flashColor1 : "white" ,
228
+ flashColor2 : "red" ,
229
+ flashColorCurrent : "red"
230
+ }
231
+ }
232
+
233
+ const flashColorProcessor = {
234
+ name : "flashColorProcessor" ,
235
+ target : "flashColor" ,
236
+ update ( component , entities ) {
237
+ entities . forEach ( ( entity ) => {
238
+ entity . state . flashColorTimer += 20
239
+
240
+ if ( entity . state . flashColorTimer >= entity . state . flashColorSpeed ) {
241
+ entity . state . flashColorTimer = 0
242
+ entity . state . flashColorCurrent = entity . state . flashColorCurrent === entity . state . flashColor1 ? entity . state . flashColor2 : entity . state . flashColor1
243
+ }
244
+ } )
245
+ }
246
+ }
247
+
145
248
ECS . addComponent ( positionComponent )
146
249
ECS . addComponent ( boxComponent )
147
250
ECS . addComponent ( jitterComponent )
148
251
ECS . addComponent ( controlComponent )
149
252
ECS . addComponent ( playerBox )
150
253
ECS . addComponent ( growComponent )
254
+ ECS . addComponent ( collisionComponent )
255
+ ECS . addComponent ( flashColorComponent )
256
+
257
+ ECS . addProcessor ( flashColorProcessor )
258
+ ECS . addProcessor ( collisionProcessor )
151
259
ECS . addProcessor ( growProcesssor )
152
- ECS . addProcessor ( playerBoxRenderingProcessor )
153
260
ECS . addProcessor ( jitterProcessor )
154
261
ECS . addProcessor ( boxRenderingProcessor )
155
262
ECS . addProcessor ( controlProcessor )
263
+ ECS . addProcessor ( playerBoxRenderingProcessor )
156
264
157
265
158
266
const create100Boxes = ( ) => {
159
- console . log ( ECS )
160
267
for ( let i = 0 ; i < 100 ; i ++ ) {
161
- const box = ECS . createEntity ( "Box " , [ "position" , "box" , "growEffect " ] )
268
+ const box = ECS . createEntity ( "enemy " , [ "position" , "box" , "onCollisionDeleteComponent " ] )
162
269
163
270
box . state . x = Math . random ( ) * canvas . width
164
271
box . state . y = Math . random ( ) * canvas . height
@@ -167,8 +274,17 @@ const create100Boxes = () => {
167
274
}
168
275
}
169
276
277
+ const create1EnemyBox = ( ) => {
278
+ const enemyBox = ECS . createEntity ( "enemy" , [ "position" , "box" , "onCollisionDeleteComponent" ] )
279
+
280
+ enemyBox . state . x = 100
281
+ enemyBox . state . y = 100
282
+
283
+ ECS . addEntity ( enemyBox )
284
+ }
285
+
170
286
const createPlayerBox = ( ) => {
171
- const playerBox = ECS . createEntity ( "Player" , [ "position" , "playerBox" , "control" , "growEffect" ] )
287
+ const playerBox = ECS . createEntity ( "Player" , [ "position" , "playerBox" , "control" ] )
172
288
173
289
playerBox . state . x = canvas . width / 2
174
290
playerBox . state . y = canvas . height / 2
@@ -182,13 +298,83 @@ canvas.style.backgroundColor = "black"
182
298
183
299
const ctx = canvas . getContext ( "2d" ) as CanvasRenderingContext2D
184
300
301
+ let score = 0
302
+
185
303
const gameloop = ( ) => {
186
304
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height )
187
305
ECS . update ( )
306
+ ctx . font = "30px Comis-Sans"
307
+ ctx . fillText ( "Score:" , 30 , 30 )
308
+ ctx . fillText ( String ( score ) , 110 , 32 )
188
309
189
310
requestAnimationFrame ( gameloop )
190
311
}
191
312
192
313
create100Boxes ( )
314
+ // create1EnemyBox()
193
315
createPlayerBox ( )
194
- // gameloop()
316
+ gameloop ( )
317
+
318
+
319
+ const playerRendererCheckbox = document . getElementById ( "player-renderer" ) as HTMLInputElement
320
+ const playerRendererContainer = document . getElementById ( "player-renderer-container" )
321
+ const playerRendererSize = document . getElementById ( "player-renderer-size" ) as HTMLInputElement
322
+
323
+ const playerControlsCheckbox = document . getElementById ( "player-controls" ) as HTMLInputElement
324
+ const playerControlsContainer = document . getElementById ( "player-controls-container" )
325
+ const playerControlsSpeed = document . getElementById ( "player-controls-speed" ) as HTMLInputElement
326
+ const playerControlsRunSpeed = document . getElementById ( "player-controls-run-speed" ) as HTMLInputElement
327
+
328
+ playerRendererCheckbox . addEventListener ( "change" , ( ) => {
329
+ const checkbox = playerRendererCheckbox
330
+ const isChecked = checkbox . checked
331
+ const player = ECS . getEntity ( "Player" )
332
+
333
+ if ( isChecked ) {
334
+ playerRendererContainer . style . display = "block"
335
+ ECS . addComponentToEntity ( player , "playerBox" )
336
+ }
337
+
338
+ if ( ! isChecked ) {
339
+ playerRendererContainer . style . display = "none"
340
+ ECS . removeComponentFromEntity ( player , "playerBox" )
341
+ }
342
+ } )
343
+
344
+ playerRendererSize . addEventListener ( "change" , ( ) => {
345
+ const player = ECS . getEntity ( "Player" )
346
+ player . state . height = Number ( playerRendererSize . value )
347
+ player . state . width = Number ( playerRendererSize . value )
348
+ } )
349
+
350
+ playerControlsCheckbox . addEventListener ( "change" , ( ) => {
351
+ const checkbox = playerControlsCheckbox
352
+ const isChecked = checkbox . checked
353
+ const player = ECS . getEntity ( "Player" )
354
+
355
+ if ( isChecked ) {
356
+ playerControlsContainer . style . display = "block"
357
+ ECS . addComponentToEntity ( player , "controls" )
358
+ }
359
+
360
+ if ( ! isChecked ) {
361
+ playerControlsContainer . style . display = "none"
362
+ ECS . removeComponentFromEntity ( player , "controls" )
363
+ }
364
+ } )
365
+
366
+ playerControlsSpeed . addEventListener ( "change" , ( ) => {
367
+ const player = ECS . getEntity ( "Player" )
368
+ player . state . moveSpeed = Number ( playerControlsSpeed . value )
369
+
370
+ console . log ( player )
371
+ } )
372
+
373
+ playerControlsRunSpeed . addEventListener ( "change" , ( ) => {
374
+ const player = ECS . getEntity ( "Player" )
375
+ player . state . runSpeed = Number ( playerControlsRunSpeed . value )
376
+ } )
377
+
378
+ const getPlayerCodeSnippet = ( ) => {
379
+ const player = ECS . getEntity ( "Player" )
380
+ }
0 commit comments