examples: support directly loading sokoban level files by path

This commit is contained in:
Delyan Angelov 2025-05-12 11:49:56 +03:00
parent 4f46aa84e5
commit d96310f07d
4 changed files with 22 additions and 14 deletions

View file

@ -1,12 +1,12 @@
Push the boxes to the corners of the warehouse.@It is a lot of work...@Have fun.
#########################
#@ # @#
#. # .#
# # #
# ## ### #### ####
# ## # # #
# b # b # b # b #
# $ # $ # $ # $ #
# # # # #
# # # ### ####
# # # ### #
#@ # p @#
#. # @ .#
#########################

View file

@ -2,8 +2,8 @@ Move your player to the box and push it.@Use the arrows to move your player.
##############
# #
# #
# b #
# @ #
# p #
# $ #
# . #
# @ #
# #
##############

View file

@ -1,3 +1,4 @@
import os
import os.asset
import gg
import gx
@ -55,17 +56,20 @@ fn (mut g Game) parse_level(lnumber int) ! {
mut row := []rune{}
for x, c in line {
match c {
`#`, ` `, `@` {
`#`, ` ` {
row << c
}
`b` { // a normal box
`b`, `$` { // a normal box
row << ` `
boxes << Pos{x, y}
}
`p` { // a normal player
`p`, `@` { // a normal player
row << ` `
player = Pos{x, y}
}
`.` { // storage
row << `@`
}
`B` { // box on storage
row << `@`
boxes << Pos{x, y}
@ -247,8 +251,12 @@ fn (mut g Game) iid(name string) !int {
fn main() {
mut g := &Game{}
all_level_names := asset.read_text('/', '_all_levels.txt')!.split_into_lines()
g.levels = all_level_names.map(asset.read_text('/', it)!)
if os.args.len > 1 {
g.levels = os.args[1..].map(os.read_file(it)!)
} else {
all_level_names := asset.read_text('/', '_all_levels.txt')!.split_into_lines()
g.levels = all_level_names.map(asset.read_text('/', it)!)
}
g.parse_level(0)!
g.ctx = gg.new_context(
width: 800

View file

@ -1,10 +1,10 @@
It seems tricky, but it is not ...@Be careful to not get stuck.
### #
###### ###### #
# p # #
# @ # #
## #######
# bbb # #
# @ @ @ # #
# $$$ # #
# . . . # #
# # # ### #
# # ### #
############ #