Files
bworld/server/game/entity/mod_entity.ts
T
2026-09-28 17:42:56 -03:00

41 lines
1.6 KiB
TypeScript

// mobs mods define in entities/<name>.json (see EntityJson): each one is a kind of animal that wanders around,
// panics when hurt and looks at players now and then, like minecraft's simpler animals
import type { EntityJson } from "$/common/mod_data.ts";
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
import type { EntityFactory } from "./entity_types.ts";
import { FloatGoal, LookAtPlayerGoal, PanicGoal, RandomLookAroundGoal, RandomStrollGoal } from "./goals.ts";
import { Mob } from "./mob.ts";
// the entity's class. the attributes are read while LivingEntity is being built, before any field of a subclass is
// set, so each kind gets its own class that has its json already
export function mod_entity_factory(json: EntityJson): EntityFactory {
class ModEntity extends Mob {
readonly type = json.id;
override width = json.width;
override height = json.height;
override eye_height = json.height * 0.85;
override default_attributes(): [AttributeType, number?][] {
return [
...super.default_attributes(),
[ATTRIBUTES.max_health, json.max_health ?? 10],
[ATTRIBUTES.movement_speed, json.movement_speed ?? 0.25],
];
}
override register_goals() {
this.goals.add_goal(0, new FloatGoal(this));
this.goals.add_goal(1, new PanicGoal(this, 1.4));
this.goals.add_goal(5, new RandomStrollGoal(this, 1));
this.goals.add_goal(6, new LookAtPlayerGoal(this, 6));
this.goals.add_goal(7, new RandomLookAroundGoal(this));
}
// animals stay, like minecraft's farm animals
override remove_when_far_away() {
return false;
}
}
return (level) => new ModEntity(level);
}