[add] Game and Render
This commit is contained in:
@@ -49,6 +49,9 @@ const app = new Project(
|
||||
sources: [
|
||||
'src/haxe',
|
||||
],
|
||||
assets: [
|
||||
'src/resources',
|
||||
],
|
||||
main: 'ru.m.puzzlez.PuzzlezApp',
|
||||
meta: {
|
||||
width: 1024,
|
||||
|
||||
13
src/haxe/ru/m/puzzlez/core/Game.hx
Normal file
13
src/haxe/ru/m/puzzlez/core/Game.hx
Normal 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 {
|
||||
|
||||
}
|
||||
}
|
||||
8
src/haxe/ru/m/puzzlez/core/GamePreset.hx
Normal file
8
src/haxe/ru/m/puzzlez/core/GamePreset.hx
Normal file
@@ -0,0 +1,8 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
|
||||
typedef GamePreset = {
|
||||
var image:BitmapData;
|
||||
var grid:Grid;
|
||||
}
|
||||
8
src/haxe/ru/m/puzzlez/core/GameState.hx
Normal file
8
src/haxe/ru/m/puzzlez/core/GameState.hx
Normal file
@@ -0,0 +1,8 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
|
||||
typedef GameState = {
|
||||
var preset:GamePreset;
|
||||
var image:BitmapData;
|
||||
}
|
||||
31
src/haxe/ru/m/puzzlez/core/GameUtil.hx
Normal file
31
src/haxe/ru/m/puzzlez/core/GameUtil.hx
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/haxe/ru/m/puzzlez/core/Grid.hx
Normal file
6
src/haxe/ru/m/puzzlez/core/Grid.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
typedef Grid = {
|
||||
var width:Int;
|
||||
var height:Int;
|
||||
}
|
||||
7
src/haxe/ru/m/puzzlez/core/IGame.hx
Normal file
7
src/haxe/ru/m/puzzlez/core/IGame.hx
Normal file
@@ -0,0 +1,7 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
interface IGame {
|
||||
public var state(default, null):GameState;
|
||||
|
||||
public function start():Void;
|
||||
}
|
||||
8
src/haxe/ru/m/puzzlez/render/IRender.hx
Normal file
8
src/haxe/ru/m/puzzlez/render/IRender.hx
Normal 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;
|
||||
}
|
||||
40
src/haxe/ru/m/puzzlez/render/Render.hx
Normal file
40
src/haxe/ru/m/puzzlez/render/Render.hx
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/haxe/ru/m/puzzlez/render/RenderUtil.hx
Normal file
12
src/haxe/ru/m/puzzlez/render/RenderUtil.hx
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
+onPress: ~start()
|
||||
- id: render
|
||||
$type: ru.m.puzzlez.render.Render
|
||||
geometry.width: 100%
|
||||
geometry.height: 100%
|
||||
geometry.margin: 5
|
||||
style: frame
|
||||
|
||||
BIN
src/resources/warty-final-ubuntu.png
Normal file
BIN
src/resources/warty-final-ubuntu.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
Reference in New Issue
Block a user