mr-crocs-adventures/js/effects/RainEffect.js

52 lines
1.1 KiB
JavaScript

import {FullscreenEffect} from "./FullscreenEffect.js";
export class RainEffect extends FullscreenEffect
{
static NAME = 'rain';
constructor()
{
super();
this.image = new Image();
this.image.src = 'js/effects/rain.png';
this.sound = new Audio('js/effects/rain.mp3');
this.sound.loop = true;
this.offset = 0;
this.speed = 1.3;
}
init()
{
super.init();
this.sound.play();
}
update(timestamp)
{
super.update(timestamp);
this.offsetX = (timestamp * 0.65 * this.speed) % this.image.width;
this.offsetY = (timestamp * 1.5 * this.speed) % this.image.height;
}
render(context, camera)
{
super.render(context, camera);
const cameraX = -1 * (camera.position.x % this.image.width);
const cameraY = -1 * (camera.position.y % this.image.height);
for (let y = -1; y < Math.ceil(context.canvas.height / this.image.height) + 1; y++) {
for (let x = -1; x < Math.ceil(context.canvas.width / this.image.width) + 1; x++) {
context.drawImage(
this.image,
cameraX + this.offsetX + this.image.width * x,
cameraY + this.offsetY + this.image.height * y,
);
}
}
}
}