Skip to content
Open
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
92 changes: 63 additions & 29 deletions game.html
Original file line number Diff line number Diff line change
Expand Up @@ -822,19 +822,17 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
function generateWorld() {
for (let x = 0; x < WORLD_SIZE; x++) {
for (let z = 0; z < WORLD_SIZE; z++) {
// Create the ground layer with grass blocks
// Create the ground layer with grass blocks at Y=-60
const groundBlock = new THREE.Mesh(geometry, materials.grass);
groundBlock.position.set(x, 0, z);
groundBlock.position.set(x, -60, z);
scene.add(groundBlock);
blocks.set(`${x},0,${z}`, groundBlock);
blocks.set(`${x},-60,${z}`, groundBlock);

// Add dirt layer beneath
const dirtBlock = new THREE.Mesh(geometry, materials.dirt);
dirtBlock.position.set(x, -1, z);
dirtBlock.position.set(x, -61, z);
scene.add(dirtBlock);
blocks.set(`${x},-1,${z}`, dirtBlock);

// Removed the random stone formations code
blocks.set(`${x},-61,${z}`, dirtBlock);
}
}
}
Expand All @@ -843,7 +841,7 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>

// Player controls
const player = {
position: new THREE.Vector3(WORLD_SIZE/2, 2, WORLD_SIZE/2),
position: new THREE.Vector3(WORLD_SIZE/2, -58, WORLD_SIZE/2), // Changed Y from 2 to -58 (2 blocks above ground)
rotation: new THREE.Euler(0, 0, 0, 'YXZ'),
velocity: new THREE.Vector3(),
onGround: false,
Expand Down Expand Up @@ -928,17 +926,37 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
}
});

// Modify the updatePlayer function
// Modify the updatePlayer function to improve straight line movement
function updatePlayer(delta) {
if (isDead) return;

const speed = player.isFlying ? player.flySpeed : 5;
const direction = new THREE.Vector3();

if (keys['KeyW']) direction.z -= 1;
if (keys['KeyS']) direction.z += 1;
if (keys['KeyA']) direction.x -= 1;
if (keys['KeyD']) direction.x += 1;
// Calculate forward/backward direction based on camera rotation
if (keys['KeyW']) {
direction.z -= Math.cos(player.rotation.y);
direction.x -= Math.sin(player.rotation.y);
}
if (keys['KeyS']) {
direction.z += Math.cos(player.rotation.y);
direction.x += Math.sin(player.rotation.y);
}

// Calculate left/right strafing direction (perpendicular to forward)
if (keys['KeyA']) {
direction.z -= Math.cos(player.rotation.y - Math.PI/2);
direction.x -= Math.sin(player.rotation.y - Math.PI/2);
}
if (keys['KeyD']) {
direction.z += Math.cos(player.rotation.y - Math.PI/2);
direction.x += Math.sin(player.rotation.y - Math.PI/2);
}

// Normalize direction only if moving
if (direction.lengthSq() > 0) {
direction.normalize();
}

// Add vertical movement when flying
if (player.isFlying) {
Expand All @@ -949,9 +967,6 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
player.onGround = false;
}

direction.normalize();
direction.applyEuler(new THREE.Euler(0, player.rotation.y, 0));

const newPosition = player.position.clone();

// Try movement on each axis separately
Expand All @@ -965,13 +980,14 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
}
};

// Apply movement speed
player.velocity.x = direction.x * speed;
player.velocity.z = direction.z * speed;

if (player.isFlying) {
player.velocity.y = direction.y * speed;
} else if (!player.onGround) {
player.velocity.y -= 20 * delta; // Only apply gravity when not flying
player.velocity.y -= 20 * delta;
}

tryMove('x');
Expand All @@ -993,7 +1009,7 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
}

// Only check for falling death when not flying
if (!player.isFlying && player.position.y < -10) {
if (!player.isFlying && player.position.y < -80) { // Adjusted to 20 blocks below surface
showDeathScreen();
return;
}
Expand Down Expand Up @@ -1133,7 +1149,7 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
function respawn() {
const deathScreen = document.querySelector('.death-screen');
deathScreen.style.display = 'none';
player.position.set(WORLD_SIZE/2, 2, WORLD_SIZE/2);
player.position.set(WORLD_SIZE/2, -58, WORLD_SIZE/2); // Changed Y from 2 to -58
player.velocity.set(0, 0, 0);
isDead = false;
}
Expand Down Expand Up @@ -1457,10 +1473,30 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
const speed = player.isFlying ? player.flySpeed : 5;
const direction = new THREE.Vector3();

if (keys['KeyW']) direction.z -= 1;
if (keys['KeyS']) direction.z += 1;
if (keys['KeyA']) direction.x -= 1;
if (keys['KeyD']) direction.x += 1;
// Calculate forward/backward direction based on camera rotation
if (keys['KeyW']) {
direction.z -= Math.cos(player.rotation.y);
direction.x -= Math.sin(player.rotation.y);
}
if (keys['KeyS']) {
direction.z += Math.cos(player.rotation.y);
direction.x += Math.sin(player.rotation.y);
}

// Calculate left/right strafing direction (perpendicular to forward)
if (keys['KeyA']) {
direction.z -= Math.cos(player.rotation.y - Math.PI/2);
direction.x -= Math.sin(player.rotation.y - Math.PI/2);
}
if (keys['KeyD']) {
direction.z += Math.cos(player.rotation.y - Math.PI/2);
direction.x += Math.sin(player.rotation.y - Math.PI/2);
}

// Normalize direction only if moving
if (direction.lengthSq() > 0) {
direction.normalize();
}

// Add vertical movement when flying
if (player.isFlying) {
Expand All @@ -1471,9 +1507,6 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
player.onGround = false;
}

direction.normalize();
direction.applyEuler(new THREE.Euler(0, player.rotation.y, 0));

const newPosition = player.position.clone();

// Try movement on each axis separately
Expand All @@ -1487,13 +1520,14 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
}
};

// Apply movement speed
player.velocity.x = direction.x * speed;
player.velocity.z = direction.z * speed;

if (player.isFlying) {
player.velocity.y = direction.y * speed;
} else if (!player.onGround) {
player.velocity.y -= 20 * delta; // Only apply gravity when not flying
player.velocity.y -= 20 * delta;
}

tryMove('x');
Expand All @@ -1515,7 +1549,7 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
}

// Only check for falling death when not flying
if (!player.isFlying && player.position.y < -10) {
if (!player.isFlying && player.position.y < -80) { // Adjusted to 20 blocks below surface
showDeathScreen();
return;
}
Expand Down Expand Up @@ -1588,4 +1622,4 @@ <h2 class="inventory-menu-title">Inventory (Press E to close)</h2>
console.log("Error getting pointer lock:", event);
});
</script>
</body></html>
</body></html>