Facility door
This commit is contained in:
@@ -1,10 +1,51 @@
|
||||
import type { ClientContext } from "cubi/client";
|
||||
import type { ServerContext } from "cubi/server";
|
||||
import { DOOR } from "./shared.ts";
|
||||
|
||||
export { shared } from "./shared.ts";
|
||||
|
||||
// a standalone game that doesn't build on bworld, so everything it needs comes from this mod or the engine
|
||||
|
||||
export function server(_ctx: ServerContext) {
|
||||
export function server(ctx: ServerContext) {
|
||||
register_door(ctx);
|
||||
}
|
||||
|
||||
export function client(_ctx: ClientContext) {
|
||||
}
|
||||
|
||||
// doors are two blocks, a lower and an upper half, see register_door in shared.ts. one is only placed with air above
|
||||
// it, where the upper half goes, and breaking either half takes the other away with it. only the half that was broken
|
||||
// drops the door, the other is just taken away
|
||||
function register_door(ctx: ServerContext) {
|
||||
const is_door = (id: string) => DOOR in ctx.blocks.components(id);
|
||||
ctx.events.before.block_place.subscribe((event) => {
|
||||
const { id, x, y, z } = event.block;
|
||||
if (is_door(id) && ctx.world.get_block(x, y + 1, z) !== "cubi:air") event.cancel = true;
|
||||
});
|
||||
ctx.events.after.block_place.subscribe(({ block: { id, x, y, z } }) => {
|
||||
if (!is_door(id)) return;
|
||||
ctx.world.set_block(x, y + 1, z, id);
|
||||
for (const state of ["facing", "hinge"]) {
|
||||
ctx.world.set_state(x, y + 1, z, state, ctx.world.get_state(x, y, z, state) ?? 0);
|
||||
}
|
||||
ctx.world.set_state(x, y + 1, z, "half", 1);
|
||||
});
|
||||
|
||||
// the halves being taken away, so the other half's on_break doesn't take the first away again
|
||||
const breaking = new Set<string>();
|
||||
ctx.components.register_block(DOOR, {
|
||||
on_break(block) {
|
||||
const { x, y, z } = block;
|
||||
if (breaking.has(`${x},${y},${z}`)) return;
|
||||
const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1;
|
||||
if (ctx.world.get_block(x, other, z) !== block.id) return;
|
||||
const halves = [`${x},${y},${z}`, `${x},${other},${z}`];
|
||||
for (const half of halves) breaking.add(half);
|
||||
try {
|
||||
ctx.world.set_block(x, other, z, "cubi:air");
|
||||
} finally {
|
||||
for (const half of halves) breaking.delete(half);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user