rotatingBlock
Fait tourner des objets autour d'un point
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
tag | string | "rotate-e" | |
speed | number | 60 | |
rotateSprite | boolean | true | |
relativeSpeed | boolean | true |
example
rotatingBlock({ tag: "rotate-e", speed: 60, rotateSprite: true, relativeSpeed: true}),function rotatingBlock(p) { const param = { tag: "rotate-e", speed: 60, rotateSprite: true, relativeSpeed: true, ...p } return { rotationTarget: null, currentRotation: 0, rotationDistance: 0, startingRotation: 0, add() { requestAnimationFrame(() => { for (const t of get(param.tag, { recursive: true })) { if (this.rotationTarget == null || this.pos.dist(t.pos) < this.pos.dist(this.rotationTarget.pos)) { this.rotationTarget = t } } if (this.rotationTarget != null) { this.startingRotation = -this.rotationTarget.pos.angle(this.pos) this.rotationDistance = this.rotationTarget.pos.dist(this.pos) this.pos = Vec2.fromAngle(this.currentRotation).scale(this.rotationDistance).add(this.rotationTarget.pos) } }) }, update() { if (param.relativeSpeed) this.currentRotation += dt() * param.speed / this.rotationDistance * 100 else this.currentRotation += dt() * param.speed this.pos = Vec2.fromAngle(this.currentRotation + this.startingRotation).scale(this.rotationDistance).add(this.rotationTarget.pos) if (param.rotateSprite) this.angle = this.currentRotation }, }}