-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·166 lines (153 loc) · 4.14 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
60,
window.innerWidth / window.innerHeight,
1,
10000
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xaaaaaa, 1);
const canvas = $('#canvascontainer').append(renderer.domElement);
let distance = 4;
let started = false;
let percent = 0;
camera.position.set(0, 5, distance);
camera.rotation.x -= 0.75;
let scoreSubmitted = false;
let level = 1;
let data;
let reqId;
$.getJSON('levels.json', d => {
data = d;
loadLevel(level);
$('#play').show();
$('#play').click(start);
$('#next').show();
$('#score').hide();
$('#level').html('Level ' + level);
reqId = requestAnimationFrame(render);
console.clear();
console.log(
"If you're interested to look into the source code, it's avaliable here: https://github.com/gldanoob/webgl-rolling-sky"
);
});
//start function
function start(e) {
e.preventDefault();
if (!started) {
started = true;
ball.speed.z = -0.15;
$('#main').fadeOut(300);
$('#name').hide();
reset();
world.forEach(v => {
if (v instanceof Bouncer) {
v.mesh.position.y = 0;
}
});
$('#main').css('pointer-events', 'none');
}
}
function reset() {
ball.landed = true;
ball.tmpZ = 0;
camera.position.set(0, 5, distance);
ball.mesh.position.set(0, 0.6, 0);
ball.speed.y = 0;
ball.count2Lose = 0;
}
function nextLevel() {
percent = 0;
while (
(selectedObject = scene.getObjectByName('level component')) !== undefined
) {
scene.remove(selectedObject);
}
world.length = 0;
level++;
loadLevel(level);
reset();
$('#score').hide();
$('#level').show();
$('#level').html('Level ' + level);
$('#prev').show();
$('#retry').hide();
$('#play').show();
if (level == data.length) {
$('#next').hide();
}
}
function prevLevel() {
percent = 0;
while (
(selectedObject = scene.getObjectByName('level component')) != undefined
) {
scene.remove(selectedObject);
}
world.length = 0;
level--;
loadLevel(level);
reset();
$('#score').hide();
$('#level').show();
$('#level').html('Level ' + level);
$('#next').show();
$('#retry').hide();
$('#play').show();
if (level == 1) {
$('#prev').hide();
}
}
const light = new THREE.HemisphereLight(0xeeeeee, 0x777777);
scene.add(light);
const world = [];
function loadLevel(level) {
const index = level - 1;
renderer.setClearColor(parseInt(data[index].background));
ball.mesh.material.color.setHex(parseInt(data[index].ball));
for (var i in data[index].data) {
for (var j in data[index].data[i]) {
switch (data[index].data[i][j]) {
case 1:
world.push(new Mat(j - 2, -i, data[index].mat));
break;
case 2:
world.push(new Bouncer(j - 2, -i, data[index].bouncer));
break;
case 3:
world.push(new Mat(j - 2, -i, data[index].mat));
world.push(new Obstacle(j - 2, -i, data[index].obstacle));
}
}
}
}
var ball = new Ball();
keystate = [];
time = 0;
function render(timestamp) {
dt = timestamp - time;
time = timestamp;
renderer.render(scene, camera);
ball.update(dt * 3 / 50);
percent = Math.ceil(
Math.abs(ball.mesh.position.z) / data[level - 1].data.length * 100
);
percent = percent > 100 ? 100 : percent;
$('#percent').html(percent + '%');
if (keystate[37]) ball.mesh.position.x -= 0.15;
if (keystate[39]) ball.mesh.position.x += 0.15;
reqId = requestAnimationFrame(render);
};
//controls
function gameover() {
started = false;
ball.speed.z = 0;
$('#main').fadeIn(500);
$('#retry').show();
$('#retry').click(start);
$('#level').hide();
$('#score').show();
$('#score').html($('#percent').html());
$('#main').css('pointer-events', 'auto');
}