Block Behaviours

This commit is contained in:
Holly Stubbs 2023-08-20 01:17:05 +01:00
parent 36ef685813
commit 310f1bd091
Signed by: tgpholly
GPG Key ID: B8583C4B7D18119E
4 changed files with 46 additions and 6 deletions

View File

@ -1,9 +1,16 @@
import { World } from "../World";
import { BlockBehaviourFlower } from "./BlockBehaviourFlower";
import { IBlockBehaviour } from "./IBlockBehaviour";
abstract class Behaviour {
public static flower = new BlockBehaviourFlower();
}
export class Block {
public readonly blockId:number;
public static readonly blocks:Array<Block> = new Array<Block>();
public static readonly lightPassage:Array<number> = new Array<number>();
public static readonly blockBehaviours:Array<IBlockBehaviour> = new Array<IBlockBehaviour>();
public static readonly blockNames:Array<string> = new Array<string>();
public constructor(blockId:number) {
@ -29,6 +36,19 @@ export class Block {
Block.blockNames[this.blockId] = value;
}
public get behaviour() {
return Block.blockBehaviours[this.blockId];
}
public set behaviour(value:IBlockBehaviour) {
Block.blockBehaviours[this.blockId] = value;
}
public setBehaviour(value:IBlockBehaviour) {
this.behaviour = value;
return this;
}
public setLightPassage(value:number) {
this.lightPassage = value;
return this;
@ -40,11 +60,8 @@ export class Block {
}
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);
}
if (this.behaviour !== undefined) {
this.behaviour.neighborBlockChange(world, x, y, z, blockId);
}
}
@ -67,7 +84,7 @@ export class Block {
static readonly glass = new Block(20).setLightPassage(255).setBlockName("Glass");
static readonly tallGrass = new Block(31).setLightPassage(255).setBlockName("Tall Grass");
static readonly tallGrass = new Block(31).setLightPassage(255).setBehaviour(Behaviour.flower).setBlockName("Tall Grass");
static readonly flowerDandelion = new Block(37).setLightPassage(255).setBlockName("Dandelion");
static readonly flowerRose = new Block(38).setLightPassage(255).setBlockName("Rose");

View File

@ -0,0 +1,6 @@
import { World } from "../World";
import { IBlockBehaviour } from "./IBlockBehaviour";
export class BlockBehaviour implements IBlockBehaviour {
public neighborBlockChange(world:World, x:number, y:number, z:number, blockId:number) {}
}

View File

@ -0,0 +1,12 @@
import { World } from "../World";
import { Block } from "./Block";
import { BlockBehaviour } from "./BlockBehaviour";
export class BlockBehaviourFlower extends BlockBehaviour {
public neighborBlockChange(world:World, x:number, y:number, z:number, blockId:number) {
const block = world.getBlockId(x, y - 1, z);
if (block !== Block.grass.blockId && block !== Block.dirt.blockId) {
world.setBlockWithNotify(x, y, z, 0);
}
}
}

View File

@ -0,0 +1,5 @@
import { World } from "../World";
export interface IBlockBehaviour {
neighborBlockChange(world:World, x:number, y:number, z:number, blockId:number): void
}