fertig-classic-games/src/games/forbiddenisland/IslandStorm.js

87 lines
3.9 KiB
JavaScript

// The Drowning Isle: the monsoon. The storm follows the tide gauge: clouds
// thicken, the light drops, rain goes from drizzle to sheets, and from gauge 6
// lightning starts to crack over the island. `strike()` is also called directly
// for dramatic moments (Waters Rise, a sinking shrine).
import * as Phaser from 'phaser';
import { W, H, DEPTH, RAIL_X } from './IslandLayout.js';
import * as Sound from './IslandSound.js';
export class IslandStorm {
constructor(scene) {
this.scene = scene;
this.level = 0;
this.wind = 0;
this.clouds = scene.add.tileSprite(0, 0, W, H, 'di-cloud').setOrigin(0).setDepth(DEPTH.sky).setAlpha(0).setTileScale(2.2);
// Dims the sea, island and pawns, but never the journal or dialogs.
this.dim = scene.add.rectangle(0, 0, RAIL_X - 30, H, 0x061018, 0).setOrigin(0).setDepth(DEPTH.rain - 1);
this.flashRect = scene.add.rectangle(0, 0, W, H, 0xe8f4ff, 0).setOrigin(0).setDepth(DEPTH.flash);
this.rain = scene.add.particles(0, 0, 'di-rain', {
x: { min: -200, max: RAIL_X + 100 }, y: -50,
speedY: { min: 900, max: 1250 }, speedX: { min: -140, max: -80 },
lifespan: 1300, alpha: { start: 0.45, end: 0.2 }, scaleY: { min: 0.6, max: 1.1 },
rotate: 6, frequency: 80, quantity: 1,
}).setDepth(DEPTH.rain);
this.nextStrike = 0;
scene.events.on('update', this.update, this);
scene.events.once('shutdown', () => scene.events.off('update', this.update, this));
}
setLevel(level) {
if (level === this.level) return;
this.level = level;
const k = Math.min(1, (level - 1) / 8);
this.scene.tweens.add({ targets: this.clouds, alpha: 0.15 + k * 0.6, duration: 1800 });
this.scene.tweens.add({ targets: this.dim, fillAlpha: k * 0.32, duration: 1800 });
// drizzle → downpour
const freq = Math.round(Phaser.Math.Linear(70, 6, k));
const qty = 1 + Math.round(k * 3);
this.rain.setFrequency(freq, qty);
this.rain.particleRotate = 6 + k * 10;
if (level >= 6 && !this.nextStrike) this.nextStrike = this.scene.time.now + 2500;
}
update(time, delta) {
const k = Math.min(1, (this.level - 1) / 8);
this.clouds.tilePositionX -= delta * (0.012 + k * 0.03);
this.clouds.tilePositionY += delta * 0.002;
if (this.level >= 6 && this.nextStrike && time >= this.nextStrike) {
this.strike();
const gap = Phaser.Math.Linear(14000, 5000, (this.level - 6) / 3);
this.nextStrike = time + gap * (0.6 + Math.random() * 0.8);
}
}
// A lightning strike: double flicker plus a forked bolt somewhere over the sea.
strike({ x = 80 + Math.random() * (RAIL_X - 200), bright = 0.55 } = {}) {
const s = this.scene;
const g = s.add.graphics().setDepth(DEPTH.flash);
const yEnd = 260 + Math.random() * 520;
const pts = [[x, -10]];
let cx = x, cy = -10;
while (cy < yEnd) { cy += 30 + Math.random() * 40; cx += (Math.random() - 0.5) * 70; pts.push([cx, cy]); }
const draw = (w, c, a) => {
g.lineStyle(w, c, a); g.beginPath(); g.moveTo(pts[0][0], pts[0][1]);
for (const p of pts.slice(1)) g.lineTo(p[0], p[1]);
g.strokePath();
};
draw(12, 0x9fd8ff, 0.18); draw(5, 0xcfeaff, 0.5); draw(2, 0xffffff, 1);
// a fork
const fi = 1 + Math.floor(Math.random() * (pts.length - 2));
let fx = pts[fi][0], fy = pts[fi][1];
g.lineStyle(1.5, 0xffffff, 0.8); g.beginPath(); g.moveTo(fx, fy);
for (let i = 0; i < 4; i++) { fx += (Math.random() < 0.5 ? -1 : 1) * (10 + Math.random() * 25); fy += 25 + Math.random() * 25; g.lineTo(fx, fy); }
g.strokePath();
s.tweens.add({ targets: g, alpha: 0, duration: 420, delay: 90, onComplete: () => g.destroy() });
this.flashRect.setFillStyle(0xe8f4ff, bright);
s.tweens.add({
targets: this.flashRect, fillAlpha: 0, duration: 110,
onComplete: () => {
this.flashRect.setFillStyle(0xe8f4ff, bright * 0.6);
s.tweens.add({ targets: this.flashRect, fillAlpha: 0, duration: 380 });
},
});
Sound.thunder(s, bright);
}
}