feat(app): add table shuffle and spread actions
This commit is contained in:
@@ -18,6 +18,7 @@ class PuzzlezTheme extends Theme {
|
|||||||
"restore" => "window-restore-solid.svg",
|
"restore" => "window-restore-solid.svg",
|
||||||
"compress" => "compress-solid.svg",
|
"compress" => "compress-solid.svg",
|
||||||
"expand" => "expand-solid.svg",
|
"expand" => "expand-solid.svg",
|
||||||
|
"spread" => "clone.svg",
|
||||||
];
|
];
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import hw.view.popup.ConfirmView;
|
|||||||
import promhx.Promise;
|
import promhx.Promise;
|
||||||
import ru.m.puzzlez.image.Game;
|
import ru.m.puzzlez.image.Game;
|
||||||
import ru.m.puzzlez.image.IGame;
|
import ru.m.puzzlez.image.IGame;
|
||||||
|
import ru.m.puzzlez.proto.event.GameAction;
|
||||||
import ru.m.puzzlez.proto.event.GameEvent;
|
import ru.m.puzzlez.proto.event.GameEvent;
|
||||||
|
import ru.m.puzzlez.proto.event.gameaction.Action;
|
||||||
import ru.m.puzzlez.proto.game.GameState;
|
import ru.m.puzzlez.proto.game.GameState;
|
||||||
import ru.m.puzzlez.proto.game.GameStatus;
|
import ru.m.puzzlez.proto.game.GameStatus;
|
||||||
import ru.m.puzzlez.render.IRender;
|
import ru.m.puzzlez.render.IRender;
|
||||||
@@ -91,6 +93,16 @@ import ru.m.puzzlez.view.popup.PreviewPopup;
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function shuffle():Void {
|
||||||
|
// TODO: playerId?
|
||||||
|
game.action(new GameAction().setAction(Action.TABLE_SHUFFLE).setPlayerId("local"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function spread():Void {
|
||||||
|
// TODO: playerId?
|
||||||
|
game.action(new GameAction().setAction(Action.TABLE_SPREAD).setPlayerId("local"));
|
||||||
|
}
|
||||||
|
|
||||||
private function back():Void {
|
private function back():Void {
|
||||||
(game.state.status == GameStatus.COMPLETE ? Promise.promise(true) : ConfirmView.confirm("Exit?")).then(result -> {
|
(game.state.status == GameStatus.COMPLETE ? Promise.promise(true) : ConfirmView.confirm("Exit?")).then(result -> {
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|||||||
@@ -25,6 +25,12 @@ views:
|
|||||||
- $type: hw.view.form.ButtonView
|
- $type: hw.view.form.ButtonView
|
||||||
style: icon.restore
|
style: icon.restore
|
||||||
+onPress: ~render.manager.reset()
|
+onPress: ~render.manager.reset()
|
||||||
|
- $type: hw.view.form.ButtonView
|
||||||
|
style: icon.spread
|
||||||
|
+onPress: ~shuffle()
|
||||||
|
- $type: hw.view.form.ButtonView
|
||||||
|
style: icon.spread
|
||||||
|
+onPress: ~spread()
|
||||||
- $type: hw.view.form.ButtonView
|
- $type: hw.view.form.ButtonView
|
||||||
style: icon.setting
|
style: icon.setting
|
||||||
geometry.margin.top: 30
|
geometry.margin.top: 30
|
||||||
|
|||||||
1
src/app/resources/icon/clone.svg
Normal file
1
src/app/resources/icon/clone.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M64 464H288c8.8 0 16-7.2 16-16V384h48v64c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V224c0-35.3 28.7-64 64-64h64v48H64c-8.8 0-16 7.2-16 16V448c0 8.8 7.2 16 16 16zM224 304H448c8.8 0 16-7.2 16-16V64c0-8.8-7.2-16-16-16H224c-8.8 0-16 7.2-16 16V288c0 8.8 7.2 16 16 16zm-64-16V64c0-35.3 28.7-64 64-64H448c35.3 0 64 28.7 64 64V288c0 35.3-28.7 64-64 64H224c-35.3 0-64-28.7-64-64z"/></svg>
|
||||||
|
After Width: | Height: | Size: 604 B |
17
src/common/haxe/ru/m/ArrayUtil.hx
Normal file
17
src/common/haxe/ru/m/ArrayUtil.hx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.m;
|
||||||
|
|
||||||
|
class ArrayUtil {
|
||||||
|
public static function shuffle<T>(source:Array<T>):Array<T> {
|
||||||
|
var target:Array<T> = source.slice(0);
|
||||||
|
var currentIndex = target.length;
|
||||||
|
|
||||||
|
while (currentIndex != 0) {
|
||||||
|
var randomIndex = Math.floor(Math.random() * currentIndex);
|
||||||
|
currentIndex--;
|
||||||
|
var tmp = target[currentIndex];
|
||||||
|
target[currentIndex] = target[randomIndex];
|
||||||
|
target[randomIndex] = tmp;
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,6 +50,29 @@ class Game implements IGame {
|
|||||||
var x = bound + Math.random() * (state.preset.tableRect.width - part.rect.width - bound * 2);
|
var x = bound + Math.random() * (state.preset.tableRect.width - part.rect.width - bound * 2);
|
||||||
var y = bound + Math.random() * (state.preset.tableRect.height - part.rect.height - bound * 2);
|
var y = bound + Math.random() * (state.preset.tableRect.height - part.rect.height - bound * 2);
|
||||||
part.position = new PointExt(x, y);
|
part.position = new PointExt(x, y);
|
||||||
|
events.emit(EventUtil.change(part));
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function spread():Void {
|
||||||
|
var partWidth = state.preset.imageRect.width / state.preset.grid.x;
|
||||||
|
var partHeight = state.preset.imageRect.height / state.preset.grid.y;
|
||||||
|
var bound = partWidth * 0.25;
|
||||||
|
var x = bound;
|
||||||
|
var y = bound;
|
||||||
|
var parts = ArrayUtil.shuffle(state.parts);
|
||||||
|
for (part in parts) {
|
||||||
|
switch part.location {
|
||||||
|
case PartLocation.TABLE:
|
||||||
|
part.position = new PointExt(x, y);
|
||||||
|
x += part.rect.width + bound;
|
||||||
|
if (x > state.preset.tableRect.width - partWidth - bound) {
|
||||||
|
x = bound;
|
||||||
|
y += part.rect.height + bound;
|
||||||
|
}
|
||||||
|
events.emit(EventUtil.change(part));
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,6 +130,10 @@ class Game implements IGame {
|
|||||||
part.position = event.action.position;
|
part.position = event.action.position;
|
||||||
events.emit(EventUtil.change(part));
|
events.emit(EventUtil.change(part));
|
||||||
}
|
}
|
||||||
|
case Action.TABLE_SHUFFLE:
|
||||||
|
shuffle();
|
||||||
|
case Action.TABLE_SPREAD:
|
||||||
|
spread();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ message GameAction {
|
|||||||
TAKE = 0;
|
TAKE = 0;
|
||||||
MOVE = 1;
|
MOVE = 1;
|
||||||
PUT = 2;
|
PUT = 2;
|
||||||
|
TABLE_SHUFFLE = 4;
|
||||||
|
TABLE_SPREAD = 3;
|
||||||
}
|
}
|
||||||
Action action = 3;
|
Action action = 3;
|
||||||
ru.m.puzzlez.proto.core.Point position = 4;
|
ru.m.puzzlez.proto.core.Point position = 4;
|
||||||
|
|||||||
Reference in New Issue
Block a user