// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the kinds of entity mods define in entities/.json (see EntityJson): animals that wander around, panic when hurt // and look at players now and then, like minecraft's simpler animals, monsters that go after players, and scripted // ones that stay where scripts put them import type { EntityJson } from "@/common/mod_data.ts"; import { ATTRIBUTES, type AttributeType } from "./attributes.ts"; import { Entity } from "./entity.ts"; import type { EntityFactory } from "./entity_types.ts"; import { FloatGoal, HurtByTargetGoal, LookAtPlayerGoal, MeleeAttackGoal, NearestAttackablePlayerGoal, PanicGoal, RandomLookAroundGoal, RandomStrollGoal, } from "./goals.ts"; import { Mob, type MobCategory } 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 { if (json.behavior === "scripted") { return (level) => new ScriptedEntity(level, json); } const monster = json.behavior === "monster"; class ModEntity extends Mob { readonly type = json.id; override width = json.width; override height = json.height; override eye_height = json.height * 0.85; override get category(): MobCategory { return monster ? "monster" : "creature"; } override default_attributes(): [AttributeType, number?][] { return [ ...super.default_attributes(), [ATTRIBUTES.max_health, json.max_health ?? 10], [ATTRIBUTES.movement_speed, json.movement_speed ?? 0.25], [ATTRIBUTES.physical_shielding, json.physical_shielding ?? 0], [ATTRIBUTES.arcane_shielding, json.arcane_shielding ?? 0], [ATTRIBUTES.attack_damage, json.attack_damage ?? 2], ]; } override register_goals() { if (monster) { // like undergarden's rotspawn and minecraft's zombies this.goals.add_goal(0, new FloatGoal(this)); this.goals.add_goal(1, new MeleeAttackGoal(this, 1.25)); this.goals.add_goal(2, new RandomStrollGoal(this, 0.6)); this.goals.add_goal(3, new RandomLookAroundGoal(this)); this.target_goals.add_goal(0, new HurtByTargetGoal(this)); this.target_goals.add_goal(1, new NearestAttackablePlayerGoal(this)); return; } 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. monsters go away when no player is near override remove_when_far_away() { return monster; } } return (level) => new ModEntity(level); } // it doesn't fall, think or get hurt, the script that spawned it moves it and takes it away, like the fishing hook. // it isn't saved with the world: after a restart that script wouldn't know about it anymore class ScriptedEntity extends Entity { readonly type: string; override no_gravity = true; constructor(level: Entity["level"], json: EntityJson) { super(level); this.type = json.id; this.width = json.width; this.height = json.height; this.eye_height = json.height * 0.85; } override tick() { this.base_tick(); } override get persistent() { return false; } }