-
Notifications
You must be signed in to change notification settings - Fork 16
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 BlockGravityComponent and EntityFallingBlockComponent
- Loading branch information
Showing
8 changed files
with
162 additions
and
43 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { BlockIdentifier } from "@serenityjs/block"; | ||
import { EntityIdentifier } from "@serenityjs/entity"; | ||
|
||
import { Entity } from "../../entity"; | ||
import { EntityFallingBlockComponent } from "../entity"; | ||
|
||
import { BlockComponent } from "./block-component"; | ||
|
||
import type { Block } from "../../block"; | ||
|
||
class BlockGravityComponent extends BlockComponent { | ||
public static readonly identifier = "minecraft:gravity"; | ||
|
||
public static readonly types = [ | ||
BlockIdentifier.Sand, | ||
BlockIdentifier.Gravel, | ||
BlockIdentifier.Glowingobsidian | ||
]; | ||
|
||
public constructor(block: Block) { | ||
super(block, BlockGravityComponent.identifier); | ||
} | ||
|
||
public onUpdate(): void { | ||
// Check if the block below is air | ||
const below = this.block.below(); | ||
if (!below.isAir()) return; | ||
|
||
// Create a new FallingBlock entity | ||
const entity = new Entity( | ||
EntityIdentifier.FallingBlock, | ||
this.block.dimension | ||
); | ||
|
||
// Set the entity's position | ||
entity.position.x = this.block.position.x + 0.5; | ||
entity.position.y = this.block.position.y; | ||
entity.position.z = this.block.position.z + 0.5; | ||
|
||
// Create a new EntityFallingBlockComponent | ||
const component = new EntityFallingBlockComponent(entity); | ||
|
||
// Set the permutation of the falling block | ||
component.setPermutation(this.block.permutation); | ||
|
||
// Spawn the entity | ||
entity.spawn(); | ||
|
||
// Destroy the block | ||
this.block.destroy(); | ||
} | ||
} | ||
|
||
export { BlockGravityComponent }; |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { EntityIdentifier } from "@serenityjs/entity"; | ||
import { BlockIdentifier, type BlockPermutation } from "@serenityjs/block"; | ||
|
||
import { EntityComponent } from "./entity-component"; | ||
import { EntityPhysicsComponent } from "./physics"; | ||
import { EntityHasGravityComponent } from "./flag"; | ||
import { EntityVariantComponent } from "./data"; | ||
|
||
import type { Block } from "../../block"; | ||
import type { Entity } from "../../entity"; | ||
|
||
class EntityFallingBlockComponent extends EntityComponent { | ||
public static readonly identifier = "minecraft:falling_block"; | ||
|
||
public static readonly types = [EntityIdentifier.FallingBlock]; | ||
|
||
/** | ||
* The block permutation of the falling block. | ||
*/ | ||
public permutation: BlockPermutation = this.entity | ||
.getWorld() | ||
.blocks.resolvePermutation(BlockIdentifier.Air); | ||
|
||
public constructor(entity: Entity) { | ||
super(entity, EntityFallingBlockComponent.identifier); | ||
|
||
// Add the physics component to the entity | ||
new EntityPhysicsComponent(entity); | ||
|
||
// Add the has gravity component to the entity | ||
new EntityHasGravityComponent(entity); | ||
|
||
// Add the variant component to the entity | ||
new EntityVariantComponent(entity); | ||
} | ||
|
||
public onTick(): void { | ||
// Check if the entity is on the ground | ||
if (!this.entity.onGround) return; | ||
|
||
// Get the block position | ||
const position = this.entity.position.floor(); | ||
|
||
// Get the block at the position | ||
const block = this.entity.dimension.getBlock(position); | ||
|
||
// Try to place the falling block | ||
this.tryPlace(block); | ||
} | ||
|
||
/** | ||
* Sets the permutation of the falling block. | ||
* @param permutation The permutation to set. | ||
*/ | ||
public setPermutation(permutation: BlockPermutation): void { | ||
// Update the entity's permutation | ||
this.permutation = permutation; | ||
|
||
// Update the entity's variant | ||
const variant = this.entity.getComponent("minecraft:variant"); | ||
|
||
// Set the current value of the variant to the permutation's network value | ||
variant.setCurrentValue(this.permutation.network); | ||
} | ||
|
||
/** | ||
* Tries to place the falling block at the given block. | ||
* @param block The block to place the falling block at. | ||
*/ | ||
protected tryPlace(block: Block): void { | ||
// Check if the block is air | ||
if (block.isAir()) { | ||
// Set the block at the position to the falling block | ||
block.setPermutation(this.permutation); | ||
|
||
// Destroy the falling block entity | ||
return this.entity.despawn(); | ||
} else { | ||
// Check if the block is a falling block | ||
return this.tryPlace(block.above()); | ||
} | ||
} | ||
} | ||
|
||
export { EntityFallingBlockComponent }; |
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