hardest part of integrating with jigsawX

This commit is contained in:
2022-07-08 22:54:53 +00:00
parent 9b50f893f0
commit 5fa18b4ba0
6 changed files with 116 additions and 63 deletions

View File

@@ -33,6 +33,7 @@
<!-- _______________________________ Libraries ______________________________ --> <!-- _______________________________ Libraries ______________________________ -->
<haxelib name="flixel" /> <haxelib name="flixel" />
<haxelib name="flixel-addons" />
<haxelib name="haxe-strings" /> <haxelib name="haxe-strings" />
<haxelib name="kiss" /> <haxelib name="kiss" />
<haxelib name="kiss-tools" /> <haxelib name="kiss-tools" />

View File

@@ -10,6 +10,13 @@ import flixel.util.FlxColor;
import flixel.text.FlxText; import flixel.text.FlxText;
import flixel.math.FlxRandom; import flixel.math.FlxRandom;
import flixel.math.FlxPoint; import flixel.math.FlxPoint;
import openfl.geom.Rectangle;
import openfl.geom.Point;
import openfl.geom.ColorTransform;
import flixel.util.FlxSpriteUtil;
import flixel.input.mouse.FlxMouseEventManager;
import flixel.addons.display.FlxExtendedSprite;
import flixel.addons.plugin.FlxMouseControl;
import kiss.Prelude; import kiss.Prelude;
import kiss.List; import kiss.List;
import kiss_tools.FlxKeyShortcutHandler; import kiss_tools.FlxKeyShortcutHandler;
@@ -17,5 +24,19 @@ import HabitModel;
import hx.strings.Strings; import hx.strings.Strings;
import datetime.DateTime; import datetime.DateTime;
import jigsawx.JigsawPiece;
import jigsawx.Jigsawx;
import jigsawx.math.Vec2;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class HabitState extends FlxState {} class HabitState extends FlxState {
public function drawPieceShape( surface: FlxSprite, jig: JigsawPiece, c: FlxColor )
{
var points = [for (point in jig.getPoints()) new FlxPoint(point.x, point.y)];
points.push(points[0]);
FlxSpriteUtil.drawPolygon(
surface,
points,
c);
}
}

View File

