Some block drops
This commit is contained in:
@@ -5,20 +5,22 @@ import { PlayerComponent } from "../player.ts";
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:dirt", {
|
||||
textures: "bworld:dirt",
|
||||
has_collision: false,
|
||||
drop_table: "bworld:dirt",
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
on_interact(dimension, block) {
|
||||
const [player] = dimension.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";
|
||||
dimension.add_block({ x: block.x, y: block.y, z: block.z, id: "bworld:hoed_dirt" });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:hoed_dirt", {
|
||||
textures: "bworld:hoed_dirt",
|
||||
textures: { side: "bworld:dirt", top: "bworld:hoed_dirt", bottom: "bworld:dirt" },
|
||||
has_collision: false,
|
||||
drop_table: "bworld:dirt",
|
||||
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
@@ -53,6 +55,7 @@ EverythingRegistry.register<BlockRegistry>("blocks", "bworld:hoed_dirt", {
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:hoed_watered_dirt", {
|
||||
textures: "bworld:hoed_watered_dirt",
|
||||
has_collision: false,
|
||||
drop_table: "bworld:dirt",
|
||||
|
||||
on_click(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { PlayerComponent } from "../player.ts";
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:grass", {
|
||||
textures: { top: "bworld:grass_top", bottom: "bworld:dirt", "side": "bworld:grass_side" },
|
||||
has_collision: false,
|
||||
drop_table: "bworld:dirt",
|
||||
|
||||
on_interact(world, tile) {
|
||||
const [player] = world.get_tag("player")!;
|
||||
|
||||
@@ -3,6 +3,7 @@ import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:log", {
|
||||
textures: { side: "bworld:log_side", top: "bworld:log_top", bottom: "bworld:log_top" },
|
||||
has_collision: true,
|
||||
drop_table: "bworld:log",
|
||||
});
|
||||
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:leaves", {
|
||||
|
||||
@@ -3,4 +3,5 @@ import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:sand", {
|
||||
textures: "bworld:sand",
|
||||
has_collision: true,
|
||||
drop_table: "bworld:sand",
|
||||
});
|
||||
|
||||
@@ -3,4 +3,5 @@ import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.
|
||||
EverythingRegistry.register<BlockRegistry>("blocks", "bworld:stone", {
|
||||
textures: "bworld:stone",
|
||||
has_collision: true,
|
||||
drop_table: "bworld:stone",
|
||||
});
|
||||
|
||||
@@ -2,7 +2,10 @@ import { Component } from "$/common/ecs/mod.ts";
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { Faces } from "../../common/constants.ts";
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { ClientWorld } from "../client_world.ts";
|
||||
import { generate_chunk } from "../generation.ts";
|
||||
import { ItemStack } from "../inventory.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
import { gl, Texture } from "../renderer/mod.ts";
|
||||
import { Camera } from "./camera.ts";
|
||||
|
||||
@@ -30,11 +33,17 @@ export interface Chunk {
|
||||
}
|
||||
|
||||
export class Dimension extends Component {
|
||||
world: ClientWorld;
|
||||
image: Texture = AssetManager.instance.get("bworld:textures");
|
||||
chunks: Chunk[] = [];
|
||||
second_timer = 0;
|
||||
tick_timer = 0;
|
||||
|
||||
constructor(world: ClientWorld) {
|
||||
super();
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
add_chunk(x: number, z: number) {
|
||||
const chunk = { x, z, blocks: new Int16Array(CHUNK_AREA * CHUNK_HEIGHT), dirty: true, generated: false };
|
||||
this.chunks.push(chunk);
|
||||
@@ -90,12 +99,18 @@ export class Dimension extends Component {
|
||||
break_block(x: number, y: number, z: number) {
|
||||
const block_chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const block_chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
let chunk = this.get_chunk(block_chunk_x, block_chunk_z);
|
||||
const chunk = this.get_chunk(block_chunk_x, block_chunk_z);
|
||||
if (!chunk) {
|
||||
chunk = this.add_chunk(block_chunk_x, block_chunk_z);
|
||||
return;
|
||||
}
|
||||
|
||||
// const [nid, block_info] = EverythingRegistry.get_full<TileRegistry>("blocks", block.id)!;
|
||||
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", this.get_block(x, y, z))!;
|
||||
|
||||
if (block_info.drop_table) {
|
||||
const [player] = this.world.get_tag("player")!;
|
||||
const player_component = player.get(PlayerComponent)!;
|
||||
player_component.player_inventory.container.add_item(new ItemStack(block_info.drop_table));
|
||||
}
|
||||
|
||||
const lx = x - block_chunk_x * CHUNK_SIZE;
|
||||
const lz = z - block_chunk_z * CHUNK_SIZE;
|
||||
@@ -223,7 +238,7 @@ export class Dimension extends Component {
|
||||
|
||||
const block = dimension.get_block(bx, by, bz);
|
||||
|
||||
if (block && block !== 0) {
|
||||
if (block && block !== 0 && block !== -1) {
|
||||
let face: Faces;
|
||||
|
||||
if (bx > prev_bx) {
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ export function start_game(world: ClientWorld) {
|
||||
world.clear_entities();
|
||||
|
||||
const dimension = new Entity("dimension");
|
||||
world.dimension = new Dimension();
|
||||
world.dimension = new Dimension(world);
|
||||
dimension.add(world.dimension);
|
||||
world.add_entity(dimension);
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
|
||||
EverythingRegistry.register<ItemRegistry>("items", "bworld:dirt", {
|
||||
texture_id: "bworld:dirt",
|
||||
});
|
||||
@@ -1 +1,2 @@
|
||||
import "./watering_can.ts";
|
||||
import "./dirt.ts";
|
||||
|
||||
@@ -19,6 +19,12 @@ export class ClientLoop {
|
||||
}
|
||||
|
||||
start() {
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (!document.hidden) {
|
||||
this.last_time = performance.now();
|
||||
}
|
||||
});
|
||||
|
||||
this.running = true;
|
||||
const now = performance.now();
|
||||
this.last_time = now;
|
||||
|
||||
@@ -100,21 +100,25 @@ export class PlayerControlsSystem extends System {
|
||||
if (InputManager.is_mouse_pressed(0)) {
|
||||
world.dimension.break_block(block.x, block.y, block.z);
|
||||
} else if (InputManager.is_mouse_pressed(2)) {
|
||||
const FACE_OFFSETS = {
|
||||
top: { x: 0, y: 1, z: 0 },
|
||||
bottom: { x: 0, y: -1, z: 0 },
|
||||
north: { x: 0, y: 0, z: -1 },
|
||||
south: { x: 0, y: 0, z: 1 },
|
||||
west: { x: -1, y: 0, z: 0 },
|
||||
east: { x: 1, y: 0, z: 0 },
|
||||
};
|
||||
const offset = FACE_OFFSETS[block.face];
|
||||
world.dimension.add_block({
|
||||
x: block.x + offset.x,
|
||||
y: block.y + offset.y,
|
||||
z: block.z + offset.z,
|
||||
id: "bworld:dirt",
|
||||
});
|
||||
const inventory = player_component.player_inventory;
|
||||
const hotbar_slot = inventory.container.get_slot(inventory.hotbar_selected);
|
||||
if (hotbar_slot.has_item()) {
|
||||
const FACE_OFFSETS = {
|
||||
top: { x: 0, y: 1, z: 0 },
|
||||
bottom: { x: 0, y: -1, z: 0 },
|
||||
north: { x: 0, y: 0, z: -1 },
|
||||
south: { x: 0, y: 0, z: 1 },
|
||||
west: { x: -1, y: 0, z: 0 },
|
||||
east: { x: 1, y: 0, z: 0 },
|
||||
};
|
||||
const offset = FACE_OFFSETS[block.face];
|
||||
world.dimension.add_block({
|
||||
x: block.x + offset.x,
|
||||
y: block.y + offset.y,
|
||||
z: block.z + offset.z,
|
||||
id: "bworld:dirt",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user