Skip to content

Commit a11fe92

Browse files
committed
onlineMode
1 parent f667ba0 commit a11fe92

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

index.html

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.4/umd/popper.min.js"></script>
4848
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
4949
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.19.0/js/mdb.min.js"></script>
50+
<script src="http://wmc.glitch.me/socket.io/socket.io.js"></script>
5051
</div>
5152

5253
</head>
@@ -242,7 +243,7 @@ <h3>Gra zatrzymana</h3>
242243
const cellX = Math.floor(x / cellSize);
243244
const cellY = Math.floor(y / cellSize);
244245
const cellZ = Math.floor(z / cellSize);
245-
return `${cellX}:${cellY}:${cellZ}`
246+
return this.parseVec(cellX,cellY,cellZ)
246247
}
247248
getCellForVoxel(x, y, z) {
248249
return this.cells[this.computeCellId(x, y, z)];
@@ -680,12 +681,25 @@ <h3>Gra zatrzymana</h3>
680681

681682

682683
var canvas,renderer,scene,camera,stats,raycaster,
683-
gameState,stairsGeometry,world,cube,FPC,anvilGeometry;
684+
gameState,stairsGeometry,world,cube,FPC,anvilGeometry,socket;
684685

685686
init()
686687
animate()
687688

688689
function init(){
690+
socket=io.connect("https://wmc.glitch.me/");
691+
socket.on("connect",()=>{
692+
console.log("Połączono z serverem socketio")
693+
socket.emit("siemka","siemka")
694+
695+
})
696+
socket.on("firstLoad",function (v){
697+
console.log("Otrzymano pakiet świata!")
698+
replaceWorld(v)
699+
})
700+
socket.on("blockUpdate",function (block){
701+
world.setVoxel(...block)
702+
})
689703
canvas = document.querySelector('#c');
690704
renderer = new THREE.WebGLRenderer({
691705
canvas
@@ -743,8 +757,8 @@ <h3>Gra zatrzymana</h3>
743757
object.children[1].position.set(26,26,26)
744758
object.children[0].material.map=texturex
745759
object.children[0].material.color=new THREE.Color( 0xffffff );
746-
object.children[1].children[0].children[1].children[2].rotation.x=Math.PI/4
747-
console.log(object.children[1])
760+
object.children[1].children[0].children[1].children[2].children[0].rotation.x=Math.PI/5
761+
// console.log(object.children[1].children[0].children[1].children[2])
748762
scene.add(object)
749763
});
750764

@@ -754,17 +768,17 @@ <h3>Gra zatrzymana</h3>
754768
textureCols: 16,
755769
cellSize: 16,
756770
})
757-
var vari=50
758-
for (let y = 0; y < vari; ++y) {
759-
for (let z = 0; z < vari; ++z) {
760-
for (let x = 0; x < vari; ++x) {
761-
const height = (Math.sin(x / vari * Math.PI * 2) + Math.sin(z / vari * Math.PI * 3)) * (vari / 6) + (vari / 2);
762-
if (y < height) {
763-
world.setVoxel(x, y, z, 2);
764-
}
765-
}
766-
}
767-
}
771+
// var vari=50
772+
// for (let y = 0; y < vari; ++y) {
773+
// for (let z = 0; z < vari; ++z) {
774+
// for (let x = 0; x < vari; ++x) {
775+
// const height = (Math.sin(x / vari * Math.PI * 2) + Math.sin(z / vari * Math.PI * 3)) * (vari / 6) + (vari / 2);
776+
// if (y < height) {
777+
// world.setVoxel(x, y, z, 2);
778+
// }
779+
// }
780+
// }
781+
// }
768782
var inv_bar = new InventoryBar({
769783
boxSize: 60,
770784
boxes: 9,
@@ -855,11 +869,15 @@ <h3>Gra zatrzymana</h3>
855869
const pos = intersection.position.map((v, ndx) => {
856870
return v + intersection.normal[ndx] * (voxelId > 0 ? 0.5 : -0.5);
857871
});
858-
world.setVoxel(...pos, voxelId);
872+
// world.setVoxel(...pos, voxelId);
873+
socket.emit("blockUpdate",[...pos,voxelId])
859874
}
860875
}
861876
})
862877
}
878+
879+
880+
863881
function updateCursor(){
864882
const start = new THREE.Vector3().setFromMatrixPosition(camera.matrixWorld);
865883
const end = new THREE.Vector3().set(0,0, 1).unproject(camera);
@@ -879,6 +897,14 @@ <h3>Gra zatrzymana</h3>
879897
}
880898
}
881899

900+
function replaceWorld(voxels){
901+
Object.keys(voxels).forEach(function (id){
902+
// console.log(id)
903+
if(voxels[id]!=world.getVoxel(...id.split(":"))){
904+
world.setVoxel(...id.split(":"),voxels[id]);
905+
}
906+
})
907+
}
882908

883909
function animate() {
884910
stats.begin();

0 commit comments

Comments
 (0)