Rename everything "tile" to "block"
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { Container, Inventory } from "../inventory.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
import { GuiChest } from "$/client/gui/gui_chest.ts";
|
||||
|
||||
interface TileChestData {
|
||||
inventory: Inventory;
|
||||
}
|
||||
|
||||
EverythingRegistry.register<BlockRegistry<TileChestData>>("blocks", "bworld:chest", {
|
||||
texture_id: "bworld:chest",
|
||||
has_collision: false,
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_component = player.get(PlayerComponent)!;
|
||||
if (tile.data && tile.data.inventory) {
|
||||
player_component.screens.push(new GuiChest(tile.data.inventory, player_component.player_inventory));
|
||||
}
|
||||
},
|
||||
on_create(_, tile) {
|
||||
tile.data = { inventory: new Inventory(new Container(9 * 3)) };
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,146 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { ItemStack } from "../inventory.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
|
||||
interface CropsRegistry {
|
||||
total_stages: number;
|
||||
time_to_grow: number[];
|
||||
item_drop: string;
|
||||
sprite_ids: string[];
|
||||
regrowable: boolean;
|
||||
}
|
||||
|
||||
EverythingRegistry.register<CropsRegistry>("crops", "bworld:carrot", {
|
||||
total_stages: 5,
|
||||
time_to_grow: [60, 60 * 3, 60 * 3, 60 * 3],
|
||||
sprite_ids: [
|
||||
"bworld:carrot_seeds",
|
||||
"bworld:carrot_stage_1",
|
||||
"bworld:carrot_stage_2",
|
||||
"bworld:carrot_stage_3",
|
||||
"bworld:carrot_stage_4",
|
||||
],
|
||||
item_drop: "bworld:carrot",
|
||||
regrowable: false,
|
||||
});
|
||||
EverythingRegistry.register<CropsRegistry>("crops", "bworld:potato", {
|
||||
total_stages: 5,
|
||||
time_to_grow: [45, 60 * 2, 60 * 2, 60 * 2],
|
||||
sprite_ids: [
|
||||
"bworld:potato_seeds",
|
||||
"bworld:potato_stage_1",
|
||||
"bworld:potato_stage_2",
|
||||
"bworld:potato_stage_3",
|
||||
"bworld:potato_stage_4",
|
||||
],
|
||||
item_drop: "bworld:potato",
|
||||
regrowable: false,
|
||||
});
|
||||
EverythingRegistry.register<CropsRegistry>("crops", "bworld:tomato", {
|
||||
total_stages: 5,
|
||||
time_to_grow: [90, 60 * 2, 60 * 2, 60 * 3],
|
||||
sprite_ids: [
|
||||
"bworld:tomato_seeds",
|
||||
"bworld:tomato_stage_1",
|
||||
"bworld:tomato_stage_2",
|
||||
"bworld:tomato_stage_3",
|
||||
"bworld:tomato_stage_4",
|
||||
],
|
||||
item_drop: "bworld:tomato",
|
||||
regrowable: true,
|
||||
});
|
||||
EverythingRegistry.register<CropsRegistry>("crops", "bworld:pumpkin", {
|
||||
total_stages: 5,
|
||||
time_to_grow: [60 * 2, 60 * 4, 60 * 4, 60 * 4],
|
||||
sprite_ids: [
|
||||
"bworld:pumpkin_seeds",
|
||||
"bworld:pumpkin_stage_1",
|
||||
"bworld:pumpkin_stage_2",
|
||||
"bworld:pumpkin_stage_3",
|
||||
"bworld:pumpkin_stage_4",
|
||||
],
|
||||
item_drop: "bworld:pumpkin",
|
||||
regrowable: false,
|
||||
});
|
||||
|
||||
interface TileCropData {
|
||||
current_stage: number;
|
||||
growth_time: number;
|
||||
}
|
||||
|
||||
function create_crop_tile(crop_id: string) {
|
||||
const crop_info = EverythingRegistry.get<CropsRegistry>("crops", crop_id);
|
||||
|
||||
return {
|
||||
has_collision: false,
|
||||
on_create(_, tile) {
|
||||
tile.data = {
|
||||
current_stage: 0,
|
||||
growth_time: 0,
|
||||
};
|
||||
},
|
||||
texture_id(tile) {
|
||||
return crop_info.sprite_ids[tile.data!.current_stage];
|
||||
},
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.type_id === "bworld:pickaxe") {
|
||||
world.dimension.delete_tile(world, tile);
|
||||
}
|
||||
},
|
||||
on_interact(world, tile) {
|
||||
if (tile.data!.current_stage + 1 !== crop_info.total_stages) {
|
||||
return;
|
||||
}
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
player_inventory.container.add_item(new ItemStack(crop_info.item_drop));
|
||||
if (crop_info.regrowable) {
|
||||
tile.data!.current_stage -= 1;
|
||||
} else {
|
||||
world.dimension.delete_tile(world, tile);
|
||||
}
|
||||
},
|
||||
on_second(_, tile, delta) {
|
||||
const crop = tile.data!;
|
||||
// finished growing
|
||||
if (crop.current_stage + 1 === crop_info.total_stages) {
|
||||
return;
|
||||
}
|
||||
|
||||
// grow !
|
||||
crop.growth_time += delta;
|
||||
if (crop.growth_time >= crop_info.time_to_grow[crop.current_stage]) {
|
||||
crop.current_stage += 1;
|
||||
crop.growth_time = 0;
|
||||
}
|
||||
},
|
||||
} as BlockRegistry<TileCropData>;
|
||||
}
|
||||
|
||||
EverythingRegistry.register<BlockRegistry<TileCropData>>(
|
||||
"blocks",
|
||||
"bworld:tomato_crop",
|
||||
create_crop_tile("bworld:tomato"),
|
||||
);
|
||||
EverythingRegistry.register<BlockRegistry<TileCropData>>(
|
||||
"blocks",
|
||||
"bworld:carrot_crop",
|
||||
create_crop_tile("bworld:carrot"),
|
||||
);
|
||||
EverythingRegistry.register<BlockRegistry<TileCropData>>(
|
||||
"blocks",
|
||||
"bworld:potato_crop",
|
||||
create_crop_tile("bworld:potato"),
|
||||
);
|
||||
EverythingRegistry.register<BlockRegistry<TileCropData>>(
|
||||
"blocks",
|
||||
"bworld:pumpkin_crop",
|
||||
create_crop_tile("bworld:pumpkin"),
|
||||
);
|
||||
@@ -0,0 +1,95 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { WateringCanData } from "../items/watering_can.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:dirt", {
|
||||
texture_id: "bworld:dirt",
|
||||
has_collision: false,
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const maybe_item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (maybe_item && maybe_item.type_id === "bworld:hoe") {
|
||||
tile.id = "bworld:hoed_dirt";
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:hoed_dirt", {
|
||||
texture_id: "bworld:hoed_dirt",
|
||||
has_collision: false,
|
||||
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.type_id === "bworld:pickaxe") {
|
||||
tile.id = "bworld:dirt";
|
||||
}
|
||||
},
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.type_id === "bworld:watering_can") {
|
||||
const data = item.data as WateringCanData;
|
||||
if (data.water > 0) {
|
||||
tile.id = "bworld:hoed_watered_dirt";
|
||||
data.water -= 1;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:hoed_watered_dirt", {
|
||||
texture_id: "bworld:hoed_watered_dirt",
|
||||
has_collision: false,
|
||||
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.type_id === "bworld:pickaxe") {
|
||||
tile.id = "bworld:dirt";
|
||||
}
|
||||
},
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.type_id === "bworld:tomato_seeds") {
|
||||
world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:tomato_crop", tickable: true });
|
||||
item.amount -= 1;
|
||||
player_inventory.container.set_item(player_inventory.hotbar_selected, item);
|
||||
} else if (item.type_id === "bworld:potato_seeds") {
|
||||
world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:potato_crop", tickable: true });
|
||||
item.amount -= 1;
|
||||
player_inventory.container.set_item(player_inventory.hotbar_selected, item);
|
||||
} else if (item.type_id === "bworld:pumpkin_seeds") {
|
||||
world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:pumpkin_crop", tickable: true });
|
||||
item.amount -= 1;
|
||||
player_inventory.container.set_item(player_inventory.hotbar_selected, item);
|
||||
} else if (item.type_id === "bworld:carrot_seeds") {
|
||||
world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:carrot_crop", tickable: true });
|
||||
item.amount -= 1;
|
||||
player_inventory.container.set_item(player_inventory.hotbar_selected, item);
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { Container, Inventory, ItemStack } from "../inventory.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
import { GuiFurnace } from "$/client/gui/gui_furnace.ts";
|
||||
|
||||
function can_craft(container: Container) {
|
||||
const input = container.get_item(0);
|
||||
const output = container.get_item(2);
|
||||
|
||||
if (!input) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (["bworld:log"].includes(input.type_id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!output) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (output.amount >= output.max_amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function has_fuel(container: Container) {
|
||||
const fuel = container.get_item(1);
|
||||
|
||||
if (!fuel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (["bworld:coal", "bworld:log"].includes(fuel.type_id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function consume_fuel(container: Container) {
|
||||
const fuel = container.get_item(1)!;
|
||||
fuel.amount -= 1;
|
||||
container.set_item(1, fuel);
|
||||
}
|
||||
|
||||
function craft(container: Container, item: ItemStack) {
|
||||
const input = container.get_item(0)!;
|
||||
const output = container.get_item(2);
|
||||
|
||||
if (!output) {
|
||||
container.set_item(2, item);
|
||||
} else {
|
||||
output.amount += item.amount;
|
||||
}
|
||||
|
||||
input.amount -= 1;
|
||||
container.set_item(0, input);
|
||||
}
|
||||
|
||||
interface TileChestData {
|
||||
inventory: Inventory;
|
||||
progress: number;
|
||||
fuel: number;
|
||||
}
|
||||
|
||||
EverythingRegistry.register<BlockRegistry<TileChestData>>("blocks", "bworld:furnace", {
|
||||
texture_id: "bworld:furnace",
|
||||
has_collision: false,
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_component = player.get(PlayerComponent)!;
|
||||
if (tile.data && tile.data.inventory) {
|
||||
const gui = new GuiFurnace(tile.data.inventory, player_component.player_inventory, tile.data);
|
||||
player_component.screens.push(gui);
|
||||
}
|
||||
},
|
||||
on_create(_, tile) {
|
||||
tile.data = {
|
||||
inventory: new Inventory(new Container(3)),
|
||||
progress: 0,
|
||||
fuel: 0,
|
||||
};
|
||||
},
|
||||
on_tick(_, tile) {
|
||||
if (!tile.data) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tile.data.fuel > 0) {
|
||||
tile.data.fuel -= 1;
|
||||
}
|
||||
|
||||
const container = tile.data.inventory.container;
|
||||
|
||||
if (can_craft(container)) {
|
||||
if (tile.data.fuel === 0 && has_fuel(container)) {
|
||||
consume_fuel(container);
|
||||
tile.data.fuel = 200;
|
||||
}
|
||||
|
||||
if (tile.data.fuel > 0) {
|
||||
tile.data.progress += 1;
|
||||
|
||||
if (tile.data.progress >= 100) {
|
||||
tile.data.progress = 0;
|
||||
craft(container, new ItemStack("bworld:coal"));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:grass", {
|
||||
texture_id: "bworld:grass",
|
||||
has_collision: false,
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (item?.type_id === "bworld:hoe") {
|
||||
tile.id = "bworld:hoed_dirt";
|
||||
} else if (item?.type_id === "bworld:chest") {
|
||||
// world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:chest" });
|
||||
} else if (item?.type_id === "bworld:furnace") {
|
||||
// world.dimension.add_block(world, { x: tile.x, y: tile.y, id: "bworld:furnace", tickable: true });
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:log", {
|
||||
texture_id: "bworld:log_side",
|
||||
has_collision: true,
|
||||
});
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:leaves", {
|
||||
texture_id: "bworld:leaves",
|
||||
has_collision: true,
|
||||
transparent: true,
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import "./grass.ts";
|
||||
import "./dirt.ts";
|
||||
import "./tree.ts";
|
||||
import "./crops.ts";
|
||||
import "./water.ts";
|
||||
import "./chest.ts";
|
||||
import "./furnace.ts";
|
||||
import "./stone.ts";
|
||||
import "./log.ts";
|
||||
import "./sand.ts";
|
||||
import "./snow.ts";
|
||||
@@ -0,0 +1,6 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:sand", {
|
||||
texture_id: "bworld:sand",
|
||||
has_collision: true,
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:snow", {
|
||||
texture_id: "bworld:snow",
|
||||
has_collision: true,
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:stone", {
|
||||
texture_id: "bworld:stone",
|
||||
has_collision: true,
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { ItemStack } from "../inventory.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:tree", {
|
||||
texture_id: "bworld:tree",
|
||||
has_collision: false,
|
||||
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
const player_inventory = player.get(PlayerComponent)!.player_inventory;
|
||||
const maybe_item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (maybe_item && maybe_item.type_id === "bworld:axe") {
|
||||
tile.id = "bworld:grass";
|
||||
player_inventory.container.add_item(new ItemStack("bworld:log", 7));
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:water", {
|
||||
texture_id: "bworld:water",
|
||||
has_collision: false,
|
||||
});
|
||||
Reference in New Issue
Block a user