@@ -1,4 +1,9 @@
(method &override :Void create [] (super.create)) (prop &mut :Jigsawx jigsaw)
(method &override :Void create []
(FlxG.plugins.add (new FlxMouseControl))
(set bgColor FlxColor.GRAY)
(super.create))
(method &override :Void update [:Float elapsed] (method &override :Void update [:Float elapsed]
(super.update elapsed) (super.update elapsed)
// Hold left-click to hide the habit text and see the image clearly: // Hold left-click to hide the habit text and see the image clearly:
@@ -8,15 +13,15 @@
(shortcutHandler.update))) (shortcutHandler.update)))
(prop &mut :FlxTypedGroup<FlxText> entryTexts null) (prop &mut :FlxTypedGroup<FlxText> entryTexts null)
(prop &mut :FlxTypedGroup<FlxSprite> rewardBlockers null)
(prop &mut :FlxKeyShortcutHandler<Entry> shortcutHandler null) (prop &mut :FlxKeyShortcutHandler<Entry> shortcutHandler null)
(prop &mut :HabitModel model null) (prop &mut :HabitModel model null)
(prop EDGE_LEEWAY 20)
(var PUZZLE_WIDTH 4) (var PUZZLE_WIDTH 4)
(var PUZZLE_HEIGHT 4) (var PUZZLE_HEIGHT 4)
(var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT)) (var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT))
(prop &mut :FlxSprite rewardSprite null) (prop &mut :FlxTypedGroup<FlxExtendedSprite> rewardSprites null)
(method setModel [m] (method setModel [m]
(set model m) (set model m)
@@ -31,38 +36,61 @@
(set currentRewardFile (nth m.rewardFiles i)) (set currentRewardFile (nth m.rewardFiles i))
(if (>= ++i m.rewardFiles.length) (if (>= ++i m.rewardFiles.length)
(break))) (break)))
(when rewardSprite
(remove rewardSprite))
(set rewardSprite (new FlxSprite 0 0 (BitmapData.fromFile (joinPath (Path.directory m.textFile) currentRewardFile.path))))
(rewardSprite.setGraphicSize FlxG.width 0)
(rewardSprite.updateHitbox)
(when (> rewardSprite.height FlxG.height)
(rewardSprite.setGraphicSize 0 FlxG.height))
(rewardSprite.updateHitbox)
(rewardSprite.screenCenter)
(add rewardSprite)
(when rewardBlockers (when rewardSprites
(remove rewardBlockers)) (remove rewardSprites))
(set rewardBlockers (new FlxTypedGroup)) (let [rewardSprite
(add rewardBlockers) (new FlxSprite 0 0
(BitmapData.fromFile
(joinPath
(Path.directory m.textFile)
currentRewardFile.path)))]
(rewardSprite.setGraphicSize FlxG.width 0)
(rewardSprite.updateHitbox)
(when (> rewardSprite.height FlxG.height)
(rewardSprite.setGraphicSize 0 FlxG.height))
(rewardSprite.updateHitbox)
(rewardSprite.screenCenter)
(let [PIECE_WIDTH
(/ rewardSprite.width PUZZLE_WIDTH) (set rewardSprites (new FlxTypedGroup))
PIECE_HEIGHT
(/ rewardSprite.height PUZZLE_HEIGHT) (let [PIECE_WIDTH
:Array<FlxPoint> blockerPoints []] (/ rewardSprite.width PUZZLE_WIDTH)
(doFor x (range PUZZLE_WIDTH) PIECE_HEIGHT
(/ rewardSprite.height PUZZLE_HEIGHT)
:Array<FlxPoint> startingPoints []
:Array<Rectangle> sourceRectangles []
pieceAssetWidth (Std.int (/ rewardSprite.pixels.width PUZZLE_WIDTH))
pieceAssetHeight (Std.int (/ rewardSprite.pixels.height PUZZLE_HEIGHT))]
(doFor y (range PUZZLE_HEIGHT) (doFor y (range PUZZLE_HEIGHT)
(blockerPoints.push (new FlxPoint (+ rewardSprite.x (* x PIECE_WIDTH)) (+ rewardSprite.y (* y PIECE_HEIGHT)))))) (doFor x (range PUZZLE_WIDTH)
// Cover it up with (TOTAL_PIECES - p) black squares placed randomly by choosing and removing from a zipped coordinate list (startingPoints.push (new FlxPoint (+ rewardSprite.x (* x PIECE_WIDTH)) (+ rewardSprite.y (* y PIECE_HEIGHT))))
(let [r (new FlxRandom (Strings.hashCode currentRewardFile.path))] (sourceRectangles.push (new Rectangle (* x pieceAssetWidth) (* y pieceAssetHeight) pieceAssetWidth pieceAssetHeight))))
(r.shuffle blockerPoints) (let [r (new FlxRandom (Strings.hashCode currentRewardFile.path))]
(doFor i (range (- (+ TOTAL_PIECES currentRewardFile.startingPoints) p)) (r.shuffle startingPoints)
(let [pos (nth blockerPoints i) (set jigsaw (new Jigsawx pieceAssetWidth pieceAssetHeight EDGE_LEEWAY PUZZLE_HEIGHT PUZZLE_WIDTH r))
s (new FlxSprite pos.x pos.y)] (doFor i (range (- p currentRewardFile.startingPoints))
(s.makeGraphic (Math.ceil PIECE_WIDTH) (Math.ceil PIECE_HEIGHT) FlxColor.BLACK) (let [pos (nth startingPoints i)
(rewardBlockers.add s)))))) s (new FlxExtendedSprite pos.x pos.y)
source (new FlxSprite)
mask (new FlxSprite)]
(set s.draggable true)
(s.enableMouseDrag false true)
(s.makeGraphic pieceAssetWidth pieceAssetHeight FlxColor.TRANSPARENT true)
(source.makeGraphic pieceAssetWidth pieceAssetHeight FlxColor.TRANSPARENT true)
(source.pixels.copyPixels rewardSprite.pixels (nth sourceRectangles i) (new Point 0 0))
(mask.makeGraphic pieceAssetWidth pieceAssetHeight FlxColor.TRANSPARENT true)
(drawPieceShape mask (nth jigsaw.jigs i) FlxColor.BLACK)
(FlxSpriteUtil.alphaMask s source.pixels mask.pixels)
(s.setGraphicSize PIECE_WIDTH PIECE_HEIGHT)
(s.updateHitbox)
(rewardSprites.add s)))
(add rewardSprites)))))
(when entryTexts (remove entryTexts)) (when entryTexts (remove entryTexts))

View File

