Health and hunger system
This commit is contained in:
@@ -22,6 +22,7 @@ import { ModLoadError } from "$/common/mod_loader.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { OpenScreen, ServerPlayer } from "./player.ts";
|
||||
import { MAX_HEALTH } from "./vitals.ts";
|
||||
import type { ScreenLayout } from "$/common/protocol.ts";
|
||||
|
||||
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
|
||||
@@ -89,6 +90,7 @@ export class ModRuntime {
|
||||
block_place: new Signal(),
|
||||
block_interact: new Signal(),
|
||||
chat_send: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
};
|
||||
after: After = {
|
||||
block_break: new Signal(),
|
||||
@@ -97,6 +99,7 @@ export class ModRuntime {
|
||||
chat_send: new Signal(),
|
||||
player_join: new Signal(),
|
||||
player_leave: new Signal(),
|
||||
player_death: new Signal(),
|
||||
server_start: new Signal(),
|
||||
tick: new Signal(),
|
||||
new_day: new Signal(),
|
||||
@@ -662,5 +665,35 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
teleport(x, y, z) {
|
||||
game.teleport(player, x, y, z);
|
||||
},
|
||||
get health() {
|
||||
return player.vitals.health;
|
||||
},
|
||||
max_health: MAX_HEALTH,
|
||||
get food() {
|
||||
return player.vitals.food;
|
||||
},
|
||||
get saturation() {
|
||||
return player.vitals.saturation;
|
||||
},
|
||||
damage(amount, cause = "script") {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`damage must be a number`);
|
||||
game.damage_player(player, amount, String(cause));
|
||||
},
|
||||
heal(amount) {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`heal must be a number`);
|
||||
game.heal_player(player, amount);
|
||||
},
|
||||
feed(nutrition, saturation_modifier) {
|
||||
if (![nutrition, saturation_modifier].every((n) => typeof n === "number" && Number.isFinite(n) && n >= 0)) {
|
||||
throw new Error("feed takes two numbers of 0 or more");
|
||||
}
|
||||
player.vitals.eat(nutrition, saturation_modifier);
|
||||
},
|
||||
add_exhaustion(amount) {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) {
|
||||
throw new Error("exhaustion must be a number of 0 or more");
|
||||
}
|
||||
player.vitals.add_exhaustion(amount);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user