edtris/game/rules.fnl
2022-04-24 19:56:53 -04:00

108 lines
1.9 KiB
Fennel

(local lume (require :lib.lume))
(local map (require :game.tilemap))
(local tileset (require :game.tiles))
(local state (require :game.state))
(local Rules {})
(local tetrominoes
{:O [["##"
"##"]]
:L [["###"
"# "]
["# "
"# "
"##"]
[" #"
"###"]
["##"
" #"
" #"]]
:J [["###"
" #"]
["##"
"# "
"# "]
["# "
"###"]
[" #"
" #"
"##"]]
:I [["####"]
["#"
"#"
"#"
"#"]]
:S [[" ##"
"## "]
["# "
"##"
" #"]]
:Z [["## "
" ##"]
[" #"
"##"
"# "]]
:T [["###"
" # "]
["# "
"##"
"# "]
[" # "
"###"]
[" #"
"##"
" #"]]})
(fn Rules.choose-random-tetromino []
(let [possible-values (icollect [k (pairs state.tetrominoes)] k)
ivalue (love.math.random (length possible-values))]
(. possible-values ivalue)))
(fn Rules.tetromino [k irotation]
(let [rotations (or (. state.tetrominoes k) [["##" "##"]])
irotation (+ (% (- irotation 1) (length rotations)) 1)]
(. rotations irotation)))
(fn Rules.reset-game []
(set state.tetrominoes tetrominoes)
(set state.well (map.new-tilemap 12 21))
(for [y 0 19]
(map.set-itile-at 0 y state.well "X")
(map.set-itile-at 11 y state.well "X"))
(for [x 0 11]
(map.set-itile-at x 20 state.well "X"))
(set state.drop-ms 750)
(Rules.play-next-piece))
(fn Rules.play-next-piece []
(let [piece (or state.next-piece (Rules.choose-random-tetromino))
x (/ (- (length (. state.well 1)) 2) 2)]
(set state.current {: piece : x :y 0 :irotation 1})
(set state.next-piece (Rules.choose-random-tetromino))))
(fn Rules.current-tetromino []
(when state.current
(Rules.tetromino state.current.piece state.current.irotation)))
Rules