[common] add map size

This commit is contained in:
2019-06-11 17:29:18 +03:00
parent 2c42d993ca
commit a66b72ba22
19 changed files with 143 additions and 62 deletions

View File

@@ -120,7 +120,7 @@ const editor = new Project(
height: 850, height: 850,
}, },
flags: [ flags: [
//'dev_layout', 'dev_layout',
] ]
}) })
).bind(module, gulp); ).bind(module, gulp);

View File

@@ -6,6 +6,7 @@ import ru.m.tankz.game.IGame;
interface IRender extends IView<Dynamic> extends GameListener { interface IRender extends IView<Dynamic> extends GameListener {
public var config(default, set):Config; public var config(default, set):Config;
public var gridSize(default, set):GridSize;
public function draw():Void; public function draw():Void;
public function reset():Void; public function reset():Void;
} }

View File

@@ -26,6 +26,10 @@ using ru.m.display.DisplayObjectContainerExtender;
class Render extends SpriteView implements IRender { class Render extends SpriteView implements IRender {
public var config(default, set):Config; public var config(default, set):Config;
public var gridSize(default, set):GridSize;
private var mapWidth(get, null):Float;
private var mapHeight(get, null):Float;
private var backgroundLayer:Sprite; private var backgroundLayer:Sprite;
private var groundLayer:Sprite; private var groundLayer:Sprite;
@@ -51,18 +55,33 @@ class Render extends SpriteView implements IRender {
reset(); reset();
} }
private function get_mapWidth():Float {
return config.map.cell.width * (gridSize != null ? gridSize.width : config.map.grid.width);
}
private function get_mapHeight():Float {
return config.map.cell.height * (gridSize != null ? gridSize.height : config.map.grid.height);
}
private function set_config(value:Config):Config { private function set_config(value:Config):Config {
config = value; config = value;
setContentSize(config.mapWidth, config.mapHeight); setContentSize(mapWidth, mapHeight, "render");
drawBackground(); drawBackground();
return config; return config;
} }
private function set_gridSize(value:GridSize):GridSize {
gridSize = value;
setContentSize(mapWidth, mapHeight, "render");
drawBackground();
return gridSize;
}
private function drawBackground():Void { private function drawBackground():Void {
var g:Graphics = backgroundLayer.graphics; var g:Graphics = backgroundLayer.graphics;
g.clear(); g.clear();
g.beginFill(0x000000); g.beginFill(0x000000);
g.drawRect(0, 0, config.mapWidth, config.mapHeight); g.drawRect(0, 0, mapWidth, mapHeight);
g.endFill(); g.endFill();
} }

View File

@@ -34,6 +34,7 @@ import ru.m.tankz.view.game.GameView;
gameView.type = game.type; gameView.type = game.type;
soundManager.config = game.config; soundManager.config = game.config;
gameView.render.config = game.config; gameView.render.config = game.config;
gameView.render.gridSize = game.state.level.size;
game.connect(gameView.render); game.connect(gameView.render);
game.connect(soundManager); game.connect(soundManager);
if (gameView.panel != null) { if (gameView.panel != null) {

View File

@@ -23,11 +23,19 @@ typedef SpawnPoint = {
var direction:String; var direction:String;
} }
typedef CellSize = {
var width:Float;
var height:Float;
}
typedef GridSize = {
var width:Int;
var height:Int;
}
typedef MapConfig = { typedef MapConfig = {
var cellWidth:Float; var cell:CellSize;
var cellHeight:Float; var grid:GridSize;
var gridWidth:Int;
var gridHeight:Int;
} }
typedef BrickConfig = { typedef BrickConfig = {
@@ -103,6 +111,7 @@ typedef LevelConfig = {
var data:Array<BrickConfig>; var data:Array<BrickConfig>;
@:optional var name:String; @:optional var name:String;
@:optional var points:Array<SpawnPoint>; @:optional var points:Array<SpawnPoint>;
@:optional var size:{width:Int, height:Int};
} }
typedef PlayerControl = { typedef PlayerControl = {
@@ -139,8 +148,6 @@ class Config {
public var controls(default, null):Array<ControlPreset>; public var controls(default, null):Array<ControlPreset>;
public var points(default, null):Array<SpawnPoint>; public var points(default, null):Array<SpawnPoint>;
public var bonuses(default, null):Array<BonusConfig>; public var bonuses(default, null):Array<BonusConfig>;
public var mapWidth(get, null):Float;
public var mapHeight(get, null):Float;
private var brickMap:Map<BrickType, BrickConfig>; private var brickMap:Map<BrickType, BrickConfig>;
private var brickMapByIndex:Map<Int, BrickConfig>; private var brickMapByIndex:Map<Int, BrickConfig>;
@@ -208,14 +215,6 @@ class Config {
} }
} }
private function get_mapWidth():Float {
return map.cellWidth * map.gridWidth;
}
private function get_mapHeight():Float {
return map.cellHeight * map.gridHeight;
}
public function getBrick(type:BrickType):BrickConfig { public function getBrick(type:BrickType):BrickConfig {
return brickMap.get(type); return brickMap.get(type);
} }

View File

@@ -21,9 +21,9 @@ import ru.m.tankz.map.LevelMap;
public var allEntities(default, null):Map<Int, Entity>; public var allEntities(default, null):Map<Int, Entity>;
public function new(config:Config) { public function new(config:Config, size:GridSize = null) {
this.config = config; this.config = config;
map = new LevelMap(config.map); map = new LevelMap(config.map, size);
allEntities = new Map(); allEntities = new Map();
entities = new Map(); entities = new Map();
ticker = new Ticker(); ticker = new Ticker();

View File

@@ -29,8 +29,8 @@ class EntityBuilder {
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle { public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
return new Rectangle( return new Rectangle(
(point.x + 1) * config.map.cellWidth - width / 2, (point.x + 1) * config.map.cell.width - width / 2,
(point.y + 1) * config.map.cellHeight - height / 2, (point.y + 1) * config.map.cell.height - height / 2,
width, width,
height, height,
Direction.fromString(point.direction) Direction.fromString(point.direction)
@@ -39,7 +39,7 @@ class EntityBuilder {
public function buildEagle(point:EntityPoint, teamId:TeamId):Eagle { public function buildEagle(point:EntityPoint, teamId:TeamId):Eagle {
var eageleConfig = config.getTeam(teamId).eagle; var eageleConfig = config.getTeam(teamId).eagle;
var eagle = new Eagle(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), teamId, eageleConfig); var eagle = new Eagle(++entityId, buildRect(point, config.map.cell.width * 2, config.map.cell.height * 2), teamId, eageleConfig);
eagle.color = config.getColor(new PlayerId(teamId, -1)); eagle.color = config.getColor(new PlayerId(teamId, -1));
return eagle; return eagle;
} }
@@ -65,7 +65,7 @@ class EntityBuilder {
public function buildBonus(point:EntityPoint, type:BonusType):Bonus { public function buildBonus(point:EntityPoint, type:BonusType):Bonus {
var bonusConfig = config.getBonus(type); var bonusConfig = config.getBonus(type);
var bonus = new Bonus(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), bonusConfig); var bonus = new Bonus(++entityId, buildRect(point, config.map.cell.width * 2, config.map.cell.height * 2), bonusConfig);
return bonus; return bonus;
} }
} }

View File

@@ -22,7 +22,7 @@ class GameRunner extends Game implements EngineListener {
public function new(state:GameState) { public function new(state:GameState) {
super(state); super(state);
this.builder = new EntityBuilder(config); this.builder = new EntityBuilder(config);
this.engine = new Engine(config); this.engine = new Engine(config, state.level.size);
this.engine.connect(this); this.engine.connect(this);
} }

View File

@@ -27,10 +27,10 @@ class Brick {
this.mapConfig = mapConfig; this.mapConfig = mapConfig;
this.config = config; this.config = config;
this.rect = new Rectangle( this.rect = new Rectangle(
cellX * mapConfig.cellWidth, cellX * mapConfig.cell.width,
cellY * mapConfig.cellHeight, cellY * mapConfig.cell.height,
mapConfig.cellWidth, mapConfig.cell.width,
mapConfig.cellHeight mapConfig.cell.height
); );
} }

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.map; package ru.m.tankz.map;
import ru.m.tankz.config.Config.GridSize;
import ru.m.tankz.config.Config.CellSize;
import ru.m.geom.Line; import ru.m.geom.Line;
import ru.m.geom.Point; import ru.m.geom.Point;
import ru.m.geom.Rectangle; import ru.m.geom.Rectangle;

View File

@@ -24,12 +24,12 @@ class LevelMap {
private var bricksMap(default, null):HashMap<Point, Brick>; private var bricksMap(default, null):HashMap<Point, Brick>;
public function new(config:MapConfig) { public function new(config:MapConfig, size:GridSize = null) {
this.config = config; this.config = config;
cellWidth = config.cellWidth; cellWidth = config.cell.width;
cellHeight = config.cellHeight; cellHeight = config.cell.width;
gridWidth = config.gridWidth; gridWidth = size != null ? size.width : config.grid.width;
gridHeight = config.gridHeight; gridHeight = size != null ? size.height : config.grid.width;
bricksMap = new HashMap(); bricksMap = new HashMap();
bricks = []; bricks = [];
grid = new Grid( grid = new Grid(
@@ -67,16 +67,16 @@ class LevelMap {
} }
public function getPointBrick(point:Point):Brick { public function getPointBrick(point:Point):Brick {
var cellX:Int = Math.floor(point.x / config.cellWidth); var cellX:Int = Math.floor(point.x / cellWidth);
var cellY:Int = Math.floor(point.y / config.cellHeight); var cellY:Int = Math.floor(point.y / cellHeight);
return bricks[cellX + cellY * config.gridWidth]; return bricks[cellX + cellY * gridWidth];
} }
private inline function get_width():Float { private inline function get_width():Float {
return config.cellWidth * config.gridWidth; return cellWidth * gridWidth;
} }
private inline function get_height():Float { private inline function get_height():Float {
return config.cellHeight * config.gridHeight; return cellHeight * gridHeight;
} }
} }

View File

@@ -9,6 +9,7 @@ typedef LevelSource = {
var data:String; var data:String;
@:optional var name:String; @:optional var name:String;
@:optional var points:Array<SpawnPoint>; @:optional var points:Array<SpawnPoint>;
@:optional var size:GridSize;
} }
class LevelUtil { class LevelUtil {
@@ -42,6 +43,7 @@ class LevelUtil {
data: obj.data.split('').map(function(c) return config.getBrickByIndex(Std.parseInt(c))), data: obj.data.split('').map(function(c) return config.getBrickByIndex(Std.parseInt(c))),
points: obj.points, points: obj.points,
name: obj.name, name: obj.name,
size: obj.size,
} }
} }
} }
@@ -52,12 +54,13 @@ class LevelUtil {
data: bricksStr, data: bricksStr,
points: level.points, points: level.points,
name: level.name, name: level.name,
size: level.size,
}, Renderer.options().setFlowLevel(1)); }, Renderer.options().setFlowLevel(1));
} }
public static function empty(config:Config):LevelConfig { public static function empty(size:GridSize, filler:BrickConfig):LevelConfig {
return { return {
data: [for (i in 0...config.map.gridWidth * config.map.gridHeight) config.bricks[1]] data: [for (i in 0...size.width * size.height) filler]
} }
} }
} }

