[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,
},
flags: [
//'dev_layout',
'dev_layout',
]
})
).bind(module, gulp);

View File

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

View File

@@ -26,6 +26,10 @@ using ru.m.display.DisplayObjectContainerExtender;
class Render extends SpriteView implements IRender {
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 groundLayer:Sprite;
@@ -51,18 +55,33 @@ class Render extends SpriteView implements IRender {
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 {
config = value;
setContentSize(config.mapWidth, config.mapHeight);
setContentSize(mapWidth, mapHeight, "render");
drawBackground();
return config;
}
private function set_gridSize(value:GridSize):GridSize {
gridSize = value;
setContentSize(mapWidth, mapHeight, "render");
drawBackground();
return gridSize;
}
private function drawBackground():Void {
var g:Graphics = backgroundLayer.graphics;
g.clear();
g.beginFill(0x000000);
g.drawRect(0, 0, config.mapWidth, config.mapHeight);
g.drawRect(0, 0, mapWidth, mapHeight);
g.endFill();
}

View File

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

View File

@@ -23,11 +23,19 @@ typedef SpawnPoint = {
var direction:String;
}
typedef CellSize = {
var width:Float;
var height:Float;
}
typedef GridSize = {
var width:Int;
var height:Int;
}
typedef MapConfig = {
var cellWidth:Float;
var cellHeight:Float;
var gridWidth:Int;
var gridHeight:Int;
var cell:CellSize;
var grid:GridSize;
}
typedef BrickConfig = {
@@ -103,6 +111,7 @@ typedef LevelConfig = {
var data:Array<BrickConfig>;
@:optional var name:String;
@:optional var points:Array<SpawnPoint>;
@:optional var size:{width:Int, height:Int};
}
typedef PlayerControl = {
@@ -139,8 +148,6 @@ class Config {
public var controls(default, null):Array<ControlPreset>;
public var points(default, null):Array<SpawnPoint>;
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 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 {
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 function new(config:Config) {
public function new(config:Config, size:GridSize = null) {
this.config = config;
map = new LevelMap(config.map);
map = new LevelMap(config.map, size);
allEntities = new Map();
entities = new Map();
ticker = new Ticker();

View File

@@ -29,8 +29,8 @@ class EntityBuilder {
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
return new Rectangle(
(point.x + 1) * config.map.cellWidth - width / 2,
(point.y + 1) * config.map.cellHeight - height / 2,
(point.x + 1) * config.map.cell.width - width / 2,
(point.y + 1) * config.map.cell.height - height / 2,
width,
height,
Direction.fromString(point.direction)
@@ -39,7 +39,7 @@ class EntityBuilder {
public function buildEagle(point:EntityPoint, teamId: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));
return eagle;
}
@@ -65,7 +65,7 @@ class EntityBuilder {
public function buildBonus(point:EntityPoint, type:BonusType):Bonus {
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;
}
}

View File

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

View File

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

View File

@@ -1,5 +1,7 @@
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.Point;
import ru.m.geom.Rectangle;

View File

@@ -24,12 +24,12 @@ class LevelMap {
private var bricksMap(default, null):HashMap<Point, Brick>;
public function new(config:MapConfig) {
public function new(config:MapConfig, size:GridSize = null) {
this.config = config;
cellWidth = config.cellWidth;
cellHeight = config.cellHeight;
gridWidth = config.gridWidth;
gridHeight = config.gridHeight;
cellWidth = config.cell.width;
cellHeight = config.cell.width;
gridWidth = size != null ? size.width : config.grid.width;
gridHeight = size != null ? size.height : config.grid.width;
bricksMap = new HashMap();
bricks = [];
grid = new Grid(
@@ -67,16 +67,16 @@ class LevelMap {
}
public function getPointBrick(point:Point):Brick {
var cellX:Int = Math.floor(point.x / config.cellWidth);
var cellY:Int = Math.floor(point.y / config.cellHeight);
return bricks[cellX + cellY * config.gridWidth];
var cellX:Int = Math.floor(point.x / cellWidth);
var cellY:Int = Math.floor(point.y / cellHeight);
return bricks[cellX + cellY * gridWidth];
}
private inline function get_width():Float {
return config.cellWidth * config.gridWidth;
return cellWidth * gridWidth;
}
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;
@:optional var name:String;
@:optional var points:Array<SpawnPoint>;
@:optional var size:GridSize;
}
class LevelUtil {
@@ -42,6 +43,7 @@ class LevelUtil {
data: obj.data.split('').map(function(c) return config.getBrickByIndex(Std.parseInt(c))),
points: obj.points,
name: obj.name,
size: obj.size,
}
}
}
@@ -52,12 +54,13 @@ class LevelUtil {
data: bricksStr,
points: level.points,
name: level.name,
size: level.size,
}, Renderer.options().setFlowLevel(1));
}
public static function empty(config:Config):LevelConfig {
public static function empty(size:GridSize, filler:BrickConfig):LevelConfig {
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
map:
cellWidth: 22
cellHeight: 22
gridWidth: 26
gridHeight: 26
cell:
width: 22
height: 22
grid:
width: 26
height: 26
bricks:
- {type: border, index: -1, layer: 2, armor: -1}

View File

@@ -1,14 +1,16 @@
game:
levels: 1
levels: 2
friendlyFire: true
complete:
- team: alpha
map:
cellWidth: 22
cellHeight: 22
gridWidth: 20
gridHeight: 20
cell:
width: 22
height: 22
grid:
width: 20
height: 20
bricks:
- {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
map:
cellWidth: 22
cellHeight: 22
gridWidth: 40
gridHeight: 30
cell:
width: 22
height: 22
grid:
width: 40
height: 30
bricks:
- {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 spawnPointList:DataView<SpawnPoint, SpawnPointView>;
@:view var brickList:DataView<BrickConfig, BrickView>;
@:view("width") var widthInput:InputView;
@:view("height") var heightInput:InputView;
@:provide var configBundle:IConfigBundle;
@:provide var config:Config;
@@ -68,7 +70,6 @@ import ru.m.tankz.util.LevelUtil;
config = configBundle.get(type);
mapView.config = config;
mapView.data = LevelUtil.empty(config);
brickList.data = config.bricks.filter(function(brick) return brick.index > -1);
spawnPointList.data = config.points;
@@ -90,6 +91,13 @@ import ru.m.tankz.util.LevelUtil;
var data = LevelUtil.loads(config, content.content);
mapView.data = data;
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':
L.d(TAG, 'SAVE');
@@ -102,4 +110,12 @@ import ru.m.tankz.util.LevelUtil;
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.height: 26
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
- $type: haxework.view.HGroupView
views:

View File

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