Allow blocks to update neighboring blocks

This commit is contained in:
Holly Stubbs 2023-08-12 22:06:16 +01:00
parent 3351888f19
commit c80dcd2d4f
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 40 additions and 2 deletions

View file

@ -70,7 +70,7 @@ export class Console {
Console.QUEUED_FOR_LOG = "";
Console.logFileWriteStream.write(strRef);
}
}, 1000 * 10);
}, 5000);
public static printInfo(s:string) : void {
log(LogTag.INFO, s);

View file

@ -139,7 +139,7 @@ export class MPClient {
// Started digging
} else if (packet.status === 2) {
if (this.entity.world.getBlockId(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z) != 0) {
this.entity.world.setBlockWithMetadata(0, 0, this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, true);
this.entity.world.setBlockWithNotify(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, 0);
}
}
}

View file

@ -1,6 +1,7 @@
import { FunkyArray } from "../funkyArray";
import { Chunk } from "./Chunk";
import { WorldSaveManager } from "./WorldSaveManager";
import { Block } from "./blocks/Block";
import { IEntity } from "./entities/IEntity";
import { Player } from "./entities/Player";
//import { FlatGenerator } from "./generators/Flat";
@ -159,6 +160,32 @@ export class World {
}
}
public setBlockWithNotify(x:number, y:number, z:number, blockId:number) {
this.setBlock(blockId, x, y, z, true);
this.notifyNeighborBlocksOfChange(x, y, z, blockId);
}
public setBlockAndMetadataWithNotify(x:number, y:number, z:number, blockId:number, metadata:number) {
this.setBlockWithMetadata(blockId, metadata, x, y, z, true);
this.notifyNeighborBlocksOfChange(x, y, z, blockId);
}
public notifyNeighborBlocksOfChange(x:number, y:number, z:number, blockId:number) {
this.notifyNeighborBlockOfChange(x - 1, y, z, blockId);
this.notifyNeighborBlockOfChange(x + 1, y, z, blockId);
this.notifyNeighborBlockOfChange(x, y - 1, z, blockId);
this.notifyNeighborBlockOfChange(x, y + 1, z, blockId);
this.notifyNeighborBlockOfChange(x, y, z - 1, blockId);
this.notifyNeighborBlockOfChange(x, y, z + 1, blockId);
}
private notifyNeighborBlockOfChange(x:number, y:number, z:number, blockId:number) {
const block = Block.blocks[this.getBlockId(x, y, z)];
if (block != null && block.blockId !== 0) {
block.neighborBlockChange(this, x, y, z, block.blockId);
}
}
public sendToNearbyClients(sentFrom:IEntity, buffer:Buffer) {
this.players.forEach(player => {
if (sentFrom.entityId !== player.entityId && Math.abs(sentFrom.distanceTo(player)) < World.ENTITY_MAX_SEND_DISTANCE) {

View file

@ -1,3 +1,5 @@
import { World } from "../World";
export class Block {
public readonly blockId:number;
public static readonly blocks:Array<Block> = new Array<Block>();
@ -37,6 +39,15 @@ export class Block {
return this;
}
public neighborBlockChange(world:World, x:number, y:number, z:number, blockId:number) {
if (blockId === Block.flowerDandelion.blockId || blockId === Block.flowerRose.blockId || blockId === Block.tallGrass.blockId) {
const block = world.getBlockId(x, y - 1, z);
if (block !== Block.grass.blockId && block !== Block.dirt.blockId) {
world.setBlockWithNotify(x, y, z, 0);
}
}
}
// Define statics here
static readonly stone = new Block(1).setBlockName("Stone");
static readonly grass = new Block(2).setBlockName("Grass");