71 lines
2.5 KiB
TypeScript
71 lines
2.5 KiB
TypeScript
// the kinds of entity mods define in entities/<name>.json (see EntityJson): animals that wander around, panic when hurt
|
|
// and look at players now and then, like minecraft's simpler animals, 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, 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 {
|
|
if (json.behavior === "scripted") {
|
|
return (level) => new ScriptedEntity(level, json);
|
|
}
|
|
|
|
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],
|
|
[ATTRIBUTES.physical_shielding, json.physical_shielding ?? 0],
|
|
[ATTRIBUTES.arcane_shielding, json.arcane_shielding ?? 0],
|
|
];
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|