[config] refactored
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tankz",
|
"name": "tankz",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ansi-colors": "^1.0.1",
|
"ansi-colors": "^1.0.1",
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class BrickState implements IState<Brick> {
|
|||||||
class EntityState implements IState<Entity> {
|
class EntityState implements IState<Entity> {
|
||||||
private var x:Float;
|
private var x:Float;
|
||||||
private var y:Float;
|
private var y:Float;
|
||||||
private var level:Int;
|
private var type:Int;
|
||||||
|
|
||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
@@ -47,8 +47,8 @@ class EntityState implements IState<Entity> {
|
|||||||
}
|
}
|
||||||
if (Std.is(object, Tank)) {
|
if (Std.is(object, Tank)) {
|
||||||
var tank:Tank = cast object;
|
var tank:Tank = cast object;
|
||||||
if (tank.config.level != level) {
|
if (tank.config.type != type) {
|
||||||
level = tank.config.level;
|
type = tank.config.type;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ class Render extends SpriteView implements IRender {
|
|||||||
var image:String = null;
|
var image:String = null;
|
||||||
if (Std.is(ent, Tank)) {
|
if (Std.is(ent, Tank)) {
|
||||||
var tank:Tank = cast ent;
|
var tank:Tank = cast ent;
|
||||||
image = 'resources/images/tank/player/tank_p${tank.config.level}_${tank.index}-0.png';
|
image = 'resources/images/tank/${tank.config.group}/tank_${tank.config.group.charAt(0)}${tank.config.type}_${tank.index}-0.png';
|
||||||
} else if (Std.is(ent, Bullet)) {
|
} else if (Std.is(ent, Bullet)) {
|
||||||
var bullet:Bullet = cast ent;
|
var bullet:Bullet = cast ent;
|
||||||
image = 'resources/images/bullet/bullet_${bullet.config.piercing > 1 ? 1 : 0}.png';
|
image = 'resources/images/bullet/bullet_${bullet.config.piercing > 1 ? 1 : 0}.png';
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.view.frames;
|
package ru.m.tankz.view.frames;
|
||||||
|
|
||||||
|
import ru.m.tankz.config.ConfigBundle;
|
||||||
import ru.m.tankz.proto.core.GameType;
|
import ru.m.tankz.proto.core.GameType;
|
||||||
import ru.m.tankz.proto.core.Game;
|
import ru.m.tankz.proto.core.Game;
|
||||||
import ru.m.tankz.core.PlayerControl;
|
import ru.m.tankz.core.PlayerControl;
|
||||||
@@ -8,11 +9,11 @@ import protohx.Message;
|
|||||||
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
import haxework.gui.ViewBuilder;
|
import haxework.gui.ViewBuilder;
|
||||||
import ru.m.tankz.config.Config;
|
|
||||||
import flash.events.Event;
|
import flash.events.Event;
|
||||||
import haxework.provider.Provider;
|
import haxework.provider.Provider;
|
||||||
import haxework.gui.VGroupView;
|
import haxework.gui.VGroupView;
|
||||||
|
|
||||||
|
|
||||||
@:template("layout/frames/game.json", "layout/styles.json")
|
@:template("layout/frames/game.json", "layout/styles.json")
|
||||||
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
|
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
|
||||||
|
|
||||||
|
|||||||
@@ -15,34 +15,49 @@ map:
|
|||||||
x: 16
|
x: 16
|
||||||
y: 24
|
y: 24
|
||||||
direction: top
|
direction: top
|
||||||
|
- type: bot
|
||||||
|
index: 1
|
||||||
|
x: 0
|
||||||
|
y: 0
|
||||||
|
direction: bottom
|
||||||
|
- type: bot
|
||||||
|
index: 2
|
||||||
|
x: 12
|
||||||
|
y: 0
|
||||||
|
direction: bottom
|
||||||
|
- type: bot
|
||||||
|
index: 3
|
||||||
|
x: 24
|
||||||
|
y: 0
|
||||||
|
direction: bottom
|
||||||
|
|
||||||
bricks:
|
bricks:
|
||||||
-1: &brick-boder
|
# border
|
||||||
type: -1
|
- type: -1
|
||||||
layer: 2
|
layer: 2
|
||||||
armor: -1
|
armor: -1
|
||||||
0: &brick-none
|
# none
|
||||||
type: 0
|
- type: 0
|
||||||
layer: 0
|
layer: 0
|
||||||
armor: 0
|
armor: 0
|
||||||
1: &brick-ace
|
# ace
|
||||||
type: 1
|
- type: 1
|
||||||
layer: 0
|
layer: 0
|
||||||
armor: 0
|
armor: 0
|
||||||
2: &brick-bush
|
# bush
|
||||||
type: 2
|
- type: 2
|
||||||
layer: 3
|
layer: 3
|
||||||
armor: 0
|
armor: 0
|
||||||
3: &brick-water
|
# water
|
||||||
type: 3
|
- type: 3
|
||||||
layer: 1
|
layer: 1
|
||||||
armor: 0
|
armor: 0
|
||||||
4: &brick-armor
|
# armor
|
||||||
type: 4
|
- type: 4
|
||||||
layer: 2
|
layer: 2
|
||||||
armor: 2
|
armor: 2
|
||||||
5: &brick-brick
|
# brick
|
||||||
type: 5
|
- type: 5
|
||||||
layer: 2
|
layer: 2
|
||||||
armor: 1
|
armor: 1
|
||||||
|
|
||||||
@@ -54,8 +69,7 @@ bullet: &bullet
|
|||||||
|
|
||||||
tanks:
|
tanks:
|
||||||
player:
|
player:
|
||||||
0:
|
- type: 0
|
||||||
level: 0
|
|
||||||
width: 36
|
width: 36
|
||||||
height: 36
|
height: 36
|
||||||
speed: 2.5
|
speed: 2.5
|
||||||
@@ -63,8 +77,8 @@ tanks:
|
|||||||
<<: *bullet
|
<<: *bullet
|
||||||
speed: 8.0
|
speed: 8.0
|
||||||
bullets: 1
|
bullets: 1
|
||||||
1:
|
|
||||||
level: 1
|
- type: 1
|
||||||
width: 40
|
width: 40
|
||||||
height: 36
|
height: 36
|
||||||
speed: 3.0
|
speed: 3.0
|
||||||
@@ -72,8 +86,8 @@ tanks:
|
|||||||
<<: *bullet
|
<<: *bullet
|
||||||
speed: 8.5
|
speed: 8.5
|
||||||
bullets: 1
|
bullets: 1
|
||||||
2:
|
|
||||||
level: 2
|
- type: 2
|
||||||
width: 40
|
width: 40
|
||||||
height: 36
|
height: 36
|
||||||
speed: 3.0
|
speed: 3.0
|
||||||
@@ -81,8 +95,8 @@ tanks:
|
|||||||
<<: *bullet
|
<<: *bullet
|
||||||
speed: 9.0
|
speed: 9.0
|
||||||
bullets: 3
|
bullets: 3
|
||||||
3:
|
|
||||||
level: 3
|
- type: 3
|
||||||
width: 42
|
width: 42
|
||||||
height: 38
|
height: 38
|
||||||
speed: 2.9
|
speed: 2.9
|
||||||
@@ -91,3 +105,45 @@ tanks:
|
|||||||
speed: 9.0
|
speed: 9.0
|
||||||
piercing: 3
|
piercing: 3
|
||||||
bullets: 2
|
bullets: 2
|
||||||
|
|
||||||
|
bot:
|
||||||
|
- type: 0
|
||||||
|
width: 38
|
||||||
|
height: 36
|
||||||
|
speed: 2.0
|
||||||
|
bullet:
|
||||||
|
<<: *bullet
|
||||||
|
speed: 7.0
|
||||||
|
bullets: 1
|
||||||
|
score: 100
|
||||||
|
|
||||||
|
- type: 1
|
||||||
|
width: 40
|
||||||
|
height: 36
|
||||||
|
speed: 4.0
|
||||||
|
bullet:
|
||||||
|
<<: *bullet
|
||||||
|
speed: 7.0
|
||||||
|
bullets: 1
|
||||||
|
score: 200
|
||||||
|
|
||||||
|
- type: 2
|
||||||
|
width: 38
|
||||||
|
height: 36
|
||||||
|
speed: 2.0
|
||||||
|
bullet:
|
||||||
|
<<: *bullet
|
||||||
|
speed: 9.0
|
||||||
|
bullets: 1
|
||||||
|
score: 300
|
||||||
|
|
||||||
|
- type: 3
|
||||||
|
width: 40
|
||||||
|
height: 36
|
||||||
|
speed: 1.8
|
||||||
|
bullet:
|
||||||
|
<<: *bullet
|
||||||
|
speed: 8.0
|
||||||
|
bullets: 1
|
||||||
|
score: 400
|
||||||
|
hits: 4
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
package ru.m.tankz.config;
|
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 PLAYER = 'player';
|
||||||
var BOT = 'bot';
|
var BOT = 'bot';
|
||||||
var EAGLE = 'eagle';
|
var EAGLE = 'eagle';
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef SpawnPoint = {
|
typedef SpawnPoint = {
|
||||||
var type:SpawnPointType;
|
var type:EntityType;
|
||||||
var index:Int;
|
var index:Int;
|
||||||
var x:Int;
|
var x:Int;
|
||||||
var y:Int;
|
var y:Int;
|
||||||
@@ -43,8 +38,10 @@ typedef BulletConfig = {
|
|||||||
var piercing:Int;
|
var piercing:Int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
typedef TankConfig = {
|
typedef TankConfig = {
|
||||||
var level:Int;
|
var group:String;
|
||||||
|
var type:Int;
|
||||||
var width:Float;
|
var width:Float;
|
||||||
var height:Float;
|
var height:Float;
|
||||||
var speed:Float;
|
var speed:Float;
|
||||||
@@ -54,80 +51,47 @@ typedef TankConfig = {
|
|||||||
|
|
||||||
|
|
||||||
class Config {
|
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) {
|
private var pointMap:Map<EntityType, Map<Int, SpawnPoint>>;
|
||||||
this.source = source;
|
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 {
|
private function init() {
|
||||||
for (point in this.source.map.points) {
|
pointMap = new Map();
|
||||||
if (point.type == type && point.index == index) {
|
for (item in map.points) {
|
||||||
return point;
|
if (!pointMap.exists(item.type)) pointMap.set(item.type, new Map<Int, SpawnPoint>());
|
||||||
|
pointMap.get(item.type).set(item.index, item);
|
||||||
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
public function getSpawnPoint(type:EntityType, index:Int):SpawnPoint {
|
||||||
|
return pointMap.get(type).get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBrick(type:Int):BrickConfig {
|
public function getBrick(type:Int):BrickConfig {
|
||||||
return source.bricks.get(type);
|
return brickMap.get(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPlayerTank(level:Int):TankConfig {
|
public function getTank(group:String, type:Int):TankConfig {
|
||||||
return source.tanks.player.get(level);
|
return tankMap.get(group).get(type);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
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 {
|
public function onDestroyBullet():Void {
|
||||||
bulletsCounter--;
|
bulletsCounter--;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import ru.m.tankz.core.Entity;
|
|||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.tankz.config.Config.TankConfig;
|
import ru.m.tankz.config.Config.TankConfig;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.tankz.map.Brick;
|
|
||||||
import ru.m.tankz.core.Bullet;
|
import ru.m.tankz.core.Bullet;
|
||||||
import ru.m.tankz.proto.game.GameObjectType;
|
import ru.m.tankz.proto.game.GameObjectType;
|
||||||
import ru.m.tankz.proto.game.GameChangeType;
|
import ru.m.tankz.proto.game.GameChangeType;
|
||||||
@@ -43,7 +42,7 @@ class Engine {
|
|||||||
|
|
||||||
public function init(config:Config):Void {
|
public function init(config:Config):Void {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
map = new LevelMap(config.source.map);
|
map = new LevelMap(config.map);
|
||||||
playerTanks = new Map<Int, Tank>();
|
playerTanks = new Map<Int, Tank>();
|
||||||
entities = new Map<Int, Entity>();
|
entities = new Map<Int, Entity>();
|
||||||
removedEntities = new Array<String>();
|
removedEntities = new Array<String>();
|
||||||
@@ -54,8 +53,8 @@ class Engine {
|
|||||||
var changes = new Array<GameChange>();
|
var changes = new Array<GameChange>();
|
||||||
for (index in 0...players.length) {
|
for (index in 0...players.length) {
|
||||||
var player:Player = players[index];
|
var player:Player = players[index];
|
||||||
var point:SpawnPoint = config.getSpawnPoint(SpawnPointType.PLAYER, index);
|
var point:SpawnPoint = config.getSpawnPoint(EntityType.PLAYER, index);
|
||||||
var tank = buildTank(index, config.getPlayerTank(0), point);
|
var tank = buildTank(index, config.getTank('player', 0), point);
|
||||||
playerTanks.set(player.id, tank);
|
playerTanks.set(player.id, tank);
|
||||||
entities.set(tank.id, tank);
|
entities.set(tank.id, tank);
|
||||||
changes.push(new GameChange()
|
changes.push(new GameChange()
|
||||||
@@ -68,6 +67,12 @@ class Engine {
|
|||||||
.setDirectionY(tank.direction.y)
|
.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;
|
return changes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +88,7 @@ class Engine {
|
|||||||
.setDirectionY(direction.y)
|
.setDirectionY(direction.y)
|
||||||
);*/
|
);*/
|
||||||
case TankAction.LEVEL_UP(level):
|
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:
|
case TankAction.STOP:
|
||||||
tank.stop();
|
tank.stop();
|
||||||
/*Provider.get(IConnection).send(
|
/*Provider.get(IConnection).send(
|
||||||
|
|||||||
@@ -42,12 +42,15 @@ class Brick implements IKey {
|
|||||||
|
|
||||||
public function get_destroyed():Bool {
|
public function get_destroyed():Bool {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
|
var result:Bool = false;
|
||||||
for (c in cells.iterator()) {
|
for (c in cells.iterator()) {
|
||||||
if (!c.destroyed) {
|
if (!c.destroyed) {
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_destroyed(value:Bool):Bool {
|
public function set_destroyed(value:Bool):Bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user