Skip to content

Commit

Permalink
feat(world): added BlockGravityComponent and EntityFallingBlockComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Sep 23, 2024
1 parent 27d94c0 commit 97bec17
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 43 deletions.
57 changes: 17 additions & 40 deletions packages/data/data/entity_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"identifier": "minecraft:balloon",
"components": []
},
{
"identifier": "minecraft:falling_block",
"components": []
},
{
"identifier": "minecraft:vindicator",
"components": [
Expand Down Expand Up @@ -175,10 +179,7 @@
},
{
"identifier": "minecraft:wind_charge_projectile",
"components": [
"minecraft:type_family",
"minecraft:projectile"
]
"components": ["minecraft:type_family", "minecraft:projectile"]
},
{
"identifier": "minecraft:sheep",
Expand Down Expand Up @@ -558,9 +559,7 @@
},
{
"identifier": "minecraft:arrow",
"components": [
"minecraft:projectile"
]
"components": ["minecraft:projectile"]
},
{
"identifier": "minecraft:llama",
Expand Down Expand Up @@ -867,9 +866,7 @@
},
{
"identifier": "minecraft:ender_crystal",
"components": [
"minecraft:fire_immune"
]
"components": ["minecraft:fire_immune"]
},
{
"identifier": "minecraft:silverfish",
Expand Down Expand Up @@ -1448,9 +1445,7 @@
},
{
"identifier": "minecraft:xp_orb",
"components": [
"minecraft:type_family"
]
"components": ["minecraft:type_family"]
},
{
"identifier": "minecraft:zombie_villager_v2",
Expand Down Expand Up @@ -1535,21 +1530,15 @@
},
{
"identifier": "minecraft:xp_bottle",
"components": [
"minecraft:projectile"
]
"components": ["minecraft:projectile"]
},
{
"identifier": "minecraft:tnt",
"components": [
"minecraft:type_family"
]
"components": ["minecraft:type_family"]
},
{
"identifier": "minecraft:splash_potion",
"components": [
"minecraft:projectile"
]
"components": ["minecraft:projectile"]
},
{
"identifier": "minecraft:lingering_potion",
Expand All @@ -1569,9 +1558,7 @@
},
{
"identifier": "minecraft:snowball",
"components": [
"minecraft:projectile"
]
"components": ["minecraft:projectile"]
},
{
"identifier": "minecraft:fishing_hook",
Expand All @@ -1583,9 +1570,7 @@
},
{
"identifier": "minecraft:egg",
"components": [
"minecraft:projectile"
]
"components": ["minecraft:projectile"]
},
{
"identifier": "minecraft:dragon_fireball",
Expand All @@ -1603,20 +1588,15 @@
},
{
"identifier": "minecraft:lightning_bolt",
"components": [
"minecraft:type_family"
]
"components": ["minecraft:type_family"]
},
{
"identifier": "minecraft:tripod_camera",
"components": []
},
{
"identifier": "minecraft:tnt_minecart",
"components": [
"minecraft:type_family",
"minecraft:is_stackable"
]
"components": ["minecraft:type_family", "minecraft:is_stackable"]
},
{
"identifier": "minecraft:minecart",
Expand Down Expand Up @@ -1644,10 +1624,7 @@
},
{
"identifier": "minecraft:command_block_minecart",
"components": [
"minecraft:inventory",
"minecraft:type_family"
]
"components": ["minecraft:inventory", "minecraft:type_family"]
},
{
"identifier": "minecraft:chest_minecart",
Expand All @@ -1669,4 +1646,4 @@
"minecraft:is_stackable"
]
}
]
]
1 change: 1 addition & 0 deletions packages/entity/src/enums/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export enum EntityIdentifier {
Balloon = "minecraft:balloon",
FallingBlock = "minecraft:falling_block",
Vindicator = "minecraft:vindicator",
VillagerV2 = "minecraft:villager_v2",
IceBomb = "minecraft:ice_bomb",
Expand Down
54 changes: 54 additions & 0 deletions packages/world/src/components/block/gravity.ts
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 };
1 change: 1 addition & 0 deletions packages/world/src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./sign";
export * from "./loot";
export * from "./supported";
export * from "./tillable";
export * from "./gravity";
4 changes: 1 addition & 3 deletions packages/world/src/components/block/supported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class BlockSupportedComponent extends BlockComponent {
const itemStack = this.block.getItemStack();

// Get the position of the block
const position = this.block.position.add(
new BlockPosition(0.5, 0, 0.5)
);
const position = this.block.position.add(new BlockPosition(0.5, 0, 0.5));

// Spawn the item stack entity
this.block.dimension.spawnItem(itemStack, position);
Expand Down
85 changes: 85 additions & 0 deletions packages/world/src/components/entity/falling-block.ts
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 };
1 change: 1 addition & 0 deletions packages/world/src/components/entity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./damage";
export * from "./projectile";
export * from "./collision-box";
export * from "./loot";
export * from "./falling-block";
2 changes: 2 additions & 0 deletions packages/world/src/components/player/entity-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class PlayerEntityRenderingComponent extends PlayerComponent {
// Check if the entity is a player and if the player is spawned
if (entity.isPlayer() && entity.status !== PlayerStatus.Spawned) continue;

if (!entity.isAlive) continue;

// Add the entity to the rendered entities
this.entities.add(unique);

Expand Down

0 comments on commit 97bec17

Please sign in to comment.