From 231fc1f57c0c2792c1a2c8529cfafedc8f091556 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 12 Aug 2022 17:13:50 +0000 Subject: [PATCH] macro to clean up (checkMatches) --- .../source/HabitState.kiss | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss index 2810fe6b..474f52c9 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss @@ -421,33 +421,29 @@ (thisConnectedPieces.push toSprite) (toConnectedPieces.push self))) +(defMacro _checkMatch [side otherSide] + (let [sideStr (symbolNameValue side) + otherSideStr (symbolNameValue otherSide) + to (symbol "to$sideStr") + mp (symbol "matchingPieces$sideStr") + mz1 (symbol "matchZone$sideStr") + mz2 (symbol "matchZone$otherSideStr")] + `(whenLet [,to (dictGet ,mp id) + mz1 (,mz1 s) + mz2 (,mz2 ,to)] + (unless .isEmpty (mz1.intersection mz2) + (connectPiece id s ,to mz1 mz2))))) + (method :Void checkMatches [id] (when !(pieceData.exists id) (return)) (let [s (dictGet spriteMap id) jig (dictGet pieceData id) row jig.row col jig.col] - - (whenLet [toLeft (dictGet matchingPiecesLeft id) - mzl (matchZoneLeft s) - mzr (matchZoneRight toLeft)] - (unless .isEmpty (mzl.intersection mzr) - (connectPiece id s toLeft mzl mzr))) - (whenLet [toRight (dictGet matchingPiecesRight id) - mzr (matchZoneRight s) - mzl (matchZoneLeft toRight)] - (unless .isEmpty (mzl.intersection mzr) - (connectPiece id s toRight mzr mzl))) - (whenLet [toUp (dictGet matchingPiecesUp id) - mzu (matchZoneUp s) - mzd (matchZoneDown toUp)] - (unless .isEmpty (mzu.intersection mzd) - (connectPiece id s toUp mzu mzd))) - (whenLet [toDown (dictGet matchingPiecesDown id) - mzd (matchZoneDown s) - mzu (matchZoneUp toDown)] - (unless .isEmpty (mzu.intersection mzd) - (connectPiece id s toDown mzd mzu))))) + (_checkMatch Left Right) + (_checkMatch Right Left) + (_checkMatch Up Down) + (_checkMatch Down Up))) (method :Array recursivelyConnectedPieces [s &opt :Array collected] (unless collected (set collected []))