Skip to content

Commit 317cc5f

Browse files
committed
Improve performance by reducing the number of update radar entity events
1 parent e902faf commit 317cc5f

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/game/component/MapMarkerComponent.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AbstractGameComponent } from '../ECS'
2+
import { Vector3 } from 'three'
23

34
export const MAP_MARKER_TYPE = {
45
default: 1,
@@ -15,6 +16,9 @@ export const MAP_MARKER_CHANGE = {
1516
export type MapMarkerChange = typeof MAP_MARKER_CHANGE[keyof typeof MAP_MARKER_CHANGE]
1617

1718
export class MapMarkerComponent extends AbstractGameComponent {
19+
readonly lastPos: Vector3 = new Vector3()
20+
lastUpdateMs: number = 0
21+
1822
constructor(readonly mapMarkerType: MapMarkerType) {
1923
super()
2024
}

src/game/system/MapMarkerUpdateSystem.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PositionComponent } from '../component/PositionComponent'
33
import { MAP_MARKER_CHANGE, MapMarkerComponent } from '../component/MapMarkerComponent'
44
import { UpdateRadarEntityEvent } from '../../event/LocalEvents'
55
import { EventBroker } from '../../event/EventBroker'
6+
import { MAP_MAX_UPDATE_INTERVAL, TILESIZE } from '../../params'
67

78
export class MapMarkerUpdateSystem extends AbstractGameSystem {
89
readonly componentsRequired: Set<Function> = new Set([PositionComponent, MapMarkerComponent])
@@ -12,9 +13,15 @@ export class MapMarkerUpdateSystem extends AbstractGameSystem {
1213
for (const entity of dirty) {
1314
try {
1415
const components = this.ecs.getComponents(entity)
15-
const mapMarkerType = components.get(MapMarkerComponent).mapMarkerType
16+
const mapMarkerComponent = components.get(MapMarkerComponent)
17+
const mapMarkerType = mapMarkerComponent.mapMarkerType
18+
mapMarkerComponent.lastUpdateMs += elapsedMs
1619
const position = components.get(PositionComponent).position
17-
EventBroker.publish(new UpdateRadarEntityEvent(mapMarkerType, entity, MAP_MARKER_CHANGE.update, position))
20+
if (mapMarkerComponent.lastUpdateMs > MAP_MAX_UPDATE_INTERVAL && position.distanceToSquared(mapMarkerComponent.lastPos) / TILESIZE * 15 > 2) {
21+
EventBroker.publish(new UpdateRadarEntityEvent(mapMarkerType, entity, MAP_MARKER_CHANGE.update, position))
22+
mapMarkerComponent.lastUpdateMs = 0
23+
mapMarkerComponent.lastPos.copy(position)
24+
}
1825
} catch (e) {
1926
console.error(e)
2027
}

src/gui/radar/MapView.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ export class MapView extends BaseElement {
9595
entities.delete(event.entity)
9696
break
9797
}
98-
// TODO This should be limited in frontend, since requests blocked by worker still produce overhead
9998
this.mapRenderer.redrawEntities(this.offset, event.mapMarkerType, this.surfaceRectSize, Array.from(entities.values())).then(() => this.notifyRedraw())
10099
})
101100
this.registerEventListener(EventKey.UPDATE_RADAR_CAMERA, (event: UpdateRadarCamera) => {
102101
this.cameraRect = event.cameraRect
103-
// TODO This should be limited in frontend, since requests blocked by worker still produce overhead
104102
this.mapRenderer.redrawCamera(this.offset, this.surfaceRectSize, this.cameraRect).then(() => this.notifyRedraw())
105103
})
106104
}

0 commit comments

Comments
 (0)