From 26af1f6e999f0420e2045ea86eee9cc9c15acaad Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 9 Jul 2022 20:06:45 +0000 Subject: [PATCH] pieces stick together (janky) --- .../source/HabitState.hx | 1 + .../source/HabitState.kiss | 61 ++++++++++++++++--- 2 files changed, 55 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 56dc2c20..e0e8bab4 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.hx @@ -16,6 +16,7 @@ import openfl.geom.Rectangle; import openfl.geom.Point; import openfl.geom.ColorTransform; import flixel.util.FlxSpriteUtil; +using flixel.util.FlxSpriteUtil; import flixel.util.FlxSave; import flixel.input.mouse.FlxMouseEventManager; import flixel.addons.display.FlxExtendedSprite; diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss index ae8bc8b7..b69ddf85 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss @@ -24,6 +24,16 @@ // Hold left-click to hide the habit text and see the image clearly: (when entryTexts (if FlxG.mouse.pressed (remove entryTexts) (add entryTexts))) + // drag along connected pieces + (when draggingSprite + (let [dx (- draggingSprite.x draggingLastPos.x) + dy (- draggingSprite.y draggingLastPos.y)] + (set draggingLastPos (new FlxPoint draggingSprite.x draggingSprite.y)) + (doFor s (recursivelyConnectedPieces draggingSprite) + (+= s.x dx) + (+= s.y dy)))) + + // Left and right arrow keys can switch between unlocked puzzles (when FlxG.keys.justPressed.LEFT (unless (= rewardFileIndex 0) @@ -62,6 +72,9 @@ (var SCROLL_BOUND_MARGIN 200) +(prop &mut :FlxExtendedSprite draggingSprite null) +(prop &mut :FlxPoint draggingLastPos null) + (method setModel [m &opt :RewardFile currentRewardFile] (set model m) (set shortcutHandler (new FlxKeyShortcutHandler)) @@ -145,9 +158,16 @@ (dictSet indexMap s i) (set s.draggable true) (s.enableMouseDrag false true) + (set s.mouseStartDragCallback + ->:Void [s x y] + { + (set draggingSprite s) + (set draggingLastPos (new FlxPoint s.x s.y)) + }) (set s.mouseStopDragCallback ->:Void [s x y] { + (set draggingSprite null) (checkMatches i) (pieceCamera.calculateScrollBounds rewardSprites SCROLL_BOUND_MARGIN) (dictSet (the Map save.data.storedPositions) i (new FlxPoint s.x s.y)) @@ -239,9 +259,12 @@ (method :FlxRect matchZoneDown [:FlxExtendedSprite s] (new FlxRect (+ s.x s.origin.x) (- (+ s.y s.height) EDGE_LEEWAY) EDGE_LEEWAY EDGE_LEEWAY)) +(prop &mut c 0) (method :Void connectPiece [id self toSprite] (let [thisConnectedPieces (dictGet connectedPieces id) toConnectedPieces (dictGet connectedPieces (dictGet indexMap toSprite))] + (print "connection $c found") + (+= c 1) // Don't add duplicates (thisConnectedPieces.remove toSprite) (thisConnectedPieces.push toSprite) @@ -254,15 +277,39 @@ jig (dictGet pieceData id) row jig.row col jig.col] + + /* // TODO tune the match zones + (let [l (matchZoneLeft s) + r (matchZoneRight s)] + (s.drawRect (- l.x s.x) (- l.y s.y) l.width l.height) + (s.drawRect (- r.x s.x) (- r.y s.y) r.width r.height)) */ + (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 + (whenLet [toRight (dictGet matchingPiecesRight id) + mzr (matchZoneRight s) + mzl (matchZoneLeft toRight)] + (unless .isEmpty (mzl.intersection mzr) + (connectPiece id s toRight))) + (whenLet [toUp (dictGet matchingPiecesUp id) + mzu (matchZoneUp s) + mzd (matchZoneDown toUp)] + (unless .isEmpty (mzu.intersection mzd) + (connectPiece id s toUp))) + (whenLet [toDown (dictGet matchingPiecesDown id) + mzd (matchZoneDown s) + mzu (matchZoneUp toDown)] + (unless .isEmpty (mzu.intersection mzd) + (connectPiece id s toDown))))) + +(method :Array recursivelyConnectedPieces [s &opt :Array collected] + (unless collected (set collected [])) + (let [directlyConnected (dictGet connectedPieces (dictGet indexMap s))] + (doFor piece directlyConnected + (unless (contains collected piece) + (collected.push piece) + (recursivelyConnectedPieces piece collected)))) + collected) \ No newline at end of file