Hand crank and shafts
This commit is contained in:
+28
-5
@@ -4,7 +4,7 @@
|
||||
// the block rules shared scripts register (see BlockRules in mod_api/shared.ts), run the same way on the server and on
|
||||
// clients: what states a placed block gets, and how blocks change with the blocks beside them. each side has its own
|
||||
// BlockRules, with its own world: the server's is the real one, a client's is what it shows and guesses ahead with
|
||||
import type { BlockInteraction, BlockPlacement, BlockRules as Rules, SharedWorld } from "./mod_api/shared.ts";
|
||||
import type { BlockInteraction, BlockPlacement, BlockRules as Rules, Face, SharedWorld } from "./mod_api/shared.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
|
||||
import { default_block_value, set_state_value } from "./utils.ts";
|
||||
|
||||
@@ -20,6 +20,19 @@ export function horizontal_facing(yaw: number): typeof FACINGS[number] {
|
||||
return FACINGS[((quarter % 4) + 4) % 4];
|
||||
}
|
||||
|
||||
// the face of the six someone looking at yaw and pitch looks most toward, like minecraft's getNearestLookingDirection.
|
||||
// pitch above 0 looks up, the way the camera aims
|
||||
export function looking_face(yaw: number, pitch: number): Face {
|
||||
const cos_pitch = Math.cos(pitch);
|
||||
const x = -Math.sin(yaw) * cos_pitch;
|
||||
const y = Math.sin(pitch);
|
||||
const z = -Math.cos(yaw) * cos_pitch;
|
||||
const [ax, ay, az] = [Math.abs(x), Math.abs(y), Math.abs(z)];
|
||||
if (ay >= ax && ay >= az) return y > 0 ? "top" : "bottom";
|
||||
if (ax >= az) return x > 0 ? "east" : "west";
|
||||
return z > 0 ? "south" : "north";
|
||||
}
|
||||
|
||||
// a block's value as a player looking at yaw places it before any rules: its default states, and a 2 bit facing
|
||||
// turned towards them, like a furnace
|
||||
export function default_placement_value(block: BlockRegistry, yaw: number): number {
|
||||
@@ -52,11 +65,21 @@ export class BlockRules {
|
||||
});
|
||||
}
|
||||
|
||||
// the block's value with the states a player placing it gets: its defaults facing them (default_placement_value),
|
||||
// then what its rules' state_for_placement say. states that don't exist or don't fit are left alone
|
||||
placement_value(block: BlockRegistry, yaw: number, where: Omit<BlockPlacement, "facing">): number {
|
||||
// the block's value with the states a player looking at yaw and pitch placing it gets: its defaults facing them
|
||||
// (default_placement_value), then what its rules' state_for_placement say. states that don't exist or don't fit are
|
||||
// left alone
|
||||
placement_value(
|
||||
block: BlockRegistry,
|
||||
yaw: number,
|
||||
pitch: number,
|
||||
where: Omit<BlockPlacement, "facing" | "looking">,
|
||||
): number {
|
||||
let value = default_placement_value(block, yaw);
|
||||
const placement: BlockPlacement = { ...where, facing: horizontal_facing(yaw) };
|
||||
const placement: BlockPlacement = {
|
||||
...where,
|
||||
facing: horizontal_facing(yaw),
|
||||
looking: looking_face(yaw, pitch),
|
||||
};
|
||||
for (const { mod, id, rules, params } of this.#of(block)) {
|
||||
if (!rules.state_for_placement) continue;
|
||||
const states = this.run(
|
||||
|
||||
Reference in New Issue
Block a user