implement block types

This commit is contained in:
LabyStudio
2022-01-31 22:30:00 +01:00
parent 33998053c1
commit 02c36a79ba
14 changed files with 178 additions and 21 deletions
@@ -0,0 +1,58 @@
window.Block = class {
static blocks = [];
static create() {
Block.STONE = new BlockStone(1, 0);
Block.GRASS = new BlockGrass(2, 1);
Block.DIRT = new BlockDirt(3, 2);
Block.LOG = new BlockLog(17, 4);
Block.LEAVE = new BlockLeave(18, 6);
Block.WATER = new BlockWater(9, 7);
Block.SAND = new BlockSand(12, 8)
}
constructor(id, textureSlotId = id) {
this.id = id;
this.textureSlotId = textureSlotId;
this.boundingBox = new BoundingBox(0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
// Register block
Block.blocks[id] = this;
}
getId() {
return this.id;
}
getTextureForFace(face) {
return this.textureSlotId;
}
isTransparent() {
return this.getOpacity() < 1.0;
}
shouldRenderFace(world, x, y, z, face) {
let typeId = world.getBlockAt(x + face.x, y + face.y, z + face.z);
return typeId === 0 || Block.getById(typeId).isTransparent();
}
isSolid() {
return true;
}
getOpacity() {
return 1.0;
}
getBoundingBox(world, x, y, z) {
return this.boundingBox;
}
static getById(typeId) {
return Block.blocks[typeId];
}
}
@@ -0,0 +1,7 @@
window.BlockDirt = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
}
@@ -0,0 +1,18 @@
window.BlockGrass = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
getTextureForFace(face) {
switch (face) {
case EnumBlockFace.TOP:
return this.textureSlotId;
case EnumBlockFace.BOTTOM:
return this.textureSlotId + 1;
default:
return this.textureSlotId + 2;
}
}
}
@@ -0,0 +1,10 @@
window.BlockLeave = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
getOpacity() {
return 0.3;
}
}
@@ -0,0 +1,10 @@
window.BlockLog = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
getTextureForFace(face) {
return this.textureSlotId + (face.isYAxis() ? 1 : 0);
}
}
@@ -0,0 +1,7 @@
window.BlockSand = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
}
@@ -0,0 +1,7 @@
window.BlockStone = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
}
@@ -0,0 +1,29 @@
window.BlockWater = class extends Block {
constructor(id, textureSlotId) {
super(id, textureSlotId);
}
getOpacity() {
return 0.3;
}
isSolid() {
return false;
}
shouldRenderFace(world, x, y, z, face) {
let typeId = world.getBlockAt(x + face.x, y + face.y, z + face.z);
return typeId === 0 || typeId !== this.id && Block.getById(typeId).isTransparent();
}
getBoundingBox(world, x, y, z) {
let box = this.boundingBox.clone();
if (world.getBlockAt(x, y + 1, z) !== this.id) {
box.maxY = 1.0 - 0.12;
}
return box;
}
}