Skip to content
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
74 changes: 71 additions & 3 deletions src/minecraft/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class Config {

public static CHUNK_SIZE: number = 64.0;

// How far you can select a cube from the player
public static SELECT_RADIUS: number = 3.0;

// Number of chunks to render outside of the player's chunk
// 1 --> 3 x 3, 2 -> 5 x 5, ... n -> 2n+1 x 2n+1
public static BORDER_CHUNKS: number = 1.0;
Expand Down Expand Up @@ -51,6 +54,13 @@ export class MinecraftAnimation extends CanvasAnimation {
private cubeGeometry: Cube;
private blankCubeRenderPass: RenderPass;

/* Cube Selection */
private selectedCubeF32: Float32Array;
private highlightSelected: boolean;
public highlightOn: boolean;
private removeCube: boolean;
private modificationLog: number[][];

/* Global Rendering Info */
private lightPosition: Vec4;
private backgroundColor: Vec4;
Expand Down Expand Up @@ -91,6 +101,9 @@ export class MinecraftAnimation extends CanvasAnimation {

this.lightPosition = new Vec4([-1000, 1000, -1000, 1]);
this.backgroundColor = new Vec4([0.0, 0.37254903, 0.37254903, 1.0]);
this.highlightSelected = false;
this.highlightOn = false;
this.modificationLog = [];
}

private chunkKey(x: number, z: number): string {
Expand Down Expand Up @@ -126,6 +139,9 @@ export class MinecraftAnimation extends CanvasAnimation {
newChunks[key] = this.cache[key];
} else {
newChunks[key] = new Chunk(xCoords[i], zCoords[i], Config.CHUNK_SIZE);

// When loading chunks, we need to update the chunk's state based on modified blocks
newChunks[key].updateFromLog(this.modificationLog);
}
if (i == Math.floor(((1 + 2 * Config.BORDER_CHUNKS) ** 2) / 2)) {
this.chunk = newChunks[key];
Expand All @@ -152,7 +168,6 @@ export class MinecraftAnimation extends CanvasAnimation {
Math.abs(cameraLocation.x) % Config.CHUNK_SIZE - Config.CHUNK_SIZE / 2);
const zMod = Math.abs(
Math.abs(cameraLocation.z) % Config.CHUNK_SIZE - Config.CHUNK_SIZE / 2);
// console.log(cameraLocation.x + " " + cameraLocation.z);
if (xMod <= 2.0) {
candidates.push(
this.chunks[this.chunkKey(center.x + Config.CHUNK_SIZE, center.z)]);
Expand Down Expand Up @@ -247,7 +262,7 @@ export class MinecraftAnimation extends CanvasAnimation {
if (!Config.CREATIVE_MODE) {
let position: Vec3 = new Vec3(this.playerPosition.xyz);
let chunks: Chunk[] = this.collisionChunks(this.playerPosition);
position.add(this.gui.walkDir());
position.add(this.gui.walkDir().scale(GUI.walkSpeed));
if (!position.equals(this.playerPosition)) {
let safe: boolean = true;
for (let i = 0; i < chunks.length; i++) {
Expand Down Expand Up @@ -288,7 +303,7 @@ export class MinecraftAnimation extends CanvasAnimation {
}
this.gui.getCamera().setPos(this.playerPosition);
} else {
this.playerPosition.add(this.gui.walkDir());
this.playerPosition.add(this.gui.walkDir().scale(GUI.walkSpeed));
this.gui.getCamera().setPos(this.playerPosition);
this.gravityTime = Date.now();
}
Expand Down Expand Up @@ -336,6 +351,12 @@ export class MinecraftAnimation extends CanvasAnimation {
'aOffset', this.chunks[chunk].cubePositions());
this.blankCubeRenderPass.drawInstanced(this.chunks[chunk].numCubes());
}
// We draw a sillouette of the selected cube on top of everything else.
if (this.highlightSelected && this.highlightOn) {
this.blankCubeRenderPass.updateAttributeBuffer(
'aOffset', this.selectedCubeF32);
this.blankCubeRenderPass.drawInstanced(1);
}
}

public getGUI(): GUI {
Expand All @@ -349,6 +370,53 @@ export class MinecraftAnimation extends CanvasAnimation {
this.verticalVelocity = new Vec3([0.0, Config.JUMP_VELOCITY, 0.0]);
}
}

public updateSelectedCube(selectedCube: Vec3) {
this.selectedCubeF32 = new Float32Array(4 * 1);
this.selectedCubeF32[0] = selectedCube.x;
this.selectedCubeF32[1] = selectedCube.y;
this.selectedCubeF32[2] = selectedCube.z;
this.selectedCubeF32[3] = 2.0; // We use 2.0 to indicate that the cube should be highlighted, as seen in the shader

// We globally determine the block selected and if it should be removed (ie with this.removeCube)
let isRemovingCube = false;
for (let chunk in this.chunks) {
isRemovingCube = isRemovingCube || this.chunks[chunk].updateSelected(this.highlightOn, selectedCube);
}
this.removeCube = isRemovingCube;
this.highlightSelected = true;
}

public modifyLandscape(selectedCube: Vec3) {
const x = selectedCube.x;
const y = selectedCube.y;
const z = selectedCube.z;
// See if the selected cube is already in the modification log
let newLog: number[][] = [];
let cubeInLog = false;
for (let i = 0; i < this.modificationLog.length; ++i) {
// Checks if the cube is already in the log
if (this.modificationLog[i][0] == x &&
this.modificationLog[i][1] == y &&
this.modificationLog[i][2] == z) {
cubeInLog = true;
} else {
newLog.push(this.modificationLog[i]);
}
}
// Remove the cube if it already exists (reverting to original chunk),
// otherwise add it
if (!cubeInLog) {
newLog.push([x, y, z, this.removeCube ? -1 : 1]);
}

// Update log and all chunks
this.modificationLog = newLog;
for (let chunk in this.chunks) {
this.chunks[chunk].updateLandscape(this.removeCube, selectedCube);
}
this.removeCube = !this.removeCube;
}
}

export function initializeCanvas(): void {
Expand Down
Loading