Basic crops system
This commit is contained in:
@@ -4,6 +4,7 @@ import { AssetManager } from "$/client/assets.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
|
||||
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
const BLOCK_SCALE = 0.25;
|
||||
@@ -40,9 +41,11 @@ function copies(amount: number) {
|
||||
export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const item_info = EverythingRegistry.get<ItemRegistry>("items", entity.item.type_id);
|
||||
const block_info = item_info?.block_id
|
||||
// blocks that aren't cubes, like plants, are drawn as their item's sprite
|
||||
let block_info = item_info?.block_id
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
|
||||
: undefined;
|
||||
if (block_info && block_item_texture(block_info) !== undefined) block_info = undefined;
|
||||
|
||||
// spinning and bobbing, like minecraft's ItemEntityRenderer
|
||||
const time = entity.age + partial_tick;
|
||||
@@ -79,7 +82,7 @@ function push_block(
|
||||
};
|
||||
|
||||
for (const face of CUBE_FACES) {
|
||||
const region = get_sprite_region(block_face_texture(block, face.texture));
|
||||
const region = get_sprite_region(cube_face_texture(block, face.texture));
|
||||
push_textured_quad(atlas, region, face.corners.map(([cx, cy, cz]) => corner(cx, cy, cz)), face.shade);
|
||||
}
|
||||
}
|
||||
@@ -121,17 +124,6 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co
|
||||
}
|
||||
}
|
||||
|
||||
function block_face_texture(block: BlockRegistry, face: "top" | "bottom" | "front" | "side") {
|
||||
const textures = block.textures;
|
||||
if (typeof textures === "string") {
|
||||
return textures;
|
||||
}
|
||||
if ("top" in textures) {
|
||||
return face === "top" ? textures.top : face === "bottom" ? textures.bottom : textures.side;
|
||||
}
|
||||
return face === "front" ? textures.front : textures.side;
|
||||
}
|
||||
|
||||
function item_texture(entity: ItemEntity, item_info: ItemRegistry | undefined) {
|
||||
const texture_id = item_info?.texture_id;
|
||||
if (typeof texture_id === "function") {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
|
||||
import { draw_text, draw_texture_region, draw_texture_region_skewed, Texture } from "$/client/renderer/mod.ts";
|
||||
|
||||
export function draw_nine_slice(
|
||||
@@ -206,8 +207,12 @@ export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
throw Error(`Didn't find item registry for '${item.type_id}'`);
|
||||
}
|
||||
|
||||
if (item_info?.block_id) {
|
||||
draw_item_block(item, item_info, x, y);
|
||||
const block_info = item_info.block_id
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
|
||||
: undefined;
|
||||
// blocks that aren't cubes, like plants, show a flat sprite
|
||||
if (block_info && block_item_texture(block_info) === undefined) {
|
||||
draw_item_block(block_info, x, y);
|
||||
} else {
|
||||
draw_item_item(item, item_info, x, y);
|
||||
}
|
||||
@@ -251,30 +256,10 @@ function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y:
|
||||
);
|
||||
}
|
||||
|
||||
function draw_item_block(_item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
|
||||
const block_info = EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id!);
|
||||
|
||||
if (!block_info) throw new Error(`no textures for ${item_info.block_id}`);
|
||||
|
||||
let front_texture = "engine:missing";
|
||||
let top_texture = "engine:missing";
|
||||
let left_texture = "engine:missing";
|
||||
|
||||
const textures = block_info.textures;
|
||||
|
||||
if (typeof textures === "string") {
|
||||
front_texture = textures;
|
||||
top_texture = textures;
|
||||
left_texture = textures;
|
||||
} else if ("top" in textures && "bottom" in textures && "side" in textures) {
|
||||
top_texture = textures.top;
|
||||
front_texture = textures.side;
|
||||
left_texture = textures.side;
|
||||
} else if ("front" in textures && "side" in textures) {
|
||||
top_texture = textures.side;
|
||||
front_texture = textures.front;
|
||||
left_texture = textures.side;
|
||||
}
|
||||
function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
||||
const top_texture = cube_face_texture(block_info, "top");
|
||||
const front_texture = cube_face_texture(block_info, "front");
|
||||
const left_texture = cube_face_texture(block_info, "side");
|
||||
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user