View File

@@ -5,10 +5,12 @@ game:
- team: human - team: human
map: map:
cellWidth: 22 cell:
cellHeight: 22 width: 22
gridWidth: 26 height: 22
gridHeight: 26 grid:
width: 26
height: 26
bricks: bricks:
- {type: border, index: -1, layer: 2, armor: -1} - {type: border, index: -1, layer: 2, armor: -1}

View File

@@ -1,14 +1,16 @@
game: game:
levels: 1 levels: 2
friendlyFire: true friendlyFire: true
complete: complete:
- team: alpha - team: alpha
map: map:
cellWidth: 22 cell:
cellHeight: 22 width: 22
gridWidth: 20 height: 22
gridHeight: 20 grid:
width: 20
height: 20
bricks: bricks:
- {type: border, index: -1, layer: 2, armor: -1} - {type: border, index: -1, layer: 2, armor: -1}

View File

@@ -0,0 +1,4 @@
name: test
size: {width: 36, height: 23}
points: [{direction: right, x: 0, index: 0, team: alpha, y: 0, type: tank}, {direction: right, x: 11, index: 0, team: beta, y: 0, type: tank}, {direction: right, x: 19, index: 0, team: gamma, y: 0, type: tank}, {direction: right, x: 0, index: 0, team: delta, y: 10, type: tank}, {direction: right, x: 15, index: 0, team: epsilon, y: 10, type: tank}, {direction: right, x: 7, index: 0, team: zeta, y: 7, type: tank}, {direction: right, x: 2, index: 0, team: eta, y: 6, type: tank}, {direction: right, x: 5, index: 0, team: theta, y: 2, type: tank}]
data: "0000000000000000000000000000000000000000000000000220000000000000000005222210000000000000445512220000000000044005500000000000004411115000000000000440000000000000000044000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

