robot_cleaning_game/two_player_cleaning_game/assets/gen_levels.bb
2026-04-10 15:57:54 +02:00

66 lines
2.6 KiB
Clojure
Executable file

#!/usr/bin/env bb
(ns gen-levels
(:require
[babashka.fs :as fs]
[clojure.pprint :as pprint]
[clojure.data.xml :as xml]
[clojure.string :as str]
[clojure.java.io :as io]))
(def walls-tsx (xml/parse (io/reader "./tiled/walls.tsx")))
(def wall-colliders
(->>
(:content walls-tsx)
(remove string?)
(filter #(= (:tag %) :tile))
(map (fn [tile] (hash-map
:id (get-in tile [:attrs :id])
:boxes
(->> (:content tile)
(remove string?)
(map :content)
(flatten)
(remove string?)
(map :attrs)
(map #(select-keys % [:x :y :height :width]))
(map #(update-vals % (fn [v] (Math/round (Double/parseDouble v)))))))))))
(def level-001-tmx (xml/parse (io/reader "./tiled/level_001.tmx")))
(def level-001
(let [tags (remove string? (:content level-001-tmx))
tile-nums (-> (filter #(= (:tag %) :layer) tags)
first
:content
second
:content
first
str/split-lines
(->>
(remove empty?)
(map #(str/split % #","))
(map (fn [row] (map Integer/parseInt row)))))
tiles (vec (map-indexed
(fn [y row]
(vec (map-indexed
(fn [x tile-id] {:x (* x 25) :y (* y 25) :tile-id tile-id})
row))) tile-nums))]
(hash-map
:tiles tiles
:spawns (vec (reduce (fn [acc tag]
(if (= (:tag tag) :object)
(conj acc
(hash-map
:name (get-in tag [:attrs :name])
:x (Math/round (Double/parseDouble (get-in tag [:attrs :x])))
:y (Math/round (Double/parseDouble (get-in tag [:attrs :y])))))
acc)) '() (:content (first (filter #(= (get-in % [:attrs :name]) "spawns") tags))))))))
;; (pprint/pprint {:row (first (:tiles level-001))})
;; (pprint/pprint (pr-str level-001))
(fs/write-lines "../levels.fnl" ["(local levels"
(str/replace (pr-str {:level01 level-001}) #",+" "")
")\n\n"
"{ :levels levels }"])