@@ -1,4 +1,5 @@
package jigsawx; package jigsawx;
import flixel.math.FlxRandom;
typedef JigsawPieceData = { typedef JigsawPieceData = {
var north: JigsawSideData; var north: JigsawSideData;
var east: JigsawSideData; var east: JigsawSideData;
@@ -24,14 +25,14 @@ class JigsawSideData{
public var rightWide: Float; public var rightWide: Float;
public var rightHi: Float; public var rightHi: Float;
// returns half a jigsawPieceData, the other side is populated from piece above and from left // returns half a jigsawPieceData, the other side is populated from piece above and from left
public static function halfPieceData(): JigsawPieceData{ public static function halfPieceData(r:FlxRandom): JigsawPieceData{
#if !noRandom return { north: null, east: create(), south: create(), west: null }; #if !noRandom return { north: null, east: create(r), south: create(r), west: null };
// Test use -D noRandom // Test use -D noRandom
#else return { north: null, east: createSimple(), south: createSimple(), west: null }; #else return { north: null, east: createSimple(r), south: createSimple(r), west: null };
#end #end
} }
private static function createBubble(): Bubble { private static function createBubble(r:FlxRandom): Bubble {
return ( Math.round( Math.random() ) == 1 )? IN: OUT; return r.bool() ? IN: OUT;
} }
private static function swapBubble( bubble: Bubble ): Bubble { private static function swapBubble( bubble: Bubble ): Bubble {
if( bubble == OUT ) return IN; if( bubble == OUT ) return IN;
@@ -56,9 +57,9 @@ class JigsawSideData{
return side; return side;
} }
// when you want to test no random. // when you want to test no random.
public static function createSimple(): JigsawSideData { public static function createSimple(r:FlxRandom): JigsawSideData {
var side = new JigsawSideData(); var side = new JigsawSideData();
side.bubble = createBubble(); side.bubble = createBubble(r);
//left right or up dawn offset. //left right or up dawn offset.
side.squew = 0.5; side.squew = 0.5;
// in out // in out
@@ -72,20 +73,20 @@ class JigsawSideData{
side.rightHi = 0.5; side.rightHi = 0.5;
return side; return side;
} }
public static function create(): JigsawSideData { public static function create(r:FlxRandom): JigsawSideData {
var side = new JigsawSideData(); var side = new JigsawSideData();
side.bubble = createBubble(); side.bubble = createBubble(r);
//left right or up dawn offset. //left right or up dawn offset.
side.squew = Math.random(); side.squew = r.float();
// in out // in out
side.inout = Math.random(); side.inout = r.float();
// radii of ellipses // radii of ellipses
side.leftWide = Math.random(); side.leftWide = r.float();
side.leftHi = Math.random(); side.leftHi = r.float();
side.centreWide = Math.random(); side.centreWide = r.float();
side.centreHi = Math.random(); side.centreHi = r.float();
side.rightWide = Math.random(); side.rightWide = r.float();
side.rightHi = Math.random(); side.rightHi = r.float();
return side; return side;
} }
// use create instead // use create instead

View File

@@ -3,6 +3,7 @@ import jigsawx.OpenEllipse ;
import jigsawx.JigsawPiece ; import jigsawx.JigsawPiece ;
import jigsawx.math.Vec2; import jigsawx.math.Vec2;
import jigsawx.JigsawSideData; import jigsawx.JigsawSideData;
import flixel.math.FlxRandom;
class Jigsawx { class Jigsawx {
private var rows: Int; private var rows: Int;
private var cols: Int; private var cols: Int;
@@ -16,31 +17,32 @@ class Jigsawx {
private var dx: Float; private var dx: Float;
private var dy: Float; private var dy: Float;
private var length: Int; private var length: Int;
public function new( dx_: Float public function new( pieceWidth: Float
, dy_: Float , pieceHeight: Float
, edgeLeeway: Float
, rows_: Int , rows_: Int
, cols_: Int , cols_: Int
) { , r: FlxRandom) {
pieces = []; pieces = [];
jigs = []; jigs = [];
sides = []; sides = [];
dx = dx_; dx = pieceWidth - edgeLeeway * 2;
dy = dy_; dy = pieceHeight - edgeLeeway * 2;
rows = rows_; rows = rows_;
cols = cols_; cols = cols_;
//corners, theoretically JigsawSideData could be modified to allow these to have a random element. //corners, theoretically JigsawSideData could be modified to allow these to have a random element.
var xy = new Vec2( 20, 20 ); var xy = new Vec2( edgeLeeway, edgeLeeway );
var lt = new Vec2( 20, 20 ); var lt = new Vec2( edgeLeeway, edgeLeeway );
var rt = new Vec2( 20 + dx, 20 ); var rt = new Vec2( edgeLeeway + dx, edgeLeeway );
var rb = new Vec2( 20 + dx, dy + 20 ); var rb = new Vec2( edgeLeeway + dx, dy + edgeLeeway );
var lb = new Vec2( 20, dy + 20 ); var lb = new Vec2( edgeLeeway, dy + edgeLeeway );
length = 0; length = 0;
var last: JigsawPieceData; var last: JigsawPieceData;
for( row in 0...rows ){ for( row in 0...rows ){
last = { north: null, east: null, south: null, west: null }; last = { north: null, east: null, south: null, west: null };
sides.push( new Array() ); sides.push( new Array() );
for( col in 0...cols ){ for( col in 0...cols ){
var jigsawPiece = JigsawSideData.halfPieceData(); var jigsawPiece = JigsawSideData.halfPieceData(r);
if( last.east != null ) jigsawPiece.west = JigsawSideData.reflect( last.east ); if( last.east != null ) jigsawPiece.west = JigsawSideData.reflect( last.east );
if( col == cols - 1 ) jigsawPiece.east = null; if( col == cols - 1 ) jigsawPiece.east = null;
sides[ row ][ col ] = jigsawPiece; sides[ row ][ col ] = jigsawPiece;
@@ -66,7 +68,7 @@ class Jigsawx {
jigs.push( jig ); jigs.push( jig );
xy.x += dx; xy.x += dx;
} }
xy.x = 20; xy.x = edgeLeeway;
xy.y += dy; xy.y += dy;
} }
} }

View File

@@ -31,7 +31,7 @@
*/ */
package jigsawx.hxopenfl; package jigsawx;