Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient data structures #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/client/src/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ planeFrontGeometry.rotateY(Math.PI);
planeFrontGeometry.translate(0.5, 0.5, 0);

export default class Block extends BlockData {
render(renderTop, renderBottom, renderLeft, renderRight, renderFront, renderBack) {
if (this.blockType === 0 || this.blockType === 1) {
static render(block, position, renderTop, renderBottom, renderLeft, renderRight, renderFront, renderBack) {
const blockType = Block.getBlockType(block);
if (blockType.transparent) {
return [];
}

const geometries = [];

const [x, y, z] = this.position;
const [x, y, z] = position;

const matrix = new THREE.Matrix4();

Expand All @@ -43,27 +44,27 @@ export default class Block extends BlockData {
z,
);

if (!this.refTop && renderTop) {
if (renderTop) {
geometries.push(planeTopGeometry.clone().applyMatrix(matrix));
}

if (!this.refBottom && renderBottom) {
if (renderBottom) {
geometries.push(planeBottomGeometry.clone().applyMatrix(matrix));
}

if (!this.refRight && renderRight) {
if (renderRight) {
geometries.push(planeRightGeometry.clone().applyMatrix(matrix));
}

if (!this.refLeft && renderLeft) {
if (renderLeft) {
geometries.push(planeLeftGeometry.clone().applyMatrix(matrix));
}

if (!this.refBack && renderBack) {
if (renderBack) {
geometries.push(planeBackGeometry.clone().applyMatrix(matrix));
}

if (!this.refFront && renderFront) {
if (renderFront) {
geometries.push(planeFrontGeometry.clone().applyMatrix(matrix));
}

Expand Down
23 changes: 13 additions & 10 deletions packages/client/src/Chunk.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import { BlockChunkData } from 'game-objects';
import Block from './Block';

export default class Chunk extends BlockChunkData {
hasUpdated() {
return false;
}

render() {
render(world) {
const geometries = [];

this.forEachBlock((block) => {
this.forEachBlock((block, position) => {
if (!block) return;
// check in all dimensions if there is null
const [x, y, z] = block.position;
const [x, y, z] = position;
// console.log(block.position);

const renderTop = this.isBlockTransparent(x, y + 1, z);
const renderBottom = this.isBlockTransparent(x, y - 1, z);
const renderLeft = this.isBlockTransparent(x + 1, y, z);
const renderRight = this.isBlockTransparent(x - 1, y, z);
const renderFront = this.isBlockTransparent(x, y, z - 1);
const renderBack = this.isBlockTransparent(x, y, z + 1);
const renderTop = world.isBlockPassable(x, y + 1, z);
const renderBottom = world.isBlockPassable(x, y - 1, z);
const renderLeft = world.isBlockPassable(x + 1, y, z);
const renderRight = world.isBlockPassable(x - 1, y, z);
const renderFront = world.isBlockPassable(x, y, z - 1);
const renderBack = world.isBlockPassable(x, y, z + 1);

// ok
if (
true || renderTop || renderBottom || renderLeft || renderRight || renderFront || renderBack
) {
geometries
.push(...block.render(
.push(...Block.render(
block,
[x, y, z],
renderTop,
renderBottom,
renderLeft,
Expand Down
50 changes: 25 additions & 25 deletions packages/client/src/MainPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class MainPlayer extends Player {
}

this.socket.emit(EVENTS.BLOCK_DELETE, {
position: destination.block.position,
position: [destination.x, destination.y, destination.z],
});
}

Expand Down Expand Up @@ -125,58 +125,58 @@ export default class MainPlayer extends Player {
let collisionArray = [];
const { x, y, z } = this.ref.object3D.position;
// collisions
const floorOne = world.getBlock(Math.floor(x - 0.4), Math.floor(y - 1.3), Math.floor(z - 0.4));
const floorTwo = world.getBlock(Math.ceil(x - 0.4), Math.floor(y - 1.3), Math.floor(z - 0.4));
const floorThree = world.getBlock(Math.floor(x - 0.4), Math.floor(y - 1.3), Math.ceil(z - 0.4));
const floorFour = world.getBlock(Math.ceil(x - 0.4), Math.floor(y - 1.3), Math.ceil(z - 0.4));
const onFloor = !(floorOne == null
&& floorTwo == null
&& floorThree == null
&& floorFour == null);
const floorOne = world.isBlockPassable(Math.floor(x - 0.4), Math.floor(y - 1.3), Math.floor(z - 0.4));
const floorTwo = world.isBlockPassable(Math.ceil(x - 0.4), Math.floor(y - 1.3), Math.floor(z - 0.4));
const floorThree = world.isBlockPassable(Math.floor(x - 0.4), Math.floor(y - 1.3), Math.ceil(z - 0.4));
const floorFour = world.isBlockPassable(Math.ceil(x - 0.4), Math.floor(y - 1.3), Math.ceil(z - 0.4));
const onFloor = !(floorOne
&& floorTwo
&& floorThree
&& floorFour);

if (onFloor && velocity.y <= 0) {
velocity.y = 0;
}


if (velocity.x > 0) {
collisionArray.push(world.getBlock(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3), Math.round(z - 0.5)));
collisionArray.push(world.getBlock(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3) + 1, Math.round(z - 0.5)));
collisionArray.push(world.getBlock(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3) + 2, Math.round(z - 0.5)));
const filteredCollisions = collisionArray.filter(element => element !== null);
collisionArray.push(world.isBlockPassable(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3), Math.round(z - 0.5)));
collisionArray.push(world.isBlockPassable(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3) + 1, Math.round(z - 0.5)));
collisionArray.push(world.isBlockPassable(Math.floor(x - 0.4) + 1, Math.floor(y - 0.3) + 2, Math.round(z - 0.5)));
const filteredCollisions = collisionArray.filter(element => element !== true);
if (filteredCollisions.length !== 0) {
velocity.x = 0;
collisionArray = [];
}
}

if (velocity.x < 0) {
collisionArray.push(world.getBlock(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3), Math.round(z - 0.5)));
collisionArray.push(world.getBlock(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3) + 1, Math.round(z - 0.5)));
collisionArray.push(world.getBlock(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3) + 2, Math.round(z - 0.5)));
const filteredCollisions = collisionArray.filter(element => element !== null);
collisionArray.push(world.isBlockPassable(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3), Math.round(z - 0.5)));
collisionArray.push(world.isBlockPassable(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3) + 1, Math.round(z - 0.5)));
collisionArray.push(world.isBlockPassable(Math.ceil(x - 0.4) - 1, Math.floor(y - 0.3) + 2, Math.round(z - 0.5)));
const filteredCollisions = collisionArray.filter(element => element !== true);
if (filteredCollisions.length !== 0) {
velocity.x = 0;
collisionArray = [];
}
}

if (velocity.z > 0) {
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3), Math.floor(z - 0.5) + 1));
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3) + 1, Math.floor(z - 0.5) + 1));
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3) + 2, Math.floor(z - 0.5) + 1));
const filteredCollisions = collisionArray.filter(element => element !== null);
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3), Math.floor(z - 0.5) + 1));
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3) + 1, Math.floor(z - 0.5) + 1));
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3) + 2, Math.floor(z - 0.5) + 1));
const filteredCollisions = collisionArray.filter(element => element !== true);
if (filteredCollisions.length !== 0) {
velocity.z = 0;
collisionArray = [];
}
}

