-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
130 lines (110 loc) · 4.37 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tetris BabylonJS</title>
<script src="js/babylon.js"></script>
<style>
html,body, #canvas{
width:100%;
height:100%;
padding:0;
margin:0;
overflow:hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var engine = new BABYLON.Engine(canvas,true);
var Scene = new BABYLON.Scene(engine);
var Camera = new BABYLON.ArcRotateCamera("Camera",0,0,20, new BABYLON.Vector3(0,0,0), Scene);
BABYLON.SceneLoader.ImportMeshAsync(["T","Z","S","O","L","I","J"], "blocks/", "TetrisBlocks.babylon",Scene).then((result) =>{
const T = Scene.getMeshByName("T");
const Z = Scene.getMeshByName("Z");
const S = Scene.getMeshByName("S");
const O = Scene.getMeshByName("O");
const L = Scene.getMeshByName("L");
const I = Scene.getMeshByName("I");
const J = Scene.getMeshByName("J");
T.setEnabled(true);
Z.setEnabled(false);
S.setEnabled(false);
O.setEnabled(false);
L.setEnabled(false);
I.setEnabled(false);
J.setEnabled(false);
const shapes = ["T","Z","S","O","L","I","J"];
T.position.x = -6;
T.position.z = 0;
const T1 = T.clone("T2");
T1.position.x = 1
const Tetris = [];
Tetris.push(T, 0, 0, 0);
Scene.onKeyboardObservable.add((kbInfo) => {
switch (kbInfo.type) {
case BABYLON.KeyboardEventTypes.KEYDOWN:
switch (kbInfo.event.key) {
case "a":
case "A":
T.rotation.y -= 90;
break
case "d":
case "D":
T.rotation.y += 90;
break
case "w":
case "W":
T.position.x -= 0.1;
break
case "s":
case "S":
T.position.x += 0.1;
break
}
break;
}
});
if (T.intersenctMesh(ground1, true)){
T.inputs.clear();
const random = Math.floor(Math.random() * shapes.length);
shapes[random].clone("shapes");
}
engine.runRenderLoop(function(){
T.position.z -= 1;
Scene.render();
});
window.addEventListener("resize", function(){
engine.resize();
});
}); //Name of the model loads one model
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), Scene);
light.specular = new BABYLON.Color3(0,0,0);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
var points = 0;
var gravity = 0.1;
var ground = BABYLON.Mesh.CreateBox("ground", 50, Scene);
ground.position.y = -10;
ground.scaling.y = 0.1;
var ground1 = BABYLON.Mesh.CreateBox("ground1", 20, Scene);
ground1.position.x= 20;
/*var box = BABYLON.Mesh.CreateBox("Box", 1, Scene);
box.position.x = -6;*/
Scene.activeCamera.attachControl(canvas);
var material = new BABYLON.StandardMaterial(Scene);
material.alpha = 1;
material.diffuseColor = new BABYLON.Color3(0,0,0);
ground1.material = material;
engine.runRenderLoop(function(){
Scene.render();
});
window.addEventListener("resize", function(){
engine.resize();
T,L,J,O,I,Z,S.position.z -= 1;
});
</script>
</body>
</html>