Reveal a puzzle in Habit game
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
<!-- _______________________________ Libraries ______________________________ -->
|
||||
|
||||
<haxelib name="flixel" />
|
||||
<haxelib name="haxe-strings" />
|
||||
<haxelib name="kiss" />
|
||||
<haxelib name="kiss-tools" />
|
||||
|
||||
|
@@ -1,16 +1,21 @@
|
||||
package;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
import haxe.io.Path;
|
||||
import flixel.FlxG;
|
||||
import flixel.FlxState;
|
||||
import flixel.group.FlxGroup;
|
||||
import flixel.FlxSprite;
|
||||
import flixel.util.FlxColor;
|
||||
import flixel.text.FlxText;
|
||||
import flixel.math.FlxRandom;
|
||||
import flixel.math.FlxPoint;
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
import kiss_tools.KeyShortcutHandler;
|
||||
import HabitModel;
|
||||
import flixel.input.keyboard.FlxKey;
|
||||
import hx.strings.Strings;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class HabitState extends FlxState {}
|
||||
|
@@ -1,19 +1,70 @@
|
||||
(method &override :Void create [] (super.create))
|
||||
(method &override :Void update [:Float elapsed]
|
||||
(super.update elapsed)
|
||||
// Hold left-click to hide the habit text and see the image clearly:
|
||||
(when entryTexts (if FlxG.mouse.pressed (remove entryTexts) (add entryTexts)))
|
||||
// Handle keyboard input:
|
||||
(let [:FlxKey id (FlxG.keys.firstJustPressed)]
|
||||
(unless (= id -1)
|
||||
(shortcutHandler.handleKey (.toLowerCase (id.toString))))))
|
||||
|
||||
(prop &mut :FlxTypedGroup<FlxText> entryTexts null)
|
||||
(prop &mut :FlxTypedGroup<FlxSprite> rewardBlockers null)
|
||||
(prop &mut :KeyShortcutHandler<Entry> shortcutHandler null)
|
||||
|
||||
(prop &mut :HabitModel model null)
|
||||
|
||||
(var PUZZLE_WIDTH 15)
|
||||
(var PUZZLE_HEIGHT 10)
|
||||
(var TOTAL_PIECES (* PUZZLE_WIDTH PUZZLE_HEIGHT))
|
||||
(prop &mut :FlxSprite rewardSprite null)
|
||||
|
||||
(method setModel [m]
|
||||
(set model m)
|
||||
~(m.totalPoints)
|
||||
(set shortcutHandler (new KeyShortcutHandler))
|
||||
|
||||
(let [p (m.totalPoints)
|
||||
&mut i 0
|
||||
&mut currentRewardFile (first m.rewardFiles)]
|
||||
// Find, load, and add the current reward image as big as possible:
|
||||
(until (< p currentRewardFile.startingPoints)
|
||||
(if (>= ++i m.rewardFiles.length)
|
||||
(break)
|
||||
(set currentRewardFile (nth m.rewardFiles i))))
|
||||
(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
|
||||
(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)
|
||||
(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))))))
|
||||
|
||||
|
||||
(when entryTexts (remove entryTexts))
|
||||
(set entryTexts (new FlxTypedGroup))
|
||||
(set textY 0)
|
||||
@@ -29,7 +80,7 @@
|
||||
(let [label (HabitModel.activeLabel e)]
|
||||
(shortcutHandler.registerItem label.label e)))
|
||||
|
||||
**(set shortcutHandler.onBadKey ->:Void [_ _] {})
|
||||
(set shortcutHandler.onBadKey ->:Void [_ _] {})
|
||||
(set shortcutHandler.onSelectItem ->:Void [:Entry e]
|
||||
(let [label (HabitModel.activeLabel e)]
|
||||
**(TODO reveal a piece of the current puzzle)
|
||||
|
Reference in New Issue
Block a user