Engine api
This commit is contained in:
@@ -25,7 +25,9 @@ export function confirm_mods(address: ServerAddress, welcome: Welcome): Promise<
|
||||
list.style.cssText = "margin:0;padding-left:20px;";
|
||||
for (const mod of welcome.mods) {
|
||||
const item = document.createElement("li");
|
||||
item.textContent = `${mod.name} ${mod.version} (${mod.id})`;
|
||||
// it can change anything about the game, not just add to it
|
||||
const access = mod.engine_access ? ", changes the engine" : "";
|
||||
item.textContent = `${mod.name} ${mod.version} (${mod.id}${access})`;
|
||||
list.append(item);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// what engine access mods get as engine.client, in client scripts. see docs/modding/misc/engine.md
|
||||
import { Client } from "./client.ts";
|
||||
import { Entity } from "./entity/entity.ts";
|
||||
import { LocalPlayer } from "./entity/local_player.ts";
|
||||
import { Player } from "./entity/player.ts";
|
||||
import { RemoteEntity } from "./entity/remote_entity.ts";
|
||||
import { RemotePlayer } from "./entity/remote_player.ts";
|
||||
import { ItemEntity } from "./entity/item_entity.ts";
|
||||
import { ClientLevel } from "./level/client_level.ts";
|
||||
import { GameRenderer } from "./rendering/game_renderer.ts";
|
||||
import { LevelRenderer } from "./rendering/level_renderer.ts";
|
||||
import { HandRenderer } from "./rendering/hand_renderer.ts";
|
||||
import { Camera } from "./camera.ts";
|
||||
import { InputManager } from "./input_manager.ts";
|
||||
import { ClientMenu } from "./menu.ts";
|
||||
import { GuiMenuScreen, GuiScreen } from "./gui/gui_screen.ts";
|
||||
|
||||
// built when it's exposed, not when this module loads, so import cycles can't leave anything undefined
|
||||
export function client_engine() {
|
||||
return {
|
||||
Client,
|
||||
Entity,
|
||||
LocalPlayer,
|
||||
Player,
|
||||
RemoteEntity,
|
||||
RemotePlayer,
|
||||
ItemEntity,
|
||||
ClientLevel,
|
||||
GameRenderer,
|
||||
LevelRenderer,
|
||||
HandRenderer,
|
||||
Camera,
|
||||
InputManager,
|
||||
ClientMenu,
|
||||
GuiScreen,
|
||||
GuiMenuScreen,
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import { physics } from "$/common/physics.ts";
|
||||
import type { ClientLevel } from "../level/client_level.ts";
|
||||
|
||||
// anything that exists in the level and moves, like minecraft's Entity. position is the middle of its feet.
|
||||
@@ -61,12 +61,12 @@ export abstract class Entity {
|
||||
|
||||
// in water, like the server's Entity.in_water
|
||||
get in_water() {
|
||||
return in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water
|
||||
get eyes_in_water() {
|
||||
return eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
@@ -130,7 +130,7 @@ export abstract class Entity {
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
||||
const can_step = this.#air_ticks <= this.step_grace_ticks && this.vy <= 0;
|
||||
const friction = this.horizontal_friction();
|
||||
const collisions = move_body(this, this.gravity, is_solid, this.step_height, can_step);
|
||||
const collisions = physics.move_body(this, this.gravity, is_solid, this.step_height, can_step);
|
||||
this.colliding_x = collisions.x;
|
||||
this.colliding_y = collisions.y;
|
||||
this.colliding_z = collisions.z;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
|
||||
import { back_off_from_edge, box_is_free } from "$/common/physics.ts";
|
||||
import { physics } from "$/common/physics.ts";
|
||||
import type { Client } from "../client.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
import { ClientInventories } from "../inventory.ts";
|
||||
@@ -158,7 +158,7 @@ export class LocalPlayer extends Player {
|
||||
// not while jumping, so it can still jump off a ledge
|
||||
if (this.sneaking && this.on_ground && this.vy <= 0) {
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
||||
const [dx, dz] = back_off_from_edge(
|
||||
const [dx, dz] = physics.back_off_from_edge(
|
||||
this,
|
||||
this.vx * TICK_DELTA,
|
||||
this.vz * TICK_DELTA,
|
||||
@@ -195,7 +195,9 @@ export class LocalPlayer extends Player {
|
||||
vy: 0,
|
||||
vz: 0,
|
||||
};
|
||||
if (box_is_free(standing, 0, 0, 0, (x, y, z) => this.level.collision_shape(x, y, z))) this.set_swimming(false);
|
||||
if (physics.box_is_free(standing, 0, 0, 0, (x, y, z) => this.level.collision_shape(x, y, z))) {
|
||||
this.set_swimming(false);
|
||||
}
|
||||
}
|
||||
|
||||
// turns up or down toward where it's looking. not up once its head is out, so it swims along the surface instead
|
||||
@@ -234,7 +236,7 @@ export class LocalPlayer extends Player {
|
||||
const blocked = (x: number, y: number, z: number) =>
|
||||
this.level.is_liquid(x, y, z) || this.level.collision_shape(x, y, z);
|
||||
const dy = from_y - this.y + this.vy * TICK_DELTA + WATER_HOP_HEIGHT;
|
||||
if (box_is_free(this, this.vx * TICK_DELTA, dy, this.vz * TICK_DELTA, blocked)) this.vy = WATER_HOP;
|
||||
if (physics.box_is_free(this, this.vx * TICK_DELTA, dy, this.vz * TICK_DELTA, blocked)) this.vy = WATER_HOP;
|
||||
}
|
||||
|
||||
// how much wider the view is than the fov option, between the last tick and this one
|
||||
|
||||
+31
-12
@@ -14,6 +14,9 @@ import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { Client } from "./client.ts";
|
||||
import { code_url, fetch_verified } from "./handshake.ts";
|
||||
import { GuiModScreen } from "./gui/gui_mod_screens.ts";
|
||||
import { clear_hooks, expose_engine, loading_mod } from "$/common/hooks.ts";
|
||||
import { common_engine } from "$/common/engine_modules.ts";
|
||||
import { client_engine } from "./engine_modules.ts";
|
||||
|
||||
// each loaded mod's credits file, for the credits screen
|
||||
export const mod_credits: { name: string; version: string; text: string }[] = [];
|
||||
@@ -110,6 +113,10 @@ export async function download_mods(listings: ModListing[], base: URL): Promise<
|
||||
const bytes = await fetch_verified(new URL(listing.file, base), listing.sha256, `${listing.name}`);
|
||||
const bmod = read_bmod(bytes, `${listing.id}.bmod`);
|
||||
if (bmod.manifest.id !== listing.id) throw new Error(`the server listed it as ${listing.id}`);
|
||||
// the confirm screen said whether it has engine access, going by the listing
|
||||
if ((bmod.manifest.engine_access ?? false) !== (listing.engine_access ?? false)) {
|
||||
throw new Error("its engine access isn't what the server listed");
|
||||
}
|
||||
return bmod;
|
||||
} catch (e) {
|
||||
throw new ModLoadError(listing.id, (e as Error).message);
|
||||
@@ -133,26 +140,38 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
|
||||
mod_names.clear();
|
||||
for (const listing of listings) mod_names.set(listing.id, listing.name);
|
||||
|
||||
// for engine access mods. the last server's hooks go first
|
||||
clear_hooks();
|
||||
expose_engine({ common: common_engine(), client: client_engine() });
|
||||
const as_mod = (bmod: Bmod, fn: () => Promise<void>) =>
|
||||
loading_mod(bmod.manifest.id, bmod.manifest.engine_access ?? false, fn);
|
||||
|
||||
// every mod's shared rules first, so client scripts can count on any mod's being there
|
||||
block_rules = new BlockRules(run_mod);
|
||||
rules_setup = true;
|
||||
for (const [i, bmod] of bmods.entries()) {
|
||||
if (!bmod.scripts.shared) continue;
|
||||
const module = await import(script_url(bmod.scripts.shared));
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(bmod.manifest.id, "the shared script doesn't export a setup function");
|
||||
}
|
||||
await module.setup(shared_context(listings[i]));
|
||||
const code = bmod.scripts.shared;
|
||||
if (!code) continue;
|
||||
await as_mod(bmod, async () => {
|
||||
const module = await import(script_url(code));
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(bmod.manifest.id, "the shared script doesn't export a setup function");
|
||||
}
|
||||
await module.setup(shared_context(listings[i]));
|
||||
});
|
||||
}
|
||||
rules_setup = false;
|
||||
|
||||
for (const [i, bmod] of bmods.entries()) {
|
||||
if (!bmod.scripts.client) continue;
|
||||
const module = await import(script_url(bmod.scripts.client));
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(bmod.manifest.id, "the client script doesn't export a setup function");
|
||||
}
|
||||
await module.setup(client_context(listings[i]));
|
||||
const code = bmod.scripts.client;
|
||||
if (!code) continue;
|
||||
await as_mod(bmod, async () => {
|
||||
const module = await import(script_url(code));
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(bmod.manifest.id, "the client script doesn't export a setup function");
|
||||
}
|
||||
await module.setup(client_context(listings[i]));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user