Skip to content

Commit 1683f9e

Browse files
committed
fix(update): many updates
1 parent 15f7da5 commit 1683f9e

5 files changed

Lines changed: 86 additions & 42 deletions

File tree

packages/app/lib/classes/Renderer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
55
import { OutlinePass } from 'three/addons/postprocessing/OutlinePass.js';
66
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
77

8-
import { ReplaySubject, type Observable } from 'rxjs';
9-
import { fromEvent } from 'rxjs';
8+
import { Observable, ReplaySubject, fromEvent } from 'rxjs';
109
import {
1110
Clock,
1211
SRGBColorSpace,
@@ -28,6 +27,7 @@ import {
2827
} from 'three';
2928
import IntersectionRendererModule from './rendererModule/Intersection';
3029
import DebugRendererModule from './rendererModule/Debug';
30+
import type { HasEventTargetAddRemove } from 'rxjs/internal/observable/fromEvent';
3131

3232
export type RendererModuleList = (
3333
| typeof DebugRendererModule
@@ -76,6 +76,7 @@ export default class Renderer<
7676
rotate: boolean;
7777
}>;
7878
rotation$: ReplaySubject<number>;
79+
controlsChange$: Observable<Event>;
7980
};
8081
shadowQuality: ShadowQuality = ShadowQuality.OFF;
8182

@@ -131,7 +132,8 @@ export default class Renderer<
131132
zoom: boolean;
132133
rotate: boolean;
133134
}>(1),
134-
rotation$: new ReplaySubject<number>(1)
135+
rotation$: new ReplaySubject<number>(1),
136+
controlsChange$: new Observable<Event>()
135137
};
136138

137139
this.dimension = dimension;
@@ -155,6 +157,10 @@ export default class Renderer<
155157
this.initComposer();
156158
if (options.controls) {
157159
this.initControls();
160+
this.observables.controlsChange$ = fromEvent<Event>(
161+
this.controls as HasEventTargetAddRemove<Event>,
162+
'change'
163+
);
158164
}
159165

160166
//#region Modules

packages/app/lib/classes/UnitChunkManager.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,23 @@ export default class UnitChunkManager {
7171

7272
updateVisibility(camera: Camera) {
7373
const visibleChunks = this.findVisibleChunks(camera); // Finde alle sichtbaren Chunks
74-
75-
this.chunks.forEach(chunk => {
76-
chunk.visible = false;
77-
Array.from(chunk.units.values())
78-
.filter(unit => !unit.modules.player.player?.client)
79-
.forEach(unit => unit.setChunkVisible(false));
80-
});
81-
8274
const visibleUnits = new Set<Unit>();
83-
visibleChunks.forEach(key => {
84-
if (this.chunks.has(key)) {
85-
const chunk = this.chunks.get(key)!;
75+
76+
Array.from(this.chunks.entries()).forEach(([key, chunk]) => {
77+
if (visibleChunks.has(key)) {
8678
chunk.visible = true;
8779
chunk.units.forEach(unit => {
8880
unit.setChunkVisible(true);
8981
visibleUnits.add(unit);
9082
});
83+
} else {
84+
chunk.visible = false;
85+
Array.from(chunk.units.values())
86+
.filter(unit => !unit.modules.player.player?.client)
87+
.forEach(unit => unit.setChunkVisible(false));
9188
}
9289
});
90+
9391
return visibleUnits;
9492
}
9593

@@ -112,7 +110,6 @@ export default class UnitChunkManager {
112110
frustum.setFromProjectionMatrix(projScreenMatrix);
113111

114112
const visibleChunkKeys = new Set<string>();
115-
116113
const allChunkPositions = this.getChunkPositions();
117114

118115
for (const pos of allChunkPositions) {

packages/app/lib/classes/roomModule/Units.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
import type { Vector3, Camera } from 'three';
1+
import type { Vector3 } from 'three';
22
import RoomModule, {
33
type RoomModuleObservables,
44
type RoomModuleState
55
} from '../RoomModule';
66
import type Unit from '../Unit';
77
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';
918
import { ArrayKeyMap } from '../ArrayKeyMap';
1019
import type { AnimationLoopValue } from '../Renderer';
1120
import { FLOOR_HEIGHT } from '../../utils/ground';
1221
import type Room from '../Room';
1322
import type { ConditionDirectionsDescription } from '../../utils/pathfindng';
23+
import { getFloorRange } from '../../utils/floor';
1424

1525
interface Observables extends RoomModuleObservables {
1626
addUnit$: Subject<Unit>;
@@ -68,6 +78,23 @@ export default class UnitsModule extends RoomModule<State, Observables> {
6878
)
6979
.subscribe(void 0)
7080
);
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+
);
7198
}
7299

73100
//#region methods
@@ -162,27 +189,29 @@ export default class UnitsModule extends RoomModule<State, Observables> {
162189
}
163190

164191
override update(v: AnimationLoopValue) {
165-
this.state.visibleUnits.forEach(unit => {
166-
unit.update(v);
167-
});
192+
this.state.visibleUnits.forEach(unit => unit.update(v));
168193
}
169194

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+
// }
174199

200+
private _updateVisiblity_lastFloorIndex: number | null = null;
175201
updateVisiblity(
176202
floorIndex?: number,
177203
units: Unit[] = Array.from(this.state.units.values())
178204
) {
179-
units.forEach(unit => unit.setVisible(false));
180205
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+
);
183212
}
213+
this._updateVisiblity_lastFloorIndex = floorIndex;
184214
}
185-
186215
//#endregion
187216
}
188217

packages/app/lib/classes/unitModule/Movement.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,23 @@ export default class MovementUnitModule extends UnitModule<
182182
}
183183

184184
return new Promise<{ position: Vector3 } | null>(resolve => {
185-
const subscription = new Subscription();
186-
subscription.add(
187-
this.observables.moveAbort$.subscribe(() => {
188-
resolve(null);
189-
subscription.unsubscribe();
190-
})
191-
);
192-
subscription.add(
193-
this.observables.moveEnd$.subscribe(position => {
194-
console.log('EEEEEEND');
195-
resolve({ position });
196-
subscription.unsubscribe();
197-
})
198-
);
185+
if (this.movements.length) {
186+
const subscription = new Subscription();
187+
subscription.add(
188+
this.observables.moveAbort$.subscribe(() => {
189+
resolve(null);
190+
subscription.unsubscribe();
191+
})
192+
);
193+
subscription.add(
194+
this.observables.moveEnd$.subscribe(position => {
195+
resolve({ position });
196+
subscription.unsubscribe();
197+
})
198+
);
199+
} else {
200+
resolve(null);
201+
}
199202
});
200203
} else {
201204
console.warn(
@@ -219,6 +222,7 @@ export default class MovementUnitModule extends UnitModule<
219222

220223
if (!this.currentMovement && this.lastMovement) {
221224
this.lastMovement = null;
225+
222226
this.observables.moveEnd$.next(this.unit.getPosition());
223227
}
224228

packages/app/lib/utils/floor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ import type { Vector3 } from 'three';
33
export function getFloorFromPosition(position: Vector3) {
44
return Math.floor(position.y);
55
}
6+
7+
export function getFloorRange(max: number, min = 0): number[] {
8+
const range: number[] = [];
9+
for (let i = min; i <= max; i++) {
10+
range.push(i);
11+
}
12+
return range;
13+
}

0 commit comments

Comments
 (0)