[common] update Point, add GridPoint
This commit is contained in:
@@ -42,7 +42,7 @@ const config = new Project.Config({
|
|||||||
'src-gen/haxe',
|
'src-gen/haxe',
|
||||||
],
|
],
|
||||||
assets: [
|
assets: [
|
||||||
'src/common/resources',
|
'src/common/resources/config',
|
||||||
],
|
],
|
||||||
flags: [
|
flags: [
|
||||||
//'proto_debug',
|
//'proto_debug',
|
||||||
@@ -76,6 +76,7 @@ const client = new Project(
|
|||||||
main: 'ru.m.tankz.Client',
|
main: 'ru.m.tankz.Client',
|
||||||
preloader: 'ru.m.tankz.Preloader',
|
preloader: 'ru.m.tankz.Preloader',
|
||||||
assets: [
|
assets: [
|
||||||
|
'src/common/resources/level',
|
||||||
'src/client/resources',
|
'src/client/resources',
|
||||||
],
|
],
|
||||||
meta: {
|
meta: {
|
||||||
@@ -138,6 +139,9 @@ const server = new Project(
|
|||||||
name: 'server',
|
name: 'server',
|
||||||
sources: ['src/server/haxe'],
|
sources: ['src/server/haxe'],
|
||||||
main: 'ru.m.tankz.server.Server',
|
main: 'ru.m.tankz.server.Server',
|
||||||
|
resources: [
|
||||||
|
'src/common/resources/level',
|
||||||
|
]
|
||||||
}),
|
}),
|
||||||
).bind(module, gulp);
|
).bind(module, gulp);
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class ConfigBundle implements IConfigBundle {
|
|||||||
|
|
||||||
public function get(type:GameType):Config {
|
public function get(type:GameType):Config {
|
||||||
if (!_cache.exists(type)) {
|
if (!_cache.exists(type)) {
|
||||||
var source:ConfigSource = Yaml.parse(Assets.getText('resources/config/${type}.yaml'), Parser.options().useObjects());
|
var source:ConfigSource = Yaml.parse(Assets.getText('config/${type}.yaml'), Parser.options().useObjects());
|
||||||
_cache.set(type, Config.fromSource(type, source));
|
_cache.set(type, Config.fromSource(type, source));
|
||||||
}
|
}
|
||||||
return _cache.get(type);
|
return _cache.get(type);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class LevelBundle implements ILevelBundle {
|
|||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
private function resolve(id:PackId):LevelPack {
|
private function resolve(id:PackId):LevelPack {
|
||||||
var bytes = Assets.getBytes('resources/level/${id}.zip');
|
var bytes = Assets.getBytes('level/${id}.zip');
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
data: LevelUtil.unpack(bytes).map(function(level) {
|
data: LevelUtil.unpack(bytes).map(function(level) {
|
||||||
@@ -33,7 +33,7 @@ class LevelBundle implements ILevelBundle {
|
|||||||
public function list():Array<PackId> {
|
public function list():Array<PackId> {
|
||||||
var result = [];
|
var result = [];
|
||||||
for (path in Assets.list(AssetType.BINARY)) {
|
for (path in Assets.list(AssetType.BINARY)) {
|
||||||
if (StringTools.startsWith(path, "resources/level")) {
|
if (StringTools.startsWith(path, "level")) {
|
||||||
result.push(PackId.fromString(path.split("/").pop().split(".").shift()));
|
result.push(PackId.fromString(path.split("/").pop().split(".").shift()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class LocalGame extends GameRunner {
|
|||||||
}
|
}
|
||||||
if (humansTeams.length == 1 && result.winner == humansTeams[0]) {
|
if (humansTeams.length == 1 && result.winner == humansTeams[0]) {
|
||||||
var progress = gameStorage.get(result.level.packId);
|
var progress = gameStorage.get(result.level.packId);
|
||||||
progress.completeLevel(result.level.id, result.state.presetId);
|
progress.completeLevel(result.level.id, result.state.presetId, {});
|
||||||
gameStorage.set(progress);
|
gameStorage.set(progress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/common/haxe/ru/m/geom/GridPoint.hx
Normal file
23
src/common/haxe/ru/m/geom/GridPoint.hx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package ru.m.geom;
|
||||||
|
|
||||||
|
abstract GridPoint({x:Int, y:Int}) {
|
||||||
|
public var x(get, set):Int;
|
||||||
|
public var y(get, set):Int;
|
||||||
|
|
||||||
|
public function new(x:Int, y:Int) {
|
||||||
|
this = {x: x, y: y};
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline function get_x():Int return this.x;
|
||||||
|
private inline function get_y():Int return this.y;
|
||||||
|
private inline function set_x(value:Int):Int return this.x = value;
|
||||||
|
private inline function set_y(value:Int):Int return this.y = value;
|
||||||
|
|
||||||
|
@:to inline public function toInt():Int {
|
||||||
|
return (this.x << 16) + this.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:to inline public function toPoint():Point {
|
||||||
|
return new Point(this.x, this.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
package ru.m.geom;
|
package ru.m.geom;
|
||||||
|
|
||||||
class Point {
|
abstract Point(Array<Float>) {
|
||||||
public var x(default, default):Float;
|
public var x(get, set):Float;
|
||||||
public var y(default, default):Float;
|
public var y(get, set):Float;
|
||||||
|
|
||||||
public function new(x:Float, y:Float) {
|
public function new(x:Float, y:Float) {
|
||||||
this.x = x;
|
this = [x, y];
|
||||||
this.y = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inline function get_x():Float return this[0];
|
||||||
|
private inline function get_y():Float return this[1];
|
||||||
|
private inline function set_x(value:Float):Float return this[0] = value;
|
||||||
|
private inline function set_y(value:Float):Float return this[1] = value;
|
||||||
|
|
||||||
public function add(point:Point):Point {
|
public function add(point:Point):Point {
|
||||||
return new Point(x + point.x, y + point.y);
|
return new Point(x + point.x, y + point.y);
|
||||||
}
|
}
|
||||||
@@ -16,12 +20,4 @@ class Point {
|
|||||||
public function clone():Point {
|
public function clone():Point {
|
||||||
return new Point(x, y);
|
return new Point(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hashCode():Int {
|
|
||||||
return Std.int(x + 1000 * y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function toString():String {
|
|
||||||
return 'Point{x=${Math.round(x * 100) / 100},y=${Math.round(y * 100) / 100}}';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.engine;
|
package ru.m.tankz.engine;
|
||||||
|
|
||||||
|
import ru.m.geom.GridPoint;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
@@ -48,7 +49,7 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function destroyCell(x:Int, y:Int):Void {
|
public function destroyCell(x:Int, y:Int):Void {
|
||||||
var cell = map.grid.cells.get(new Point(x, y));
|
var cell = map.grid.cells.get(new GridPoint(x, y));
|
||||||
cell.destroyed = true;
|
cell.destroyed = true;
|
||||||
destroySignal.emit(EntityTypeResolver.of(cell));
|
destroySignal.emit(EntityTypeResolver.of(cell));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,13 +34,13 @@ class PackProgress {
|
|||||||
return completed.exists(levelId) && completed.get(levelId).presets.get(presetId) != null;
|
return completed.exists(levelId) && completed.get(levelId).presets.get(presetId) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function completeLevel(levelId:LevelId, presetId:PresetId):Void {
|
public function completeLevel(levelId:LevelId, presetId:PresetId, result:LevelResult):Void {
|
||||||
if (!completed.exists(levelId)) {
|
if (!completed.exists(levelId)) {
|
||||||
completed[levelId] = {
|
completed[levelId] = {
|
||||||
id: levelId,
|
id: levelId,
|
||||||
presets: new Map(),
|
presets: new Map(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
completed[levelId].presets[presetId] = {};
|
completed[levelId].presets[presetId] = result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package ru.m.tankz.map;
|
package ru.m.tankz.map;
|
||||||
|
|
||||||
import haxe.ds.HashMap;
|
|
||||||
import ru.m.geom.Point;
|
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.map.Grid.GridCell;
|
import ru.m.tankz.map.Grid.GridCell;
|
||||||
@@ -16,11 +14,11 @@ class Brick {
|
|||||||
public var config(default, set):BrickConfig;
|
public var config(default, set):BrickConfig;
|
||||||
|
|
||||||
public var rect(default, null):Rectangle;
|
public var rect(default, null):Rectangle;
|
||||||
public var cells(default, null):HashMap<Point, GridCell>;
|
public var cells(default, null):Map<Int, GridCell>;
|
||||||
public var broken(get, null):Int;
|
public var broken(get, null):Int;
|
||||||
public var destroyed(get, set):Bool;
|
public var destroyed(get, set):Bool;
|
||||||
|
|
||||||
public function new(mapConfig:MapConfig, config:BrickConfig, cellX:Int, cellY:Int, cells:HashMap<Point, GridCell>) {
|
public function new(mapConfig:MapConfig, config:BrickConfig, cellX:Int, cellY:Int, cells:Map<Int, GridCell>) {
|
||||||
this.cellX = cellX;
|
this.cellX = cellX;
|
||||||
this.cellY = cellY;
|
this.cellY = cellY;
|
||||||
this.cells = cells;
|
this.cells = cells;
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package ru.m.tankz.map;
|
package ru.m.tankz.map;
|
||||||
|
|
||||||
import haxe.ds.HashMap;
|
import ru.m.geom.GridPoint;
|
||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.geom.Point;
|
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
|
|
||||||
class GridCell {
|
class GridCell {
|
||||||
public var cellX(default, null):Int;
|
public var cellX(default, null):Int;
|
||||||
public var cellY(default, null):Int;
|
public var cellY(default, null):Int;
|
||||||
|
|
||||||
public var position(default, null):Point;
|
public var position(default, null):GridPoint;
|
||||||
public var rect(default, null):Rectangle;
|
public var rect(default, null):Rectangle;
|
||||||
public var layer:Int;
|
public var layer:Int;
|
||||||
public var armor:Float;
|
public var armor:Float;
|
||||||
@@ -18,7 +17,7 @@ class GridCell {
|
|||||||
public function new(cellX:Int, cellY:Int, width:Int, height:Int, layer:Int, armor:Float) {
|
public function new(cellX:Int, cellY:Int, width:Int, height:Int, layer:Int, armor:Float) {
|
||||||
this.cellX = cellX;
|
this.cellX = cellX;
|
||||||
this.cellY = cellY;
|
this.cellY = cellY;
|
||||||
this.position = new Point(cellX, cellY);
|
this.position = new GridPoint(cellX, cellY);
|
||||||
this.rect = new Rectangle(cellX * width, cellY * height, width, height);
|
this.rect = new Rectangle(cellX * width, cellY * height, width, height);
|
||||||
this.layer = layer;
|
this.layer = layer;
|
||||||
this.armor = armor;
|
this.armor = armor;
|
||||||
@@ -47,7 +46,7 @@ class Grid {
|
|||||||
|
|
||||||
private var border:Rectangle;
|
private var border:Rectangle;
|
||||||
|
|
||||||
public var cells(default, null):HashMap<Point, GridCell>;
|
public var cells(default, null):Map<Int, GridCell>;
|
||||||
|
|
||||||
public function new(cellWidth:Int, cellHeight:Int, width:Int, height:Int) {
|
public function new(cellWidth:Int, cellHeight:Int, width:Int, height:Int) {
|
||||||
this.cellWidth = cellWidth;
|
this.cellWidth = cellWidth;
|
||||||
@@ -55,7 +54,7 @@ class Grid {
|
|||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
border = new Rectangle(0, 0, Math.floor(width / cellWidth) - 1, Math.floor(height / cellHeight) - 1);
|
border = new Rectangle(0, 0, Math.floor(width / cellWidth) - 1, Math.floor(height / cellHeight) - 1);
|
||||||
cells = new HashMap();
|
cells = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildBorderCell(cellX:Int, cellY:Int):GridCell {
|
private function buildBorderCell(cellX:Int, cellY:Int):GridCell {
|
||||||
@@ -66,7 +65,7 @@ class Grid {
|
|||||||
var result:Array<GridCell> = [];
|
var result:Array<GridCell> = [];
|
||||||
for (x in Math.floor(line.point1.x / cellWidth)...Math.ceil(line.point2.x / cellWidth)) {
|
for (x in Math.floor(line.point1.x / cellWidth)...Math.ceil(line.point2.x / cellWidth)) {
|
||||||
for (y in Math.floor(line.point1.y / cellHeight)...Math.ceil(line.point2.y / cellHeight)) {
|
for (y in Math.floor(line.point1.y / cellHeight)...Math.ceil(line.point2.y / cellHeight)) {
|
||||||
var point = new Point(x, y);
|
var point = new GridPoint(x, y);
|
||||||
if (!border.contain(point)) {
|
if (!border.contain(point)) {
|
||||||
var cell = buildBorderCell(Std.int(point.x), Std.int(point.y));
|
var cell = buildBorderCell(Std.int(point.x), Std.int(point.y));
|
||||||
cells.set(point, cell);
|
cells.set(point, cell);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.map;
|
package ru.m.tankz.map;
|
||||||
|
|
||||||
import haxe.ds.HashMap;
|
import ru.m.geom.GridPoint;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.map.Grid;
|
import ru.m.tankz.map.Grid;
|
||||||
@@ -22,7 +22,7 @@ class LevelMap {
|
|||||||
|
|
||||||
public var grid(default, null):Grid;
|
public var grid(default, null):Grid;
|
||||||
|
|
||||||
private var bricksMap(default, null):HashMap<Point, Brick>;
|
private var bricksMap(default, null):Map<Int, Brick>;
|
||||||
|
|
||||||
public function new(config:MapConfig, size:GridSize = null) {
|
public function new(config:MapConfig, size:GridSize = null) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
@@ -30,7 +30,7 @@ class LevelMap {
|
|||||||
cellHeight = config.cell.width;
|
cellHeight = config.cell.width;
|
||||||
gridWidth = size != null ? size.width : config.grid.width;
|
gridWidth = size != null ? size.width : config.grid.width;
|
||||||
gridHeight = size != null ? size.height : config.grid.height;
|
gridHeight = size != null ? size.height : config.grid.height;
|
||||||
bricksMap = new HashMap();
|
bricksMap = new Map();
|
||||||
bricks = [];
|
bricks = [];
|
||||||
bricksById = new Map();
|
bricksById = new Map();
|
||||||
grid = new Grid(
|
grid = new Grid(
|
||||||
@@ -42,17 +42,17 @@ class LevelMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function setData(data:Array<BrickConfig>):Void {
|
public function setData(data:Array<BrickConfig>):Void {
|
||||||
bricksMap = new HashMap();
|
bricksMap = new Map();
|
||||||
bricks = Lambda.array(Lambda.mapi(data, function(i:Int, brickConfig:BrickConfig):Brick {
|
bricks = Lambda.array(Lambda.mapi(data, function(i:Int, brickConfig:BrickConfig):Brick {
|
||||||
var cellX = Std.int(i % gridWidth);
|
var cellX = Std.int(i % gridWidth);
|
||||||
var cellY = Std.int(Math.floor(i / gridWidth));
|
var cellY = Std.int(Math.floor(i / gridWidth));
|
||||||
var cells:HashMap<Point, GridCell> = new HashMap();
|
var cells:Map<Int, GridCell> = new Map();
|
||||||
var point:Point = new Point(cellX * 2, cellY * 2);
|
var point:Point = new Point(cellX * 2, cellY * 2);
|
||||||
if (brickConfig.layer > 0 || brickConfig.armor > 0) {
|
if (brickConfig.layer > 0 || brickConfig.armor > 0) {
|
||||||
for (x in 0...2) for (y in 0...2) {
|
for (x in 0...2) for (y in 0...2) {
|
||||||
var cell:GridCell = new GridCell(Std.int(point.x + x), Std.int(point.y + y), Std.int(cellWidth / 2), Std.int(cellHeight / 2), brickConfig.layer, brickConfig.armor);
|
var cell:GridCell = new GridCell(Std.int(point.x + x), Std.int(point.y + y), Std.int(cellWidth / 2), Std.int(cellHeight / 2), brickConfig.layer, brickConfig.armor);
|
||||||
grid.cells.set(cell.position, cell);
|
grid.cells.set(cell.position, cell);
|
||||||
cells.set(new Point(x, y), cell);
|
cells.set(new GridPoint(x, y), cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var brick = new Brick(config, brickConfig, cellX, cellY, cells);
|
var brick = new Brick(config, brickConfig, cellX, cellY, cells);
|
||||||
@@ -68,7 +68,7 @@ class LevelMap {
|
|||||||
return bricks[Std.int(position.x + position.y * gridWidth)];
|
return bricks[Std.int(position.x + position.y * gridWidth)];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCellBrick(position:Point):Brick {
|
public function getCellBrick(position:GridPoint):Brick {
|
||||||
return bricksMap.get(position);
|
return bricksMap.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class ServerConfigBundle implements IConfigBundle {
|
|||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
public function get(type:GameType):Config {
|
public function get(type:GameType):Config {
|
||||||
var path:String = FileSystem.absolutePath('./resources/config/${type}.yaml');
|
var path:String = FileSystem.absolutePath('./config/${type}.yaml');
|
||||||
var data:String = File.getContent(path);
|
var data:String = File.getContent(path);
|
||||||
var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
|
var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
|
||||||
return Config.fromSource(type, source);
|
return Config.fromSource(type, source);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class ServerLevelBundle implements ILevelBundle {
|
|||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
public function get(id:PackId):LevelPack {
|
public function get(id:PackId):LevelPack {
|
||||||
var path = FileSystem.absolutePath('./resources/level/${id}.zip');
|
var path = FileSystem.absolutePath('./level/${id}.zip');
|
||||||
var bytes = File.getBytes(path);
|
var bytes = File.getBytes(path);
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
@@ -25,7 +25,7 @@ class ServerLevelBundle implements ILevelBundle {
|
|||||||
|
|
||||||
public function list():Array<PackId> {
|
public function list():Array<PackId> {
|
||||||
var result = [];
|
var result = [];
|
||||||
for (path in FileSystem.readDirectory("./resources/level")) {
|
for (path in FileSystem.readDirectory("./level")) {
|
||||||
result.push(PackId.fromString(path.split("/").pop().split(".").shift()));
|
result.push(PackId.fromString(path.split("/").pop().split(".").shift()));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user