multiplayerCamera
Permet à la camera de suivre plusieurs objets à la fois
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
tag | string | "player" | |
posAnimDelay | number | 0.1 | |
zoomAnimDelay | number | 0.2 | |
distanceMin | number | 200 | |
amplitudeZoom | number | 0.0008 | |
zoomReductioLimit | number | 0.4 | |
screenRatio | number | 600 |
example
multiplayerCamera({ tag: "player", posAnimDelay: 0.1, zoomAnimDelay: 0.2, distanceMin: 200, amplitudeZoom: 0.0008, zoomReductioLimit: 0.4, screenRatio: 600}),function multiplayerCamera(p) { const param = { tag: "player", posAnimDelay: 0.1, zoomAnimDelay: 0.2, distanceMin: 200, amplitudeZoom: 0.0008, zoomReductioLimit: 0.4, screenRatio: 600, ...p } return { id: "camera", pos: null, zoom: null, targets: [], add() { for (const e of this.targets) e.onPhysicsResolve(() => this.setCameraPos()) }, update() { if (this.targets.length == 0) this.updateCameraTargets() if (this.targets.length > 0) this.setCameraPos() this.updateCameraTargets() }, updateCameraTargets() { this.targets = [] for (const e of get(param.tag, { recursive: true, liveUpdate: true })) { if (e.is("alive")) if (e.isAlive) this.targets.push(e) else this.targets.push(e) } }, setCameraPos() { let min = vec2(this.targets[0].pos) let max = vec2(this.targets[0].pos) let sum = vec2(0, 0) let targetsLength = 0 for (const p of this.targets) { if (p.isAlive) { targetsLength++ sum = sum.add(p.pos) min.x = Math.min(p.pos.x, min.x) max.x = Math.max(p.pos.x, max.x) min.y = Math.min(p.pos.y, min.y) max.y = Math.max(p.pos.y, max.y) } } const dist = Math.max(min.dist(max) - param.distanceMin, 0) const zoom = Math.max(1 - dist * param.amplitudeZoom, param.zoomReductioLimit) const scale = 1 / targetsLength const tragetPos = vec2(sum).scale(scale) if (this.pos == undefined) this.pos = tragetPos if (this.zoom == undefined) this.zoom = zoom if (targetsLength > 0) { tween(this.pos, tragetPos, param.posAnimDelay, (p) => this.pos = p, easings.linear) tween(this.zoom, zoom, param.zoomAnimDelay, (p) => this.zoom = p, easings.linear) setCamPos(this.pos) setCamScale(this.zoom * height() / param.screenRatio) } }, }}