Fishing hook
This commit is contained in:
@@ -191,6 +191,11 @@ export abstract class Entity {
|
||||
return MAX_AIR;
|
||||
}
|
||||
|
||||
// saved with the world. things scripts keep track of themselves aren't, they wouldn't know them after a restart
|
||||
get persistent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
set_position(x: number, y: number, z: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
@@ -250,7 +250,9 @@ export class EntityManager implements EntityLevel {
|
||||
|
||||
// entities riding something are saved with what they ride
|
||||
save(): SavedEntity[] {
|
||||
const saved = this.all().filter((entity) => !entity.vehicle).map((entity) => entity.save());
|
||||
const saved = this.all().filter((entity) => !entity.vehicle && entity.persistent).map((entity) =>
|
||||
entity.save()
|
||||
);
|
||||
return [...saved, ...this.#unknown];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// mobs mods define in entities/<name>.json (see EntityJson): each one is a kind of animal that wanders around,
|
||||
// panics when hurt and looks at players now and then, like minecraft's simpler animals
|
||||
// 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";
|
||||
@@ -9,6 +10,10 @@ 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;
|
||||
@@ -38,3 +43,26 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import type {
|
||||
CommandSource,
|
||||
Container,
|
||||
ContainerScreenOptions,
|
||||
EntityRef,
|
||||
EventSignal,
|
||||
ItemComponent,
|
||||
ItemStack,
|
||||
@@ -40,6 +41,8 @@ import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { OpenScreen, ServerPlayer } from "./player.ts";
|
||||
import { MAX_HEALTH } from "./vitals.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import type { ScreenLayout } from "$/common/protocol.ts";
|
||||
|
||||
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
|
||||
@@ -432,6 +435,15 @@ export class ModRuntime {
|
||||
},
|
||||
get_block_data: (x, y, z) => game().world.get_tile(x, y, z)?.mod_data as never,
|
||||
drop_item: (x, y, z, item) => void game().pop_item(x, y, z, new_stack(item)),
|
||||
spawn_entity: (id, x, y, z) => {
|
||||
const entity = EverythingRegistry.get("entities", id)
|
||||
? create_entity(game().entities, id)
|
||||
: undefined;
|
||||
if (!entity) throw new Error(`unknown entity ${id}`);
|
||||
entity.set_position(x, y, z);
|
||||
game().entities.add_entity(entity);
|
||||
return entity_ref(entity);
|
||||
},
|
||||
is_loaded: () => true,
|
||||
get seed() {
|
||||
return game().world.seed;
|
||||
@@ -788,3 +800,22 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function entity_ref(entity: Entity): EntityRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
type: entity.type,
|
||||
get position() {
|
||||
return { x: entity.x, y: entity.y, z: entity.z };
|
||||
},
|
||||
get removed() {
|
||||
return entity.removed;
|
||||
},
|
||||
teleport(x, y, z) {
|
||||
if (!entity.removed) entity.set_position(x, y, z);
|
||||
},
|
||||
remove() {
|
||||
entity.remove();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user