smoother connected dragging in KissExtendedSprite
This commit is contained in:
@@ -32,6 +32,7 @@ import datetime.DateTime;
|
|||||||
import flixel.ui.FlxButton;
|
import flixel.ui.FlxButton;
|
||||||
using kiss_flixel.CameraTools;
|
using kiss_flixel.CameraTools;
|
||||||
using kiss_flixel.GroupTools;
|
using kiss_flixel.GroupTools;
|
||||||
|
import kiss_flixel.KissExtendedSprite;
|
||||||
import haxe.ds.Option;
|
import haxe.ds.Option;
|
||||||
import jigsawx.JigsawPiece;
|
import jigsawx.JigsawPiece;
|
||||||
import jigsawx.Jigsawx;
|
import jigsawx.Jigsawx;
|
||||||
|
@@ -82,15 +82,6 @@
|
|||||||
(startAdding Bonus))
|
(startAdding Bonus))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// Left and right arrow keys can switch between unlocked puzzles
|
||||||
(unless entryNameText
|
(unless entryNameText
|
||||||
@@ -119,15 +110,15 @@
|
|||||||
(var PUZZLE_HEIGHT 5)
|
(var PUZZLE_HEIGHT 5)
|
||||||
(var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT))
|
(var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT))
|
||||||
(prop &mut :FlxSprite rewardSprite null)
|
(prop &mut :FlxSprite rewardSprite null)
|
||||||
(prop &mut :FlxTypedGroup<FlxExtendedSprite> rewardSprites null)
|
(prop &mut :FlxTypedGroup<KissExtendedSprite> rewardSprites null)
|
||||||
(prop &mut :Map<Int,FlxExtendedSprite> matchingPiecesLeft (new Map))
|
(prop &mut :Map<Int,KissExtendedSprite> matchingPiecesLeft (new Map))
|
||||||
(prop &mut :Map<Int,FlxExtendedSprite> matchingPiecesRight (new Map))
|
(prop &mut :Map<Int,KissExtendedSprite> matchingPiecesRight (new Map))
|
||||||
(prop &mut :Map<Int,FlxExtendedSprite> matchingPiecesUp (new Map))
|
(prop &mut :Map<Int,KissExtendedSprite> matchingPiecesUp (new Map))
|
||||||
(prop &mut :Map<Int,FlxExtendedSprite> matchingPiecesDown (new Map))
|
(prop &mut :Map<Int,KissExtendedSprite> matchingPiecesDown (new Map))
|
||||||
(prop &mut :Map<Int,JigsawPiece> pieceData (new Map))
|
(prop &mut :Map<Int,JigsawPiece> pieceData (new Map))
|
||||||
(prop &mut :Map<Int,Array<FlxExtendedSprite>> connectedPieces (new Map))
|
(prop &mut :Map<Int,Array<KissExtendedSprite>> connectedPieces (new Map))
|
||||||
(prop &mut :Map<FlxExtendedSprite,Int> indexMap (new Map))
|
(prop &mut :Map<KissExtendedSprite,Int> indexMap (new Map))
|
||||||
(prop &mut :Map<Int,FlxExtendedSprite> spriteMap (new Map)) // Because rewardSprites will be re-ordered in depth handling, this is required
|
(prop &mut :Map<Int,KissExtendedSprite> spriteMap (new Map)) // Because rewardSprites will be re-ordered in depth handling, this is required
|
||||||
|
|
||||||
(prop &mut lastRewardFileIndex -1)
|
(prop &mut lastRewardFileIndex -1)
|
||||||
(prop &mut rewardFileIndex 0)
|
(prop &mut rewardFileIndex 0)
|
||||||
@@ -138,7 +129,7 @@
|
|||||||
(method scrollBoundMargin []
|
(method scrollBoundMargin []
|
||||||
(fHalf (/ (max FlxG.width FlxG.height) rewardSprite.scale.x)))
|
(fHalf (/ (max FlxG.width FlxG.height) rewardSprite.scale.x)))
|
||||||
|
|
||||||
(prop &mut :FlxExtendedSprite draggingSprite null)
|
(prop &mut :KissExtendedSprite draggingSprite null)
|
||||||
(prop &mut :FlxPoint draggingLastPos null)
|
(prop &mut :FlxPoint draggingLastPos null)
|
||||||
|
|
||||||
// Main.hx sets off 99% of the app's logic by parsing the model file and calling setModel on startup and on a 30s loop:
|
// Main.hx sets off 99% of the app's logic by parsing the model file and calling setModel on startup and on a 30s loop:
|
||||||
@@ -223,7 +214,7 @@
|
|||||||
pos (ifLet [point (dictGet (the Map<Int,FlxPoint> save.data.storedPositions) i)]
|
pos (ifLet [point (dictGet (the Map<Int,FlxPoint> save.data.storedPositions) i)]
|
||||||
point
|
point
|
||||||
(nth startingPoints i))
|
(nth startingPoints i))
|
||||||
s (new FlxExtendedSprite pos.x pos.y)
|
s (new KissExtendedSprite pos.x pos.y)
|
||||||
source (new FlxSprite)
|
source (new FlxSprite)
|
||||||
mask (new FlxSprite)
|
mask (new FlxSprite)
|
||||||
sourceRect (new Rectangle jig.xy.x jig.xy.y jig.wh.x jig.wh.y)]
|
sourceRect (new Rectangle jig.xy.x jig.xy.y jig.wh.x jig.wh.y)]
|
||||||
@@ -237,19 +228,19 @@
|
|||||||
(s.enableMouseDrag false true)
|
(s.enableMouseDrag false true)
|
||||||
(set s.mouseStartDragCallback
|
(set s.mouseStartDragCallback
|
||||||
->:Void [s x y]
|
->:Void [s x y]
|
||||||
{
|
(let [s (cast s KissExtendedSprite)]
|
||||||
// Bring currently held pieces to the front:
|
// Bring currently held pieces to the front:
|
||||||
(rewardSprites.bringToFront s)
|
(rewardSprites.bringToFront s)
|
||||||
(set s.priorityID (+ 1 .priorityID (last (the kiss.List<FlxExtendedSprite> rewardSprites.members))))
|
(set s.priorityID (+ 1 .priorityID (last (the kiss.List<KissExtendedSprite> rewardSprites.members))))
|
||||||
(doFor connected (recursivelyConnectedPieces s)
|
(let [connectedPieces (recursivelyConnectedPieces s)]
|
||||||
(rewardSprites.bringToFront connected))
|
(doFor connected connectedPieces
|
||||||
|
(rewardSprites.bringToFront connected))
|
||||||
|
(set s.connectedSprites connectedPieces))
|
||||||
(set draggingSprite s)
|
(set draggingSprite s)
|
||||||
(set draggingLastPos (new FlxPoint s.x s.y))
|
(set draggingLastPos (new FlxPoint s.x s.y))))
|
||||||
})
|
|
||||||
(set s.mouseStopDragCallback
|
(set s.mouseStopDragCallback
|
||||||
->:Void [s x y]
|
->:Void [s x y]
|
||||||
{
|
(let [s (cast s KissExtendedSprite)]
|
||||||
(set draggingSprite null)
|
(set draggingSprite null)
|
||||||
(checkMatches i)
|
(checkMatches i)
|
||||||
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) i (new FlxPoint s.x s.y))
|
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) i (new FlxPoint s.x s.y))
|
||||||
@@ -258,8 +249,7 @@
|
|||||||
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) (dictGet indexMap connected) (new FlxPoint connected.x connected.y)))
|
(dictSet (the Map<Int,FlxPoint> save.data.storedPositions) (dictGet indexMap connected) (new FlxPoint connected.x connected.y)))
|
||||||
|
|
||||||
(pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN)
|
(pieceCamera.calculateScrollBounds rewardSprites uiCamera SCROLL_BOUND_MARGIN)
|
||||||
(save.flush)
|
(save.flush)))
|
||||||
})
|
|
||||||
|
|
||||||
(source.makeGraphic (Std.int sourceRect.width) (Std.int sourceRect.height) FlxColor.TRANSPARENT true)
|
(source.makeGraphic (Std.int sourceRect.width) (Std.int sourceRect.height) FlxColor.TRANSPARENT true)
|
||||||
(source.pixels.copyPixels rewardSprite.pixels sourceRect (new Point 0 0))
|
(source.pixels.copyPixels rewardSprite.pixels sourceRect (new Point 0 0))
|
||||||
@@ -389,7 +379,7 @@
|
|||||||
(logTexts.add text))))
|
(logTexts.add text))))
|
||||||
|
|
||||||
// TODO properly tune the match zones to bubbles
|
// TODO properly tune the match zones to bubbles
|
||||||
(method :FlxRect matchZone [:FlxExtendedSprite s compass]
|
(method :FlxRect matchZone [:KissExtendedSprite s compass]
|
||||||
(let [bubblePoints (dictGet .bubblePoints (dictGet pieceData (dictGet indexMap s)) compass)]
|
(let [bubblePoints (dictGet .bubblePoints (dictGet pieceData (dictGet indexMap s)) compass)]
|
||||||
(unless bubblePoints
|
(unless bubblePoints
|
||||||
(return (new FlxRect 0 0 0 0)))
|
(return (new FlxRect 0 0 0 0)))
|
||||||
@@ -404,26 +394,30 @@
|
|||||||
(+= rect.y s.y)
|
(+= rect.y s.y)
|
||||||
rect)))
|
rect)))
|
||||||
|
|
||||||
(method :FlxRect matchZoneLeft [:FlxExtendedSprite s]
|
(method :FlxRect matchZoneLeft [:KissExtendedSprite s]
|
||||||
(matchZone s WEST))
|
(matchZone s WEST))
|
||||||
(method :FlxRect matchZoneRight [:FlxExtendedSprite s]
|
(method :FlxRect matchZoneRight [:KissExtendedSprite s]
|
||||||
(matchZone s EAST))
|
(matchZone s EAST))
|
||||||
(method :FlxRect matchZoneUp [:FlxExtendedSprite s]
|
(method :FlxRect matchZoneUp [:KissExtendedSprite s]
|
||||||
(matchZone s NORTH))
|
(matchZone s NORTH))
|
||||||
(method :FlxRect matchZoneDown [:FlxExtendedSprite s]
|
(method :FlxRect matchZoneDown [:KissExtendedSprite s]
|
||||||
(matchZone s SOUTH))
|
(matchZone s SOUTH))
|
||||||
|
|
||||||
(prop &mut c 0)
|
(prop &mut c 0)
|
||||||
(method :Void connectPiece [id self toSprite selfMatchZone toSpriteMatchZone]
|
(method :Void connectPiece [id self toSprite selfMatchZone toSpriteMatchZone]
|
||||||
|
~"connectPiece"
|
||||||
(let [thisConnectedPieces (dictGet connectedPieces id)
|
(let [thisConnectedPieces (dictGet connectedPieces id)
|
||||||
toConnectedPieces (dictGet connectedPieces (dictGet indexMap toSprite))]
|
toConnectedPieces (dictGet connectedPieces (dictGet indexMap toSprite))]
|
||||||
|
|
||||||
|
// Don't add duplicates or snap for pieces alread connected
|
||||||
|
(when (contains thisConnectedPieces toSprite)
|
||||||
|
(return))
|
||||||
(+= c 1)
|
(+= c 1)
|
||||||
|
|
||||||
// Snap the pieces together
|
// Snap the pieces together
|
||||||
(let [offsetX (- toSpriteMatchZone.x selfMatchZone.x)
|
(let [offsetX (- toSpriteMatchZone.x selfMatchZone.x)
|
||||||
offsetY (- toSpriteMatchZone.y selfMatchZone.y)]
|
offsetY (- toSpriteMatchZone.y selfMatchZone.y)]
|
||||||
(doFor piece (.concat (recursivelyConnectedPieces self) [self])
|
(doFor piece (.concat (recursivelyConnectedPieces self) [self])
|
||||||
~"snip snap"
|
|
||||||
(+= piece.x offsetX)
|
(+= piece.x offsetX)
|
||||||
(+= piece.y offsetY))
|
(+= piece.y offsetY))
|
||||||
// TODO check for matches created by snapping all the pieces?
|
// TODO check for matches created by snapping all the pieces?
|
||||||
@@ -431,10 +425,7 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// Don't add duplicates
|
|
||||||
(thisConnectedPieces.remove toSprite)
|
|
||||||
(thisConnectedPieces.push toSprite)
|
(thisConnectedPieces.push toSprite)
|
||||||
(toConnectedPieces.remove self)
|
|
||||||
(toConnectedPieces.push self)))
|
(toConnectedPieces.push self)))
|
||||||
|
|
||||||
(method :Void checkMatches [id]
|
(method :Void checkMatches [id]
|
||||||
@@ -465,7 +456,7 @@
|
|||||||
(unless .isEmpty (mzu.intersection mzd)
|
(unless .isEmpty (mzu.intersection mzd)
|
||||||
(connectPiece id s toDown mzd mzu)))))
|
(connectPiece id s toDown mzd mzu)))))
|
||||||
|
|
||||||
(method :Array<FlxExtendedSprite> recursivelyConnectedPieces [s &opt :Array<FlxExtendedSprite> collected]
|
(method :Array<KissExtendedSprite> recursivelyConnectedPieces [s &opt :Array<KissExtendedSprite> collected]
|
||||||
(unless collected (set collected []))
|
(unless collected (set collected []))
|
||||||
(whenLet [directlyConnected (dictGet connectedPieces (dictGet indexMap s))]
|
(whenLet [directlyConnected (dictGet connectedPieces (dictGet indexMap s))]
|
||||||
(doFor piece directlyConnected
|
(doFor piece directlyConnected
|
||||||
|
Reference in New Issue
Block a user