|
1 | | -import type { Vector3, Camera } from 'three'; |
| 1 | +import type { Vector3 } from 'three'; |
2 | 2 | import RoomModule, { |
3 | 3 | type RoomModuleObservables, |
4 | 4 | type RoomModuleState |
5 | 5 | } from '../RoomModule'; |
6 | 6 | import type Unit from '../Unit'; |
7 | 7 | import UnitChunkManager from '../UnitChunkManager'; |
8 | | -import { Subject, concatMap, distinctUntilChanged, map } from 'rxjs'; |
| 8 | +import { |
| 9 | + EMPTY, |
| 10 | + Subject, |
| 11 | + concatMap, |
| 12 | + debounceTime, |
| 13 | + distinctUntilChanged, |
| 14 | + map, |
| 15 | + merge, |
| 16 | + switchMap |
| 17 | +} from 'rxjs'; |
9 | 18 | import { ArrayKeyMap } from '../ArrayKeyMap'; |
10 | 19 | import type { AnimationLoopValue } from '../Renderer'; |
11 | 20 | import { FLOOR_HEIGHT } from '../../utils/ground'; |
12 | 21 | import type Room from '../Room'; |
13 | 22 | import type { ConditionDirectionsDescription } from '../../utils/pathfindng'; |
| 23 | +import { getFloorRange } from '../../utils/floor'; |
14 | 24 |
|
15 | 25 | interface Observables extends RoomModuleObservables { |
16 | 26 | addUnit$: Subject<Unit>; |
@@ -68,6 +78,23 @@ export default class UnitsModule extends RoomModule<State, Observables> { |
68 | 78 | ) |
69 | 79 | .subscribe(void 0) |
70 | 80 | ); |
| 81 | + |
| 82 | + this.subscription.add( |
| 83 | + merge( |
| 84 | + this.room.app.renderer.observables.controlsChange$, |
| 85 | + this.room.app.modules.player.observables.currentPlayer$.pipe( |
| 86 | + switchMap(player => player?.observables.unit$ || EMPTY), |
| 87 | + switchMap(({ unit }) => unit.observables.position$) |
| 88 | + ) |
| 89 | + ) |
| 90 | + .pipe(debounceTime(20)) |
| 91 | + .subscribe(() => { |
| 92 | + const units = this.chunkManager.updateVisibility( |
| 93 | + this.room.app.renderer.camera |
| 94 | + ); |
| 95 | + this.state.visibleUnits = Array.from(units); |
| 96 | + }) |
| 97 | + ); |
71 | 98 | } |
72 | 99 |
|
73 | 100 | //#region methods |
@@ -162,27 +189,29 @@ export default class UnitsModule extends RoomModule<State, Observables> { |
162 | 189 | } |
163 | 190 |
|
164 | 191 | override update(v: AnimationLoopValue) { |
165 | | - this.state.visibleUnits.forEach(unit => { |
166 | | - unit.update(v); |
167 | | - }); |
| 192 | + this.state.visibleUnits.forEach(unit => unit.update(v)); |
168 | 193 | } |
169 | 194 |
|
170 | | - override updateThrottle(time: number, { camera }: { camera: Camera }) { |
171 | | - const units = this.chunkManager.updateVisibility(camera); |
172 | | - this.state.visibleUnits = Array.from(units); |
173 | | - } |
| 195 | + // override updateThrottle(time: number, { camera }: { camera: Camera }) { |
| 196 | + // const units = this.chunkManager.updateVisibility(camera); |
| 197 | + // this.state.visibleUnits = Array.from(units); |
| 198 | + // } |
174 | 199 |
|
| 200 | + private _updateVisiblity_lastFloorIndex: number | null = null; |
175 | 201 | updateVisiblity( |
176 | 202 | floorIndex?: number, |
177 | 203 | units: Unit[] = Array.from(this.state.units.values()) |
178 | 204 | ) { |
179 | | - units.forEach(unit => unit.setVisible(false)); |
180 | 205 | floorIndex = floorIndex ?? this.room.modules.floor.getFloor(); |
181 | | - for (let f = 0; f <= floorIndex; f++) { |
182 | | - this.getUnitsByFloor(f).forEach(unit => unit.setVisible(true)); |
| 206 | + |
| 207 | + if (this._updateVisiblity_lastFloorIndex !== floorIndex) { |
| 208 | + const floors = getFloorRange(floorIndex); |
| 209 | + units.forEach(unit => |
| 210 | + unit.setVisible(floors.includes(Math.floor(unit.position.y))) |
| 211 | + ); |
183 | 212 | } |
| 213 | + this._updateVisiblity_lastFloorIndex = floorIndex; |
184 | 214 | } |
185 | | - |
186 | 215 | //#endregion |
187 | 216 | } |
188 | 217 |
|
|
0 commit comments