Remove duplicate code

This commit is contained in:
2026-10-13 11:57:43 -03:00
parent dfc6a6d77b
commit 5165698fe3
50 changed files with 307 additions and 812 deletions
+17 -30
View File
@@ -303,14 +303,28 @@ export class MeleeAttackGoal extends Goal {
}
}
// goes after the nearest player it can see within follow_range
export class NearestAttackablePlayerGoal extends Goal {
// picks the mob's target, and keeps it while it's alive and within follow_range
abstract class TargetGoal extends Goal {
override flags: GoalFlag[] = ["target"];
constructor(readonly mob: Mob) {
super();
}
override can_continue_to_use() {
const target = this.mob.target;
const mob = this.mob;
return target !== undefined && is_alive(target) &&
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
}
override stop() {
this.mob.target = undefined;
}
}
// goes after the nearest player it can see within follow_range
export class NearestAttackablePlayerGoal extends TargetGoal {
#find() {
const mob = this.mob;
return mob.level.nearest_player(mob.x, mob.y, mob.z, mob.attribute(ATTRIBUTES.follow_range));
@@ -320,52 +334,25 @@ export class NearestAttackablePlayerGoal extends Goal {
return this.mob.random() < 0.1 && this.#find() !== undefined;
}
override can_continue_to_use() {
const target = this.mob.target;
const mob = this.mob;
return target !== undefined && is_alive(target) &&
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
}
override start() {
this.mob.target = this.#find();
}
override stop() {
this.mob.target = undefined;
}
}
// goes after whoever hurt it last, like minecraft's HurtByTargetGoal, for as long as they're within follow_range
export class HurtByTargetGoal extends Goal {
override flags: GoalFlag[] = ["target"];
export class HurtByTargetGoal extends TargetGoal {
// the Mob.times_hurt_by_attacker it last went after someone for, so each hit only starts it once
#handled = 0;
constructor(readonly mob: Mob) {
super();
}
can_use() {
const attacker = this.mob.last_attacker;
return this.mob.times_hurt_by_attacker !== this.#handled && attacker !== undefined && is_alive(attacker);
}
override can_continue_to_use() {
const target = this.mob.target;
const mob = this.mob;
return target !== undefined && is_alive(target) &&
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
}
override start() {
this.#handled = this.mob.times_hurt_by_attacker;
this.mob.target = this.mob.last_attacker;
}
override stop() {
this.mob.target = undefined;
}
}
function is_alive(target: LivingEntity) {
+1 -8
View File
@@ -10,6 +10,7 @@ import { ItemEntity } from "./item_entity.ts";
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
import type { EntityData } from "$/common/protocol.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { wrap_angle } from "$/common/utils.ts";
// minecraft's despawning distances: gone at once past the first, maybe after a while past the second
const DESPAWN_DISTANCE = 128;
@@ -401,14 +402,6 @@ export class JumpControl {
}
}
// -pi to pi
export function wrap_angle(angle: number) {
angle %= Math.PI * 2;
if (angle >= Math.PI) angle -= Math.PI * 2;
if (angle < -Math.PI) angle += Math.PI * 2;
return angle;
}
// from one angle towards another the short way round, at most max
export function approach_angle(from: number, to: number, max: number) {
const difference = Math.max(-max, Math.min(max, wrap_angle(to - from)));