Putting it together
This is the whole of the Dungeon context's room.croft file, in one piece. Each part has its own chapter; here you can see how they sit together.
The file uses three things declared in other files of the same context: the Alive constraint, and the walkable and step functions. A context can be split across files, and the compiler joins the parts before checking them.
// The room the player explores, one screen at a time.
context Dungeon {
import { max } from Std.Numbers
export {
RoomId,
EnemyId,
Position,
Room.EnemyDefeated
}
const SCREEN_WIDTH: Int = 256
const SCREEN_HEIGHT: Int = 192
const MAX_ENEMIES: Int = 16
identity RoomId for Room: Int where Range(1, 20)
identity PlayerId for Player: Int where Range(1, 2)
identity EnemyId for Enemy: Int where Range(0, MAX_ENEMIES - 1)
enum Direction { North, East, South, West }
// Values live at context level and are shared.
value Position {
x: Int where Range(0, SCREEN_WIDTH - 1)
y: Int where Range(0, SCREEN_HEIGHT - 1)
}
aggregate Room {
id: RoomId
player: Player
enemies: List<Enemy> where Length(0, MAX_ENEMIES), each Alive
exit: Position
// Entities live only inside their aggregate.
entity Player {
id: PlayerId
position: Position
lives: Int where Range(0, 9)
}
entity Enemy {
id: EnemyId
position: Position
hit_points: Int where Range(0, 100)
}
event PlayerMoved { to: Position }
event EnemyDamaged { enemy_id: EnemyId remaining: Int }
event EnemyDefeated { enemy_id: EnemyId at: Position }
// Moves the player one step, unless a wall is in the way.
command Move(direction: Direction) = {
let next = step(player.position, direction)
if walkable(next) {
set player = player with { position: next }
emit PlayerMoved { to: next }
}
}
// Damages one enemy, and defeats it at zero hit points.
command Attack(enemy_id: EnemyId, damage: Int where Range(1, 20)) {
enemies.contains(enemy_id)
} = {
let enemy = enemies.get(enemy_id)
let remaining = max(0, enemy.hit_points - damage)
set enemies = enemies.replace(enemy with { hit_points: remaining })
emit EnemyDamaged { enemy_id, remaining }
if remaining == 0 {
emit EnemyDefeated { enemy_id, at: enemy.position }
}
}
}
}