From 310f1bd091aba5714157349f0a569a70dc0ed7af Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 20 Aug 2023 01:17:05 +0100 Subject: [PATCH] Block Behaviours --- server/blocks/Block.ts | 29 +++++++++++++++++++++------ server/blocks/BlockBehaviour.ts | 6 ++++++ server/blocks/BlockBehaviourFlower.ts | 12 +++++++++++ server/blocks/IBlockBehaviour.ts | 5 +++++ 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 server/blocks/BlockBehaviour.ts create mode 100644 server/blocks/BlockBehaviourFlower.ts create mode 100644 server/blocks/IBlockBehaviour.ts diff --git a/server/blocks/Block.ts b/server/blocks/Block.ts index 8deba52..25c15dd 100644 --- a/server/blocks/Block.ts +++ b/server/blocks/Block.ts @@ -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 = new Array(); public static readonly lightPassage:Array = new Array(); + public static readonly blockBehaviours:Array = new Array(); public static readonly blockNames:Array = new Array(); 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"); diff --git a/server/blocks/BlockBehaviour.ts b/server/blocks/BlockBehaviour.ts new file mode 100644 index 0000000..9a4f8c1 --- /dev/null +++ b/server/blocks/BlockBehaviour.ts @@ -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) {} +} \ No newline at end of file diff --git a/server/blocks/BlockBehaviourFlower.ts b/server/blocks/BlockBehaviourFlower.ts new file mode 100644 index 0000000..eb1aa66 --- /dev/null +++ b/server/blocks/BlockBehaviourFlower.ts @@ -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); + } + } +} \ No newline at end of file diff --git a/server/blocks/IBlockBehaviour.ts b/server/blocks/IBlockBehaviour.ts new file mode 100644 index 0000000..d4f99b4 --- /dev/null +++ b/server/blocks/IBlockBehaviour.ts @@ -0,0 +1,5 @@ +import { World } from "../World"; + +export interface IBlockBehaviour { + neighborBlockChange(world:World, x:number, y:number, z:number, blockId:number): void +} \ No newline at end of file