Big API refactor

This commit is contained in:
2026-06-28 00:12:52 -05:00
parent db9a5d37b6
commit 9de31d933b
31 changed files with 1036 additions and 1029 deletions
+30 -31
View File
@@ -6,66 +6,65 @@ start = "start"
# Objects reference a script by name via `script_name`.
[scripts]
greeter = """
fn init() {
log(`hello from object — player at ${Board.player_x}, ${Board.player_y}`);
fn init(me, state) {
log(`hello from object — player at ${state.player.x}, ${state.player.y}`);
set_tile(2); // change my glyph to ☻ — proves the write path
say("Hello there,\\ntraveller!");
log(`Player health: ${Player.health}`);
log(`Player health: ${state.player.health}`);
}
fn tick(dt) {
//log(`x: ${Me.x} ${Board.player_x} y: ${Me.y} ${Board.player_y} q: ${Queue.length()}`);
if Board.player_x == Me.x && Board.player_y == Me.y && Queue.length() == 0 {
fn tick(me, state, dt) {
if state.player.x == me.x && state.player.y == me.y && !me.waiting {
say("Hey! Get offa me!");
delay(5.0);
me.delay(5.0);
}
}
"""
mover = """
fn init() {
fn init(me, state) {
if Registry.get("dancer_x") != () {
let new_x = Registry.get("dancer_x");
let new_y = Registry.get("dancer_y");
if Me.x != new_x || Me.y != new_y {
if me.x != new_x || me.y != new_y {
teleport(new_x, new_y);
}
} else {
Registry.set("dancer_x", Me.x);
Registry.set("dancer_y", Me.y);
log(`Set reg to ${Me.x} ${Me.y}`);
Registry.set("dancer_x", me.x);
Registry.set("dancer_y", me.y);
log(`Set reg to ${me.x} ${me.y}`);
}
}
// move() costs 250 ms, so the object steps ~4 cells/sec no matter how often
// tick() runs. Queue.length() avoids piling up moves; blocked() avoids walking
// into a wall (or another object's already-queued move this tick).
fn tick(dt) {
if Queue.length() == 0 { dance(); }
fn tick(me, state, dt) {
if !me.waiting { dance(me); }
}
fn dance() {
fn dance(me) {
move(East);
move(South);
move(North);
move(East);
say("Hey!", 0.5);
delay(0.5);
delay(me, 0.5);
move(West);
move(South);
move(North);
move(West);
say("Ho!", 0.5);
delay(0.5);
me.delay(0.5);
}
// Fires when something steps into this object; id is the bumper's object id,
// or -1 for the player.
fn bump(id) {
fn bump(me, state, id) {
log(`mover bumped by ${id}`);
say("Ow!");
}
"""
muffin = """
fn bump(id) {
fn bump(me, state, _id) {
scroll([
"You find a small muffin on the ground, your favorite kind.",
"It smells of cinnamon and warm mornings.",
@@ -73,7 +72,7 @@ fn bump(id) {
["ignore", "This is obviously a trap ignore it."]
]);
}
fn eat() {
fn eat(me, state) {
say("Yeah it was poisoned.");
alter_health(-2);
}
@@ -84,7 +83,7 @@ fn ignore() {
"""
noticeboard = """
fn bump(id) {
fn bump(me, state, id) {
scroll([
" TOWN NOTICE BOARD",
" ",
@@ -111,13 +110,13 @@ fn bump(id) {
"dusk. All residents are warmly encouraged to attend.",
" ",
" Posted by order of the Town Council.",
" Unauthorised notices will be removed.",
" Unauthorized notices will be removed.",
]);
}
"""
bookshelf = """
fn bump(id) {
fn bump(me, state, id) {
scroll([
" THE BOOKSHELF",
" ",
@@ -136,13 +135,13 @@ fn bump(id) {
"""
fireplace = """
fn bump(id) {
fn bump(me, state, id) {
say("The fire crackles warmly.\\nYou feel at ease.");
}
"""
chest = """
fn bump(id) {
fn bump(me, state, id) {
scroll([
" THE CHEST",
" ",
@@ -160,8 +159,8 @@ fn bump(id) {
"""
yammerer = """
fn tick(dt) {
if Queue.length() == 0 {
fn tick(me, state, dt) {
if !me.waiting {
say("blahblahblah");
delay(1);
}
@@ -169,11 +168,11 @@ fn tick(dt) {
"""
shifter = """
fn tick(dt) {
let x = Me.x;
let y = Me.y;
fn tick(me, state, dt) {
let x = me.x;
let y = me.y;
if Queue.length() == 0 {
if !me.waiting {
shift([[x-1, y], [x, y-1], [x+1, y], [x, y+1]]);
delay(0.25);
}
+44
View File
@@ -0,0 +1,44 @@
[world]
name = "Tiny"
start = "start"
# Named Rhai scripts shared across all boards in this world.
# Objects reference a script by name via `script_name`.
[scripts]
greeter = """
fn init(me, state) {
log(`hello from object — player at ${state.player.x}, ${state.player.y}`);
set_tile(2); // change my glyph to ☻ — proves the write path
//say("Hello there,\\ntraveller!");
log(`Player health: ${state.player.health}`);
}
fn tick(me, state, dt) {
if state.player.x == me.x && state.player.y == me.y && !me.waiting {
say("Hey! Get offa me!");
me.queue.delay(5.0);
}
}
"""
[boards.start.map]
name = "Starting Room"
width = 21
height = 6
# Layer 1: terrain, the player, objects and the portal. A space is always a
# transparent empty cell, so the floor below shows through.
[[boards.start.layers]]
content = """
#####################
# #
# G @ #
# oo #
# #
#####################
"""
[boards.start.layers.palette]
"#" = { kind = "wall", tile = "#", fg = "#808080", bg = "#606060" }
"o" = { kind = "crate", tile = 254, fg = "#aaaaaa", bg = "#000000" }
"@" = { kind = "player" }
"G" = { kind = "object", tile = "#", fg = "#aa3333", bg = "#000000", solid = false, script_name = "greeter" }