forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snow.js
100 lines (71 loc) · 2.58 KB
/
snow.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This example was created by Jens Anders Bakke
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.spritesheet('snowflakes', 'assets/sprites/snowflakes.png', 17, 17);
game.load.spritesheet('snowflakes_large', 'assets/sprites/snowflakes_large.png', 64, 64);
}
var max = 0;
var front_emitter;
var mid_emitter;
var back_emitter;
var update_interval = 4 * 60;
var i = 0;
function create() {
back_emitter = game.add.emitter(game.world.centerX, -32, 600);
back_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
back_emitter.maxParticleScale = 0.6;
back_emitter.minParticleScale = 0.2;
back_emitter.setYSpeed(20, 100);
back_emitter.gravity = 0;
back_emitter.width = game.world.width * 1.5;
back_emitter.minRotation = 0;
back_emitter.maxRotation = 40;
mid_emitter = game.add.emitter(game.world.centerX, -32, 250);
mid_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
mid_emitter.maxParticleScale = 1.2;
mid_emitter.minParticleScale = 0.8;
mid_emitter.setYSpeed(50, 150);
mid_emitter.gravity = 0;
mid_emitter.width = game.world.width * 1.5;
mid_emitter.minRotation = 0;
mid_emitter.maxRotation = 40;
front_emitter = game.add.emitter(game.world.centerX, -32, 50);
front_emitter.makeParticles('snowflakes_large', [0, 1, 2, 3, 4, 5]);
front_emitter.maxParticleScale = 1;
front_emitter.minParticleScale = 0.5;
front_emitter.setYSpeed(100, 200);
front_emitter.gravity = 0;
front_emitter.width = game.world.width * 1.5;
front_emitter.minRotation = 0;
front_emitter.maxRotation = 40;
changeWindDirection();
back_emitter.start(false, 14000, 20);
mid_emitter.start(false, 12000, 40);
front_emitter.start(false, 6000, 1000);
}
function update() {
i++;
if (i === update_interval)
{
changeWindDirection();
update_interval = Math.floor(Math.random() * 20) * 60; // 0 - 20sec @ 60fps
i = 0;
}
}
function changeWindDirection() {
var multi = Math.floor((max + 200) / 4),
frag = (Math.floor(Math.random() * 100) - multi);
max = max + frag;
if (max > 200) max = 150;
if (max < -200) max = -150;
setXSpeed(back_emitter, max);
setXSpeed(mid_emitter, max);
setXSpeed(front_emitter, max);
}
function setXSpeed(emitter, max) {
emitter.setXSpeed(max - 20, max);
emitter.forEachAlive(setParticleXSpeed, this, max);
}
function setParticleXSpeed(particle, max) {
particle.body.velocity.x = max - Math.floor(Math.random() * 30);
}