Dungeon player
Terminal game284 lines of .st437 lines of generated Rust
................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ......................................@......................................... ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "@latest"
}
Screen = {
source = "StructOp/game"
version = "@latest"
}
}
}main.st
ability "call" "NewMap" {
function = ability.function.new_map
}
ability "game" "Dungeon" {
title = "Dungeon Crawler"
fps = 30
key = ability.value.key
on_tick = [ability.clear.Screen, ability.call.Update, ability.call.Render, ability.call.RenderPlayer]
depends_on = [ability.call.NewMap]
}
ability "clear" "Screen" {
}
ability "call" "Update" {
function = ability.function.update_player
}
ability "call" "RenderPlayer" {
function = ability.function.render_player
}
ability "call" "Render" {
function = ability.function.render_map
}vars.st
constant "SCREEN_WIDTH" {
type = Int
default = 80
}
constant "SCREEN_HEIGHT" {
type = Int
default = 50
}
# SCREEN_WIDTH * SCREEN_HEIGHT
constant "NUM_TILES" {
type = Int
default = 4000
}
ability "value" "tiles" {
type = Array
of = TileType
size = 4000
}
data "value" "tiles" {
of = ability.value.tiles
}
# Player::new(Point::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
ability "value" "player" {
type = Point
initial = {
x = 40
y = 25
}
}
data "value" "player" {
of = ability.value.player
}
ability "value" "key" {
type = Key
}
data "value" "key" {
of = ability.value.key
}map.st
ability "enum" "TileType" {
values = [Wall, Floor]
}
ability "function" "map_idx" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Int
return = y * const.SCREEN_WIDTH + x
}
# Map::new(): every tile a floor
ability "function" "new_map" {
locals = [{ name = "i", type = Int }]
body = [ability.for.Fill]
}
ability "for" "Fill" {
counter = i
from = 0
while = i < const.NUM_TILES
next = i + 1
body = [ability.set.Floor]
}
ability "set" "Floor" {
target = ability.value.tiles[i]
value = TileType.Floor
}
ability "function" "render_map" {
locals = [{ name = "x", type = Int }, { name = "y", type = Int }]
body = [ability.for.Rows]
}
ability "for" "Rows" {
counter = y
from = 0
while = y < const.SCREEN_HEIGHT
next = y + 1
body = [ability.for.Columns]
}
ability "for" "Columns" {
counter = x
from = 0
while = x < const.SCREEN_WIDTH
next = x + 1
body = [ability.switch.Tile]
}
ability "switch" "Tile" {
value = data.value.tiles.current[map_idx(x, y)]
cases = [
{ value = TileType.Floor, run = [ability.draw.FloorTile] },
{ value = TileType.Wall, run = [ability.draw.WallTile] },
]
}
ability "draw" "FloorTile" {
x = x
y = y
glyph = "."
color = Color.yellow
}
ability "draw" "WallTile" {
x = x
y = y
glyph = "#"
color = Color.green
}
ability "function" "in_bounds" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Bool
return = x >= 0 && x < const.SCREEN_WIDTH && y >= 0 && y < const.SCREEN_HEIGHT
}
ability "function" "can_enter_tile" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Bool
return = in_bounds(x, y) && data.value.tiles.current[map_idx(x, y)] == TileType.Floor
}
# try_idx: Some(map_idx(x, y)) inside the map, None (-1) outside
ability "function" "try_idx" {
params = [{ name = "x", type = Int }, { name = "y", type = Int }]
returns = Int
locals = [{ name = "idx", type = Int }]
body = [ability.set.NoIdx, ability.if.Inside]
return = idx
}
ability "set" "NoIdx" {
target = idx
value = -1
}
ability "if" "Inside" {
condition = in_bounds(x, y)
then = [ability.set.SomeIdx]
}
ability "set" "SomeIdx" {
target = idx
value = map_idx(x, y)
}player.st
ability "struct" "Point" {
fields = [{ name = "x", type = Int }, { name = "y", type = Int }]
}
ability "function" "render_player" {
body = [ability.draw.Player]
}
ability "draw" "Player" {
x = data.value.player.current.x
y = data.value.player.current.y
glyph = "@"
}
ability "function" "update_player" {
locals = [{ name = "delta", type = Point }, { name = "new_position", type = Point }]
body = [ability.switch.Move, ability.set.NewPosition, ability.if.CanEnter]
}
ability "switch" "Move" {
value = data.value.key.current
cases = [
{ value = Key.left, run = [ability.set.Left] },
{ value = Key.right, run = [ability.set.Right] },
{ value = Key.up, run = [ability.set.Up] },
{ value = Key.down, run = [ability.set.Down] },
]
default = [ability.set.Zero]
}
ability "set" "Left" {
target = delta
value = {
x = -1
y = 0
}
}
ability "set" "Right" {
target = delta
value = {
x = 1
y = 0
}
}
ability "set" "Up" {
target = delta
value = {
x = 0
y = -1
}
}
ability "set" "Down" {
target = delta
value = {
x = 0
y = 1
}
}
ability "set" "Zero" {
target = delta
value = {
x = 0
y = 0
}
}
ability "set" "NewPosition" {
target = new_position
value = {
x = data.value.player.current.x + delta.x
y = data.value.player.current.y + delta.y
}
}
ability "if" "CanEnter" {
condition = can_enter_tile(new_position.x, new_position.y)
then = [ability.set.Position]
}
ability "set" "Position" {
target = ability.value.player
value = new_position
}Input, plan and generated Rust#
input.txt · key script, one line per frame (4 lines)
input.txt · key script, one line per frame (4 lines)
left
left
upstructop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.call.NewMap
+ ability.game.Dungeon
+ ability.clear.Screen
+ ability.call.Update
+ ability.call.Render
+ ability.call.RenderPlayer
+ ability.enum.TileType
+ ability.function.map_idx
+ ability.function.new_map
+ ability.for.Fill
+ ability.set.Floor
+ ability.function.render_map
+ ability.for.Rows
+ ability.for.Columns
+ ability.switch.Tile
+ ability.draw.FloorTile
+ ability.draw.WallTile
+ ability.function.in_bounds
+ ability.function.can_enter_tile
+ ability.function.try_idx
+ ability.set.NoIdx
+ ability.if.Inside
+ ability.set.SomeIdx
+ ability.struct.Point
+ ability.function.render_player
+ ability.draw.Player
+ ability.function.update_player
+ ability.switch.Move
+ ability.set.Left
+ ability.set.Right
+ ability.set.Up
+ ability.set.Down
+ ability.set.Zero
+ ability.set.NewPosition
+ ability.if.CanEnter
+ ability.set.Position
+ ability.value.tiles
+ ability.value.player
+ ability.value.key
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
cd rust-dungeon-player # a folder holding the .st files above
structop init
structop plan
structop apply