[add] GameEvent
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
|
||||
class Game implements IGame {
|
||||
public var state(default, null):GameState;
|
||||
public var signal(default, null):Signal<GameEvent>;
|
||||
|
||||
public function new(preset:GamePreset) {
|
||||
state = GameUtil.buildState(preset);
|
||||
signal = new Signal();
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
signal.emit(GameEvent.START(state));
|
||||
}
|
||||
|
||||
public function stop():Void {
|
||||
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
signal.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
5
src/haxe/ru/m/puzzlez/core/GameEvent.hx
Normal file
5
src/haxe/ru/m/puzzlez/core/GameEvent.hx
Normal file
@@ -0,0 +1,5 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
enum GameEvent {
|
||||
START(state:GameState);
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
|
||||
typedef GamePreset = {
|
||||
var image:BitmapData;
|
||||
var image:String;
|
||||
var grid:Grid;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
|
||||
typedef GameState = {
|
||||
var preset:GamePreset;
|
||||
var image:BitmapData;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,10 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import flash.geom.Matrix;
|
||||
import flash.display.BitmapData;
|
||||
import flash.geom.Rectangle;
|
||||
|
||||
class GameUtil {
|
||||
public static function buildRectangle(source:Rectangle, grid:Grid):Rectangle {
|
||||
var r1 = grid.width / grid.height;
|
||||
var r2 = source.width / source.height;
|
||||
if (r1 > r2) {
|
||||
var height = source.width * r1;
|
||||
return new Rectangle(0, (source.height - height) / 2, source.width, height);
|
||||
} else {
|
||||
var width = source.height * r1;
|
||||
return new Rectangle((source.width - width) / 2, 0, width, source.height);
|
||||
}
|
||||
}
|
||||
|
||||
public static function buildState(preset:GamePreset):GameState {
|
||||
var rect = buildRectangle(new Rectangle(0, 0, preset.image.width, preset.image.height), preset.grid);
|
||||
var image = new BitmapData(Std.int(rect.width), Std.int(rect.height));
|
||||
var matrix = new Matrix();
|
||||
matrix.translate(-rect.x, -rect.y);
|
||||
image.draw(preset.image, matrix);
|
||||
return {
|
||||
preset: preset,
|
||||
image: image,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
|
||||
interface IGame {
|
||||
public var state(default, null):GameState;
|
||||
public var signal(default, null):Signal<GameEvent>;
|
||||
|
||||
public function start():Void;
|
||||
public function stop():Void;
|
||||
public function dispose():Void;
|
||||
}
|
||||
|
||||
interface IGameListener {
|
||||
public function onGameEvent(event:GameEvent):Void;
|
||||
}
|
||||
|
||||
6
src/haxe/ru/m/puzzlez/core/Part.hx
Normal file
6
src/haxe/ru/m/puzzlez/core/Part.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
typedef Part = {
|
||||
var x:Int;
|
||||
var y:Int;
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package ru.m.puzzlez.render;
|
||||
|
||||
import haxework.view.IView;
|
||||
import ru.m.puzzlez.core.GameEvent;
|
||||
import ru.m.puzzlez.core.GameState;
|
||||
|
||||
interface IRender extends IView<Dynamic> {
|
||||
public function draw(state:GameState):Void;
|
||||
public function onGameEvent(event:GameEvent):Void;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package ru.m.puzzlez.render;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
import flash.geom.Rectangle;
|
||||
import haxework.view.SpriteView;
|
||||
import haxework.view.utils.DrawUtil;
|
||||
import openfl.Assets;
|
||||
import ru.m.puzzlez.core.GameEvent;
|
||||
import ru.m.puzzlez.core.GameState;
|
||||
|
||||
class Render extends SpriteView implements IRender {
|
||||
|
||||
private var state:GameState;
|
||||
private var image:BitmapData;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
@@ -18,11 +22,23 @@ class Render extends SpriteView implements IRender {
|
||||
toRedraw();
|
||||
}
|
||||
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case START(state): onStart(state);
|
||||
}
|
||||
}
|
||||
|
||||
private function onStart(state:GameState):Void {
|
||||
image = Assets.getBitmapData(state.preset.image);
|
||||
image = RenderUtil.cropImage(image, state.preset.grid);
|
||||
draw(state);
|
||||
}
|
||||
|
||||
override public function redraw():Void {
|
||||
super.redraw();
|
||||
if (state != null) {
|
||||
var rect = RenderUtil.containRectangle(new Rectangle(0, 0, state.image.width, state.image.height), rect);
|
||||
DrawUtil.draw(content.graphics, state.image, rect, FillType.CONTAIN);
|
||||
if (state != null && image != null) {
|
||||
var rect = RenderUtil.containRectangle(new Rectangle(0, 0, image.width, image.height), rect);
|
||||
DrawUtil.draw(content.graphics, image, rect, FillType.CONTAIN);
|
||||
content.graphics.lineStyle(1, 0x00ff00);
|
||||
for (i in 0...state.preset.grid.width + 1) {
|
||||
var x = rect.x + i * rect.width / state.preset.grid.width;
|
||||
|
||||
@@ -1,8 +1,33 @@
|
||||
package ru.m.puzzlez.render;
|
||||
|
||||
import flash.geom.Matrix;
|
||||
import flash.display.BitmapData;
|
||||
import ru.m.puzzlez.core.Grid;
|
||||
import flash.geom.Rectangle;
|
||||
|
||||
class RenderUtil {
|
||||
|
||||
public static function buildRectangle(source:Rectangle, grid:Grid):Rectangle {
|
||||
var r1 = grid.width / grid.height;
|
||||
var r2 = source.width / source.height;
|
||||
if (r1 > r2) {
|
||||
var height = source.width * r1;
|
||||
return new Rectangle(0, (source.height - height) / 2, source.width, height);
|
||||
} else {
|
||||
var width = source.height * r1;
|
||||
return new Rectangle((source.width - width) / 2, 0, width, source.height);
|
||||
}
|
||||
}
|
||||
|
||||
public static function cropImage(source:BitmapData, grid:Grid):BitmapData {
|
||||
var rect = buildRectangle(new Rectangle(0, 0, source.width, source.height), grid);
|
||||
var image = new BitmapData(Std.int(rect.width), Std.int(rect.height));
|
||||
var matrix = new Matrix();
|
||||
matrix.translate(-rect.x, -rect.y);
|
||||
image.draw(source, matrix);
|
||||
return image;
|
||||
}
|
||||
|
||||
public static function containRectangle(source:Rectangle, target:Rectangle):Rectangle {
|
||||
var s = Math.min(1, Math.min(target.width / source.width, target.height / source.height));
|
||||
var width = source.width * s;
|
||||
|
||||
@@ -1,23 +1,48 @@
|
||||
package ru.m.puzzlez.view;
|
||||
|
||||
import haxework.view.utils.DrawUtil.FillType;
|
||||
import haxework.view.data.DataView;
|
||||
import haxework.view.group.VGroupView;
|
||||
import haxework.view.ImageView;
|
||||
import openfl.utils.Assets;
|
||||
import openfl.utils.AssetType;
|
||||
import ru.m.puzzlez.core.Game;
|
||||
import ru.m.puzzlez.core.IGame;
|
||||
import ru.m.puzzlez.render.IRender;
|
||||
|
||||
@:template class PuzzlezAppView extends VGroupView {
|
||||
|
||||
@:view private var images:DataView<String, ImageView>;
|
||||
@:view private var render:IRender;
|
||||
private var game:IGame;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
images.data = Assets.list(AssetType.IMAGE);
|
||||
}
|
||||
|
||||
public function start() {
|
||||
var image = Assets.getBitmapData('resources/warty-final-ubuntu.png');
|
||||
private function imageViewFactory(index:Int, image:String):ImageView {
|
||||
var result = new ImageView();
|
||||
result.stretch = false;
|
||||
result.fillType = FillType.COVER;
|
||||
result.geometry.width = 192;
|
||||
result.geometry.height = 128;
|
||||
result.image = Assets.getBitmapData(image);
|
||||
return result;
|
||||
}
|
||||
|
||||
public function start(image:String):Void {
|
||||
stop();
|
||||
game = new Game({image:image, grid: {width: 7, height: 5}});
|
||||
render.draw(game.state);
|
||||
game.signal.connect(render.onGameEvent);
|
||||
game.start();
|
||||
}
|
||||
|
||||
public function stop():Void {
|
||||
if (game != null) {
|
||||
game.stop();
|
||||
game.dispose();
|
||||
game = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
style: background
|
||||
layout.hAlign: center
|
||||
layout.vAlign: middle
|
||||
layout.margin: 20
|
||||
# geometry.padding: 5
|
||||
views:
|
||||
- $type: haxework.view.form.LabelView
|
||||
text: Puzzle'z
|
||||
font.size: 42
|
||||
- $type: haxework.view.form.ButtonView
|
||||
text: Start
|
||||
+onPress: ~start()
|
||||
- id: images
|
||||
$type: haxework.view.data.DataView
|
||||
layout:
|
||||
$type: haxework.view.layout.HorizontalLayout
|
||||
geometry.width: 100%
|
||||
# geometry.height: 100
|
||||
factory: ~imageViewFactory
|
||||
+onDataSelect: ~start
|
||||
geometry.margin: 5
|
||||
- id: render
|
||||
$type: ru.m.puzzlez.render.Render
|
||||
geometry.width: 100%
|
||||
|
||||
Reference in New Issue
Block a user