feat(game): add TableUtil
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
"hxcpp": "4.2.1",
|
||||
"svg": "1.1.3",
|
||||
"protohx": "0.4.6",
|
||||
"yield": "3.2.2",
|
||||
"formatter": "1.16.0"
|
||||
},
|
||||
"haxe": "4.2.5"
|
||||
|
||||
@@ -81,6 +81,10 @@ class Render extends SpriteView implements IRender {
|
||||
onStart(event.start.state);
|
||||
} else if (event.hasChange()) {
|
||||
var part:PartView = parts[event.change.partId];
|
||||
// TODO:
|
||||
if (part == null) {
|
||||
return;
|
||||
}
|
||||
part.playerId = event.change.playerId;
|
||||
part.position = event.change.position;
|
||||
switch event.change.location {
|
||||
|
||||
@@ -5,8 +5,8 @@ import hw.view.frame.FrameSwitcher;
|
||||
import hw.view.frame.FrameView;
|
||||
import hw.view.popup.ConfirmView;
|
||||
import promhx.Promise;
|
||||
import ru.m.puzzlez.image.Game;
|
||||
import ru.m.puzzlez.image.IGame;
|
||||
import ru.m.puzzlez.game.Game;
|
||||
import ru.m.puzzlez.game.IGame;
|
||||
import ru.m.puzzlez.proto.event.GameAction;
|
||||
import ru.m.puzzlez.proto.event.GameEvent;
|
||||
import ru.m.puzzlez.proto.event.gameaction.Action;
|
||||
|
||||
@@ -5,7 +5,7 @@ import hw.view.data.DataView;
|
||||
import hw.view.form.ToggleButtonView;
|
||||
import hw.view.frame.FrameSwitcher;
|
||||
import hw.view.frame.FrameView;
|
||||
import ru.m.puzzlez.image.GameUtil;
|
||||
import ru.m.puzzlez.game.GameUtil;
|
||||
import ru.m.puzzlez.proto.game.ImageId;
|
||||
import ru.m.puzzlez.view.common.PresetView;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package ru.m.puzzlez.view.common;
|
||||
import hw.view.theme.StyleId;
|
||||
import hw.view.form.LabelView;
|
||||
import hw.view.group.GroupView;
|
||||
import ru.m.puzzlez.image.GameUtil;
|
||||
import ru.m.puzzlez.game.GameUtil;
|
||||
import ru.m.puzzlez.proto.game.GameState;
|
||||
|
||||
@:template class GameStateView extends GroupView {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.m.puzzlez.image;
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
import ru.m.puzzlez.proto.event.GameComplete;
|
||||
import ru.m.puzzlez.proto.event.GameChange;
|
||||
@@ -1,10 +1,10 @@
|
||||
package ru.m.puzzlez.image;
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
import hw.signal.Signal;
|
||||
import ru.m.puzzlez.proto.core.Point;
|
||||
import ru.m.puzzlez.proto.event.gameaction.Action;
|
||||
import ru.m.puzzlez.proto.event.GameAction;
|
||||
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.GameStatus;
|
||||
import ru.m.puzzlez.proto.game.Part;
|
||||
@@ -43,38 +43,16 @@ class Game implements IGame {
|
||||
}
|
||||
|
||||
private function shuffle():Void {
|
||||
for (part in state.parts) {
|
||||
switch part.location {
|
||||
case PartLocation.TABLE:
|
||||
var bound = part.rect.width * 0.25;
|
||||
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);
|
||||
part.position = new PointExt(x, y);
|
||||
events.emit(EventUtil.change(part));
|
||||
case _:
|
||||
}
|
||||
var changedParts = TableUtil.shuffle(state.parts, state.preset);
|
||||
for (part in changedParts) {
|
||||
events.emit(EventUtil.change(part));
|
||||
}
|
||||
}
|
||||
|
||||
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 _:
|
||||
}
|
||||
var changedParts = TableUtil.spread(state.parts, state.preset);
|
||||
for (part in changedParts) {
|
||||
events.emit(EventUtil.change(part));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.m.puzzlez.image;
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
import ru.m.puzzlez.proto.game.ImageId;
|
||||
import ru.m.puzzlez.image.Side;
|
||||
import ru.m.puzzlez.game.Side;
|
||||
import ru.m.puzzlez.proto.core.IntPoint;
|
||||
import ru.m.puzzlez.proto.core.Point;
|
||||
import ru.m.puzzlez.proto.core.Rectangle;
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.m.puzzlez.image;
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
import hw.signal.Signal;
|
||||
import ru.m.puzzlez.proto.event.GameAction;
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.m.puzzlez.image;
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
enum Side {
|
||||
TOP;
|
||||
56
src/common/haxe/ru/m/puzzlez/game/TableUtil.hx
Normal file
56
src/common/haxe/ru/m/puzzlez/game/TableUtil.hx
Normal file
@@ -0,0 +1,56 @@
|
||||
package ru.m.puzzlez.game;
|
||||
|
||||
import ru.m.puzzlez.wrap.RectangleExt;
|
||||
import ru.m.puzzlez.proto.game.GamePreset;
|
||||
import ru.m.puzzlez.proto.game.Part;
|
||||
import ru.m.puzzlez.proto.game.PartLocation;
|
||||
import ru.m.puzzlez.wrap.PointExt;
|
||||
import ru.m.puzzlez.wrap.RectangleExt;
|
||||
|
||||
@:yield
|
||||
class TableUtil {
|
||||
public static function shuffle(parts:Array<Part>, preset:GamePreset):Iterator<Part> {
|
||||
for (part in parts) {
|
||||
switch part.location {
|
||||
case PartLocation.TABLE:
|
||||
var bound:Float = part.rect.width * 0.25;
|
||||
var x:Float = bound + Math.random() * (preset.tableRect.width - part.rect.width - bound * 2);
|
||||
var y:Float = bound + Math.random() * (preset.tableRect.height - part.rect.height - bound * 2);
|
||||
part.position = new PointExt(x, y);
|
||||
@yield return part;
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function spread(parts:Array<Part>, preset:GamePreset):Iterator<Part> {
|
||||
var partWidth:Float = preset.imageRect.width / preset.grid.x;
|
||||
var partHeight:Float = preset.imageRect.height / preset.grid.y;
|
||||
var bound:Float = partWidth * 0.25;
|
||||
var x:Float = bound;
|
||||
var y:Float = bound;
|
||||
var imageRect:RectangleExt = preset.imageRect;
|
||||
var parts:Array<Part> = ArrayUtil.shuffle(parts);
|
||||
for (part in parts) {
|
||||
switch part.location {
|
||||
case PartLocation.TABLE:
|
||||
part.position = new PointExt(x, y);
|
||||
part.rect.x = part.position.x;
|
||||
part.rect.y = part.position.y;
|
||||
if (imageRect.intersect(part.rect)) {
|
||||
x = imageRect.right + bound;
|
||||
part.position = new PointExt(x, y);
|
||||
part.rect.x = part.position.x;
|
||||
part.rect.y = part.position.y;
|
||||
}
|
||||
x += partWidth + bound;
|
||||
if (x > preset.tableRect.width - partWidth - bound) {
|
||||
x = bound;
|
||||
y += partHeight + bound;
|
||||
}
|
||||
@yield return part;
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,21 @@ abstract RectangleExt(Rectangle) from Rectangle to Rectangle {
|
||||
return new Point().setX(this.width).setY(this.height);
|
||||
}
|
||||
|
||||
public function contain(point:Point):Bool {
|
||||
return point.x >= left && point.y >= top && point.x <= right && point.y <= bottom;
|
||||
}
|
||||
|
||||
public function intersect(rect:Rectangle):Bool {
|
||||
var thisRect:RectangleExt = this;
|
||||
var otherRect:RectangleExt = rect;
|
||||
return !(thisRect.left > otherRect.right || otherRect.left > thisRect.right || thisRect.top > otherRect.bottom || otherRect.top > thisRect.bottom);
|
||||
}
|
||||
|
||||
public function clone():Rectangle {
|
||||
return new Rectangle().setX(this.x).setY(this.y).setWidth(this.width).setHeight(this.height);
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return 'Rectangle{x=${this.x},y=${this.y},width=${this.width},height=${this.height}}';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user