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 ______________________________ -->
<haxelib name="flixel" />
<haxelib name="flixel-addons" />
<haxelib name="haxe-strings" />
<haxelib name="kiss" />
<haxelib name="kiss-tools" />

View File

@@ -10,6 +10,13 @@ import flixel.util.FlxColor;
import flixel.text.FlxText;
import flixel.math.FlxRandom;
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.List;
import kiss_tools.FlxKeyShortcutHandler;
@@ -17,5 +24,19 @@ import HabitModel;
import hx.strings.Strings;
import datetime.DateTime;
import jigsawx.JigsawPiece;
import jigsawx.Jigsawx;
import jigsawx.math.Vec2;
@: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]
(super.update elapsed)
// Hold left-click to hide the habit text and see the image clearly:
@@ -8,15 +13,15 @@
(shortcutHandler.update)))
(prop &mut :FlxTypedGroup<FlxText> entryTexts null)
(prop &mut :FlxTypedGroup<FlxSprite> rewardBlockers null)
(prop &mut :FlxKeyShortcutHandler<Entry> shortcutHandler null)
(prop &mut :HabitModel model null)
(prop EDGE_LEEWAY 20)
(var PUZZLE_WIDTH 4)
(var PUZZLE_HEIGHT 4)
(var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT))
(prop &mut :FlxSprite rewardSprite null)
(prop &mut :FlxTypedGroup<FlxExtendedSprite> rewardSprites null)
(method setModel [m]
(set model m)
@@ -31,38 +36,61 @@
(set currentRewardFile (nth m.rewardFiles i))
(if (>= ++i m.rewardFiles.length)
(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 rewardSprites
(remove rewardSprites))
(let [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)
(when rewardBlockers
(remove rewardBlockers))
(set rewardBlockers (new FlxTypedGroup))
(add rewardBlockers)
(let [PIECE_WIDTH
(/ rewardSprite.width PUZZLE_WIDTH)
PIECE_HEIGHT
(/ rewardSprite.height PUZZLE_HEIGHT)
:Array<FlxPoint> blockerPoints []]
(doFor x (range PUZZLE_WIDTH)
(set rewardSprites (new FlxTypedGroup))
(let [PIECE_WIDTH
(/ rewardSprite.width 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)
(blockerPoints.push (new FlxPoint (+ rewardSprite.x (* x PIECE_WIDTH)) (+ rewardSprite.y (* y PIECE_HEIGHT))))))
// Cover it up with (TOTAL_PIECES - p) black squares placed randomly by choosing and removing from a zipped coordinate list
(let [r (new FlxRandom (Strings.hashCode currentRewardFile.path))]
(r.shuffle blockerPoints)
(doFor i (range (- (+ TOTAL_PIECES currentRewardFile.startingPoints) p))
(let [pos (nth blockerPoints i)
s (new FlxSprite pos.x pos.y)]
(s.makeGraphic (Math.ceil PIECE_WIDTH) (Math.ceil PIECE_HEIGHT) FlxColor.BLACK)
(rewardBlockers.add s))))))
(doFor x (range PUZZLE_WIDTH)
(startingPoints.push (new FlxPoint (+ rewardSprite.x (* x PIECE_WIDTH)) (+ rewardSprite.y (* y PIECE_HEIGHT))))
(sourceRectangles.push (new Rectangle (* x pieceAssetWidth) (* y pieceAssetHeight) pieceAssetWidth pieceAssetHeight))))
(let [r (new FlxRandom (Strings.hashCode currentRewardFile.path))]
(r.shuffle startingPoints)
(set jigsaw (new Jigsawx pieceAssetWidth pieceAssetHeight EDGE_LEEWAY PUZZLE_HEIGHT PUZZLE_WIDTH r))
(doFor i (range (- p currentRewardFile.startingPoints))
(let [pos (nth startingPoints i)
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))

View File

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

View File

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

View File

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