From 3b7c7f6d40496b48cfbe6d921f18e0660e92f0c7 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 9 Jul 2022 16:03:15 +0000 Subject: [PATCH] left-side match checking --- .../source/HabitState.hx | 1 + .../source/HabitState.kiss | 62 ++++++++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx index cd59a9ff..56dc2c20 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx @@ -11,6 +11,7 @@ import flixel.util.FlxColor; import flixel.text.FlxText; import flixel.math.FlxRandom; import flixel.math.FlxPoint; +import flixel.math.FlxRect; import openfl.geom.Rectangle; import openfl.geom.Point; import openfl.geom.ColorTransform; diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss index f980b1af..ae8bc8b7 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss @@ -53,7 +53,9 @@ (prop &mut :Map matchingPiecesRight (new Map)) (prop &mut :Map matchingPiecesUp (new Map)) (prop &mut :Map matchingPiecesDown (new Map)) +(prop &mut :Map pieceData (new Map)) (prop &mut :Map> connectedPieces (new Map)) +(prop &mut :Map indexMap (new Map)) (prop &mut rewardFileIndex 0) (prop &mut maxRewardFile 0) @@ -103,6 +105,8 @@ (doFor map [matchingPiecesLeft matchingPiecesRight matchingPiecesUp matchingPiecesDown] (map.clear)) (connectedPieces.clear) + (doFor i (range TOTAL_PIECES) (dictSet connectedPieces i [])) + (indexMap.clear) (let [r (new FlxRandom (Strings.hashCode currentRewardFile.path)) graphicWidth rewardSprite.pixels.width @@ -137,12 +141,14 @@ sourceRect (new Rectangle jig.xy.x jig.xy.y jig.wh.x jig.wh.y)] (setNth spriteGrid jig.row jig.col s) (setNth indexGrid jig.row jig.col i) + (dictSet pieceData i jig) + (dictSet indexMap s i) (set s.draggable true) (s.enableMouseDrag false true) (set s.mouseStopDragCallback ->:Void [s x y] { - // TODO check for matches (in a function that is also called once at generation) + (checkMatches i) (pieceCamera.calculateScrollBounds rewardSprites SCROLL_BOUND_MARGIN) (dictSet (the Map save.data.storedPositions) i (new FlxPoint s.x s.y)) (save.flush) @@ -161,15 +167,20 @@ (doFor row (range PUZZLE_HEIGHT) (doFor col (range PUZZLE_WIDTH) (let [id (nth indexGrid row col)] - (try (let [toLeft (nth spriteGrid row (- col 1))] + // combination of try/whenLet should cover target languages + // where out-of-bounds nth throws an error AND languages + // where it returns null + (try (whenLet [toLeft (nth spriteGrid row (- col 1))] (dictSet matchingPiecesLeft id toLeft)) (catch [e] null)) - (try (let [toRight (nth spriteGrid row (+ col 1))] + (try (whenLet [toRight (nth spriteGrid row (+ col 1))] (dictSet matchingPiecesRight id toRight)) (catch [e] null)) - (try (let [toUp (nth spriteGrid (- row 1) col)] + (try (whenLet [toUp (nth spriteGrid (- row 1) col)] (dictSet matchingPiecesUp id toUp)) (catch [e] null)) - (try (let [toDown (nth spriteGrid (+ row 1) col)] + (try (whenLet [toDown (nth spriteGrid (+ row 1) col)] (dictSet matchingPiecesDown id toDown)) (catch [e] null))))) - (add rewardSprites)))) + (add rewardSprites) + (doFor i (range TOTAL_PIECES) + (checkMatches i))))) (pieceCamera.calculateScrollBounds rewardSprites SCROLL_BOUND_MARGIN) @@ -217,4 +228,41 @@ (set text.color color) (set text.cameras [uiCamera]) (+= textY text.height) - (entryTexts.add text))) \ No newline at end of file + (entryTexts.add text))) + +(method :FlxRect matchZoneLeft [:FlxExtendedSprite s] + (new FlxRect s.x (+ s.y s.origin.y) EDGE_LEEWAY EDGE_LEEWAY)) +(method :FlxRect matchZoneRight [:FlxExtendedSprite s] + (new FlxRect (- (+ s.x s.width) EDGE_LEEWAY) (+ s.y s.origin.y) EDGE_LEEWAY EDGE_LEEWAY)) +(method :FlxRect matchZoneUp [:FlxExtendedSprite s] + (new FlxRect (+ s.x s.origin.x) s.y EDGE_LEEWAY EDGE_LEEWAY)) +(method :FlxRect matchZoneDown [:FlxExtendedSprite s] + (new FlxRect (+ s.x s.origin.x) (- (+ s.y s.height) EDGE_LEEWAY) EDGE_LEEWAY EDGE_LEEWAY)) + +(method :Void connectPiece [id self toSprite] + (let [thisConnectedPieces (dictGet connectedPieces id) + toConnectedPieces (dictGet connectedPieces (dictGet indexMap toSprite))] + // Don't add duplicates + (thisConnectedPieces.remove toSprite) + (thisConnectedPieces.push toSprite) + (toConnectedPieces.remove self) + (toConnectedPieces.push self))) + +(method :Void checkMatches [id] + (when !(pieceData.exists id) (return)) + (let [s (nth rewardSprites.members 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))) + // TODO implement these: + (whenLet [toRight (dictGet matchingPiecesRight id)] + (dictSet matchingPiecesRight id toRight)) + (whenLet [toUp (dictGet matchingPiecesUp id)] + (dictSet matchingPiecesUp id toUp)) + (whenLet [toDown (dictGet matchingPiecesDown id)] + (dictSet matchingPiecesDown id toDown)))) \ No newline at end of file