if (velocity.z < 0) {
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3), Math.ceil(z - 0.5) - 1));
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3) + 1, Math.ceil(z - 0.5) - 1));
collisionArray.push(world.getBlock(Math.round(x - 0.4), Math.floor(y - 0.3) + 2, Math.ceil(z - 0.5) - 1));
const filteredCollisions = collisionArray.filter(element => element !== null);
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3), Math.ceil(z - 0.5) - 1));
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3) + 1, Math.ceil(z - 0.5) - 1));
collisionArray.push(world.isBlockPassable(Math.round(x - 0.4), Math.floor(y - 0.3) + 2, Math.ceil(z - 0.5) - 1));
const filteredCollisions = collisionArray.filter(element => element !== true);
if (filteredCollisions.length !== 0) {
velocity.z = 0;
collisionArray = [];
Expand Down
26 changes: 10 additions & 16 deletions packages/client/src/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,7 @@ export default class World {
this.chunksToUnMount.push(this.map.getChunk(x, z));
}

const blocks = chunk.blocks
.map(
(i, x) => i
.map(
(j, y) => j
.map((block, z) => {
if (block !== null) {
return new Block(block);
}
return null;
}),
),
);

this.map.addChunk(new Chunk({ ...chunk, blocks }));
this.map.addChunk(new Chunk({ ...chunk, BlockData: Block }));
}

getBlock(x, y, z) {
Expand All @@ -72,6 +58,14 @@ export default class World {
return this.map.blockChunks[chunkX][chunkZ].getBlock(x, y, z);
}

isBlockPassable(x, y, z) {
const block = this.getBlock(x, y, z);

const type = Block.getBlockType(block);

return type.passable;
}

update(delta) {
// this.forEachBlock(block => block.update(delta));
}
Expand All @@ -93,7 +87,7 @@ export default class World {
scene.object3D.remove(oldMesh);
}
if (!oldMesh || updated) {
const newMesh = chunk.render();
const newMesh = chunk.render(this);
if (!newMesh || newMesh.length === 0) return;
this.chunkToMesh.set(chunk, newMesh);
scene.object3D.add(newMesh);
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async function go() {
? `http://${window.location.hostname}:3000`
: '/',
{
transports: ['websocket']
}
transports: ['websocket'],
},
);

// world instance, contains all chunks and blocks
Expand Down
8 changes: 4 additions & 4 deletions packages/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
{ "id": 0, "name": "Green", "color": "green" },
{ "id": 1, "name": "Blue", "color": "blue" }
],
"CHUNK_SIZE": 32,
"CHUNK_SIZE": 16,
"GROUND_HEIGHT": 3,
"MAP_HEIGHT": 256,
"SKYBOX_COLOR": "cornflowerblue",
"BLOCK_SIZE": 1,
"BLOCK_TYPES": [
{ "id": 0, "name": "Unknown", "health": -1, "color": "pink", "passable": true },
{ "id": 1, "name": "Air", "health": -1, "color": "transparent", "passable": true },
{ "id": 2, "name": "Stone", "health": 3, "color": "grey", "passable": false }
{ "id": 0, "name": "Air", "health": -1, "color": "transparent", "passable": true, "transparent": true },
{ "id": 1, "name": "Unknown", "health": -1, "color": "pink", "passable": true, "transparent": false },
{ "id": 2, "name": "Stone", "health": 3, "color": "grey", "passable": false, "transparent": false }
],
"EVENTS": {
"WORLD_CREATE": 1,
Expand Down
Loading