Skip to content

Commit

Permalink
feat(world): added directional block components
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 22, 2024
1 parent ffdda86 commit c945188
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 6 deletions.
16 changes: 16 additions & 0 deletions packages/world/src/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ class Block {
) ?? [])
new component(this, component.identifier);

// Register the components that are state specific.
for (const key of Object.keys(permutation.state)) {
// Get the component from the registry
const component = [...BlockComponent.components.values()].find((x) => {
// If the identifier is undefined, we will skip it.
if (!x.identifier) return false;

// Check if the identifier includes the key.
// As some states dont include a namespace.
return x.identifier.includes(key);
});

// Check if the component exists.
if (component) new component(this, key);
}

// Check if the change was initiated by a player.
// If so, we will play the block place sound.
if (playerInitiated) {
Expand Down
5 changes: 5 additions & 0 deletions packages/world/src/components/block/block-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class BlockComponent extends Component {
Array<typeof BlockComponent>
>();

/**
* A collective registry of all block components.
*/
public static readonly components = new Map<string, typeof BlockComponent>();

/**
* The block the component is binded to.
*/
Expand Down
7 changes: 4 additions & 3 deletions packages/world/src/components/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Block state components
export * from "./state";

// Block components
export * from "./block-component";
export * from "./inventory";
export * from "./crafting-table";
export * from "./nbt";
export * from "./nametag";
export * from "./directional";
export * from "./sign";
51 changes: 51 additions & 0 deletions packages/world/src/components/block/state/cardinal-direction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { CardinalDirection } from "../../../enums";
import { BlockComponent } from "../block-component";

import type { Player } from "../../../player";
import type { Block } from "../../../block";

class BlockCardinalDirectionComponent extends BlockComponent {
public static readonly identifier = "minecraft:cardinal_direction";

public constructor(block: Block) {
super(block, BlockCardinalDirectionComponent.identifier);
}

public onPlace(player: Player): void {
// Get the player's cardinal direction
const direction = player.getCardinalDirection();

// Flip south and north, north and south; east and west, west and east
const cardinal =
direction === CardinalDirection.South
? CardinalDirection.North
: direction === CardinalDirection.North
? CardinalDirection.South
: direction === CardinalDirection.East
? CardinalDirection.West
: CardinalDirection.East;

// Set the direction of the block
this.setDirection(cardinal);
}

/**
* Sets the direction of the block.
* @param direction The direction to set.
*/
public setDirection(direction: CardinalDirection): void {
// Get the block type
const type = this.block.getType();

// Get the permutation of the block
const permutation = type.getPermutation({
[BlockCardinalDirectionComponent.identifier]:
CardinalDirection[direction].toLowerCase()
});

// Set the permutation of the block
if (permutation) this.block.setPermutation(permutation);
}
}

export { BlockCardinalDirectionComponent };
2 changes: 2 additions & 0 deletions packages/world/src/components/block/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./cardinal-direction";
export * from "./weirdo-direction";
40 changes: 40 additions & 0 deletions packages/world/src/components/block/state/weirdo-direction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BlockComponent } from "../block-component";

import type { CardinalDirection } from "../../../enums";
import type { Player } from "../../../player";
import type { Block } from "../../../block";

class BlockWeirdoDirectionComponent extends BlockComponent {
public static readonly identifier = "minecraft:weirdo_direction";

public constructor(block: Block) {
super(block, BlockWeirdoDirectionComponent.identifier);
}

public onPlace(player: Player): void {
// Get the player's cardinal direction
const direction = player.getCardinalDirection();

// Set the direction of the block
this.setDirection(direction);
}

/**
* Sets the direction of the block.
* @param direction The direction to set.
*/
public setDirection(direction: CardinalDirection): void {
// Get the block type
const type = this.block.getType();

// Get the permutation of the block
const permutation = type.getPermutation({
weirdo_direction: direction
});

// Set the permutation of the block
if (permutation) this.block.setPermutation(permutation);
}
}

export { BlockWeirdoDirectionComponent };
15 changes: 15 additions & 0 deletions packages/world/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ for (const key in ItemComponents) {
// Register the component to the collective registry.
ItemComponent.components.set(value.identifier, value as typeof ItemComponent);
}

//
// Register all valid block components.
//
import { BlockComponent } from "./block";
import * as BlockComponents from "./block";
for (const key in BlockComponents) {
const value = BlockComponents[key as keyof typeof BlockComponents];

// Register the component to the collective registry.
BlockComponent.components.set(
value.identifier,
value as typeof BlockComponent
);
}
8 changes: 5 additions & 3 deletions packages/world/src/types/components/block.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type {
BlockDirectionalComponent,
BlockInventoryComponent,
BlockNametagComponent,
BlockSignComponent
BlockSignComponent,
BlockCardinalDirectionComponent,
BlockWeirdoDirectionComponent
} from "../../components";

interface BlockComponents {
"minecraft:inventory": BlockInventoryComponent;
"minecraft:nametag": BlockNametagComponent;
"minecraft:directional": BlockDirectionalComponent;
"minecraft:sign": BlockSignComponent;
"minecraft:cardinal_direction": BlockCardinalDirectionComponent;
"minecraft:weirdo_direction": BlockWeirdoDirectionComponent;
}

export { BlockComponents };

0 comments on commit c945188

Please sign in to comment.