edtris/game/rules.fnl

39 lines
1.1 KiB
Fennel

(local lume (require :lib.lume))
(local map (require :game.tilemap))
(local tileset (require :game.tiles))
(local state (require :game.state))
(local Bomb (require :game.entities.bomb))
(local Rules {})
(fn tile-at [[x y]]
(map.tile-at-overlay x y [state.bombs state.map]))
(fn Rules.place-bomb [[x y]]
(when (. (tile-at [x y]) :empty)
(map.set-entity-at x y state.bombs (Bomb.new))))
(fn Rules.clear-bomb [bomb]
(map.remove-entity state.bombs bomb))
(fn explode-in-dir [x y dx dy len]
(when (and (>= x 0) (< x state.map.w) (>= y 0) (< y state.map.h))
(let [tile (tile-at [x y])]
(if tile.empty
(do (map.set-entity-at x y state.bombs (Bomb.new-explosion))
(when (> len 0)
(explode-in-dir (+ x dx) (+ y dy) dx dy (- len 1))))
(= tile.state :ticking)
(Bomb.explode tile Rules)
tile.breakable
(map.set-itile-at x y state.map (tileset.itile-named state.map.tileset :empty))))))
(fn Rules.explode-bomb [bomb]
(each [_ [dx dy] (ipairs [[-1 0] [1 0] [0 -1] [0 1]])]
(explode-in-dir (+ bomb.x dx) (+ bomb.y dy) dx dy 1)))
Rules