Make players mobs too !

This commit is contained in:
2026-09-30 10:42:52 -03:00
parent 65f27d6099
commit 4fbcca6851
23 changed files with 314 additions and 132 deletions
+10 -11
View File
@@ -1,5 +1,5 @@
// a player's health, hunger and saturation, with minecraft's rules (its FoodData). the server owns them, clients only
// draw them
// a player's hunger and saturation, with minecraft's rules (its FoodData), and what they do to health. the server
// owns them, clients only draw them
import { TICKS_PER_SECOND } from "$/common/constants.ts";
export const MAX_HEALTH = 20;
@@ -37,7 +37,6 @@ export interface SavedVitals {
}
export class Vitals {
health = MAX_HEALTH;
food = MAX_FOOD;
saturation = START_SATURATION;
exhaustion = 0;
@@ -57,8 +56,9 @@ export class Vitals {
return this.food < MAX_FOOD;
}
// one tick. heal and hurt go through the game server, which fires events and handles dying
tick(heal: (amount: number) => void, hurt: (amount: number) => void) {
// one tick, for someone with that much health. heal and hurt go through the game server, which fires events and
// handles dying
tick(health: number, heal: (amount: number) => void, hurt: (amount: number) => void) {
if (this.exhaustion > EXHAUSTION_PER_POINT) {
this.exhaustion -= EXHAUSTION_PER_POINT;
if (this.saturation > 0) {
@@ -68,7 +68,7 @@ export class Vitals {
}
}
const hurt_some = this.health < MAX_HEALTH;
const hurt_some = health < MAX_HEALTH;
if (this.saturation > 0 && hurt_some && this.food >= MAX_FOOD) {
if (++this.#timer >= FAST_REGEN_TICKS) {
const amount = Math.min(this.saturation, 6);
@@ -84,7 +84,7 @@ export class Vitals {
}
} else if (this.food <= 0) {
if (++this.#timer >= STARVE_TICKS) {
if (this.health > STARVE_MIN_HEALTH) hurt(1);
if (health > STARVE_MIN_HEALTH) hurt(1);
this.#timer = 0;
}
} else {
@@ -93,20 +93,19 @@ export class Vitals {
}
reset() {
this.health = MAX_HEALTH;
this.food = MAX_FOOD;
this.saturation = START_SATURATION;
this.exhaustion = 0;
this.#timer = 0;
}
save(): SavedVitals {
return { health: this.health, food: this.food, saturation: this.saturation, exhaustion: this.exhaustion };
// health is the player's, they add it
save(): Omit<SavedVitals, "health"> {
return { food: this.food, saturation: this.saturation, exhaustion: this.exhaustion };
}
load(saved: SavedVitals | undefined) {
if (!saved) return;
this.health = saved.health;
this.food = saved.food;
this.saturation = saved.saturation;
this.exhaustion = saved.exhaustion;