View File

@@ -5,10 +5,12 @@ game:
- team: radiant - team: radiant
map: map:
cellWidth: 22 cell:
cellHeight: 22 width: 22
gridWidth: 40 height: 22
gridHeight: 30 grid:
width: 40
height: 30
bricks: bricks:
- {type: border, index: -1, layer: 2, armor: -1} - {type: border, index: -1, layer: 2, armor: -1}

View File

@@ -29,6 +29,8 @@ import ru.m.tankz.util.LevelUtil;
@:view var mapView:MapEditView; @:view var mapView:MapEditView;
@:view var spawnPointList:DataView<SpawnPoint, SpawnPointView>; @:view var spawnPointList:DataView<SpawnPoint, SpawnPointView>;
@:view var brickList:DataView<BrickConfig, BrickView>; @:view var brickList:DataView<BrickConfig, BrickView>;
@:view("width") var widthInput:InputView;
@:view("height") var heightInput:InputView;
@:provide var configBundle:IConfigBundle; @:provide var configBundle:IConfigBundle;
@:provide var config:Config; @:provide var config:Config;
@@ -68,7 +70,6 @@ import ru.m.tankz.util.LevelUtil;
config = configBundle.get(type); config = configBundle.get(type);
mapView.config = config; mapView.config = config;
mapView.data = LevelUtil.empty(config);
brickList.data = config.bricks.filter(function(brick) return brick.index > -1); brickList.data = config.bricks.filter(function(brick) return brick.index > -1);
spawnPointList.data = config.points; spawnPointList.data = config.points;
@@ -90,6 +91,13 @@ import ru.m.tankz.util.LevelUtil;
var data = LevelUtil.loads(config, content.content); var data = LevelUtil.loads(config, content.content);
mapView.data = data; mapView.data = data;
levelName.text = data.name; levelName.text = data.name;
/*if (data.size != null) {
widthInput.text = Std.string(data.size.width);
heightInput.text = Std.string(data.size.height);
} else {
widthInput.text = "";
heightInput.text = "";
}*/
}); });
case 'saveButton': case 'saveButton':
L.d(TAG, 'SAVE'); L.d(TAG, 'SAVE');
@@ -102,4 +110,12 @@ import ru.m.tankz.util.LevelUtil;
case _: case _:
} }
} }
private function applySize():Void {
mapView.gridSize = {width: Std.parseInt(widthInput.text), height: Std.parseInt(heightInput.text)};
}
private function resetSize():Void {
mapView.gridSize = null;
}
} }

