✨ ARCANUM RUNE DIFFICULTY: Adept Behavioral

The State Hex

Allow an object to alter its behavior when its internal state changes.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Phase Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
use fmt;

type Context = struct {
	state: *State,
	request: *fn(c: *Context) void,
};

type State = struct {
	handle: *fn(c: *Context) void,
};

fn handle_liquid(c: *Context) void;
fn handle_solid(c: *Context) void;

let state_liquid: State = State { handle = &handle_liquid };
let state_solid: State = State { handle = &handle_solid };

fn handle_liquid(c: *Context) void = {
	fmt::println("Water flows... Freezing to solid.")!;
	c.state = &state_solid;
};

fn handle_solid(c: *Context) void = {
	fmt::println("Ice is rigid... Melting to liquid.")!;
	c.state = &state_liquid;
};

fn context_request(c: *Context) void = {
	c.state.handle(c);
};

export fn main() void = {
	let ctx = Context { state = &state_liquid, request = &context_request };
	ctx.request(&ctx);
	ctx.request(&ctx);
};

The State hex enables an entity to phase-shift its entire paradigm. It changes its core functional logic on the fly depending on whether it is hot, cold, enraged, or dormant.

Runic Tags: #behavioral #state #simple-systems-hexes