-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(world): added directional block components
- Loading branch information
Showing
8 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
packages/world/src/components/block/state/cardinal-direction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
packages/world/src/components/block/state/weirdo-direction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |