feat(pbs): add Beacon Workshop composition example
Add an original-asset minigame with modern controls, independent movement and tool animations, randomized safe beacon layouts and 2D drone patrols. Distribute the canonical project through installed knowledge and exercise compiled cartridges and animation boundaries. Fix lowering of admitted sibling constants and add a regression test. Focused compiler, installed knowledge and direct native checks passed; user tested and approved. Full make verify remains blocked by sandbox sysctl restrictions in ACP tests. WRK-0022 remains open.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { Animator, AnimationError, PlaybackState } from @core:animation;
|
||||
import { Sprite } from @core:sprite;
|
||||
import { math } from @sdk:math;
|
||||
|
||||
declare const BUFFER_TICKS: int = 4;
|
||||
|
||||
declare struct LocomotionVisual(pub animator: Animator) {
|
||||
fn advance(spatial: Spatial2D, moving: bool) -> result<AnimationError> {
|
||||
let clip = body_clip(spatial.facing_x, spatial.facing_y, moving);
|
||||
if clip != this.animator.active_clip() { this.animator.play(clip)!; }
|
||||
this.animator.advance(1)!;
|
||||
return ok(());
|
||||
}
|
||||
}
|
||||
|
||||
declare struct PulseTool(pub animator: Animator, duration: int,
|
||||
pub mut active: bool, pub mut remaining: int, pub mut pending: bool,
|
||||
pub mut direction_x: int, pub mut direction_y: int, pub mut starts: int) {
|
||||
fn cancel() -> void {
|
||||
this.active = false; this.remaining = 0; this.pending = false;
|
||||
this.animator.stop();
|
||||
}
|
||||
fn request(pressed: bool, spatial: Spatial2D) -> result<AnimationError> bool {
|
||||
// Animator is authoritative for completion. remaining is only the input
|
||||
// window, derived from the authored clip duration and decremented with advance.
|
||||
if this.active && this.animator.state() == PlaybackState.Finished { this.active = false; }
|
||||
if this.active {
|
||||
if pressed && this.remaining <= BUFFER_TICKS { this.pending = true; }
|
||||
return ok(false);
|
||||
}
|
||||
if !pressed && !this.pending { return ok(false); }
|
||||
this.pending = false; this.direction_x = spatial.facing_x; this.direction_y = spatial.facing_y;
|
||||
this.animator.play("pulse")!;
|
||||
this.active = true; this.remaining = this.duration;
|
||||
// Bounded diagnostic count: it never controls gameplay or grows without limit.
|
||||
this.starts = (this.starts + 1) % 1000000;
|
||||
return ok(true);
|
||||
}
|
||||
fn advance() -> result<AnimationError> {
|
||||
if this.active {
|
||||
this.animator.advance(1)!;
|
||||
if this.remaining > 0 { this.remaining -= 1; }
|
||||
}
|
||||
return ok(());
|
||||
}
|
||||
}
|
||||
|
||||
declare struct Robot(pub spatial: Spatial2D, pub motor: Motor2D,
|
||||
pub visual: LocomotionVisual, pub tool: PulseTool) {
|
||||
fn move(source: Controller) -> bool {
|
||||
let command = source.intent(this.spatial.pixel_x(), this.spatial.pixel_y());
|
||||
this.motor.move(command.move_x, command.move_y);
|
||||
return command.pulse;
|
||||
}
|
||||
fn reset() -> result<AnimationError> {
|
||||
this.spatial.reset(HOME_X, HOME_Y); this.motor.moving = false;
|
||||
this.tool.cancel(); this.tool.starts = 0;
|
||||
this.visual.animator.play("idle_s")!;
|
||||
return ok(());
|
||||
}
|
||||
}
|
||||
|
||||
declare struct Drone(pub spatial: Spatial2D, pub motor: Motor2D,
|
||||
pub animator: Animator, pub controller: PatrolController) {
|
||||
fn move(source: Controller) -> void {
|
||||
let command = source.intent(this.spatial.pixel_x(), this.spatial.pixel_y());
|
||||
this.motor.move(command.move_x, command.move_y);
|
||||
}
|
||||
}
|
||||
|
||||
declare struct Beacon(pub spatial: Spatial2D, pub off: Sprite, pub on: Sprite, pub mut lit: bool) {
|
||||
fn receive(robot: Robot) -> void {
|
||||
if this.lit { return; }
|
||||
let dx = this.spatial.pixel_x() - robot.spatial.pixel_x();
|
||||
let dy = this.spatial.pixel_y() - robot.spatial.pixel_y();
|
||||
let forward = dx * robot.tool.direction_x + dy * robot.tool.direction_y;
|
||||
let across = math.abs(dx * robot.tool.direction_y - dy * robot.tool.direction_x);
|
||||
if forward > 0 && dx * dx + dy * dy <= 56 * 56 && across <= 24 { this.lit = true; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { Pad, Button } from @sdk:input;
|
||||
import { Random, random } from @sdk:random;
|
||||
import { math } from @sdk:math;
|
||||
|
||||
// A value payload, not a new command object per tick. Controllers decide;
|
||||
// concrete actors validate and execute. Coordinates are observations, not ownership.
|
||||
declare contract Controller {
|
||||
fn intent(x: int, y: int) -> (move_x: int, move_y: int, pulse: bool);
|
||||
}
|
||||
|
||||
declare struct PlayerController() {
|
||||
fn intent(x: int, y: int) -> (move_x: int, move_y: int, pulse: bool) {
|
||||
let dx = 0; let dy = 0;
|
||||
if Pad.down(Button.Right) { dx += 1; }
|
||||
if Pad.down(Button.Left) { dx -= 1; }
|
||||
if Pad.down(Button.Down) { dy += 1; }
|
||||
if Pad.down(Button.Up) { dy -= 1; }
|
||||
return (move_x: dx, move_y: dy, pulse: Pad.pressed(Button.A));
|
||||
}
|
||||
}
|
||||
implements Controller for PlayerController using value {
|
||||
fn intent(x: int, y: int) -> (move_x: int, move_y: int, pulse: bool) {
|
||||
return value.intent(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
// The ring's adjacent segments stay entirely outside the central obstacle and
|
||||
// the robot's return area. Direction changes happen at waypoints, not per frame.
|
||||
fn route_x(node: int) -> int {
|
||||
if node == 0 || node == 8 { return 144; }
|
||||
if node == 1 { return 256; }
|
||||
if node == 2 { return 384; }
|
||||
if node == 3 { return 512; }
|
||||
if node == 4 { return 528; }
|
||||
if node == 5 { return 448; }
|
||||
if node == 6 { return 320; }
|
||||
return 192;
|
||||
}
|
||||
fn route_y(node: int) -> int {
|
||||
if node < 3 { return 80; }
|
||||
if node == 3 { return 112; }
|
||||
if node == 4 || node == 7 { return 208; }
|
||||
if node == 5 { return 288; }
|
||||
if node == 6 { return 272; }
|
||||
return 144;
|
||||
}
|
||||
|
||||
declare struct PatrolController(rng: Random, pub mut node: int, pub mut target: int,
|
||||
pub mut heading: int, pub mut stalled: int, pub mut recovering: bool, last_x: int, last_y: int) {
|
||||
fn reset(node: int, heading: int) -> void {
|
||||
this.node = node; this.target = (node + heading + 9) % 9; this.heading = heading;
|
||||
this.last_x = route_x(node); this.last_y = route_y(node); this.stalled = 0; this.recovering = false;
|
||||
}
|
||||
fn intent(x: int, y: int) -> (move_x: int, move_y: int, pulse: bool) {
|
||||
if math.abs(x - route_x(this.target)) <= 2 && math.abs(y - route_y(this.target)) <= 2 {
|
||||
this.node = this.target;
|
||||
if !this.recovering && random.range(this.rng, 0 as long, 4 as long) == (0 as long) { this.heading = -this.heading; }
|
||||
this.recovering = false;
|
||||
this.target = (this.node + this.heading + 9) % 9;
|
||||
this.stalled = 0;
|
||||
} else {
|
||||
if x == this.last_x && y == this.last_y { this.stalled += 1; }
|
||||
else { this.stalled = 0; }
|
||||
if this.stalled >= 24 {
|
||||
// Return along the known leg before choosing the opposite connection.
|
||||
this.heading = -this.heading; this.target = this.node; this.stalled = 0; this.recovering = true;
|
||||
}
|
||||
}
|
||||
this.last_x = x; this.last_y = y;
|
||||
let dx = route_x(this.target) - x; let dy = route_y(this.target) - y;
|
||||
let ix = 0; let iy = 0;
|
||||
if dx > 2 { ix = 1; } else if dx < -2 { ix = -1; }
|
||||
if dy > 2 { iy = 1; } else if dy < -2 { iy = -1; }
|
||||
return (move_x: ix, move_y: iy, pulse: false);
|
||||
}
|
||||
}
|
||||
implements Controller for PatrolController using value {
|
||||
fn intent(x: int, y: int) -> (move_x: int, move_y: int, pulse: bool) {
|
||||
return value.intent(x, y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Generated by tools/author_assets.py from original code-authored art.
|
||||
declare const GLYPH_ROBOT_S_IDLE: int = 0;
|
||||
declare const GLYPH_ROBOT_S_STEP: int = 1;
|
||||
declare const GLYPH_ROBOT_SE_IDLE: int = 2;
|
||||
declare const GLYPH_ROBOT_SE_STEP: int = 3;
|
||||
declare const GLYPH_ROBOT_E_IDLE: int = 4;
|
||||
declare const GLYPH_ROBOT_E_STEP: int = 5;
|
||||
declare const GLYPH_ROBOT_NE_IDLE: int = 6;
|
||||
declare const GLYPH_ROBOT_NE_STEP: int = 7;
|
||||
declare const GLYPH_ROBOT_N_IDLE: int = 8;
|
||||
declare const GLYPH_ROBOT_N_STEP: int = 9;
|
||||
declare const GLYPH_ROBOT_NW_IDLE: int = 10;
|
||||
declare const GLYPH_ROBOT_NW_STEP: int = 11;
|
||||
declare const GLYPH_ROBOT_W_IDLE: int = 12;
|
||||
declare const GLYPH_ROBOT_W_STEP: int = 13;
|
||||
declare const GLYPH_ROBOT_SW_IDLE: int = 14;
|
||||
declare const GLYPH_ROBOT_SW_STEP: int = 15;
|
||||
declare const GLYPH_DRONE_0: int = 16;
|
||||
declare const GLYPH_DRONE_1: int = 17;
|
||||
declare const GLYPH_PULSE_0: int = 18;
|
||||
declare const GLYPH_PULSE_1: int = 19;
|
||||
declare const GLYPH_PULSE_2: int = 20;
|
||||
declare const GLYPH_BEACON_OFF: int = 21;
|
||||
declare const GLYPH_BEACON_ON: int = 22;
|
||||
declare const GLYPH_FLOOR: int = 23;
|
||||
declare const GLYPH_WALL: int = 24;
|
||||
declare const GLYPH_EXIT_CLOSED: int = 25;
|
||||
declare const GLYPH_EXIT_OPEN: int = 26;
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Random, random } from @sdk:random;
|
||||
import { math } from @sdk:math;
|
||||
|
||||
declare const DEFAULT_SEED: long = 2026;
|
||||
|
||||
// Four authored anchors in each of three separated regions. This changes
|
||||
// instance placement; the room and collision geometry remain fixed.
|
||||
fn beacon_x(region: int, choice: int) -> int {
|
||||
if region == 0 {
|
||||
if choice == 1 { return 144; }
|
||||
if choice == 3 { return 176; }
|
||||
return 64;
|
||||
}
|
||||
if region == 1 {
|
||||
if choice == 1 { return 496; }
|
||||
if choice == 3 { return 528; }
|
||||
return 400;
|
||||
}
|
||||
return 192 + choice * 96;
|
||||
}
|
||||
fn beacon_y(region: int, choice: int) -> int {
|
||||
if region == 0 {
|
||||
if choice == 2 { return 144; }
|
||||
if choice == 3 { return 112; }
|
||||
return 80;
|
||||
}
|
||||
if region == 1 { return if choice >= 2 { 144 } else { 80 }; }
|
||||
return if choice == 3 { 288 } else { 256 };
|
||||
}
|
||||
|
||||
fn valid_anchor(x: int, y: int) -> bool {
|
||||
if !clear_position(x * SCALE, y * SCALE) { return false; }
|
||||
if math.abs(x - HOME_X) < 64 && math.abs(y - HOME_Y) < 64 { return false; }
|
||||
if math.abs(x - 576) < 48 && math.abs(y - 256) < 48 { return false; }
|
||||
// At least one axial approach from inside pulse range must be walkable.
|
||||
return clear_position(x * SCALE, (y + 32) * SCALE) || clear_position(x * SCALE, (y - 32) * SCALE) ||
|
||||
clear_position((x + 32) * SCALE, y * SCALE) || clear_position((x - 32) * SCALE, y * SCALE);
|
||||
}
|
||||
|
||||
declare struct BeaconLayout(rng: Random, pub mut west: int, pub mut east: int, pub mut south: int) {
|
||||
fn next() -> void {
|
||||
let west = random.range(this.rng, 0 as long, 4 as long) as int;
|
||||
let east = random.range(this.rng, 0 as long, 4 as long) as int;
|
||||
let south = random.range(this.rng, 0 as long, 4 as long) as int;
|
||||
// Exactly three draws. A repeated triple has a finite, valid alternative.
|
||||
if west == this.west && east == this.east && south == this.south { south = (south + 1) % 4; }
|
||||
this.west = west; this.east = east; this.south = south;
|
||||
}
|
||||
fn choice(region: int) -> int {
|
||||
if region == 0 { return this.west; }
|
||||
return if region == 1 { this.east } else { this.south };
|
||||
}
|
||||
fn x(region: int) -> int { return beacon_x(region, this.choice(region)); }
|
||||
fn y(region: int) -> int { return beacon_y(region, this.choice(region)); }
|
||||
fn valid() -> bool {
|
||||
if this.west < 0 || this.west > 3 || this.east < 0 || this.east > 3 || this.south < 0 || this.south > 3 { return false; }
|
||||
for region: int from 0 until 3 {
|
||||
for choice: int from 0 until 4 {
|
||||
if !valid_anchor(beacon_x(region, choice), beacon_y(region, choice)) { return false; }
|
||||
}
|
||||
}
|
||||
for a: int from 0 until 3 {
|
||||
for b: int from a + 1 until 3 {
|
||||
if math.abs(this.x(a) - this.x(b)) < 64 && math.abs(this.y(a) - this.y(b)) < 64 { return false; }
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Pad, Button } from @sdk:input;
|
||||
import { Log } from @sdk:log;
|
||||
import { Gfx } from @sdk:gfx;
|
||||
import { Color } from @core:color;
|
||||
import { AnimationError } from @core:animation;
|
||||
|
||||
// Procedural factories are not global initializers. A directly constructed
|
||||
// holder provides absence until the Init hook has recovered factory errors.
|
||||
declare struct GameSlot(pub mut value: optional Workshop) {
|
||||
ctor make() { this.value = none; }
|
||||
}
|
||||
declare global slot: GameSlot = new GameSlot.make();
|
||||
declare global failed: bool = false;
|
||||
|
||||
fn fail(message: str) -> void { failed = true; Log.failure(message); }
|
||||
|
||||
fn install_game() -> result<AnimationError> {
|
||||
let value = make_workshop()!;
|
||||
if !value.layout.valid() { fail("Beacon Workshop: invalid authored beacon anchors"); return ok(()); }
|
||||
slot.value = some(value);
|
||||
return ok(());
|
||||
}
|
||||
|
||||
fn initialize() -> result<AnimationError> {
|
||||
failed = false;
|
||||
handle install_game() { _ -> { fail("Beacon Workshop: unable to initialize authored animation"); ok(()) } };
|
||||
return ok(());
|
||||
}
|
||||
|
||||
fn run_frame() -> result<AnimationError> {
|
||||
if !failed {
|
||||
if let value = slot.value {
|
||||
let updated = handle tick(value) { _ -> { fail("Beacon Workshop: animation update failed"); ok(false) } };
|
||||
if updated && !render(value) { fail("Beacon Workshop: scene/sprite emission failed"); }
|
||||
}
|
||||
}
|
||||
if failed {
|
||||
Gfx.fill_rect(0, 0, 640, 360, Color.rgb(16, 28, 42));
|
||||
Gfx.draw_text(32, 160, "WORKSHOP ERROR - SEE STUDIO DIAGNOSTICS", Color.rgb(226, 120, 88));
|
||||
}
|
||||
return ok(());
|
||||
}
|
||||
|
||||
// handle belongs inside a result-returning callable in PBS. These private
|
||||
// boundary helpers recover every domain error and always return ok; only that
|
||||
// already-recovered result is discarded by the required void entrypoints.
|
||||
[Init] fn init() -> void { initialize(); }
|
||||
[Frame] fn frame() -> void { run_frame(); }
|
||||
|
||||
fn tick(value: Workshop) -> result<AnimationError> bool {
|
||||
if Pad.pressed(Button.Start) {
|
||||
value.reset()!;
|
||||
if !value.layout.valid() { fail("Beacon Workshop: invalid beacon placement"); return ok(false); }
|
||||
}
|
||||
else { value.update()!; }
|
||||
return ok(true);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
pub fn init() -> void;
|
||||
pub fn frame() -> void;
|
||||
mod contract Controller;
|
||||
mod struct PlayerController;
|
||||
mod struct PatrolController;
|
||||
mod struct Spatial2D;
|
||||
mod struct Motor2D;
|
||||
mod struct LocomotionVisual;
|
||||
mod struct PulseTool;
|
||||
mod struct Robot;
|
||||
mod struct Drone;
|
||||
mod struct Beacon;
|
||||
mod struct Workshop;
|
||||
mod struct BeaconLayout;
|
||||
mod const DEFAULT_SEED;
|
||||
mod const SCALE;
|
||||
mod const HOME_X;
|
||||
mod const HOME_Y;
|
||||
mod const GLYPH_BEACON_OFF;
|
||||
mod const GLYPH_BEACON_ON;
|
||||
mod const GLYPH_EXIT_CLOSED;
|
||||
mod const GLYPH_EXIT_OPEN;
|
||||
mod fn clear_position(x: int, y: int) -> bool;
|
||||
mod fn touches(a: Spatial2D, b: Spatial2D) -> bool;
|
||||
mod fn body_clip(dx: int, dy: int, moving: bool) -> str;
|
||||
mod fn make_workshop() -> result<AnimationError> Workshop;
|
||||
mod fn make_workshop_seed(seed: long) -> result<AnimationError> Workshop;
|
||||
mod fn beacon_x(region: int, choice: int) -> int;
|
||||
mod fn beacon_y(region: int, choice: int) -> int;
|
||||
mod fn route_x(node: int) -> int;
|
||||
mod fn route_y(node: int) -> int;
|
||||
mod fn render(value: Workshop) -> bool;
|
||||
@@ -0,0 +1,60 @@
|
||||
import { math } from @sdk:math;
|
||||
|
||||
declare const SCALE: int = 256;
|
||||
declare const SPEED: int = 512;
|
||||
declare const DIAGONAL_SPEED: int = 362;
|
||||
declare const HOME_X: int = 64;
|
||||
declare const HOME_Y: int = 256;
|
||||
|
||||
declare struct Spatial2D(pub mut x: int, pub mut y: int,
|
||||
pub mut facing_x: int, pub mut facing_y: int) {
|
||||
fn reset(x: int, y: int) -> void {
|
||||
this.x = x * SCALE; this.y = y * SCALE;
|
||||
this.facing_x = 0; this.facing_y = 1;
|
||||
}
|
||||
fn pixel_x() -> int { return this.x / SCALE; }
|
||||
fn pixel_y() -> int { return this.y / SCALE; }
|
||||
}
|
||||
|
||||
// Room-specific collision is deliberately local to this game. The visible body
|
||||
// is 32x32, its footprint is inset by eight pixels on each side.
|
||||
fn clear_position(x: int, y: int) -> bool {
|
||||
if x < 32 * SCALE || x > 576 * SCALE || y < 64 * SCALE || y > 288 * SCALE { return false; }
|
||||
return !(x + 24 * SCALE > 288 * SCALE && x + 8 * SCALE < 352 * SCALE &&
|
||||
y + 24 * SCALE > 128 * SCALE && y + 8 * SCALE < 192 * SCALE);
|
||||
}
|
||||
|
||||
declare struct Motor2D(spatial: Spatial2D, pub mut moving: bool) {
|
||||
fn move(dx: int, dy: int) -> void {
|
||||
let ix = math.clamp(dx, -1, 1); let iy = math.clamp(dy, -1, 1);
|
||||
if ix != 0 || iy != 0 {
|
||||
this.spatial.facing_x = ix; this.spatial.facing_y = iy;
|
||||
}
|
||||
let stride = if ix != 0 && iy != 0 { DIAGONAL_SPEED } else { SPEED };
|
||||
let old_x = this.spatial.x; let old_y = this.spatial.y;
|
||||
let x = old_x + ix * stride;
|
||||
if clear_position(x, old_y) { this.spatial.x = x; }
|
||||
let y = old_y + iy * stride;
|
||||
if clear_position(this.spatial.x, y) { this.spatial.y = y; }
|
||||
this.moving = old_x != this.spatial.x || old_y != this.spatial.y;
|
||||
}
|
||||
}
|
||||
|
||||
fn touches(a: Spatial2D, b: Spatial2D) -> bool {
|
||||
return math.abs(a.x - b.x) < 20 * SCALE && math.abs(a.y - b.y) < 20 * SCALE;
|
||||
}
|
||||
|
||||
fn body_clip(dx: int, dy: int, moving: bool) -> str {
|
||||
if dy > 0 {
|
||||
if dx > 0 { return if moving { "move_se" } else { "idle_se" }; }
|
||||
if dx < 0 { return if moving { "move_sw" } else { "idle_sw" }; }
|
||||
return if moving { "move_s" } else { "idle_s" };
|
||||
}
|
||||
if dy < 0 {
|
||||
if dx > 0 { return if moving { "move_ne" } else { "idle_ne" }; }
|
||||
if dx < 0 { return if moving { "move_nw" } else { "idle_nw" }; }
|
||||
return if moving { "move_n" } else { "idle_n" };
|
||||
}
|
||||
if dx > 0 { return if moving { "move_e" } else { "idle_e" }; }
|
||||
return if moving { "move_w" } else { "idle_w" };
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { SpriteView } from @core:sprite;
|
||||
import { Composer } from @sdk:composer;
|
||||
import { Gfx } from @sdk:gfx;
|
||||
import { Color } from @core:color;
|
||||
|
||||
fn emit_image(image: SpriteView, x: int, y: int, priority: int) -> bool {
|
||||
let view = image.sprite();
|
||||
return Composer.emit_sprite(view.glyph_id, 0, x, y, 1, view.bank_id, false, false, priority) == 0;
|
||||
}
|
||||
|
||||
fn draw_beacon(beacon: Beacon) -> bool {
|
||||
if beacon.lit { return emit_image(beacon.on, beacon.spatial.pixel_x(), beacon.spatial.pixel_y(), 1); }
|
||||
return emit_image(beacon.off, beacon.spatial.pixel_x(), beacon.spatial.pixel_y(), 1);
|
||||
}
|
||||
|
||||
fn render(value: Workshop) -> bool {
|
||||
if Composer.bind_scene(0) != 0 { return false; }
|
||||
Composer.set_camera(0, 0);
|
||||
if !draw_beacon(value.first_beacon) || !draw_beacon(value.second_beacon) || !draw_beacon(value.third_beacon) { return false; }
|
||||
let exit_ok = if value.all_lit() { emit_image(value.exit_open, 576, 256, 1) } else { emit_image(value.exit_closed, 576, 256, 1) };
|
||||
if !exit_ok { return false; }
|
||||
if !emit_image(value.first.animator, value.first.spatial.pixel_x(), value.first.spatial.pixel_y(), 2) { return false; }
|
||||
if !emit_image(value.second.animator, value.second.spatial.pixel_x(), value.second.spatial.pixel_y(), 2) { return false; }
|
||||
if !emit_image(value.robot.visual.animator, value.robot.spatial.pixel_x(), value.robot.spatial.pixel_y(), 3) { return false; }
|
||||
if value.robot.tool.active {
|
||||
let x = value.robot.spatial.pixel_x() + value.robot.tool.direction_x * 20;
|
||||
let y = value.robot.spatial.pixel_y() + value.robot.tool.direction_y * 20;
|
||||
if !emit_image(value.robot.tool.animator, x, y, 4) { return false; }
|
||||
}
|
||||
Gfx.fill_rect(0, 0, 640, 32, Color.rgb(16, 28, 42));
|
||||
Gfx.draw_text(16, 10, "BEACON WORKSHOP", Color.rgb(201, 237, 240));
|
||||
Gfx.draw_text(248, 10, "ACTIVATE ALL THREE BEACONS", Color.rgb(112, 174, 192));
|
||||
Gfx.fill_rect(0, 336, 640, 24, Color.rgb(16, 28, 42));
|
||||
Gfx.draw_text(16, 342, "D-PAD MOVE / A PULSE / START RESTART", Color.rgb(112, 174, 192));
|
||||
if value.won {
|
||||
Gfx.fill_rect(144, 136, 352, 80, Color.rgb(16, 28, 42));
|
||||
Gfx.draw_text(176, 156, "WORKSHOP RESTORED", Color.rgb(77, 227, 181));
|
||||
Gfx.draw_text(176, 184, "PRESS START TO PLAY AGAIN", Color.rgb(249, 234, 211));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { create_robot, create_drone, create_pulse } from @workshop:generated/animations/b_baec0001202640008000000000000001;
|
||||
import { AnimationError } from @core:animation;
|
||||
import { Sprite, make_sprite } from @core:sprite;
|
||||
import { Random, random } from @sdk:random;
|
||||
|
||||
declare struct Workshop(pub robot: Robot, pub first: Drone, pub second: Drone,
|
||||
pub first_beacon: Beacon, pub second_beacon: Beacon, pub third_beacon: Beacon,
|
||||
pub player: PlayerController, pub exit_closed: Sprite,
|
||||
pub exit_open: Sprite, pub layout: BeaconLayout, pub mut won: bool, pub mut contacts: int) {
|
||||
fn update() -> result<AnimationError> {
|
||||
if this.won { return ok(()); }
|
||||
let pressed = this.robot.move(this.player);
|
||||
this.first.move(this.first.controller); this.second.move(this.second.controller);
|
||||
if touches(this.robot.spatial, this.first.spatial) || touches(this.robot.spatial, this.second.spatial) {
|
||||
this.robot.spatial.reset(HOME_X, HOME_Y); this.robot.motor.moving = false;
|
||||
this.robot.tool.cancel(); this.contacts = (this.contacts + 1) % 1000000;
|
||||
} else {
|
||||
let pulse = this.robot.tool.request(pressed, this.robot.spatial)!;
|
||||
if pulse {
|
||||
this.first_beacon.receive(this.robot); this.second_beacon.receive(this.robot); this.third_beacon.receive(this.robot);
|
||||
}
|
||||
if this.all_lit() && this.robot.spatial.pixel_x() >= 560 && this.robot.spatial.pixel_y() >= 240 {
|
||||
this.won = true; this.robot.tool.cancel();
|
||||
}
|
||||
}
|
||||
this.robot.visual.advance(this.robot.spatial, this.robot.motor.moving)!;
|
||||
this.robot.tool.advance()!;
|
||||
this.first.animator.advance(1)!; this.second.animator.advance(1)!;
|
||||
return ok(());
|
||||
}
|
||||
fn all_lit() -> bool { return this.first_beacon.lit && this.second_beacon.lit && this.third_beacon.lit; }
|
||||
fn reset() -> result<AnimationError> {
|
||||
this.robot.reset()!;
|
||||
this.first.spatial.reset(route_x(0), route_y(0)); this.second.spatial.reset(route_x(4), route_y(4));
|
||||
this.first.controller.reset(0, 1); this.second.controller.reset(4, -1);
|
||||
this.first.motor.moving = false; this.second.motor.moving = false;
|
||||
this.first.animator.play("fly")!; this.second.animator.play("fly")!;
|
||||
this.second.animator.advance(5)!;
|
||||
this.first_beacon.lit = false; this.second_beacon.lit = false; this.third_beacon.lit = false;
|
||||
this.layout.next(); this.place_beacons();
|
||||
this.won = false; this.contacts = 0;
|
||||
return ok(());
|
||||
}
|
||||
fn place_beacons() -> void {
|
||||
this.first_beacon.spatial.reset(this.layout.x(0), this.layout.y(0));
|
||||
this.second_beacon.spatial.reset(this.layout.x(1), this.layout.y(1));
|
||||
this.third_beacon.spatial.reset(this.layout.x(2), this.layout.y(2));
|
||||
}
|
||||
}
|
||||
|
||||
fn make_drone(seed: long, node: int, heading: int) -> result<AnimationError> Drone {
|
||||
let x = route_x(node); let y = route_y(node);
|
||||
let spatial = new Spatial2D(x * SCALE, y * SCALE, 1, 0);
|
||||
let animator = create_drone(0)!; animator.play("fly")!;
|
||||
let controller = new PatrolController(random.new(seed), node, (node + heading + 9) % 9, heading, 0, false, x, y);
|
||||
return ok(new Drone(spatial, new Motor2D(spatial, false), animator, controller));
|
||||
}
|
||||
|
||||
fn make_beacon(x: int, y: int) -> result<AnimationError> Beacon {
|
||||
let off = handle make_sprite(0, GLYPH_BEACON_OFF) { _ -> AnimationError.invalid_glyph };
|
||||
let on = handle make_sprite(0, GLYPH_BEACON_ON) { _ -> AnimationError.invalid_glyph };
|
||||
return ok(new Beacon(new Spatial2D(x * SCALE, y * SCALE, 0, 1), off, on, false));
|
||||
}
|
||||
|
||||
fn make_workshop() -> result<AnimationError> Workshop {
|
||||
return make_workshop_seed(DEFAULT_SEED);
|
||||
}
|
||||
|
||||
fn make_workshop_seed(seed: long) -> result<AnimationError> Workshop {
|
||||
let seeds = random.new(seed);
|
||||
let layout_seed = random.next(seeds); let first_seed = random.next(seeds); let second_seed = random.next(seeds);
|
||||
let layout = new BeaconLayout(random.new(layout_seed), -1, -1, -1);
|
||||
layout.next();
|
||||
let spatial = new Spatial2D(HOME_X * SCALE, HOME_Y * SCALE, 0, 1);
|
||||
let body = create_robot(0)!; body.play("idle_s")!;
|
||||
let effect = create_pulse(0)!;
|
||||
let duration: long = 0;
|
||||
let count = effect.frame_count("pulse")!;
|
||||
for index: long from (0 as long) until count {
|
||||
let definition = effect.frame_at("pulse", index)!;
|
||||
duration += definition.duration_ticks;
|
||||
}
|
||||
let tool = new PulseTool(effect, duration as int, false, 0, false, 0, 1, 0);
|
||||
let robot = new Robot(spatial, new Motor2D(spatial, false), new LocomotionVisual(body), tool);
|
||||
let closed = handle make_sprite(0, GLYPH_EXIT_CLOSED) { _ -> AnimationError.invalid_glyph };
|
||||
let opened = handle make_sprite(0, GLYPH_EXIT_OPEN) { _ -> AnimationError.invalid_glyph };
|
||||
let value = new Workshop(robot, make_drone(first_seed, 0, 1)!, make_drone(second_seed, 4, -1)!,
|
||||
make_beacon(layout.x(0), layout.y(0))!, make_beacon(layout.x(1), layout.y(1))!, make_beacon(layout.x(2), layout.y(2))!,
|
||||
new PlayerController(), closed, opened, layout, false, 0);
|
||||
value.second.animator.advance(5)!;
|
||||
return ok(value);
|
||||
}
|
||||
Reference in New Issue
Block a user