-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
67 lines (51 loc) · 1.85 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as PIXI from "pixi.js"
import * as PARTICLES from "pixi-particles"
import {rain} from "./emitter-configs.js"
export default class Weather {
constructor({app}){
this.lightningGap = {min:9000,max:29000};
this.app = app;
this.createAudio();
this.lightning = new PIXI.Sprite(PIXI.Texture.WHITE);
this.lightning.width = this.lightning.height = app.screen.width;
this.lightning.tint = 0xffffff;
this.lightning.alpha = 0.8;
this.flash();
// Defining the rain
const container = new PIXI.ParticleContainer();
container.zIndex = 2;
app.stage.addChild(container);
const emitter = new PARTICLES.Emitter(container,[PIXI.Loader.shared.resources["rain"].texture],rain);
let elapsed = Date.now();
const update = function(){
requestAnimationFrame(update);
let now = Date.now();
emitter.update((now-elapsed)*0.001);
elapsed = now;
}
emitter.emit = true;
update();
}
createAudio(){
this.thunder = new Audio("./assets/thunder.mp3");
this.rain = new Audio("./assets/rain.mp3");
// Loop without glitch from rain
this.rain.addEventListener("timeupdate",function(){
if(this.currentTime> this.duration - 0.2){
this.currentTime = 0;
}
})
}
async flash(){
await new Promise(res => setTimeout(res,this.lightningGap.min+(this.lightningGap.max - this.lightningGap.min)* Math.random()))
this.app.stage.addChild(this.lightning);
if(this.sound) this.thunder.play();
await new Promise(res=> setTimeout(res,200));
this.app.stage.removeChild(this.lightning);
this.flash();
}
enableSound(){
this.sound = true;
this.rain.play();
}
}