Proper directional blocks
This commit is contained in:
+16
-18
@@ -56,7 +56,8 @@ const cube_face = (texture: string, cullface: ModelFace): ModelFaceJson => ({ te
|
||||
|
||||
// the engine's own models
|
||||
export const BUILTIN_MODELS: Record<string, ModelJson> = {
|
||||
// a full block. "textures" can be one texture, { top, bottom, side } or { front, side }
|
||||
// a full block. "textures" can be one texture, { top, bottom, side } or { front, side }. the front is north, like
|
||||
// minecraft's furnace, so a facing state turns it towards whoever placed it
|
||||
"engine:cube": {
|
||||
id: "engine:cube",
|
||||
textures: { top: "#side", bottom: "#side", front: "#side" },
|
||||
@@ -66,8 +67,8 @@ export const BUILTIN_MODELS: Record<string, ModelJson> = {
|
||||
faces: {
|
||||
top: cube_face("#top", "top"),
|
||||
bottom: cube_face("#bottom", "bottom"),
|
||||
north: cube_face("#side", "north"),
|
||||
south: cube_face("#front", "south"),
|
||||
north: cube_face("#front", "north"),
|
||||
south: cube_face("#side", "south"),
|
||||
west: cube_face("#side", "west"),
|
||||
east: cube_face("#side", "east"),
|
||||
},
|
||||
@@ -141,24 +142,21 @@ export function texture_variables(reference: string, textures: BlockTextures | u
|
||||
return variables;
|
||||
}
|
||||
|
||||
// the block's model and textures for a value with state bits, from the first variant whose conditions match
|
||||
// the block's model, textures and turn for a value with state bits. each comes from the first variant whose conditions
|
||||
// match that sets it, so "lit=1" can change the textures and "facing=1" the turn of the same block
|
||||
export function block_variant(block: BlockRegistry, states?: Record<string, number>): Required<BlockVariant> {
|
||||
const variant: Required<BlockVariant> = {
|
||||
model: block.model ?? DEFAULT_MODEL,
|
||||
textures: block.textures,
|
||||
y: 0,
|
||||
};
|
||||
if (!block.variants || !states) return variant;
|
||||
for (const [condition, override] of Object.entries(block.variants)) {
|
||||
if (variant_matches(condition, states)) {
|
||||
return {
|
||||
model: override.model ?? variant.model,
|
||||
textures: override.textures ?? variant.textures,
|
||||
y: override.y ?? 0,
|
||||
};
|
||||
let model: string | undefined;
|
||||
let textures: BlockTextures | undefined;
|
||||
let y: number | undefined;
|
||||
if (block.variants && states) {
|
||||
for (const [condition, override] of Object.entries(block.variants)) {
|
||||
if (!variant_matches(condition, states)) continue;
|
||||
model ??= override.model;
|
||||
textures ??= override.textures;
|
||||
y ??= override.y;
|
||||
}
|
||||
}
|
||||
return variant;
|
||||
return { model: model ?? block.model ?? DEFAULT_MODEL, textures: textures ?? block.textures, y: y ?? 0 };
|
||||
}
|
||||
|
||||
// "age=7" or "age=7,facing=2", "" matches everything
|
||||
|
||||
@@ -207,8 +207,6 @@ export interface Player {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly position: Readonly<Position>;
|
||||
// where they look, in radians. 0 is north (-z), going up turning west
|
||||
readonly yaw: number;
|
||||
readonly inventory: Container; // 36 slots, hotbar is 0-8
|
||||
readonly selected_slot: number;
|
||||
readonly held_item: ItemStack | undefined;
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
"drops": "bworld:furnace",
|
||||
"interactive": true,
|
||||
"variants": {
|
||||
"facing=1": { "y": 90 },
|
||||
"facing=2": { "y": 180 },
|
||||
"facing=3": { "y": 270 },
|
||||
"lit=1": {
|
||||
"textures": {
|
||||
"front": "bworld:furnace_front_lit",
|
||||
@@ -30,6 +33,11 @@
|
||||
"name": "lit",
|
||||
"bits": 1,
|
||||
"default": 0
|
||||
},
|
||||
{
|
||||
"name": "facing",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
"side": "farmersdelight:stove_side"
|
||||
},
|
||||
"variants": {
|
||||
"facing=1": { "y": 90 },
|
||||
"facing=2": { "y": 180 },
|
||||
"facing=3": { "y": 270 },
|
||||
"lit=1": {
|
||||
"textures": {
|
||||
"top": "farmersdelight:stove_top_on",
|
||||
@@ -29,6 +32,11 @@
|
||||
"name": "lit",
|
||||
"bits": 1,
|
||||
"default": 1
|
||||
},
|
||||
{
|
||||
"name": "facing",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
|
||||
@@ -317,9 +317,6 @@ function register_cooking_pot(ctx: ServerContext) {
|
||||
|
||||
// cutting board
|
||||
|
||||
// its facing state, the way its front points: north, east, south or west, like the java mod's HORIZONTAL_FACING
|
||||
const FACINGS = 4;
|
||||
|
||||
// a farmersdelight:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the
|
||||
// tool type that cuts it, and everything one of it is cut into
|
||||
interface CuttingRecipe {
|
||||
@@ -364,15 +361,6 @@ function register_cutting_board(ctx: ServerContext) {
|
||||
const set_on_board = (block: BlockRef, item: ItemStack | undefined) =>
|
||||
ctx.world.set_block_item(block.x, block.y, block.z, item);
|
||||
|
||||
// placed facing whoever placed it, like getStateForPlacement. they look along (-sin yaw, -cos yaw), so the way
|
||||
// back to them is (sin yaw, cos yaw), and north (-z) is 0 going clockwise
|
||||
ctx.events.after.block_place.subscribe(({ player, block }) => {
|
||||
if (block.id !== "farmersdelight:cutting_board") return;
|
||||
const quarter = Math.round((Math.PI - player.yaw) / (Math.PI / 2));
|
||||
const facing = ((quarter % FACINGS) + FACINGS) % FACINGS;
|
||||
ctx.world.set_state(block.x, block.y, block.z, "facing", facing);
|
||||
});
|
||||
|
||||
ctx.components.register_block("farmersdelight:cutting_board", {
|
||||
on_interact(block, _params, player) {
|
||||
const held = player.inventory.get(player.selected_slot);
|
||||
|
||||
@@ -26,7 +26,14 @@ import type { ModListing, RecipeBook } from "$/common/mod_loader.ts";
|
||||
import type { WorldgenSetup } from "$/common/generation.ts";
|
||||
import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts";
|
||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||
import { can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts";
|
||||
import {
|
||||
can_place,
|
||||
can_stand_on,
|
||||
default_block_value,
|
||||
display_name,
|
||||
placement_target,
|
||||
set_state_value,
|
||||
} from "$/common/utils.ts";
|
||||
import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts";
|
||||
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import {
|
||||
@@ -879,7 +886,7 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
this.set_block(tx, ty, tz, held.block_id, player);
|
||||
this.set_block(tx, ty, tz, held.block_id, player, placed_state(held.block_id, player));
|
||||
held_slot.amount = held_slot.amount! - 1;
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
@@ -1602,3 +1609,15 @@ function death_message(name: string, cause: string) {
|
||||
return `${name} died`;
|
||||
}
|
||||
}
|
||||
|
||||
// the states a block is placed with: its defaults, and a "facing" state turned towards whoever placed it, like
|
||||
// minecraft's getStateForPlacement with getHorizontalDirection().getOpposite(). facing is 0 north, 1 east, 2 south,
|
||||
// 3 west, and variants turn the model with it (its north side is the front, like engine:cube's). they look along
|
||||
// (-sin yaw, -cos yaw), so the way back to them is (sin yaw, cos yaw)
|
||||
function placed_state(block_id: string, player: ServerPlayer): number | undefined {
|
||||
const info = EverythingRegistry.get<BlockRegistry>("blocks", block_id);
|
||||
if (!info?.states?.some((state) => state.name === "facing" && state.bits >= 2)) return undefined;
|
||||
const quarter = Math.round((Math.PI - player.yaw) / (Math.PI / 2));
|
||||
const facing = ((quarter % 4) + 4) % 4;
|
||||
return set_state_value(default_block_value(0, info), info, "facing", facing)! >>> 16;
|
||||
}
|
||||
|
||||
@@ -763,9 +763,6 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
get position() {
|
||||
return { x: player.x, y: player.y, z: player.z };
|
||||
},
|
||||
get yaw() {
|
||||
return player.yaw;
|
||||
},
|
||||
inventory,
|
||||
get selected_slot() {
|
||||
return player.selected_slot;
|
||||
|
||||
@@ -41,6 +41,18 @@ Deno.test("the board faces whoever placed it", async () => {
|
||||
assertEquals((await setup(-Math.PI / 2)).state("facing"), 3);
|
||||
});
|
||||
|
||||
Deno.test("so does any block with a facing state, like the furnace, keeping its other states", async () => {
|
||||
const t = await setup(Math.PI / 2);
|
||||
const at = [0, 100, 2] as const;
|
||||
t.send(1, { type: "chat", text: "/give bworld:furnace" });
|
||||
t.send(1, { type: "select_slot", slot: 0 });
|
||||
t.send(1, { type: "use_block", x: at[0], y: at[1] - 1, z: at[2], face: "top" });
|
||||
const info = t.game.world.get_block_info(...at)!;
|
||||
assertEquals(info.id, "bworld:furnace");
|
||||
assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "facing"), 1);
|
||||
assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "lit"), 0);
|
||||
});
|
||||
|
||||
Deno.test("a stack goes on the board, and a knife cuts one at a time by its recipe", async () => {
|
||||
const t = await setup();
|
||||
t.send(1, { type: "chat", text: "/give bworld:cod 3" });
|
||||
|
||||
@@ -247,4 +247,8 @@ Deno.test("a lit furnace shows its lit front", async () => {
|
||||
const front = (lit: number) => (block_variant(furnace, { lit }).textures as Record<string, string>).front;
|
||||
assertEquals(front(0), "bworld:furnace_front");
|
||||
assertEquals(front(1), "bworld:furnace_front_lit");
|
||||
// turned and lit at once: each comes from the first variant that sets it
|
||||
const variant = block_variant(furnace, { lit: 1, facing: 3 });
|
||||
assertEquals((variant.textures as Record<string, string>).front, "bworld:furnace_front_lit");
|
||||
assertEquals(variant.y, 270);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user