Blocks interacting on the world api
This commit is contained in:
@@ -150,14 +150,41 @@ interface EntityRef {
|
||||
readonly biome: string; // where its feet are, see ../worldgen/biomes.md#seasons
|
||||
readonly season: Season; // that biome's
|
||||
readonly removed: boolean;
|
||||
readonly velocity: { x: number; y: number; z: number }; // in blocks a second
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
push(x: number, y: number, z: number): void; // adds to its velocity
|
||||
remove(): void;
|
||||
get_item(): ItemStack | undefined; // for an item lying around, a copy
|
||||
set_item(item: ItemStack | undefined): void; // for an item lying around, undefined takes it away
|
||||
}
|
||||
```
|
||||
|
||||
Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check
|
||||
`removed` before relying on one you kept from earlier.
|
||||
|
||||
`push` adds to how fast it's moving, like Minecraft's `Entity.push`: a fan blowing it along does it every tick. Players
|
||||
have a `push` too, see [Players](players.md#pushing).
|
||||
|
||||
## Finding Entities
|
||||
|
||||
`ctx.world.entities_in(from, to, type?)` finds the entities whose boxes overlap the box between two corners, like
|
||||
Minecraft's `getEntitiesOfClass`, only the ones of `type` when it's given. Players aren't among them, they're in
|
||||
`ctx.players`.
|
||||
|
||||
Items lying around are entities of type `bworld:item`. A machine that works on them, like a press, finds them, changes
|
||||
them with `set_item`, and adds what it made with `ctx.world.spawn_item`, which lays an item down still where it's put,
|
||||
unlike `drop_item`, which pops it out of a block:
|
||||
|
||||
```ts
|
||||
const [item] = ctx.world.entities_in({ x, y: y - 1, z }, { x: x + 1, y, z: z + 1 }, "bworld:item");
|
||||
const stack = item?.get_item();
|
||||
if (stack?.id === "bworld:iron_ingot") {
|
||||
item.set_item(stack.count > 1 ? { ...stack, count: stack.count - 1 } : undefined);
|
||||
const { x: ix, y: iy, z: iz } = item.position;
|
||||
ctx.world.spawn_item(ix, iy, iz, { id: "create:iron_sheet", count: 1 });
|
||||
}
|
||||
```
|
||||
|
||||
## Entity Models
|
||||
|
||||
Entity models are `models/<name>.geo.json`, as Blockbench exports them for Bedrock (the "Bedrock Entity" format), and
|
||||
|
||||
@@ -200,6 +200,11 @@ starts it with `player.start_using(ticks)`, and the item's components hear how i
|
||||
[Using Over Time](../items/index.md#using-over-time). `stop_using()` ends it without either ending, and `is_using()`
|
||||
says whether they're using one. While they are, they walk at a fifth of their speed and can't sprint.
|
||||
|
||||
## Pushing
|
||||
|
||||
`player.push(x, y, z)` adds to how fast they're moving, in blocks a second, like Minecraft's `Entity.push`: a fan
|
||||
blowing them along does it every tick. Their client moves them, so it's sent there and added to theirs.
|
||||
|
||||
## Death
|
||||
|
||||
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
|
||||
|
||||
Reference in New Issue
Block a user