Files
bworld/docs/modding/entities/index.md
T
2026-09-30 09:56:53 -03:00

100 lines
4.2 KiB
Markdown

# Entities
Entities are the things in the world that aren't blocks and move on their own: animals, dropped items, the fishing hook,
and players. Mods define new kinds in their `entities/` folder, and draw them with a model from `models/`.
## Defining an Entity
`entities/chicken.json`:
```json
{
"format_version": 1,
"entity": {
"id": "bworld:chicken",
"model": "bworld:chicken",
"texture": "bworld:chicken",
"width": 0.3,
"height": 0.4,
"max_health": 4,
"movement_speed": 0.25
}
}
```
| Field | Default | Meaning |
| -------------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
| `id` | required | The entity's id. |
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
| `behavior` | `"animal"` | `"animal"` or `"scripted"`, see below. |
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
| `max_health` | `10` | In half hearts. |
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
| `physical_shielding` | `0` | Protection from physical damage. Not used yet. |
| `arcane_shielding` | `0` | Protection from arcane damage. Not used yet. |
`/summon <entity>` spawns one where you stand.
## Behaviors
**Animals** (the default) are mobs: they wander around, panic when hurt, look at players now and then, swim up in water,
and never despawn. They're saved with the world, and players can't place solid blocks where one is standing.
**Scripted** entities stay exactly where a script puts them. They don't fall, think or get hurt, and aren't saved with
the world, so a script never finds one it doesn't know about after a restart. The fishing hook is one:
```json
{
"format_version": 1,
"entity": {
"id": "bworld:fishing_hook",
"texture": "bworld:fishing_hook",
"behavior": "scripted",
"width": 0.25,
"height": 0.25
}
}
```
## Spawning From Scripts
```ts
const hook = ctx.world.spawn_entity("bworld:fishing_hook", x, y, z); // feet at x, y, z
hook.teleport(x, y + 0.1, z);
if (!hook.removed) hook.remove();
```
`spawn_entity` throws for a kind that doesn't exist, and returns an `EntityRef`:
```ts
interface EntityRef {
readonly id: string; // this entity's own id
readonly type: string; // its kind, like "bworld:fishing_hook"
readonly position: { x: number; y: number; z: number };
readonly removed: boolean;
teleport(x: number, y: number, z: number): void;
remove(): void;
}
```
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.
## Entity Models
Entity models are `models/<name>.geo.json`, as Blockbench exports them for Bedrock (the "Bedrock Entity" format), and
get the id `<mod id>:<name>`. From the file's first geometry, bworld uses:
- the description's `texture_width` and `texture_height`,
- each bone's `name`, `parent`, `pivot` and `rotation`,
- each cube's `origin`, `size`, `uv` and `inflate`.
Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to
a block with its feet at 0, and shows the way Blockbench shows it.
Models don't animate yet. The whole model turns with the entity, and a bone named `head` turns to where it looks.
Without a model, the whole texture is drawn facing the camera, 16 pixels to a block, centered on the middle of the
entity's box.