[config] refactored
This commit is contained in:
@@ -1,19 +1,14 @@
|
||||
package ru.m.tankz.config;
|
||||
|
||||
import Reflect;
|
||||
import yaml.Parser;
|
||||
import yaml.Yaml;
|
||||
import openfl.utils.Assets;
|
||||
import ru.m.tankz.proto.core.GameType;
|
||||
|
||||
@:enum abstract SpawnPointType(String) from String to String {
|
||||
@:enum abstract EntityType(String) from String to String {
|
||||
var PLAYER = 'player';
|
||||
var BOT = 'bot';
|
||||
var EAGLE = 'eagle';
|
||||
}
|
||||
|
||||
typedef SpawnPoint = {
|
||||
var type:SpawnPointType;
|
||||
var type:EntityType;
|
||||
var index:Int;
|
||||
var x:Int;
|
||||
var y:Int;
|
||||
@@ -43,8 +38,10 @@ typedef BulletConfig = {
|
||||
var piercing:Int;
|
||||
}
|
||||
|
||||
|
||||
typedef TankConfig = {
|
||||
var level:Int;
|
||||
var group:String;
|
||||
var type:Int;
|
||||
var width:Float;
|
||||
var height:Float;
|
||||
var speed:Float;
|
||||
@@ -54,80 +51,47 @@ typedef TankConfig = {
|
||||
|
||||
|
||||
class Config {
|
||||
public var source(default, null):ConfigSource;
|
||||
public var map(default, null):MapConfig;
|
||||
public var bricks(default, null):Array<BrickConfig>;
|
||||
public var tanks(default, null):Array<TankConfig>;
|
||||
|
||||
public function new(source: ConfigSource) {
|
||||
this.source = source;
|
||||
private var pointMap:Map<EntityType, Map<Int, SpawnPoint>>;
|
||||
private var brickMap:Map<Int, BrickConfig>;
|
||||
private var tankMap:Map<String, Map<Int, TankConfig>>;
|
||||
|
||||
public function new(map:MapConfig, bricks:Array<BrickConfig>, tanks:Array<TankConfig>) {
|
||||
this.map = map;
|
||||
this.bricks = bricks;
|
||||
this.tanks = tanks;
|
||||
init();
|
||||
}
|
||||
|
||||
public function getSpawnPoint(type:SpawnPointType, index:Int):SpawnPoint {
|
||||
for (point in this.source.map.points) {
|
||||
if (point.type == type && point.index == index) {
|
||||
return point;
|
||||
}
|
||||
private function init() {
|
||||
pointMap = new Map();
|
||||
for (item in map.points) {
|
||||
if (!pointMap.exists(item.type)) pointMap.set(item.type, new Map<Int, SpawnPoint>());
|
||||
pointMap.get(item.type).set(item.index, item);
|
||||
}
|
||||
return null;
|
||||
brickMap = new Map();
|
||||
for (item in bricks) {
|
||||
brickMap.set(item.type, item);
|
||||
}
|
||||
tankMap = new Map();
|
||||
for (item in tanks) {
|
||||
if (!tankMap.exists(item.group)) tankMap.set(item.group, new Map<Int, TankConfig>());
|
||||
tankMap.get(item.group).set(item.type, item);
|
||||
}
|
||||
}
|
||||
|
||||
public function getSpawnPoint(type:EntityType, index:Int):SpawnPoint {
|
||||
return pointMap.get(type).get(index);
|
||||
}
|
||||
|
||||
public function getBrick(type:Int):BrickConfig {
|
||||
return source.bricks.get(type);
|
||||
return brickMap.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 {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 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<BrickConfig> = [];
|
||||
for (line in ~/\s+/g.split(bricksData)) {
|
||||
for (c in line.split('')) {
|
||||
if (c.length > 0) {
|
||||
bricks.push(source.bricks.get(Std.parseInt(c)));
|
||||
}
|
||||
}
|
||||
}
|
||||
source.map.bricks = bricks;
|
||||
return new Config(source);
|
||||
case _:
|
||||
return null;
|
||||
}
|
||||
public function getTank(group:String, type:Int):TankConfig {
|
||||
return tankMap.get(group).get(type);
|
||||
}
|
||||
}
|
||||
|
||||
49
src/common/haxe/ru/m/tankz/config/ConfigBundle.hx
Normal file
49
src/common/haxe/ru/m/tankz/config/ConfigBundle.hx
Normal file
@@ -0,0 +1,49 @@
|
||||
package ru.m.tankz.config;
|
||||
|
||||
import yaml.Parser;
|
||||
import openfl.Assets;
|
||||
import yaml.Yaml;
|
||||
import ru.m.tankz.proto.core.GameType;
|
||||
import ru.m.tankz.config.Config;
|
||||
|
||||
|
||||
typedef ConfigSource = {
|
||||
var map: MapConfig;
|
||||
var bricks: Array<BrickConfig>;
|
||||
var tanks: Dynamic<Array<TankConfig>>;
|
||||
}
|
||||
|
||||
class ConfigBundle {
|
||||
|
||||
private static function convert(raw:Dynamic):ConfigSource {
|
||||
return raw;
|
||||
}
|
||||
|
||||
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<BrickConfig> = [];
|
||||
for (line in ~/\s+/g.split(bricksData)) {
|
||||
for (c in line.split('')) {
|
||||
if (c.length > 0) {
|
||||
bricks.push(source.bricks[Std.parseInt(c) + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
source.map.bricks = bricks;
|
||||
var tanks:Array<TankConfig> = [];
|
||||
for (group in Reflect.fields(source.tanks)) {
|
||||
var data:Array<TankConfig> = Reflect.field(source.tanks, group);
|
||||
for (item in data) {
|
||||
item.group = group;
|
||||
tanks.push(item);
|
||||
}
|
||||
}
|
||||
return new Config(source.map, source.bricks, tanks);
|
||||
case _:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,15 @@ class Tank extends MobileEntity {
|
||||
return bullet;
|
||||
}
|
||||
|
||||
override public function move(direction:Direction):Void {
|
||||
// ToDo: spike
|
||||
if (direction != this.direction) {
|
||||
rect.x -= this.direction.x * 4;
|
||||
rect.y -= this.direction.y * 4;
|
||||
}
|
||||
super.move(direction);
|
||||
}
|
||||
|
||||
public function onDestroyBullet():Void {
|
||||
bulletsCounter--;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import ru.m.tankz.core.Entity;
|
||||
import ru.m.geom.Direction;
|
||||
import ru.m.tankz.config.Config.TankConfig;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.tankz.map.Brick;
|
||||
import ru.m.tankz.core.Bullet;
|
||||
import ru.m.tankz.proto.game.GameObjectType;
|
||||
import ru.m.tankz.proto.game.GameChangeType;
|
||||
@@ -43,7 +42,7 @@ class Engine {
|
||||
|
||||
public function init(config:Config):Void {
|
||||
this.config = config;
|
||||
map = new LevelMap(config.source.map);
|
||||
map = new LevelMap(config.map);
|
||||
playerTanks = new Map<Int, Tank>();
|
||||
entities = new Map<Int, Entity>();
|
||||
removedEntities = new Array<String>();
|
||||
@@ -54,8 +53,8 @@ class Engine {
|
||||
var changes = new Array<GameChange>();
|
||||
for (index in 0...players.length) {
|
||||
var player:Player = players[index];
|
||||
var point:SpawnPoint = config.getSpawnPoint(SpawnPointType.PLAYER, index);
|
||||
var tank = buildTank(index, config.getPlayerTank(0), point);
|
||||
var point:SpawnPoint = config.getSpawnPoint(EntityType.PLAYER, index);
|
||||
var tank = buildTank(index, config.getTank('player', 0), point);
|
||||
playerTanks.set(player.id, tank);
|
||||
entities.set(tank.id, tank);
|
||||
changes.push(new GameChange()
|
||||
@@ -68,6 +67,12 @@ class Engine {
|
||||
.setDirectionY(tank.direction.y)
|
||||
);
|
||||
}
|
||||
|
||||
for (index in 1...4) {
|
||||
var point:SpawnPoint = config.getSpawnPoint(EntityType.BOT, index);
|
||||
var tank = buildTank(0, config.getTank('bot', 0), point);
|
||||
entities.set(tank.id, tank);
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
@@ -83,7 +88,7 @@ class Engine {
|
||||
.setDirectionY(direction.y)
|
||||
);*/
|
||||
case TankAction.LEVEL_UP(level):
|
||||
tank.config = config.getPlayerTank(Std.int(Math.min(tank.config.level + level, 3)));
|
||||
tank.config = config.getTank('player', Std.int(Math.min(tank.config.type + level, 3)));
|
||||
case TankAction.STOP:
|
||||
tank.stop();
|
||||
/*Provider.get(IConnection).send(
|
||||
|
||||
@@ -42,12 +42,15 @@ class Brick implements IKey {
|
||||
|
||||
public function get_destroyed():Bool {
|
||||
var i = 0;
|
||||
var result:Bool = false;
|
||||
for (c in cells.iterator()) {
|
||||
if (!c.destroyed) {
|
||||
return false;
|
||||
} else {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
public function set_destroyed(value:Bool):Bool {
|
||||
|
||||
Reference in New Issue
Block a user