View File

@@ -30,6 +30,24 @@ views:
geometry.size.width: 300 geometry.size.width: 300
geometry.size.height: 26 geometry.size.height: 26
text: "" text: ""
- $type: haxework.view.HGroupView
views:
- id: width
$type: haxework.view.InputView
skinId: text.box
geometry.size.fixed: [50, 26]
- id: height
$type: haxework.view.InputView
skinId: text.box
geometry.size.fixed: [50, 26]
- $type: haxework.view.ButtonView
skinId: button.simple
text: apply
+onPress: "$code:applySize()"
- $type: haxework.view.ButtonView
skinId: button.simple
text: reset
+onPress: "$code:resetSize()"
# map # map
- $type: haxework.view.HGroupView - $type: haxework.view.HGroupView
views: views:

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.editor.level; package ru.m.tankz.editor.level;
import ru.m.tankz.util.LevelUtil;
import flash.display.Graphics; import flash.display.Graphics;
import flash.events.MouseEvent; import flash.events.MouseEvent;
import ru.m.geom.Point; import ru.m.geom.Point;
@@ -40,21 +41,31 @@ enum Brush {
} }
override private function set_config(value:Config):Config { override private function set_config(value:Config):Config {
var result = super.set_config(value);
builder = new EntityBuilder(value); builder = new EntityBuilder(value);
return super.set_config(value); data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1]);
return result;
}
override private function set_gridSize(value:GridSize):GridSize {
var result = super.set_gridSize(value);
data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1]);
return result;
} }
override private function drawBackground():Void { override private function drawBackground():Void {
super.drawBackground(); super.drawBackground();
var g:Graphics = backgroundLayer.graphics; var g:Graphics = backgroundLayer.graphics;
g.lineStyle(1, 0x007700); g.lineStyle(1, 0x007700);
for (x in 0...config.map.gridWidth) { var gridWidth = gridSize != null ? gridSize.width : config.map.grid.width;
g.moveTo(x * config.map.cellWidth, 0); var gridHeight = gridSize != null ? gridSize.height : config.map.grid.height;
g.lineTo(x * config.map.cellWidth, config.mapHeight); for (x in 0...gridWidth) {
g.moveTo(x * config.map.cell.width, 0);
g.lineTo(x * config.map.cell.height, mapHeight);
} }
for (y in 0...config.map.gridHeight) { for (y in 0...gridHeight) {
g.moveTo(0, y * config.map.cellHeight); g.moveTo(0, y * config.map.cell.height);
g.lineTo(config.mapWidth, y * config.map.cellHeight); g.lineTo(mapWidth, y * config.map.cell.height);
} }
} }
@@ -95,6 +106,7 @@ enum Brush {
return { return {
data: map.bricks.map(function(brick:Brick) return brick.config), data: map.bricks.map(function(brick:Brick) return brick.config),
points: config.points, points: config.points,
size: gridSize,
} }
} }
@@ -102,8 +114,8 @@ enum Brush {
reset(); reset();
pointEntities = new Map(); pointEntities = new Map();
map = new LevelMap(config.map); map = new LevelMap(config.map);
setContentSize(map.gridWidth * map.cellWidth, map.gridHeight * map.cellHeight, "map");
map.setData(value.data); map.setData(value.data);
gridSize = value.size;
gameEventSignal.emit(EventUtil.buildBricksSpawn(map)); gameEventSignal.emit(EventUtil.buildBricksSpawn(map));
for (point in config.points) { for (point in config.points) {
switch point.type { switch point.type {