Dangerous business work

This commit is contained in:
2026-11-16 21:27:24 -03:00
parent 5301e0307c
commit 7369bf2e5f
7 changed files with 240 additions and 7 deletions
@@ -79,7 +79,7 @@
},
"interactive": true,
"components": {
"dangerous_business:door": {}
"dangerous_business:facility_door": {}
}
}
}
@@ -0,0 +1,93 @@
{
"format_version": 1,
"block": {
"id": "dangerous_business:factory_door",
"name": "Factory Door",
"textures": {
"item": "dangerous_business:factory_door_top",
"bottom": "dangerous_business:factory_door_bottom",
"top": "dangerous_business:factory_door_top"
},
"model": "dangerous_business:door_bottom",
"render_layer": "cutout",
"states": [
{
"name": "facing",
"bits": 2,
"default": 0
},
{
"name": "half",
"bits": 1,
"default": 0
},
{
"name": "open",
"bits": 1,
"default": 0
},
{
"name": "hinge",
"bits": 1,
"default": 0
}
],
"variants": {
"half=0,hinge=0,open=1": {
"model": "dangerous_business:door_bottom_rh"
},
"half=0,hinge=1,open=0": {
"model": "dangerous_business:door_bottom_rh"
},
"half=1,hinge=0,open=1": {
"model": "dangerous_business:door_top_rh"
},
"half=1,hinge=1,open=0": {
"model": "dangerous_business:door_top_rh"
},
"half=1": {
"model": "dangerous_business:door_top"
},
"facing=0,open=0": {
"y": 270
},
"facing=0,open=1,hinge=0": {
"y": 0
},
"facing=0,open=1,hinge=1": {
"y": 180
},
"facing=1,open=0": {
"y": 0
},
"facing=1,open=1,hinge=0": {
"y": 90
},
"facing=1,open=1,hinge=1": {
"y": 270
},
"facing=2,open=0": {
"y": 90
},
"facing=2,open=1,hinge=0": {
"y": 180
},
"facing=2,open=1,hinge=1": {
"y": 0
},
"facing=3,open=0": {
"y": 180
},
"facing=3,open=1,hinge=0": {
"y": 270
},
"facing=3,open=1,hinge=1": {
"y": 90
}
},
"interactive": true,
"components": {
"dangerous_business:door": { "pairs_with": "dangerous_business:door" }
}
}
}
Binary file not shown.
+85 -3
View File
@@ -1,13 +1,15 @@
import type { ClientContext } from "cubi/client";
import type { ServerContext } from "cubi/server";
import { DOOR } from "./shared.ts";
import { DOOR, FACILITY_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) {
register_facility_door(ctx);
register_door(ctx);
register_commands(ctx);
}
export function client(_ctx: ClientContext) {
@@ -16,8 +18,8 @@ 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);
function register_facility_door(ctx: ServerContext) {
const is_door = (id: string) => FACILITY_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;
@@ -31,6 +33,45 @@ function register_door(ctx: ServerContext) {
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(FACILITY_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);
}
},
});
}
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", "open", "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);
// it took its open state from the lower half, but it can be the half that's powered
if (ctx.world.get_state(x, y + 1, z, "powered")) {
ctx.world.set_state(x, y, z, "open", 1);
ctx.world.set_state(x, y + 1, z, "open", 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, {
@@ -47,5 +88,46 @@ function register_door(ctx: ServerContext) {
for (const half of halves) breaking.delete(half);
}
},
on_state_change(block, _params, before) {
const { x, y, z } = block;
const powered = ctx.world.get_state(x, y, z, "powered");
if (powered === before.powered) return;
const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1;
const paired = ctx.world.get_block(x, other, z) === block.id;
const other_powered = paired && ctx.world.get_state(x, other, z, "powered") === 1;
const open = powered === 1 || other_powered;
if (open === (before.powered === 1 || other_powered)) return;
ctx.world.set_state(x, y, z, "open", open ? 1 : 0);
if (paired) ctx.world.set_state(x, other, z, "open", open ? 1 : 0);
},
});
}
function register_commands(ctx: ServerContext) {
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
literal("startday")
.describe("Starts a new day")
.executes((context) => {
clear_map(ctx);
place_surface(ctx);
context.source.send_message("A new day starts");
}),
);
}
const interior_tiles = [];
function clear_map(ctx: ServerContext) {
// theres no need to clear the surface but we do need to clear each tile
}
function place_surface(ctx: ServerContext) {
ctx.schematics.place(
"dangerous_business:assurance",
{ position: { x: -80, y: 150, z: -80 } },
);
}
function generate_interior(ctx: ServerContext) {
}
+61 -3
View File
@@ -1,5 +1,6 @@
import type { SharedContext } from "cubi/shared";
export const FACILITY_DOOR = "dangerous_business:facility_door";
export const DOOR = "dangerous_business:door";
const FACINGS = ["north", "east", "south", "west"];
@@ -8,20 +9,21 @@ const FACING_STEPS = [[0, -1], [1, 0], [0, 1], [-1, 0]];
export function shared(ctx: SharedContext) {
register_door(ctx);
register_facility_door(ctx);
}
// a door is two blocks, its lower half (half 0) and its upper half (half 1), with the same states. it faces the way the
// player placing it looks, and is hinged on the side away from a door of the same kind beside it, so the two meet in
// the middle as a double door. without one, on the side of the block the player clicked toward. the server places the
// upper half and takes away the other half when one breaks, see register_door in main.ts
function register_door(ctx: SharedContext) {
ctx.components.register_block(DOOR, {
function register_facility_door(ctx: SharedContext) {
ctx.components.register_block(FACILITY_DOOR, {
state_for_placement(placement): Record<string, number> {
const facing = FACINGS.indexOf(placement.facing);
const { x, y, z } = placement;
const door_at = ([step_x, step_z]: number[]) => {
const id = ctx.world.get_block(x + step_x, y, z + step_z);
return id !== undefined && DOOR in ctx.blocks.components(id) &&
return id !== undefined && FACILITY_DOOR in ctx.blocks.components(id) &&
ctx.world.get_state(x + step_x, y, z + step_z, "half") === 0;
};
const door_left = door_at(FACING_STEPS[(facing + 3) % 4]);
@@ -35,3 +37,59 @@ function register_door(ctx: SharedContext) {
},
});
}
interface DoorParams {
pairs_with: string;
}
function register_door(ctx: SharedContext) {
// a door's lower half there, of the kind this one pairs with
const door_at = (x: number, y: number, z: number, params: DoorParams) => {
const id = ctx.world.get_block(x, y, z);
return id !== undefined && ctx.tags.has("blocks", params.pairs_with, id) &&
ctx.world.get_state(x, y, z, "half") === 0;
};
// opens or closes both halves of the door whose lower half is there
const set_open = (x: number, y: number, z: number, open: number) => {
if (ctx.world.get_block(x, y + 1, z) === ctx.world.get_block(x, y, z)) {
ctx.world.set_state(x, y + 1, z, "open", open);
}
return ctx.world.set_state(x, y, z, "open", open);
};
ctx.components.register_block<DoorParams>("dangerous_business:door", {
on_interact(block, params) {
const { x, z } = block;
const y = ctx.world.get_state(x, block.y, z, "half") ? block.y - 1 : block.y;
const state = (name: string, at_x = x, at_z = z) => ctx.world.get_state(at_x, y, at_z, name);
const was_open = state("open");
const open = was_open ? 0 : 1;
// the other door of a double door is beside its free edge, facing the same way, hinged the other way, and
// open or closed like it
const facing = state("facing")!;
const [step_x, step_z] = FACING_STEPS[(facing + (state("hinge") ? 3 : 1)) % 4];
const [px, pz] = [x + step_x, z + step_z];
if (
door_at(px, y, pz, params) && state("facing", px, pz) === facing &&
state("hinge", px, pz) !== state("hinge") && state("open", px, pz) === was_open
) {
set_open(px, y, pz, open);
}
return set_open(x, y, z, open);
},
state_for_placement(placement, params): Record<string, number> {
const facing = FACINGS.indexOf(placement.facing);
const { x, y, z } = placement;
const [left_x, left_z] = FACING_STEPS[(facing + 3) % 4];
const [right_x, right_z] = FACING_STEPS[(facing + 1) % 4];
const door_left = door_at(x + left_x, y, z + left_z, params);
const door_right = door_at(x + right_x, y, z + right_z, params);
if (door_left !== door_right) return { facing, half: 0, hinge: door_left ? 1 : 0 };
const [step_x, step_z] = FACING_STEPS[facing];
const [hit_x, , hit_z] = placement.hit;
const right = (step_x < 0 && hit_z < 0.5) || (step_x > 0 && hit_z > 0.5) ||
(step_z < 0 && hit_x > 0.5) || (step_z > 0 && hit_x < 0.5);
return { facing, half: 0, hinge: right ? 1 : 0 };
},
});
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B