[common] added GameState
This commit is contained in:
@@ -14,7 +14,6 @@ typedef MapConfig = {
|
||||
var cellHeight:Float;
|
||||
var gridWidth:Int;
|
||||
var gridHeight:Int;
|
||||
var bricks:Array<BrickConfig>;
|
||||
}
|
||||
|
||||
typedef BrickConfig = {
|
||||
@@ -31,9 +30,12 @@ typedef BulletConfig = {
|
||||
}
|
||||
|
||||
|
||||
typedef TankConfig = {
|
||||
typedef TankType = {
|
||||
var group:String;
|
||||
var type:String;
|
||||
}
|
||||
|
||||
typedef TankConfig = { > TankType,
|
||||
var width:Float;
|
||||
var height:Float;
|
||||
var speed:Float;
|
||||
|
||||
@@ -20,20 +20,10 @@ class ConfigBundle {
|
||||
return raw;
|
||||
}
|
||||
|
||||
public static function get(type:String, level:Int):Config {
|
||||
public static function get(type:String):Config {
|
||||
switch (type) {
|
||||
case ClassicGame.TYPE:
|
||||
var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/${type}.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);
|
||||
|
||||
22
src/common/haxe/ru/m/tankz/config/MapBundle.hx
Normal file
22
src/common/haxe/ru/m/tankz/config/MapBundle.hx
Normal file
@@ -0,0 +1,22 @@
|
||||
package ru.m.tankz.config;
|
||||
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.config.Config;
|
||||
|
||||
|
||||
class MapBundle {
|
||||
|
||||
public static function get(type:GameType, config:Config, level:Int):Array<BrickConfig> {
|
||||
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(config.bricks[Std.parseInt(c) + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bricks;
|
||||
}
|
||||
}
|
||||
@@ -122,7 +122,8 @@ class Engine implements ControlHandler {
|
||||
case TankAction.MOVE(direction):
|
||||
tank.move(direction);
|
||||
case TankAction.LEVEL_UP(level):
|
||||
tank.config = config.getTank('player', Std.string(Std.int(Math.min(Std.parseInt(tank.config.type) + level, 3))));
|
||||
// ToDo:
|
||||
tank.config = config.getTank('human', Std.string(Std.int(Math.min(Std.parseInt(tank.config.type) + level, 3))));
|
||||
case TankAction.STOP:
|
||||
tank.stop();
|
||||
case TankAction.SHOT:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.GameState.PlayerState;
|
||||
import ru.m.tankz.bot.Bot;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.config.Config;
|
||||
@@ -9,14 +10,43 @@ class ClassicGame extends Game {
|
||||
|
||||
public static var TYPE(default, never):GameType = 'classic';
|
||||
|
||||
public function new(config:Config) {
|
||||
super(TYPE, config);
|
||||
public static var HUMAN(default, never):TeamId = 'human';
|
||||
public static var BOT(default, never):TeamId = 'bot';
|
||||
|
||||
public function new() {
|
||||
super(TYPE);
|
||||
}
|
||||
|
||||
override public function start():Void {
|
||||
super.start();
|
||||
for (player in teams.get('bot').players) {
|
||||
override public function start(state:GameState):Void {
|
||||
super.start(state);
|
||||
for (player in teams.get(BOT).players) {
|
||||
setControl(player.id, new Bot());
|
||||
}
|
||||
}
|
||||
|
||||
public static function buildState(level:Int, humans:Int):GameState {
|
||||
var state = new GameState();
|
||||
state.type = TYPE;
|
||||
state.level = level;
|
||||
state.players[HUMAN] = new Map();
|
||||
state.players[BOT] = new Map();
|
||||
for (i in 0...humans) {
|
||||
state.players[HUMAN][i] = {
|
||||
index:i,
|
||||
tank:{
|
||||
group: HUMAN,
|
||||
type: '1'
|
||||
},
|
||||
life:3,
|
||||
};
|
||||
}
|
||||
for (i in 0...humans*2+2) {
|
||||
state.players[BOT][i] = {
|
||||
index:i,
|
||||
tank:null,
|
||||
life:-1,
|
||||
};
|
||||
}
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.config.ConfigBundle;
|
||||
import ru.m.tankz.config.MapBundle;
|
||||
import ru.m.tankz.game.Spawner;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
@@ -25,15 +27,16 @@ typedef PlayerId = {
|
||||
class Game implements EngineListener {
|
||||
|
||||
public var type(default, null):GameType;
|
||||
public var state(default, null):GameState;
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):Engine;
|
||||
|
||||
private var spawners:Map<TeamId, Spawner>;
|
||||
|
||||
public function new(type:GameType, config:Config) {
|
||||
public function new(type:GameType) {
|
||||
this.type = type;
|
||||
this.config = config;
|
||||
this.config = ConfigBundle.get(type);
|
||||
this.engine = new Engine(config);
|
||||
engine.listeners.push(this);
|
||||
}
|
||||
@@ -53,13 +56,16 @@ class Game implements EngineListener {
|
||||
entity.rect.direction = Direction.fromString(point.direction);
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
public function start(state:GameState):Void {
|
||||
this.state = state;
|
||||
var bricks = MapBundle.get(type, config, state.level);
|
||||
engine.map.setData(bricks);
|
||||
teams = new Map<TeamId, Team>();
|
||||
spawners = new Map<TeamId, Spawner>();
|
||||
for (teamConfig in config.teams) {
|
||||
var team = new Team(teamConfig);
|
||||
for (index in 0...team.config.size) {
|
||||
var player = new Player({team:team.id, index:index});
|
||||
for (playerState in state.players.get(team.id)) {
|
||||
var player = new Player({team:team.id, index:playerState.index});
|
||||
team.players.push(player);
|
||||
teams.set(team.id, team);
|
||||
}
|
||||
|
||||
24
src/common/haxe/ru/m/tankz/game/GameState.hx
Normal file
24
src/common/haxe/ru/m/tankz/game/GameState.hx
Normal file
@@ -0,0 +1,24 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.config.Config;
|
||||
|
||||
|
||||
typedef PlayerState = {
|
||||
var index:Int;
|
||||
var tank:TankType;
|
||||
var life:Int;
|
||||
}
|
||||
|
||||
|
||||
class GameState {
|
||||
public var type:GameType;
|
||||
public var level:Int;
|
||||
public var players:Map<TeamId, Map<Int, PlayerState>>;
|
||||
|
||||
public function new() {
|
||||
type = null;
|
||||
level = -1;
|
||||
players = new Map();
|
||||
}
|
||||
}
|
||||
@@ -28,13 +28,18 @@ class LevelMap {
|
||||
gridWidth = config.gridWidth;
|
||||
gridHeight = config.gridHeight;
|
||||
bricksMap = new HashMap();
|
||||
bricks = [];
|
||||
grid = new Grid(
|
||||
Std.int(cellWidth / 2),
|
||||
Std.int(cellHeight / 2),
|
||||
Std.int(cellWidth * gridWidth),
|
||||
Std.int(cellHeight * gridHeight)
|
||||
);
|
||||
bricks = Lambda.array(Lambda.mapi(config.bricks, function(i:Int, brickConfig:BrickConfig):Brick {
|
||||
}
|
||||
|
||||
public function setData(data:Array<BrickConfig>):Void {
|
||||
bricksMap = new HashMap();
|
||||
bricks = Lambda.array(Lambda.mapi(data, function(i:Int, brickConfig:BrickConfig):Brick {
|
||||
var cellX = Std.int(i % gridWidth);
|
||||
var cellY = Std.int(Math.floor(i / gridHeight));
|
||||
var cells:HashMap<Point, GridCell> = new HashMap();
|
||||
|
||||
Reference in New Issue
Block a user