Remove duplicate code
This commit is contained in:
+17
-30
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user