Messy but working chicken

This commit is contained in:
2026-09-28 17:42:56 -03:00
parent fe149bb9ad
commit d606bc4b55
22 changed files with 622 additions and 21 deletions
+5
View File
@@ -14,6 +14,11 @@ export function register_entity_type(id: string, factory: EntityFactory) {
types.set(id, factory);
}
// every kind that can be summoned, items can't be without an item
export function entity_type_ids() {
return [...types.keys()].filter((id) => id !== ITEM_ENTITY_TYPE).sort();
}
export function has_entity_type(id: string) {
return types.has(id);
}
+40
View File
@@ -0,0 +1,40 @@
// 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
import type { EntityJson } from "$/common/mod_data.ts";
import { ATTRIBUTES, type AttributeType } from "./attributes.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 {
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],
];
}
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);
}
+21
View File
@@ -55,6 +55,7 @@ import {
type SavedItemEntity,
} from "./entity/item_entity.ts";
import { EntityManager } from "./entity/entity_manager.ts";
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
import type { SavedEntity } from "./entity/entity.ts";
// how far from a player's eyes a block can be changed, a bit more than the client's reach
@@ -1046,6 +1047,26 @@ export class GameServer {
),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("summon").describe("Spawns an entity where you are").then(
argument<string, ServerCommandSource>("entity", word())
.suggests((partial) =>
entity_type_ids().filter((id) => id.startsWith(partial)).map((text) => ({ text }))
)
.executes((context) => {
const id = context.get<string>("entity");
const entity = entity_type_ids().includes(id) ? create_entity(this.entities, id) : undefined;
if (!entity) throw new CommandError(`There's no entity ${id}`);
const player = context.source.server_player;
entity.set_position(player.x, player.y, player.z);
entity.yaw = entity.head_yaw = player.yaw;
this.entities.add_entity(entity);
context.source.send_message(`Summoned ${id}`);
}),
),
);
register(
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
if (!this.loop) {
+5
View File
@@ -6,6 +6,8 @@ import { load_worldgen } from "$/common/worldgen_loader.ts";
import { GameHost, GameServer } from "./game_server.ts";
import type { WorldSave } from "./saves.ts";
import { ModRuntime } from "./mod_runtime.ts";
import { register_entity_type } from "./entity/entity_types.ts";
import { mod_entity_factory } from "./entity/mod_entity.ts";
export interface ServerModSource {
listing: ModListing;
@@ -22,6 +24,9 @@ export async function start_game(
mods: ServerModSource[],
): Promise<GameServer> {
const recipes = register_mod_data(mods.map((mod) => ({ id: mod.listing.id, data: mod.data })));
for (const mod of mods) {
for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json));
}
const worldgen_scripts = mods.flatMap((mod) =>
mod.worldgen_url ? [{ mod: mod.listing.id, url: mod.worldgen_url }] : []
+2
View File
@@ -100,6 +100,8 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
items: entries(bmod.data.items),
recipes: entries(bmod.data.recipes),
ores: entries(bmod.data.ores),
entities: entries(bmod.data.entities),
entity_models: entries(bmod.data.entity_models),
textures: [...bmod.textures.keys()],
texture_files: new Map(),
};