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

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-12-08 16:53:56 +01:00
import {FullscreenEffect} from "./FullscreenEffect.js";
export class SnowEffect extends FullscreenEffect
{
static NAME = 'snow';
constructor()
{
super();
2024-12-14 12:09:49 +01:00
this.audio = new Audio('js/effects/storm.mp3');
this.audio.loop = true;
2024-12-08 16:53:56 +01:00
this.image = new Image();
2024-12-08 17:02:18 +01:00
this.image.src = 'js/effects/snow.png';
2024-12-08 16:53:56 +01:00
this.offset = 0;
2024-12-14 12:09:49 +01:00
this.speed = 1.0;
}
init()
{
super.init();
this.audio.play();
2024-12-08 16:53:56 +01:00
}
update(timestamp)
{
super.update(timestamp);
2024-12-14 12:09:49 +01:00
this.offsetX = (timestamp * 0.65 * this.speed) % this.image.width;
this.offsetY = (timestamp * 1.5 * this.speed) % this.image.height;
2024-12-08 16:53:56 +01:00
}
2024-12-14 12:09:49 +01:00
render(context, camera)
2024-12-08 16:53:56 +01:00
{
super.render(context);
2024-12-14 12:09:49 +01:00
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,
);
2024-12-08 16:53:56 +01:00
}
}
}
}