[add] Game and Render

This commit is contained in:
2020-01-10 15:33:24 +03:00
parent 78db6d6765
commit f5abc29af1
13 changed files with 158 additions and 1 deletions

View File

@@ -0,0 +1,13 @@
package ru.m.puzzlez.core;
class Game implements IGame {
public var state(default, null):GameState;
public function new(preset:GamePreset) {
state = GameUtil.buildState(preset);
}
public function start():Void {
}
}

View File

@@ -0,0 +1,8 @@
package ru.m.puzzlez.core;
import flash.display.BitmapData;
typedef GamePreset = {
var image:BitmapData;
var grid:Grid;
}

View File

@@ -0,0 +1,8 @@
package ru.m.puzzlez.core;
import flash.display.BitmapData;
typedef GameState = {
var preset:GamePreset;
var image:BitmapData;
}

View File

@@ -0,0 +1,31 @@
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,
}
}
}

View File

@@ -0,0 +1,6 @@
package ru.m.puzzlez.core;
typedef Grid = {
var width:Int;
var height:Int;
}

View File

@@ -0,0 +1,7 @@
package ru.m.puzzlez.core;
interface IGame {
public var state(default, null):GameState;
public function start():Void;
}

View File

@@ -0,0 +1,8 @@
package ru.m.puzzlez.render;
import haxework.view.IView;
import ru.m.puzzlez.core.GameState;
interface IRender extends IView<Dynamic> {
public function draw(state:GameState):Void;
}

View File

@@ -0,0 +1,40 @@
package ru.m.puzzlez.render;
import flash.geom.Rectangle;
import haxework.view.SpriteView;
import haxework.view.utils.DrawUtil;
import ru.m.puzzlez.core.GameState;
class Render extends SpriteView implements IRender {
private var state:GameState;
public function new() {
super();
}
public function draw(state:GameState):Void {
this.state = state;
toRedraw();
}
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);
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;
content.graphics.moveTo(x, rect.top);
content.graphics.lineTo(x, rect.bottom);
}
for (i in 0...state.preset.grid.height + 1) {
var y = rect.y + i * rect.height / state.preset.grid.height;
content.graphics.moveTo(rect.left, y);
content.graphics.lineTo(rect.right, y);
}
content.graphics.lineStyle();
}
}
}

View File

@@ -0,0 +1,12 @@
package ru.m.puzzlez.render;
import flash.geom.Rectangle;
class RenderUtil {
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;
var height = source.height * s;
return new Rectangle((target.width - width) / 2, (target.height - height) / 2, width, height);
}
}

View File

@@ -1,10 +1,23 @@
package ru.m.puzzlez.view;
import haxework.view.group.VGroupView;
import openfl.utils.Assets;
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 render:IRender;
private var game:IGame;
public function new() {
super();
}
public function start() {
var image = Assets.getBitmapData('resources/warty-final-ubuntu.png');
game = new Game({image:image, grid: {width: 7, height: 5}});
render.draw(game.state);
}
}

View File

@@ -2,9 +2,17 @@ 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
text: Start
+onPress: ~start()
- id: render
$type: ru.m.puzzlez.render.Render
geometry.width: 100%
geometry.height: 100%
geometry.margin: 5
style: frame

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB