Skip to content

Commit 8485134

Browse files
improved movement and spawn
1 parent ed2bc3c commit 8485134

4 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/block_manager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,12 @@ class ChunkManager {
692692

693693
return chunk.data[lx][wy][lz];
694694
}
695+
696+
worldLoaded() {
697+
const allChunks = SETTINGS.RENDER_DISTANCE * 2;
698+
console.log(`Loaded chunks: ${this.chunks.size}/${(allChunks + 1) ** 2}`);
699+
return this.chunks.size >= allChunks;
700+
}
695701
}
696702

697703
function getAffectedChunks(wx, wy, wz) {

src/game.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const dayCycle = new ATMOSPHERE.DayCycle(scene);
2424
const chunkManager = new BLOCK_MANAGER.ChunkManager(scene, SETTINGS.SEED);
2525
chunkManager.loadWorld();
2626

27-
const spawnDist = 16;
28-
const spawnPosition = BLOCK_MANAGER.TopPositionInWorld(-spawnDist, spawnDist, -spawnDist, spawnDist, chunkManager);
27+
const spawnPosition = BLOCK_MANAGER.TopPositionInWorld(-SETTINGS.SPAWN_DISTANCE, SETTINGS.SPAWN_DISTANCE, -SETTINGS.SPAWN_DISTANCE, SETTINGS.SPAWN_DISTANCE, chunkManager);
2928
chunkManager.update(spawnPosition);
3029

3130
let player = new PLAYER_MANAGER.Player("Player1");
@@ -58,7 +57,6 @@ function RenderFrame(time) {
5857

5958
if (currentChunkX !== lastChunkX || currentChunkZ !== lastChunkZ) {
6059
chunkManager.update(player.position);
61-
player.physicsEnabled = true;
6260
}
6361
chunkManager.updateDirtyChunks();
6462

src/player_manager.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Player {
2929
this.lastWPressTime = 0;
3030
this.wWasDown = false;
3131

32-
this.physicsEnabled = false;
32+
this.enablePhysics = true;
3333

3434
this.camera = new THREE.PerspectiveCamera(75, SETTINGS.ASPECT_RATIO, 0.1, SETTINGS.CLIPPING_DISTANCE);
3535
this.camera.rotation.order = "YXZ";
@@ -42,7 +42,7 @@ class Player {
4242
moveX(delta, world) {
4343
const newX = this.position.x + this.velocity.x * delta;
4444

45-
if (!isColliding(world, newX, this.position.y, this.position.z, this.width, this.height) && this.physicsEnabled) {
45+
if (!isColliding(world, newX, this.position.y, this.position.z, this.width, this.height) && this.enablePhysics) {
4646
if (this.isCrouching && this.isGrounded && !hasGroundBelow(world, newX, this.position.y, this.position.z, this.width)) {
4747
// nothing
4848
} else {
@@ -54,7 +54,7 @@ class Player {
5454
moveY(delta, world) {
5555
const newY = this.position.y + this.velocity.y * delta;
5656

57-
if (!isColliding(world, this.position.x, newY, this.position.z, this.width, this.height) && this.position.y > 0 && this.physicsEnabled) {
57+
if (!isColliding(world, this.position.x, newY, this.position.z, this.width, this.height) && this.position.y > 0 && this.enablePhysics) {
5858
this.position.y = newY;
5959
} else {
6060
if (this.velocity.y < 0) {
@@ -68,7 +68,7 @@ class Player {
6868
moveZ(delta, world) {
6969
const newZ = this.position.z + this.velocity.z * delta;
7070

71-
if (!isColliding(world, this.position.x, this.position.y, newZ, this.width, this.height) && this.physicsEnabled) {
71+
if (!isColliding(world, this.position.x, this.position.y, newZ, this.width, this.height) && this.enablePhysics) {
7272
if (this.isCrouching && this.isGrounded && !hasGroundBelow(world, this.position.x, this.position.y, newZ, this.width)) {
7373
// nothing
7474
} else {
@@ -131,7 +131,8 @@ class Player {
131131
}
132132

133133
applyGravity(delta) {
134-
this.velocity.y -= this.gravity * delta;
134+
if (this.enablePhysics)
135+
this.velocity.y -= this.gravity * delta;
135136
}
136137

137138
handleJump() {
@@ -143,7 +144,11 @@ class Player {
143144

144145
update(delta, world) {
145146
INPUT_MANAGER.updateRotation(this.rotation);
146-
147+
148+
if (this.position.y < -5) {
149+
this.position = BLOCK_MANAGER.TopPositionInWorld(-SETTINGS.SPAWN_DISTANCE, SETTINGS.SPAWN_DISTANCE, -SETTINGS.SPAWN_DISTANCE, SETTINGS.SPAWN_DISTANCE, world);
150+
}
151+
147152
this.updateMovement(delta);
148153
this.handleJump();
149154
this.applyGravity(delta);

src/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ export const SETTINGS = {
2626
BLOCK_REACH: 5,
2727
TILE_SIZE: 128,
2828
SEED: 12345,
29+
SPAWN_DISTANCE: 16,
2930
}

0 commit comments

Comments
 (0)