Jester time
This commit is contained in:
@@ -4,6 +4,8 @@ import type { Position, Schematic } from "cubi/server";
|
||||
// placed, so a plan that comes out too small can be tried again without touching the world
|
||||
|
||||
export const DOORWAY = "dangerous_business:doorway";
|
||||
// where the surface is placed, high above the interior. anyone higher than it is outside the facility
|
||||
export const SURFACE_Y = 150;
|
||||
export const VENT_SPOT = "dangerous_business:vent_spot";
|
||||
|
||||
// a marker block in a tile, a doorway or a vent spot, with its facing state: 0 north, 1 east, 2 south, 3 west. in a
|
||||
@@ -40,6 +42,10 @@ export interface TileType extends TileRules {
|
||||
vent_spots: Marker[];
|
||||
}
|
||||
|
||||
export function in_facility(position: Position) {
|
||||
return position.y <= SURFACE_Y;
|
||||
}
|
||||
|
||||
export interface PlacedTile {
|
||||
tile: string;
|
||||
// where the schematic's lowest corner goes, before it's turned around it
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
// the jester: a box on legs that wanders until it sees a player. it follows them for a while, stops to wind its crank,
|
||||
// and pops open, then runs down every player in the facility, faster and faster, until there's nobody left inside
|
||||
import type { MobRef, Player, ScriptGoal, ServerContext } from "cubi/server";
|
||||
import { type AnimatedEntity, AnimationState, type ClientContext, type Clip, type EntityPose } from "cubi/client";
|
||||
import { opens_doors } from "./coilhead.ts";
|
||||
import { in_facility } from "./interior.ts";
|
||||
|
||||
const JESTER = "dangerous_business:jester";
|
||||
|
||||
// its "state" client data, what its animator plays
|
||||
const ROAMING = 1;
|
||||
const CRANKING = 2;
|
||||
const AGGRO = 3;
|
||||
|
||||
// how far away it notices a player it can see
|
||||
const SEE_RANGE = 24;
|
||||
// how close it comes to the player it's following
|
||||
const FOLLOW_DISTANCE = 3;
|
||||
// seconds after it first notices someone before it starts cranking, shorter with more players around
|
||||
const COUNTDOWN_ALONE = [25, 42];
|
||||
const COUNTDOWN_TOGETHER = [13, 21];
|
||||
const CRANK_TICKS = 40 * 20;
|
||||
// how long it stands after popping before it goes
|
||||
const POP_TICKS = 2 * 20;
|
||||
// it chases a fifth faster than it walks, and another fifth every SPEED_UP_TICKS, up to twice as fast
|
||||
const SPEED_STEP = 0.2;
|
||||
const SPEED_UP_TICKS = 120;
|
||||
const MAX_SPEED_STEPS = 8;
|
||||
const CHASE_SPEED = "dangerous_business:jester_chase";
|
||||
// it only waits a moment at a closed door
|
||||
const OPEN_DOOR_TICKS = 20;
|
||||
|
||||
export function register_jester(ctx: ServerContext) {
|
||||
const builtin = ctx.entities.goals;
|
||||
const chase = builtin.melee_attack();
|
||||
const stroll = builtin.random_stroll();
|
||||
const look_around = builtin.random_look_around();
|
||||
|
||||
ctx.entities.register_behavior(JESTER, (mob, goals) => {
|
||||
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
||||
// ticks in its state
|
||||
let ticks = 0;
|
||||
// ticks left until it cranks, from when it first noticed someone
|
||||
let countdown: number | undefined;
|
||||
let followed: Player | undefined;
|
||||
const set_state = (next: number) => {
|
||||
state = next;
|
||||
ticks = 0;
|
||||
mob.set_client_data("state", next);
|
||||
};
|
||||
|
||||
// what it's doing, which the goals after it follow
|
||||
goals.add(0, {
|
||||
can_use: () => true,
|
||||
tick: () => {
|
||||
ticks++;
|
||||
if (state === ROAMING) {
|
||||
if (followed && (followed.health <= 0 || !in_facility(followed.position))) followed = undefined;
|
||||
followed ??= ctx.players.all().find((player) => can_see(ctx, mob, player));
|
||||
if (followed && countdown === undefined) {
|
||||
const [low, high] = ctx.players.all().length > 1 ? COUNTDOWN_TOGETHER : COUNTDOWN_ALONE;
|
||||
countdown = Math.round((low + (high - low) * mob.random()) * 20);
|
||||
}
|
||||
if (countdown !== undefined && --countdown <= 0) {
|
||||
countdown = undefined;
|
||||
followed = undefined;
|
||||
set_state(CRANKING);
|
||||
}
|
||||
} else if (state === CRANKING) {
|
||||
if (ticks >= CRANK_TICKS) set_state(AGGRO);
|
||||
} else {
|
||||
const target = nearest_player(ctx, mob);
|
||||
if (!target) {
|
||||
mob.target = undefined;
|
||||
mob.remove_modifier("cubi:movement_speed", CHASE_SPEED);
|
||||
set_state(ROAMING);
|
||||
return;
|
||||
}
|
||||
if (ticks < POP_TICKS) return;
|
||||
mob.target = target;
|
||||
const steps = Math.min(MAX_SPEED_STEPS, 1 + Math.floor((ticks - POP_TICKS) / SPEED_UP_TICKS));
|
||||
mob.add_modifier("cubi:movement_speed", {
|
||||
id: CHASE_SPEED,
|
||||
amount: steps * SPEED_STEP,
|
||||
operation: "add_multiplied_total",
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
// stands still while it cranks and while it pops
|
||||
goals.add(1, {
|
||||
flags: ["move", "look", "jump"],
|
||||
can_use: () => state === CRANKING || (state === AGGRO && ticks < POP_TICKS),
|
||||
start: () => mob.stop(),
|
||||
});
|
||||
goals.add(2, chase);
|
||||
goals.add(3, follow(mob, () => state === ROAMING ? followed : undefined));
|
||||
goals.add(4, stroll);
|
||||
goals.add(5, look_around);
|
||||
goals.add(6, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
|
||||
});
|
||||
}
|
||||
|
||||
// keeps close to who(), looking at them, and stands by them once it's there instead of wandering off. it goes a new
|
||||
// way only once they've moved to another block
|
||||
function follow(mob: MobRef, who: () => Player | undefined): ScriptGoal {
|
||||
let block = "";
|
||||
return {
|
||||
flags: ["move", "look"],
|
||||
can_use: () => who() !== undefined,
|
||||
start: () => block = "",
|
||||
stop: () => mob.stop(),
|
||||
tick: () => {
|
||||
// the goal deciding what the jester does can let them go this tick
|
||||
const player = who();
|
||||
if (!player) return;
|
||||
const { x, y, z } = player.position;
|
||||
mob.look_at(x, y + 1.5, z);
|
||||
const at = mob.position;
|
||||
if (Math.hypot(x - at.x, y - at.y, z - at.z) <= FOLLOW_DISTANCE) {
|
||||
if (block !== "") mob.stop();
|
||||
block = "";
|
||||
return;
|
||||
}
|
||||
const now = `${Math.floor(x)},${Math.floor(y)},${Math.floor(z)}`;
|
||||
if (now === block) return;
|
||||
block = now;
|
||||
mob.move_to(x, y, z);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// whether it can see the player's eyes from its own, close enough to notice them, in the facility
|
||||
function can_see(ctx: ServerContext, mob: MobRef, player: Player) {
|
||||
if (player.health <= 0 || !in_facility(player.position)) return false;
|
||||
const from = mob.eye_position;
|
||||
const to = player.eye_position;
|
||||
if (Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z) > SEE_RANGE) return false;
|
||||
return ctx.world.raycast(from, to) === undefined;
|
||||
}
|
||||
|
||||
// the closest living player in the facility, however far
|
||||
function nearest_player(ctx: ServerContext, mob: MobRef) {
|
||||
const at = mob.position;
|
||||
const distance = ({ position: { x, y, z } }: Player) => Math.hypot(x - at.x, y - at.y, z - at.z);
|
||||
const inside = ctx.players.all().filter((player) => player.health > 0 && in_facility(player.position));
|
||||
return inside.sort((a, b) => distance(a) - distance(b))[0] as Player | undefined;
|
||||
}
|
||||
|
||||
// what bedrock's animation controller did: idle and walk while it roams, crank, then pop, then its chase. what its
|
||||
// clips did with expressions is here as code
|
||||
export function register_jester_animator(ctx: ClientContext) {
|
||||
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
|
||||
const idle = clip("idle");
|
||||
const walk = clip("walk");
|
||||
const crank = clip("crank");
|
||||
const pop = clip("pop");
|
||||
const aggro_idle = clip("aggro_idle");
|
||||
const aggro_walk = clip("aggro_walk");
|
||||
const attack = clip("attack");
|
||||
|
||||
ctx.render.register_entity_animator(JESTER, (jester) => {
|
||||
const cranking = new AnimationState();
|
||||
const popped = new AnimationState();
|
||||
const attacking = new AnimationState();
|
||||
return {
|
||||
on_data(field, time) {
|
||||
if (field !== "state") return;
|
||||
const state = jester.get_client_data<number>("state");
|
||||
cranking.animate_when(state === CRANKING, time);
|
||||
popped.animate_when(state === AGGRO, time);
|
||||
},
|
||||
pose(pose, time) {
|
||||
if (cranking.started) {
|
||||
pose.play_state(crank, cranking, time);
|
||||
} else if (popped.started && !popped.finished(pop, time)) {
|
||||
pose.play_state(pop, popped, time);
|
||||
} else if (popped.started) {
|
||||
const swinging = jester.attack_progress(time) > 0;
|
||||
if (swinging && (!attacking.started || attacking.finished(attack, time))) attacking.start(time);
|
||||
if (attacking.started && !attacking.finished(attack, time)) {
|
||||
pose.play_state(attack, attacking, time);
|
||||
} else {
|
||||
// its walk goes half as fast as when it roams
|
||||
walking(pose, jester, time, aggro_walk, 40, (weight) => {
|
||||
pose.play(aggro_idle, time / 20, weight);
|
||||
sway(pose, time, weight, true);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
walking(pose, jester, time, walk, 20, (weight) => {
|
||||
pose.play(idle, time / 20, weight);
|
||||
sway(pose, time, weight, false);
|
||||
});
|
||||
// both clips hide them, which blended halfway would only shrink them
|
||||
pose.scale("tendrils", 0, 0, 0);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// clip as it walks, a second of it for ticks of walk_position, and standing as strongly as it isn't walking
|
||||
function walking(
|
||||
pose: EntityPose,
|
||||
jester: AnimatedEntity,
|
||||
time: number,
|
||||
clip: Clip,
|
||||
ticks: number,
|
||||
standing: (weight: number) => void,
|
||||
) {
|
||||
const speed = Math.min(jester.walk_speed(time), 1);
|
||||
pose.play(clip, jester.walk_position(time) / ticks, speed);
|
||||
standing(1 - speed);
|
||||
}
|
||||
|
||||
// the box bobbing and the arm swaying as it stands, and once it's popped the lid, skull, jaw and crank too
|
||||
function sway(pose: EntityPose, time: number, weight: number, popped: boolean) {
|
||||
// bedrock's math.cos and math.sin of life_time (seconds) times degrees a second
|
||||
const seconds = time / 20;
|
||||
const cos = (degrees_a_second: number) => Math.cos(seconds * degrees_a_second * Math.PI / 180) * weight;
|
||||
const sin = (degrees_a_second: number) => Math.sin(seconds * degrees_a_second * Math.PI / 180) * weight;
|
||||
pose.rotate("box", cos(180) * 2.5, 0, 0);
|
||||
pose.rotate("left_arm", (-1.6248 * weight) + cos(90) * 5, -22.22485 * weight, 70.78761 * weight);
|
||||
pose.rotate("left_forearm", cos(180) * 5, 62.5 * weight, 0);
|
||||
if (!popped) {
|
||||
pose.move("box", 0, -weight + cos(180), 0);
|
||||
pose.move("left_arm", 0, 3 * weight - cos(180), 0);
|
||||
return;
|
||||
}
|
||||
pose.rotate("tendrils", sin(180) * 2.5, 0, 0);
|
||||
pose.rotate("box_lid", -127.5 * weight + sin(180) * 5, 0, 0);
|
||||
pose.rotate("skull", cos(180) * -2.5, 0, 0);
|
||||
pose.rotate("jaw", 10 * weight + sin(180) * 5, 0, 0);
|
||||
pose.rotate("crank", sin(180) * 2.5, 0, 0);
|
||||
}
|
||||
@@ -3,7 +3,17 @@ import type { ServerContext } from "cubi/server";
|
||||
import { DOOR, FACILITY_DOOR } from "./shared.ts";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { register_coilhead, register_coilhead_animator } from "./coilhead.ts";
|
||||
import { DOORWAY, MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
|
||||
import { register_jester, register_jester_animator } from "./jester.ts";
|
||||
import {
|
||||
DOORWAY,
|
||||
MAIN_TILE,
|
||||
type Marker,
|
||||
type PlacedTile,
|
||||
plan_interior,
|
||||
SURFACE_Y,
|
||||
tile_type,
|
||||
TILES,
|
||||
} from "./interior.ts";
|
||||
|
||||
export { shared } from "./shared.ts";
|
||||
|
||||
@@ -15,11 +25,13 @@ export function server(ctx: ServerContext) {
|
||||
register_death(ctx);
|
||||
register_commands(ctx);
|
||||
register_coilhead(ctx);
|
||||
register_jester(ctx);
|
||||
}
|
||||
|
||||
export function client(ctx: ClientContext) {
|
||||
register_hud(ctx);
|
||||
register_coilhead_animator(ctx);
|
||||
register_jester_animator(ctx);
|
||||
}
|
||||
|
||||
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
|
||||
@@ -142,7 +154,7 @@ function clear_map(ctx: ServerContext) {
|
||||
function place_surface(ctx: ServerContext) {
|
||||
ctx.schematics.place(
|
||||
"dangerous_business:assurance",
|
||||
{ position: { x: -80, y: 150, z: -80 } },
|
||||
{ position: { x: -80, y: SURFACE_Y, z: -80 } },
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user