Implement main game as a mod

This commit is contained in:
2026-09-24 23:49:34 -03:00
parent bb42dd662e
commit 6458bc0440
131 changed files with 1810 additions and 786 deletions
+5 -77
View File
@@ -1,80 +1,8 @@
import { Container, ItemStack } from "$/common/inventory.ts";
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
import type { GridRecipe } from "$/common/mod_data.ts";
export interface CraftingRecipe {
width: number;
height: number;
pattern: (string | undefined)[];
result: { id: string; count: number };
}
export const CRAFTING_RECIPES: CraftingRecipe[] = [
{
width: 3,
height: 3,
pattern: [
"bworld:planks",
"bworld:planks",
"bworld:planks",
"bworld:planks",
undefined,
"bworld:planks",
"bworld:planks",
"bworld:planks",
"bworld:planks",
],
result: { id: "bworld:chest", count: 1 },
},
{
width: 3,
height: 3,
pattern: [
"bworld:stone",
"bworld:stone",
"bworld:stone",
"bworld:stone",
undefined,
"bworld:stone",
"bworld:stone",
"bworld:stone",
"bworld:stone",
],
result: { id: "bworld:furnace", count: 1 },
},
{
width: 1,
height: 1,
pattern: [
"bworld:log",
],
result: { id: "bworld:planks", count: 2 },
},
{
width: 1,
height: 2,
pattern: [
"bworld:planks",
"bworld:planks",
],
result: { id: "bworld:stick", count: 2 },
},
{
width: 3,
height: 3,
pattern: [
"bworld:planks",
"bworld:planks",
"bworld:planks",
undefined,
"bworld:stick",
undefined,
undefined,
"bworld:stick",
undefined,
],
result: { id: "bworld:wood_pickaxe", count: 1 },
},
];
// recipes come from mods, see RecipeBook in common/mod_loader.ts
function get_crafting_grid(crafting: Container): (string | undefined)[] {
const grid: (string | undefined)[] = [];
@@ -84,7 +12,7 @@ function get_crafting_grid(crafting: Container): (string | undefined)[] {
return grid;
}
function matches_recipe(grid: (string | undefined)[], recipe: CraftingRecipe): boolean {
function matches_recipe(grid: (string | undefined)[], recipe: GridRecipe): boolean {
for (let y = 0; y <= 3 - recipe.height; y++) {
for (let x = 0; x <= 3 - recipe.width; x++) {
let match = true;
@@ -115,9 +43,9 @@ function matches_recipe(grid: (string | undefined)[], recipe: CraftingRecipe): b
}
// puts what the grid makes in the result slot
export function update_crafting_result(crafting: Container) {
export function update_crafting_result(crafting: Container, recipes: GridRecipe[]) {
const grid = get_crafting_grid(crafting);
const recipe = CRAFTING_RECIPES.find((recipe) => matches_recipe(grid, recipe));
const recipe = recipes.find((recipe) => matches_recipe(grid, recipe));
crafting.set_item(CRAFTING_RESULT_SLOT, recipe ? new ItemStack(recipe.result.id, recipe.result.count) : undefined);
}