179 lines
6.1 KiB
TypeScript
179 lines
6.1 KiB
TypeScript
import type { ClientContext, Color } from "cubi/client";
|
|
import type { Position, ServerContext } from "cubi/server";
|
|
import { DOOR, FACILITY_DOOR } from "./shared.ts";
|
|
import { Alea } from "@paulaboks/rng";
|
|
|
|
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_death(ctx);
|
|
register_commands(ctx);
|
|
}
|
|
|
|
export function client(ctx: ClientContext) {
|
|
register_hud(ctx);
|
|
}
|
|
|
|
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
|
|
function register_hud(ctx: ClientContext) {
|
|
const color: Color = [1, 1, 1, 1];
|
|
ctx.hud.register("dangerous_business:crosshair", {
|
|
on_render(g) {
|
|
const x = Math.floor(g.width / 2) - 2;
|
|
const y = Math.floor(g.height / 2) - 2;
|
|
g.rect(x + 1, y, 3, 1, color);
|
|
g.rect(x + 1, y + 4, 3, 1, color);
|
|
g.rect(x, y + 1, 1, 3, color);
|
|
g.rect(x + 4, y + 1, 1, 3, color);
|
|
},
|
|
});
|
|
}
|
|
|
|
// 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_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;
|
|
});
|
|
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(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, {
|
|
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);
|
|
}
|
|
},
|
|
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);
|
|
},
|
|
});
|
|
}
|
|
|
|
// an invisible block that kills whatever walks or falls into it, for the bottoms of pits and the like
|
|
function register_death(ctx: ServerContext) {
|
|
ctx.components.register_block("dangerous_business:death", {
|
|
on_entity_inside(_block, _params, entity) {
|
|
entity.kill("death_block");
|
|
},
|
|
});
|
|
}
|
|
|
|
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");
|
|
}),
|
|
);
|
|
}
|
|
|
|
interface Tile {
|
|
tile: string;
|
|
position: Position;
|
|
rotation: number;
|
|
}
|
|
|
|
let interior_tiles: Tile[] = [];
|
|
|
|
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 } },
|
|
);
|
|
}
|
|
|
|
interface InteriorGenerationConfig {
|
|
size_multipler: number;
|
|
}
|
|
|
|
function plan_interior(rng: Alea, config: InteriorGenerationConfig): Tile[] {
|
|
const tiles: Tile[] = [];
|
|
return tiles;
|
|
}
|
|
|
|
function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig) {
|
|
interior_tiles = plan_interior(new Alea(), config);
|
|
// generate main
|
|
// generate other rooms
|
|
}
|