From 3c708cd6f60b8d7a3ae9595f9e07845995b2f60f Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 10 Aug 2022 18:02:33 +0000 Subject: [PATCH] Solve the black bar jigsaw problem. close #104 --- .../source/HabitState.kiss | 4 +-- .../source/jigsawx/JigsawPiece.hx | 23 +++++++++++++ .../source/jigsawx/Jigsawx.hx | 34 +++++++++++-------- .../source/jigsawx/math/Vec2.hx | 6 ++++ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss index 358abd07..5bec2ffb 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss +++ b/projects/flixel-desktop-habit-puzzle-game/source/HabitState.kiss @@ -195,8 +195,8 @@ (let [r (new FlxRandom (Strings.hashCode currentRewardFile.path)) graphicWidth rewardSprite.pixels.width graphicHeight rewardSprite.pixels.height - pieceAssetWidth (Std.int (/ graphicWidth PUZZLE_WIDTH)) - pieceAssetHeight (Std.int (/ graphicHeight PUZZLE_HEIGHT)) + pieceAssetWidth (/ (- graphicWidth (* EDGE_LEEWAY 2)) PUZZLE_WIDTH) + pieceAssetHeight (/ (- graphicHeight (* EDGE_LEEWAY 2)) PUZZLE_HEIGHT) j (new Jigsawx pieceAssetWidth pieceAssetHeight graphicWidth graphicHeight EDGE_LEEWAY BUBBLE_SIZE PUZZLE_HEIGHT PUZZLE_WIDTH r) PIECE_WIDTH (/ rewardSprite.width PUZZLE_WIDTH) diff --git a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/JigsawPiece.hx b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/JigsawPiece.hx index 93e1c865..3e674132 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/JigsawPiece.hx +++ b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/JigsawPiece.hx @@ -35,6 +35,29 @@ class JigsawPiece{ points = []; stepAngle = JigsawMagicNumbers.stepSize*Math.PI/180; first = lt; + + lt = lt.copy(); + rt = rt.copy(); + lb = lb.copy(); + rb = rb.copy(); + var edgeLeeway = lt.x; + if (sideData.north == null) { + lt.y = 0; + rt.y = 0; + } + if (sideData.east == null) { + rt.x += edgeLeeway; + rb.x += edgeLeeway; + } + if (sideData.south == null) { + lb.y += edgeLeeway; + rb.y += edgeLeeway; + } + if (sideData.west == null) { + lt.x = 0; + lb.x = 0; + } + // NORTH side if( sideData.north != null ) createVertSide( lt, rt, bubbleSize, sideData.north, NORTH ); points.push( rt ); diff --git a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/Jigsawx.hx b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/Jigsawx.hx index bbccb1d1..a5f9b471 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/Jigsawx.hx +++ b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/Jigsawx.hx @@ -35,7 +35,7 @@ class Jigsawx { rows = rows_; cols = cols_; //corners, theoretically JigsawSideData could be modified to allow these to have a random element. - var xy = new Vec2( edgeLeeway, edgeLeeway ); + var xy = new Vec2( 0, 0 ); var lt = new Vec2( edgeLeeway, edgeLeeway ); var rt = new Vec2( edgeLeeway + dx, edgeLeeway ); var rb = new Vec2( edgeLeeway + dx, dy + edgeLeeway ); @@ -72,7 +72,7 @@ class Jigsawx { jigs.push( jig ); xy.x += dx; } - xy.x = edgeLeeway; + xy.x = 0; xy.y += dy; } @@ -80,18 +80,22 @@ class Jigsawx { var corners = ["top left", "top right", "bottom left", "bottom right"]; function contains(p:JigsawPiece, v:Vec2) { + var minOffset = Math.POSITIVE_INFINITY; for (pt in p.getPoints()) { - if (pt.x == v.x - p.xy.x && pt.y == v.y - p.xy.y) - return true; - } - return false; - } - var containsCorners = [ - contains(pieces[0][0], new Vec2(0, 0)), - contains(pieces[0][-1], new Vec2(totalWidth, 0)), - contains(pieces[-1][0], new Vec2(0, totalHeight)), - contains(pieces[-1][-1], new Vec2(totalWidth, totalHeight)) - ]; + var offset = Math.abs(pt.x - (v.x - p.xy.x)) + Math.abs(pt.y - (v.y - p.xy.y)); + if (offset < minOffset) + minOffset = offset; + if (offset < 1) + return true; + } + return false; + } + var containsCorners = [ + contains(pieces[0][0], new Vec2(0, 0)), + contains(pieces[0][-1], new Vec2(totalWidth, 0)), + contains(pieces[-1][0], new Vec2(0, totalHeight)), + contains(pieces[-1][-1], new Vec2(totalWidth, totalHeight)) + ]; var containsAllCorners = true; for (i in 0...corners.length) { if (!containsCorners[i]) { @@ -100,7 +104,9 @@ class Jigsawx { } } if (!containsAllCorners) { - throw "jigsawX geometry doesn't cover the whole image dimensions!"; + #if debug + throw "jigsawX geometry doesn't align with the whole image dimensions!"; + #end } } } diff --git a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/math/Vec2.hx b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/math/Vec2.hx index 34f5bc44..79036e79 100644 --- a/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/math/Vec2.hx +++ b/projects/flixel-desktop-habit-puzzle-game/source/jigsawx/math/Vec2.hx @@ -6,4 +6,10 @@ class Vec2{ x = x_; y = y_; } + public function copy() { + return new Vec2(x, y); + } + public function toString() { + return '(${x}, ${y})'; + } }