Skip to content

Commit

Permalink
Merge pull request #3 from dgreenheck/dgreenheck/updates
Browse files Browse the repository at this point in the history
Bug fixes and tweaks
  • Loading branch information
dgreenheck authored Nov 8, 2023
2 parents 1e256e6 + 4348331 commit 42ebe5c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
8 changes: 6 additions & 2 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ function animate() {
sun.position.sub(new THREE.Vector3(-50, -50, -50));
sun.target.position.copy(player.camera.position);

physics.update(dt, player, world);
player.update(world);
// Only update physics when player controls are locked
if (player.controls.isLocked) {
physics.update(dt, player, world);
player.update(world);
}

world.update(player.position);
renderer.render(scene, player.controls.isLocked ? player.camera : orbitCamera);
stats.update();
Expand Down
7 changes: 0 additions & 7 deletions scripts/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ export class Physics {
broadPhase(player, world) {
const candidates = [];

// Get the block containing the center of the camera
const playerBlockPos = {
x: Math.floor(player.position.x),
y: Math.floor(player.position.y),
z: Math.floor(player.position.z)
};

// Get the block extents of the player
const minX = Math.floor(player.position.x - player.radius);
const maxX = Math.ceil(player.position.x + player.radius);
Expand Down
16 changes: 9 additions & 7 deletions scripts/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class Player {
break;
case 'KeyR':
if (this.repeat) break;
this.position.set(32, 32, 32);
this.position.y = 32;
this.velocity.set(0, 0, 0);
break;
case 'Space':
Expand All @@ -299,19 +299,21 @@ export class Player {
*/
onMouseDown(event) {
if (this.controls.isLocked) {
// Is a block selected?
if (this.selectedCoords) {
if (this.activeBlockId !== blocks.empty.id) {
this.world.addBlock(
// If active block is an empty block, then we are in delete mode
if (this.activeBlockId === blocks.empty.id) {
this.world.removeBlock(
this.selectedCoords.x,
this.selectedCoords.y,
this.selectedCoords.z,
this.activeBlockId
this.selectedCoords.z
);
} else {
this.world.removeBlock(
this.world.addBlock(
this.selectedCoords.x,
this.selectedCoords.y,
this.selectedCoords.z
this.selectedCoords.z,
this.activeBlockId
);
}

Expand Down
20 changes: 12 additions & 8 deletions scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,34 @@ export function setupUI(world, player, physics, scene) {
worldFolder.add(scene.fog, 'near', 1, 200, 1).name('Fog Near');
worldFolder.add(scene.fog, 'far', 1, 200, 1).name('Fog Far');

const terrainFolder = worldFolder.addFolder('Terrain');
const terrainFolder = worldFolder.addFolder('Terrain').close();
terrainFolder.add(world.params, 'seed', 0, 10000, 1).name('Seed');
terrainFolder.add(world.params.terrain, 'scale', 10, 100).name('Scale');
terrainFolder.add(world.params.terrain, 'magnitude', 0, 1).name('Magnitude');
terrainFolder.add(world.params.terrain, 'offset', 0, 1).name('Offset');
terrainFolder.add(world.params.terrain, 'offset', 0, 1).name('Ground Offset');
terrainFolder.add(world.params.terrain, 'waterHeight', 0, 16).name('Water Offset');

const resourcesFolder = worldFolder.addFolder('Resources');
const resourcesFolder = worldFolder.addFolder('Resources').close();
for (const resource of resources) {
const resourceFolder = resourcesFolder.addFolder(resource.name);
resourceFolder.add(resource, 'scarcity', 0, 1).name('Scarcity');
const scaleFolder = resourceFolder.addFolder('Scale').close();
scaleFolder.add(resource.scale, 'x', 10, 100).name('X Scale');
scaleFolder.add(resource.scale, 'y', 10, 100).name('Y Scale');
scaleFolder.add(resource.scale, 'z', 10, 100).name('Z Scale');
resourceFolder.add(resource.scale, 'x', 10, 100).name('Scale X');
resourceFolder.add(resource.scale, 'y', 10, 100).name('Scale Y');
resourceFolder.add(resource.scale, 'z', 10, 100).name('Scale Z');
}

const treesFolder = worldFolder.addFolder('Trees');
const treesFolder = worldFolder.addFolder('Trees').close();
treesFolder.add(world.params.trees, 'frequency', 0, 0.1).name('Frequency');
treesFolder.add(world.params.trees.trunkHeight, 'min', 0, 10, 1).name('Min Trunk Height');
treesFolder.add(world.params.trees.trunkHeight, 'max', 0, 10, 1).name('Max Trunk Height');
treesFolder.add(world.params.trees.canopy.size, 'min', 0, 10, 1).name('Min Canopy Size');
treesFolder.add(world.params.trees.canopy.size, 'max', 0, 10, 1).name('Max Canopy Size');
treesFolder.add(world.params.trees.canopy, 'density', 0, 1).name('Canopy Density');

const cloudsFolder = worldFolder.addFolder('Clouds').close();
cloudsFolder.add(world.params.clouds, 'density', 0, 1).name('Density');
cloudsFolder.add(world.params.clouds, 'scale', 1, 100, 1).name('Scale');

worldFolder.onFinishChange((event) => {
world.regenerate(player);
});
Expand Down
3 changes: 3 additions & 0 deletions scripts/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export class World extends THREE.Group {
const coords = this.worldToChunkCoords(x, y, z);
const chunk = this.getChunk(coords.chunk.x, coords.chunk.z);

// Don't allow removing the first layer of blocks
if (coords.block.y === 0) return;

if (chunk) {
chunk.removeBlock(coords.block.x, coords.block.y, coords.block.z);

Expand Down

0 comments on commit 42ebe5c

Please sign in to comment.