Undergarden mod !
This commit is contained in:
@@ -8,7 +8,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { type Collisions, move_body } from "$/common/physics.ts";
|
||||
import type { EntityData, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
|
||||
// below this, entities have fallen out of the world
|
||||
@@ -71,7 +71,7 @@ export interface EntityLevel {
|
||||
// an item on the ground was picked up whole by who, clients see it fly to them. it's gone after
|
||||
take_item(item: Entity, by: string): void;
|
||||
// sent to clients tracking the entity, for the renderer: "hurt" flashes it red, "death" starts falling over
|
||||
entity_event(entity: Entity, event: "hurt" | "death"): void;
|
||||
entity_event(entity: Entity, event: EntityEvent): void;
|
||||
// it died, after dropping its things
|
||||
on_death(entity: Entity, source: DamageSource): void;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { CHUNK_SIZE } from "$/common/constants.ts";
|
||||
import { is_within_view } from "$/common/chunk_streaming.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityInfo, ServerMessage } from "$/common/protocol.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { ServerWorld } from "../world.ts";
|
||||
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
|
||||
@@ -131,7 +131,7 @@ export class EntityManager implements EntityLevel {
|
||||
}
|
||||
|
||||
// players aren't tracked like entities, everyone else knows about them
|
||||
entity_event(entity: Entity, event: "hurt" | "death") {
|
||||
entity_event(entity: Entity, event: EntityEvent) {
|
||||
const is_player = this.#host.players().includes(entity as ServerPlayer);
|
||||
for (const [player, tracked] of this.#tracked) {
|
||||
if (is_player ? player !== entity : tracked.has(entity.id)) {
|
||||
|
||||
@@ -293,6 +293,8 @@ export class MeleeAttackGoal extends Goal {
|
||||
const distance = (target.x - mob.x) ** 2 + (target.y - mob.y) ** 2 + (target.z - mob.z) ** 2;
|
||||
if (distance <= reach && this.#cooldown === 0) {
|
||||
this.#cooldown = this.attack_interval;
|
||||
// clients see its arm swing, hit or not
|
||||
mob.level.entity_event(mob, "swing");
|
||||
mob.do_hurt_target(target);
|
||||
}
|
||||
}
|
||||
@@ -331,6 +333,38 @@ export class NearestAttackablePlayerGoal extends Goal {
|
||||
}
|
||||
}
|
||||
|
||||
// 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"];
|
||||
// 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) {
|
||||
return target.alive;
|
||||
}
|
||||
|
||||
@@ -57,6 +57,9 @@ export abstract class Mob extends LivingEntity {
|
||||
target_goals = new GoalSelector();
|
||||
// who it's after, set by target goals
|
||||
target: LivingEntity | undefined;
|
||||
// who hurt it last, and how many times something has, for HurtByTargetGoal
|
||||
last_attacker: LivingEntity | undefined;
|
||||
times_hurt_by_attacker = 0;
|
||||
|
||||
move_control = new MoveControl(this);
|
||||
look_control = new LookControl(this);
|
||||
@@ -109,6 +112,10 @@ export abstract class Mob extends LivingEntity {
|
||||
override on_hurt(source: DamageSource) {
|
||||
super.on_hurt(source);
|
||||
this.no_action_time = 0;
|
||||
if (source.attacker instanceof LivingEntity && source.attacker !== this) {
|
||||
this.last_attacker = source.attacker;
|
||||
this.times_hurt_by_attacker += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// despawning
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
// 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
|
||||
// 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, LookAtPlayerGoal, PanicGoal, RandomLookAroundGoal, RandomStrollGoal } from "./goals.ts";
|
||||
import {
|
||||
FloatGoal,
|
||||
HurtByTargetGoal,
|
||||
LookAtPlayerGoal,
|
||||
MeleeAttackGoal,
|
||||
NearestAttackablePlayerGoal,
|
||||
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
|
||||
@@ -14,6 +24,8 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
||||
return (level) => new ScriptedEntity(level, json);
|
||||
}
|
||||
|
||||
const monster = json.behavior === "monster";
|
||||
|
||||
class ModEntity extends Mob {
|
||||
readonly type = json.id;
|
||||
override width = json.width;
|
||||
@@ -27,10 +39,21 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
||||
[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));
|
||||
@@ -38,9 +61,9 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
||||
this.goals.add_goal(7, new RandomLookAroundGoal(this));
|
||||
}
|
||||
|
||||
// animals stay, like minecraft's farm animals
|
||||
// animals stay, like minecraft's farm animals. monsters go away when no player is near
|
||||
override remove_when_far_away() {
|
||||
return false;
|
||||
return monster;
|
||||
}
|
||||
}
|
||||
return (level) => new ModEntity(level);
|
||||
|
||||
Reference in New Issue
Block a user