diff --git a/package.json b/package.json index 07eaabf..fd2548a 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/app/haxe/ru/m/puzzlez/render/Render.hx b/src/app/haxe/ru/m/puzzlez/render/Render.hx index fbb5e21..54e9ca2 100644 --- a/src/app/haxe/ru/m/puzzlez/render/Render.hx +++ b/src/app/haxe/ru/m/puzzlez/render/Render.hx @@ -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 { diff --git a/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx b/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx index 4ec21e6..cc5df69 100644 --- a/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx +++ b/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx @@ -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; diff --git a/src/app/haxe/ru/m/puzzlez/view/PresetFrame.hx b/src/app/haxe/ru/m/puzzlez/view/PresetFrame.hx index 5b9c5b8..1715993 100644 --- a/src/app/haxe/ru/m/puzzlez/view/PresetFrame.hx +++ b/src/app/haxe/ru/m/puzzlez/view/PresetFrame.hx @@ -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; diff --git a/src/app/haxe/ru/m/puzzlez/view/common/GameStateView.hx b/src/app/haxe/ru/m/puzzlez/view/common/GameStateView.hx index 3955f6a..2c0d895 100644 --- a/src/app/haxe/ru/m/puzzlez/view/common/GameStateView.hx +++ b/src/app/haxe/ru/m/puzzlez/view/common/GameStateView.hx @@ -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 { diff --git a/src/common/haxe/ru/m/puzzlez/image/EventUtil.hx b/src/common/haxe/ru/m/puzzlez/game/EventUtil.hx similarity index 97% rename from src/common/haxe/ru/m/puzzlez/image/EventUtil.hx rename to src/common/haxe/ru/m/puzzlez/game/EventUtil.hx index ef8ea37..869053e 100644 --- a/src/common/haxe/ru/m/puzzlez/image/EventUtil.hx +++ b/src/common/haxe/ru/m/puzzlez/game/EventUtil.hx @@ -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; diff --git a/src/common/haxe/ru/m/puzzlez/image/Game.hx b/src/common/haxe/ru/m/puzzlez/game/Game.hx similarity index 74% rename from src/common/haxe/ru/m/puzzlez/image/Game.hx rename to src/common/haxe/ru/m/puzzlez/game/Game.hx index 7fdf13d..a0e143e 100644 --- a/src/common/haxe/ru/m/puzzlez/image/Game.hx +++ b/src/common/haxe/ru/m/puzzlez/game/Game.hx @@ -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)); } } diff --git a/src/common/haxe/ru/m/puzzlez/image/GameUtil.hx b/src/common/haxe/ru/m/puzzlez/game/GameUtil.hx similarity index 98% rename from src/common/haxe/ru/m/puzzlez/image/GameUtil.hx rename to src/common/haxe/ru/m/puzzlez/game/GameUtil.hx index f708643..3ffa4d6 100644 --- a/src/common/haxe/ru/m/puzzlez/image/GameUtil.hx +++ b/src/common/haxe/ru/m/puzzlez/game/GameUtil.hx @@ -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; diff --git a/src/common/haxe/ru/m/puzzlez/image/IGame.hx b/src/common/haxe/ru/m/puzzlez/game/IGame.hx similarity index 93% rename from src/common/haxe/ru/m/puzzlez/image/IGame.hx rename to src/common/haxe/ru/m/puzzlez/game/IGame.hx index 01e67d0..9ebd965 100644 --- a/src/common/haxe/ru/m/puzzlez/image/IGame.hx +++ b/src/common/haxe/ru/m/puzzlez/game/IGame.hx @@ -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; diff --git a/src/common/haxe/ru/m/puzzlez/image/Side.hx b/src/common/haxe/ru/m/puzzlez/game/Side.hx similarity index 63% rename from src/common/haxe/ru/m/puzzlez/image/Side.hx rename to src/common/haxe/ru/m/puzzlez/game/Side.hx index cb8342f..f038427 100644 --- a/src/common/haxe/ru/m/puzzlez/image/Side.hx +++ b/src/common/haxe/ru/m/puzzlez/game/Side.hx @@ -1,4 +1,4 @@ -package ru.m.puzzlez.image; +package ru.m.puzzlez.game; enum Side { TOP; diff --git a/src/common/haxe/ru/m/puzzlez/game/TableUtil.hx b/src/common/haxe/ru/m/puzzlez/game/TableUtil.hx new file mode 100644 index 0000000..b412990 --- /dev/null +++ b/src/common/haxe/ru/m/puzzlez/game/TableUtil.hx @@ -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, preset:GamePreset):Iterator { + 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, preset:GamePreset):Iterator { + 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 = 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 _: + } + } + } +} diff --git a/src/common/haxe/ru/m/puzzlez/wrap/RectangleExt.hx b/src/common/haxe/ru/m/puzzlez/wrap/RectangleExt.hx index 9862814..30b7b68 100644 --- a/src/common/haxe/ru/m/puzzlez/wrap/RectangleExt.hx +++ b/src/common/haxe/ru/m/puzzlez/wrap/RectangleExt.hx @@ -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}}'; + } }