1
- import { DestroyRef , Directive , effect , inject , input , model , signal , WritableSignal } from '@angular/core' ;
1
+ import { DestroyRef , Directive , effect , inject , input , model , signal , untracked , WritableSignal } from '@angular/core' ;
2
2
import { injectStore } from 'angular-three' ;
3
3
import { Object3D } from 'three' ;
4
4
import { COLLECTIBLES_SPEED , PLANE_AMP_HEIGHT , PLANE_DEFAULT_HEIGHT , SEA_RADIUS } from '../constants' ;
5
5
import { GameStore } from '../game.store' ;
6
6
7
+ export type CollectibleState = 'spawned' | 'collected' | 'skipped' ;
8
+
7
9
@Directive ( )
8
10
export class Collectible {
9
11
initialAngle = input ( 0 ) ;
10
12
initialDistance = input ( 0 ) ;
11
13
positionX = input ( 0 ) ;
12
14
positionY = input ( 0 ) ;
13
- state = model . required < 'spawned' | 'collected' | 'skipped' > ( ) ;
15
+ state = model . required < CollectibleState > ( ) ;
14
16
15
17
protected angle = 0 ;
16
18
protected distance = 0 ;
@@ -56,7 +58,7 @@ export class CollectiblesStore {
56
58
coins = signal <
57
59
Array < {
58
60
key : number ;
59
- state : WritableSignal < 'spawned' | 'collected' | 'skipped' > ;
61
+ state : WritableSignal < CollectibleState > ;
60
62
angle : number ;
61
63
distance : number ;
62
64
positionY : number ;
@@ -70,14 +72,15 @@ export class CollectiblesStore {
70
72
const amplitude = 10 + Math . round ( Math . random ( ) * 10 ) ;
71
73
72
74
this . coins . update ( ( prev ) => [
73
- ...prev ,
75
+ // NOTE: we filter out all the coins that are already collected or skipped
76
+ ...prev . filter ( ( coin ) => untracked ( coin . state ) === 'spawned' ) ,
74
77
...Array . from ( { length : nCoins } ) . map ( ( _ , index ) => {
75
78
const angle = - ( index * 0.02 ) ;
76
79
const distance = d + Math . cos ( index * 0.5 ) * amplitude ;
77
80
78
81
return {
79
82
key : this . gameStore . statistics . coinsSpawned + index ,
80
- state : signal < 'spawned' | 'collected' | 'skipped' > ( 'spawned' ) ,
83
+ state : signal < CollectibleState > ( 'spawned' ) ,
81
84
angle,
82
85
distance,
83
86
positionY : - SEA_RADIUS + Math . sin ( angle ) * distance ,
0 commit comments