[common] update Point, add GridPoint

This commit is contained in:
2019-09-12 17:43:08 +03:00
parent de831fcb26
commit bddb590d30
13 changed files with 63 additions and 42 deletions

View File

@@ -15,7 +15,7 @@ class ConfigBundle implements IConfigBundle {
public function get(type:GameType):Config {
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));
}
return _cache.get(type);

View File

@@ -13,7 +13,7 @@ class LevelBundle implements ILevelBundle {
public function new() {}
private function resolve(id:PackId):LevelPack {
var bytes = Assets.getBytes('resources/level/${id}.zip');
var bytes = Assets.getBytes('level/${id}.zip');
return {
id: id,
data: LevelUtil.unpack(bytes).map(function(level) {
@@ -33,7 +33,7 @@ class LevelBundle implements ILevelBundle {
public function list():Array<PackId> {
var result = [];
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()));
}
}

View File

@@ -34,7 +34,7 @@ class LocalGame extends GameRunner {
}
if (humansTeams.length == 1 && result.winner == humansTeams[0]) {
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);
}
}

View 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);
}
}

View File

@@ -1,14 +1,18 @@
package ru.m.geom;
class Point {
public var x(default, default):Float;
public var y(default, default):Float;
abstract Point(Array<Float>) {
public var x(get, set):Float;
public var y(get, set):Float;
public function new(x:Float, y:Float) {
this.x = x;
this.y = y;
this = [x, 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 {
return new Point(x + point.x, y + point.y);
}
@@ -16,12 +20,4 @@ class Point {
public function clone():Point {
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}}';
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.engine;
import ru.m.geom.GridPoint;
import ru.m.geom.Direction;
import ru.m.geom.Line;
import ru.m.geom.Point;
@@ -48,7 +49,7 @@ import ru.m.tankz.map.LevelMap;
}
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;
destroySignal.emit(EntityTypeResolver.of(cell));
}

View File

@@ -34,13 +34,13 @@ class PackProgress {
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)) {
completed[levelId] = {
id: levelId,
presets: new Map(),
}
}
completed[levelId].presets[presetId] = {};
completed[levelId].presets[presetId] = result;
}
}

View File

@@ -1,7 +1,5 @@
package ru.m.tankz.map;
import haxe.ds.HashMap;
import ru.m.geom.Point;
import ru.m.geom.Rectangle;
import ru.m.tankz.config.Config;
import ru.m.tankz.map.Grid.GridCell;
@@ -16,11 +14,11 @@ class Brick {
public var config(default, set):BrickConfig;
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 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.cellY = cellY;
this.cells = cells;

View File

@@ -1,15 +1,14 @@
package ru.m.tankz.map;
import haxe.ds.HashMap;
import ru.m.geom.GridPoint;
import ru.m.geom.Line;
import ru.m.geom.Point;
import ru.m.geom.Rectangle;
class GridCell {
public var cellX(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 layer:Int;
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) {
this.cellX = cellX;
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.layer = layer;
this.armor = armor;
@@ -47,7 +46,7 @@ class Grid {
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) {
this.cellWidth = cellWidth;
@@ -55,7 +54,7 @@ class Grid {
this.width = width;
this.height = height;
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 {
@@ -66,7 +65,7 @@ class Grid {
var result:Array<GridCell> = [];
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)) {
var point = new Point(x, y);
var point = new GridPoint(x, y);
if (!border.contain(point)) {
var cell = buildBorderCell(Std.int(point.x), Std.int(point.y));
cells.set(point, cell);

View File

@@ -1,6 +1,6 @@
package ru.m.tankz.map;
import haxe.ds.HashMap;
import ru.m.geom.GridPoint;
import ru.m.geom.Point;
import ru.m.tankz.config.Config;
import ru.m.tankz.map.Grid;
@@ -22,7 +22,7 @@ class LevelMap {
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) {
this.config = config;
@@ -30,7 +30,7 @@ class LevelMap {
cellHeight = config.cell.width;
gridWidth = size != null ? size.width : config.grid.width;
gridHeight = size != null ? size.height : config.grid.height;
bricksMap = new HashMap();
bricksMap = new Map();
bricks = [];
bricksById = new Map();
grid = new Grid(
@@ -42,17 +42,17 @@ class LevelMap {
}
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 {
var cellX = Std.int(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);
if (brickConfig.layer > 0 || brickConfig.armor > 0) {
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);
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);
@@ -68,7 +68,7 @@ class LevelMap {
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);
}

View File

@@ -13,7 +13,7 @@ class ServerConfigBundle implements IConfigBundle {
public function new() {}
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 source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
return Config.fromSource(type, source);

View File

@@ -12,7 +12,7 @@ class ServerLevelBundle implements ILevelBundle {
public function new() {}
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);
return {
id: id,
@@ -25,7 +25,7 @@ class ServerLevelBundle implements ILevelBundle {
public function list():Array<PackId> {
var result = [];
for (path in FileSystem.readDirectory("./resources/level")) {
for (path in FileSystem.readDirectory("./level")) {
result.push(PackId.fromString(path.split("/").pop().split(".").shift()));
}
return result;