less than half finished block states
This commit is contained in:
@@ -12,3 +12,9 @@ export interface SpriteRegion {
|
||||
|
||||
export const faces = ["west", "east", "bottom", "top", "north", "south"] as const;
|
||||
export type Faces = typeof faces[number];
|
||||
|
||||
export const AIR = 0;
|
||||
export const VOID = 0xFFFFFFFF;
|
||||
|
||||
export const ID_MASK = 0xFFFF;
|
||||
export const STATE_SHIFT = 16;
|
||||
|
||||
@@ -62,6 +62,23 @@ interface TextureFront {
|
||||
side: string;
|
||||
}
|
||||
|
||||
export interface BlockStateDefinition {
|
||||
name: string;
|
||||
bits: number;
|
||||
default: number;
|
||||
}
|
||||
|
||||
interface CompiledStateDefinition {
|
||||
name: string;
|
||||
mask: number;
|
||||
shift: number;
|
||||
}
|
||||
|
||||
interface BlockStateVariant {
|
||||
model: string;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export interface BlockRegistry {
|
||||
id: string;
|
||||
textures: string | TextureSideTopBottom | TextureFront;
|
||||
@@ -74,12 +91,17 @@ export interface BlockRegistry {
|
||||
tool_to_break?: string;
|
||||
drop_table?: string;
|
||||
|
||||
states?: BlockStateDefinition[];
|
||||
variants?: Record<string, BlockStateVariant>;
|
||||
|
||||
on_create?(dimension: Dimension, block: Block): void;
|
||||
on_break?(dimension: Dimension, block: Block): void;
|
||||
on_click?(dimension: Dimension, block: Block): void;
|
||||
on_interact?(dimension: Dimension, block: Block): boolean;
|
||||
on_tick?(dimension: Dimension, block: Block, tick_delta: number): void;
|
||||
on_second?(dimension: Dimension, block: Block, second_delta: number): void;
|
||||
|
||||
compiled_states?: CompiledStateDefinition[];
|
||||
}
|
||||
|
||||
export interface ItemRegistry<T = unknown | undefined> {
|
||||
|
||||
+51
-1
@@ -1,5 +1,5 @@
|
||||
import { AssetManager } from "../client/assets.ts";
|
||||
import { SpriteRegion } from "./constants.ts";
|
||||
import { ID_MASK, SpriteRegion, STATE_SHIFT } from "./constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts";
|
||||
|
||||
export function point_inside_rec(
|
||||
@@ -56,3 +56,53 @@ export function register_block_item(block: BlockRegistry) {
|
||||
block_id: block.id,
|
||||
});
|
||||
}
|
||||
|
||||
export function get_state_value(value: number, block_info: BlockRegistry, name: string) {
|
||||
if (!block_info.states) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!block_info.compiled_states) {
|
||||
compile_block_states(block_info);
|
||||
}
|
||||
|
||||
const state_bits = value >>> STATE_SHIFT;
|
||||
|
||||
const s = block_info.compiled_states!.find((s) => s.name === name)!;
|
||||
return (state_bits & s.mask) >>> s.shift;
|
||||
}
|
||||
|
||||
export function set_state_value(value: number, block_info: BlockRegistry, name: string, new_value: number) {
|
||||
if (!block_info.states) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!block_info.compiled_states) {
|
||||
compile_block_states(block_info);
|
||||
}
|
||||
const id = value & ID_MASK;
|
||||
let state_bits = value >>> 16;
|
||||
|
||||
const s = block_info.compiled_states!.find((s) => s.name === name)!;
|
||||
|
||||
state_bits = (state_bits & ~s.mask) | (new_value << s.shift);
|
||||
|
||||
return (state_bits << 16) | id;
|
||||
}
|
||||
|
||||
function compile_block_states(block_info: BlockRegistry) {
|
||||
if (!block_info.states) {
|
||||
return;
|
||||
}
|
||||
|
||||
let offset = 0;
|
||||
|
||||
block_info.compiled_states = block_info.states.map((s) => {
|
||||
const shift = offset;
|
||||
const mask = ((1 << s.bits) - 1) << shift;
|
||||
|
||||
offset += s.bits;
|
||||
|
||||
return { name: s.name, mask, shift };
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user