[config] config.yaml

This commit is contained in:
2018-01-11 16:34:21 +03:00
parent 729798bc25
commit 21bf2f9ba1
12 changed files with 209 additions and 168 deletions

View File

@@ -19,13 +19,13 @@ interface IState<T> {
}
class BrickState implements IState<Brick> {
private var type:BrickType;
private var type:Int;
public function new() {}
public function update(object:Brick):Bool {
if (type != object.type) {
type = object.type;
if (type != object.config.type) {
type = object.config.type;
return true;
}
return false;
@@ -59,14 +59,6 @@ class EntityState implements IState<Entity> {
class Render extends SpriteView implements IRender {
private static var GROUND_BRICKS:Array<BrickType> = [
BrickType.BRICK, BrickType.ARMOR, BrickType.WATER, BrickType.ACE
];
private static var UP_BRICKS:Array<BrickType> = [
BrickType.BUSH
];
private var backgroundLayer:Sprite;
private var groundLayer:Sprite;
private var entryLayer:Sprite;
@@ -139,14 +131,14 @@ class Render extends SpriteView implements IRender {
upLayer.graphics.clear();
for (brick in game.map.bricks) {
var g:Graphics = null;
if (GROUND_BRICKS.indexOf(brick.type) > -1) {
if (brick.config.layer < 3) {
g = groundLayer.graphics;
} else if (UP_BRICKS.indexOf(brick.type) > -1) {
} else if (brick.config.layer >= 3) {
g = upLayer.graphics;
}
if (g != null) {
g.beginFill(0x00ff00);
g.beginBitmapFill(Assets.getBitmapData('resources/images/map/map_${brick.type}.png'));
g.beginBitmapFill(Assets.getBitmapData('resources/images/map/map_${brick.config.type}.png'));
g.drawRect(
brick.cellX * game.map.cellWidth,
brick.cellY * game.map.cellHeight,
@@ -172,7 +164,7 @@ class Render extends SpriteView implements IRender {
image = 'resources/images/tank/player/tank_p${tank.config.level}_${tank.index}-0.png';
} else if (Std.is(ent, Bullet)) {
var bullet:Bullet = cast ent;
image = 'resources/images/bullet/bullet_${bullet.config.type}.png';
image = 'resources/images/bullet/bullet_${bullet.config.piercing-1}.png';
} else {
image = 'ERROR'; // ToDo:
}

View File

@@ -0,0 +1,93 @@
map:
cellWidth: 22
cellHeight: 22
gridWidth: 26
gridHeight: 26
bricks: null
points:
- type: player
index: 0
x: 8
y: 24
direction: top
- type: player
index: 1
x: 16
y: 24
direction: top
bricks:
-1: &brick-boder
type: -1
layer: 2
armor: 3
0: &brick-none
type: 0
layer: 0
armor: 0
1: &brick-ace
type: 1
layer: 0
armor: 0
2: &brick-bush
type: 2
layer: 3
armor: 0
3: &brick-water
type: 3
layer: 1
armor: 0
4: &brick-armor
type: 4
layer: 2
armor: 2
5: &brick-brick
type: 5
layer: 2
armor: 1
bullet: &bullet
width: 12
height: 12
speed: 0
piercing: 1
tanks:
player:
0:
level: 0
width: 36
height: 36
speed: 2.5
bullet:
<<: *bullet
speed: 8.0
bullets: 1
1:
level: 1
width: 40
height: 36
speed: 3.0
bullet:
<<: *bullet
speed: 8.5
bullets: 1
2:
level: 2
width: 40
height: 36
speed: 3.0
bullet:
<<: *bullet
speed: 9.0
bullets: 2
3:
level: 3
width: 42
height: 38
speed: 2.9
bullet:
<<: *bullet
speed: 9.0
piercing: 2
bullets: 2

View File

@@ -26,4 +26,14 @@ class Direction {
public static function from(x:Int, y:Int):Direction {
return directions.get(x + y * 10);
}
@:from static public function fromString(s:String):Direction {
return switch(s) {
case 'left': LEFT;
case 'top': TOP;
case 'right': RIGHT;
case 'bottom': BOTTOM;
case _: null;
}
}
}

View File

@@ -1,11 +1,24 @@
package ru.m.tankz.config;
import ru.m.tankz.core.Bullet.BulletType;
import ru.m.tankz.core.Bullet;
import Reflect;
import yaml.Parser;
import yaml.Yaml;
import openfl.utils.Assets;
import ru.m.tankz.map.Brick;
import ru.m.tankz.proto.core.GameType;
import ru.m.geom.Direction;
@:enum abstract SpawnPointType(String) from String to String {
var PLAYER = 'player';
var BOT = 'bot';
var EAGLE = 'eagle';
}
typedef SpawnPoint = {
var type:SpawnPointType;
var index:Int;
var x:Int;
var y:Int;
var direction:String;
}
typedef MapConfig = {
var cellWidth:Float;
@@ -13,14 +26,21 @@ typedef MapConfig = {
var gridWidth:Int;
var gridHeight:Int;
var bricks:Array<BrickType>;
var bricks:Array<BrickConfig>;
var points:Array<SpawnPoint>;
}
typedef BrickConfig = {
var type:Int;
var layer:Int;
var armor:Int;
}
typedef BulletConfig = {
var width:Float;
var height:Float;
var speed:Float;
var type:BulletType;
var piercing:Int;
}
typedef TankConfig = {
@@ -32,130 +52,78 @@ typedef TankConfig = {
var bullets:Int;
}
enum SpawnPointType {
PLAYER;
BOT;
EAGLE;
}
typedef SpawnPoint = {
var type:SpawnPointType;
var index:Int;
var x:Int;
var y:Int;
var direction:Direction;
}
class Config {
public var map(default,null):MapConfig;
public var points(default,null):Array<SpawnPoint>;
public var source(default, null):ConfigSource;
public function new(map:MapConfig, points:Array<SpawnPoint>) {
this.map = map;
this.points = points;
public function new(source: ConfigSource) {
this.source = source;
}
public function getSpawnPoint(type:SpawnPointType, index:Int):SpawnPoint {
for (point in points) {
for (point in this.source.map.points) {
if (point.type == type && point.index == index) {
return point;
}
}
return null;
}
public function getBrick(type:Int):BrickConfig {
return source.bricks.get(type);
}
public function getPlayerTank(level:Int):TankConfig {
return source.tanks.player.get(level);
}
}
typedef ConfigSource = {
map: MapConfig,
bricks: Map<Int, BrickConfig>,
tanks: {
player: Map<Int, TankConfig>
}
}
class ConfigBundle {
public static var CLASSIC:Config = new Config(
{
cellWidth: 22,
cellHeight: 22,
gridWidth: 26,
gridHeight: 26,
bricks: null
},
[
{
type: SpawnPointType.PLAYER,
index: 0,
x: 8,
y: 24,
direction: Direction.TOP
},
{
type: SpawnPointType.PLAYER,
index: 1,
x: 16,
y: 24,
direction: Direction.TOP
}
]
);
private static function bulletConfig(speed:Float, type:BulletType):BulletConfig {
return {
width: 12,
height: 12,
speed: speed,
type: type
private static function buildMap(object:Dynamic):Map<Int, Dynamic> {
var result:Map<Int, Dynamic> = new Map();
for (key in Reflect.fields(object)) {
result.set(Std.parseInt(key), Reflect.field(object, key));
}
return result;
}
public static var PLAYER_TANKS:Array<TankConfig> = [
{
level: 0,
width: 36,
height: 36,
speed: 3,
bullet: bulletConfig(5, BulletType.NORMAL),
bullets: 1
},
{
level: 1,
width: 40,
height: 36,
speed: 3.5,
bullet: bulletConfig(5.5, BulletType.NORMAL),
bullets: 1
},
{
level: 2,
width: 40,
height: 36,
speed: 3.5,
bullet: bulletConfig(5.5, BulletType.NORMAL),
bullets: 2
},
{
level: 3,
width: 42,
height: 38,
speed: 3.5,
bullet: bulletConfig(6, BulletType.PIERCING),
bullets: 2
private static function convert(raw:Dynamic):ConfigSource {
var map: MapConfig = Reflect.getProperty(raw, 'map');
var bricks: Map<Int, BrickConfig> = cast buildMap(Reflect.getProperty(raw, 'bricks'));
var players: Map<Int, TankConfig> = cast buildMap(Reflect.getProperty(Reflect.getProperty(raw, 'tanks'), 'player'));
return {
map: map,
bricks: bricks,
tanks: {
player: players
}
}
];
public static function getPlayerTank(level:Int):TankConfig {
return PLAYER_TANKS[level];
}
public static function get(type:Int, level:Int):Config {
switch (type) {
case GameType.CLASSIC:
var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/config.yaml'), Parser.options().useObjects()));
var bricksData:String = Assets.getText('resources/levels/level00${level}.txt');
var bricks:Array<BrickType> = [];
var bricks:Array<BrickConfig> = [];
for (line in ~/\s+/g.split(bricksData)) {
for (c in line.split('')) {
bricks.push(Std.parseInt(c));
bricks.push(source.bricks.get(Std.parseInt(c)));
}
}
CLASSIC.map.bricks = bricks; // ToDo:
return CLASSIC;
source.map.bricks = bricks;
return new Config(source);
case _:
return null;
}

View File

@@ -5,12 +5,6 @@ import ru.m.geom.Rectangle;
import ru.m.geom.Direction;
@:enum abstract BulletType(Int) from Int to Int {
var NORMAL = 0;
var PIERCING = 1;
}
class Bullet extends MobileEntity {
public var tankId(default, null):Int;
public var config(default, null):BulletConfig;

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.engine;
import ru.m.geom.Direction;
import ru.m.tankz.config.Config.TankConfig;
import ru.m.tankz.core.IEntity;
import ru.m.geom.Point;
@@ -9,7 +10,6 @@ import ru.m.tankz.proto.game.GameObjectType;
import ru.m.tankz.proto.game.GameChangeType;
import ru.m.tankz.proto.game.GameChange;
import ru.m.tankz.proto.core.Player;
import ru.m.geom.Direction;
import ru.m.tankz.core.IMobileEntity;
import ru.m.tankz.config.Config;
import ru.m.tankz.core.Tank;
@@ -19,14 +19,6 @@ import ru.m.tankz.map.ILevelMap;
class Engine implements IEngine {
private static var STOP_BRICKS:Array<BrickType> = [
BrickType.BORDER, BrickType.BRICK, BrickType.ARMOR, BrickType.WATER
];
private static var BULLET_BRICKS:Array<BrickType> = [
BrickType.BORDER, BrickType.BRICK, BrickType.ARMOR
];
public var config(default, default):Config;
public var map(default, null):ILevelMap;
@@ -45,13 +37,13 @@ class Engine implements IEngine {
private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank {
var tank = new Tank(index, config);
tank.rect.center = new Point((point.x + 1) * map.cellWidth, (point.y + 1) * map.cellHeight);
tank.direction = point.direction;
tank.direction = Direction.fromString(point.direction);
return tank;
}
public function init(config:Config):Void {
this.config = config;
map = new LevelMap(config.map);
map = new LevelMap(config.source.map);
playerTanks = new Map<Int, Tank>();
entities = new Map<Int, IEntity>();
removedEntities = new Array<String>();
@@ -63,7 +55,7 @@ class Engine implements IEngine {
for (index in 0...players.length) {
var player:Player = players[index];
var point:SpawnPoint = config.getSpawnPoint(SpawnPointType.PLAYER, index);
var tank = buildTank(index, ConfigBundle.getPlayerTank(0), point);
var tank = buildTank(index, config.getPlayerTank(0), point);
playerTanks.set(player.id, tank);
entities.set(tank.id, tank);
changes.push(new GameChange()
@@ -91,7 +83,7 @@ class Engine implements IEngine {
.setDirectionY(direction.y)
);*/
case TankAction.LEVEL_UP(level):
tank.config = ConfigBundle.getPlayerTank(Std.int(Math.min(tank.config.level + level, 3)));
tank.config = config.getPlayerTank(Std.int(Math.min(tank.config.level + level, 3)));
case TankAction.STOP:
tank.stop();
/*Provider.get(IConnection).send(
@@ -163,10 +155,10 @@ class Engine implements IEngine {
if (objectType == GameObjectType.TANK) {
if (entity.direction.x != 0) {
entity.rect.y = Math.round((entity.rect.y + entity.rect.height / 2) / config.map.cellHeight) * config.map.cellHeight - entity.rect.height / 2;
entity.rect.y = Math.round((entity.rect.y + entity.rect.height / 2) / map.cellHeight) * map.cellHeight - entity.rect.height / 2;
}
if (entity.direction.y != 0) {
entity.rect.x = Math.round((entity.rect.x + entity.rect.width / 2) / config.map.cellWidth) * config.map.cellWidth - entity.rect.width / 2;
entity.rect.x = Math.round((entity.rect.x + entity.rect.width / 2) / map.cellWidth) * map.cellWidth - entity.rect.width / 2;
}
}
@@ -177,7 +169,7 @@ class Engine implements IEngine {
if (objectType == GameObjectType.TANK) {
for (brick in bricks) {
if (STOP_BRICKS.indexOf(brick.type) > -1) {
if (0 < brick.config.layer && brick.config.layer < 3) {
if (entity.direction.x != 0) {
entity.rect.x = brick.cellX * map.cellWidth + map.cellWidth / 2 - entity.direction.x * map.cellWidth / 2 - entity.direction.x * entity.rect.width / 2 - entity.rect.width / 2;
}
@@ -200,7 +192,7 @@ class Engine implements IEngine {
if (objectType == GameObjectType.BULLET) {
var bullet:Bullet = cast ent;
for (brick in bricks) {
if (BULLET_BRICKS.indexOf(brick.type) > -1) {
if (1 < brick.config.layer && brick.config.layer < 3) {
entities.remove(entity.id);
var tank:Tank = cast entities.get(bullet.tankId);
tank.onDestroyBullet();
@@ -214,11 +206,8 @@ class Engine implements IEngine {
}
}
for (brick in bricks) {
if (brick.type == BrickType.BRICK) {
brick.type = BrickType.NONE;
}
if (bullet.config.type == BulletType.PIERCING && brick.type == BrickType.ARMOR) {
brick.type = BrickType.NONE;
if (brick.config.armor > 0 && brick.config.armor <= bullet.config.piercing) {
brick.config = config.getBrick(0);
}
}
}

View File

@@ -1,29 +1,25 @@
package ru.m.tankz.map;
import ru.m.tankz.config.Config.BrickConfig;
import ru.m.tankz.core.IKey;
@:enum abstract BrickType(Int) from Int to Int {
var BORDER = -1;
var NONE = 0;
var ACE = 1;
var BUSH = 2;
var WATER = 3;
var ARMOR = 4;
var BRICK = 5;
}
class Brick implements IKey {
public static var BORDER:BrickConfig = {
type: -1,
layer: 2,
armor: 3,
}
public var cellX(default, null):Int;
public var cellY(default, null):Int;
public var type(default, default):BrickType;
public var key(get, null):String;
public var config(default, default):BrickConfig;
public function new(type:BrickType, cellX:Int, cellY:Int) {
public function new(config:BrickConfig, cellX:Int, cellY:Int) {
this.cellX = cellX;
this.cellY = cellY;
this.type = type;
this.config = config;
}
public function get_key():String {
@@ -31,6 +27,6 @@ class Brick implements IKey {
}
public function toString() {
return 'Brick{$type,$cellX:$cellY}';
return 'Brick{${config.type},$cellX:$cellY}';
}
}

View File

@@ -1,7 +1,6 @@
package ru.m.tankz.map;
import ru.m.tankz.map.Brick.BrickType;
import ru.m.tankz.config.Config.MapConfig;
import ru.m.tankz.config.Config;
class LevelMap implements ILevelMap {
@@ -25,7 +24,7 @@ class LevelMap implements ILevelMap {
private function getBorderBrick(cellX:Int, cellY:Int):Brick {
var index:Int = cellX + cellY * gridWidth;
if (borders[index] == null) {
borders[index] = new Brick(BrickType.BORDER, cellX, cellY);
borders[index] = new Brick(Brick.BORDER, cellX, cellY);
}
return borders[index];
}