[common] diffilicity presets support

This commit is contained in:
2019-04-08 14:53:44 +03:00
parent d0104e4361
commit 13d01293f6
28 changed files with 248 additions and 132 deletions

View File

@@ -37,4 +37,5 @@ abstract PlayerId(Array<Dynamic>) {
}
}
typedef PresetId = String;
typedef LevelId = Int;
typedef PresetId = Int;

View File

@@ -3,7 +3,6 @@ package ru.m.tankz.bundle;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
interface ILevelBundle {
public function get(type:GameType, config:Config, level:Int):LevelConfig;
public function get(type:GameType, config:Config, levelId:LevelId):LevelConfig;
}

View File

@@ -75,7 +75,7 @@ typedef PlayerConfig = {
@:optional var protect:Float;
@:optional var life:Int;
@:optional var color:Color;
@:optional var human:Int;
@:optional var control:String;
}
typedef EagleConfig = {
@@ -93,11 +93,12 @@ typedef TeamConfig = {
typedef GamePreset = {
var id:PresetId;
var name:String;
var teams:Array<TeamConfig>;
}
typedef LevelConfig = {
@:optional var index:Int;
@:optional var id:LevelId;
var data:Array<BrickConfig>;
@:optional var name:String;
@:optional var points:Array<SpawnPoint>;

View File

@@ -13,12 +13,6 @@ enum TankAction {
SHOT;
}
enum Controller {
NONE;
HUMAN(index:Int);
BOT(type:String);
}
class Control {
public var playerId(default, null):PlayerId;
public var tankId(default, default):Int;

View File

@@ -0,0 +1,26 @@
package ru.m.tankz.control;
enum Controller {
NONE;
HUMAN(index:Int);
BOT(type:String);
}
abstract AController(Controller) {
public inline function new(value:Controller) {
this = value;
}
@:from public static function fromString(value:String):AController {
return new AController(switch value.split("-") {
case ["human", index]: HUMAN(Std.parseInt(index));
case ["bot", type]: BOT(type);
case _: NONE;
});
}
@:to public inline function toController():Controller {
return this;
}
}

View File

@@ -1,11 +1,11 @@
package ru.m.tankz.game;
import ru.m.tankz.control.Controller.AController;
import haxe.ds.Option;
import haxe.Timer;
import haxework.signal.Signal;
import ru.m.geom.Point;
import ru.m.tankz.bundle.IConfigBundle;
import ru.m.tankz.bundle.ILevelBundle;
import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control;
import ru.m.tankz.control.IControlFactory;
@@ -62,7 +62,6 @@ class Game extends GameDispatcher {
private var points:Array<SpawnPoint>;
@:provide var configBundle:IConfigBundle;
@:provide var levelBundle:ILevelBundle;
@:provide var controlFactory:IControlFactory;
public function new(type:GameType) {
@@ -109,7 +108,7 @@ class Game extends GameDispatcher {
public function start(state:GameState):Void {
this.state = state;
this.winner = null;
var level:LevelConfig = levelBundle.get(type, config, state.level);
var level:LevelConfig = state.level;
points = level.points != null ? level.points : config.points;
engine.map.setData(level.data);
teams = new Map<TeamId, Team>();
@@ -118,10 +117,7 @@ class Game extends GameDispatcher {
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
teams[team.id] = team;
for (player in team.players.iterator()) {
if (player.state.controller == NONE) {
player.state.controller = BOT("hard");
}
var control = controlFactory.build(player.id, player.state.controller);
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
L.d(TAG, 'control(${player.id} - ${control})');
if (control != null) {
player.control = control;
@@ -288,7 +284,7 @@ class Game extends GameDispatcher {
return Option.None;
}
}
var level = this.state.level + 1;
var level = this.state.levelId + 1;
if (level >= config.game.levels) level = 0;
return Option.Some(new GameState(type, state.presetId, level, state));
}

View File

@@ -1,22 +1,45 @@
package ru.m.tankz.game;
import ru.m.tankz.Type.GameType;
import ru.m.tankz.Type;
typedef LevelResult = {
}
typedef LevelProgress = {
var id:LevelId;
var presets:Map<Int, LevelResult>;
}
class GameProgress {
public var type(default, null):GameType;
private var completed(default, null):Map<Int, Bool>;
private var completed(default, null):Map<Int, LevelProgress>;
public function new(type:GameType) {
this.type = type;
this.completed = new Map();
}
public function isLevelAvailable(level:Int):Bool {
return level == 0 || completed.get(level - 1);
public function isLevelAvailable(levelId:LevelId):Bool {
return levelId == 0 || completed.exists(levelId - 1);
}
public function completeLevel(level:Int):Void {
completed.set(level, true);
public function isPresetAvailable(levelId:LevelId, presetId:PresetId):Bool {
return presetId == 0 || completed.exists(levelId) && completed.get(levelId).presets.get(presetId - 1) != null;
}
public function isPresetCompleted(levelId:LevelId, presetId:PresetId):Bool {
return completed.exists(levelId) && completed.get(levelId).presets.get(presetId) != null;
}
public function completeLevel(levelId:LevelId, presetId:PresetId):Void {
if (!completed.exists(levelId)) {
completed[levelId] = {
id: levelId,
presets: new Map(),
}
}
completed[levelId].presets[presetId] = {};
}
}

View File

@@ -2,8 +2,8 @@ package ru.m.tankz.game;
import haxework.color.Color;
import ru.m.tankz.bundle.IConfigBundle;
import ru.m.tankz.bundle.ILevelBundle;
import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control;
import ru.m.tankz.Type;
class State {
@@ -34,16 +34,14 @@ class State {
class PlayerState extends State {
public var id:PlayerId;
public var tank:TankType;
public var controller:Controller;
public var color:Color;
public var life:Int;
public var total:State;
public function new(id:PlayerId, controller:Controller = null) {
public function new(id:PlayerId) {
super();
this.id = id;
this.tank = null;
this.controller = controller == null ? Controller.NONE : controller;
this.life = 0;
this.total = new State();
}
@@ -70,18 +68,20 @@ class GameState {
public var type:GameType;
public var presetId:PresetId;
public var level:Int;
public var levelId:LevelId;
public var players:Map<String, PlayerState>;
public var teams:Map<TeamId, TeamState>;
public var preset(get, null):GamePreset;
public var config(get, null):Config;
public var level(get, null):LevelConfig;
@:provide private var configBundle:IConfigBundle;
@:provide private var levelBundle:ILevelBundle;
public function new(type:GameType, presetId:PresetId = null, level:Int = 0, state:GameState = null) {
public function new(type:GameType, presetId:PresetId = 0, levelId:Int = 0, state:GameState = null) {
this.type = type;
this.presetId = presetId != null ? presetId : config.presets[0].id;
this.level = level;
this.presetId = presetId;
this.levelId = levelId;
if (state == null) {
this.teams = new Map();
this.players = new Map();
@@ -89,8 +89,7 @@ class GameState {
var teamState = new TeamState(team.id);
for (player in team.players) {
var playerId = new PlayerId(team.id, player.index);
var controller = player.human > 0 ? HUMAN(player.human - 1) : NONE;
var playerState = new PlayerState(playerId, controller);
var playerState = new PlayerState(playerId);
players[playerId] = playerState;
teamState.players[player.index] = playerState;
}
@@ -112,6 +111,10 @@ class GameState {
return configBundle.get(type);
}
private function get_level():LevelConfig {
return levelBundle.get(type, config, levelId);
}
public function getTeamLife(id:TeamId):Int {
if (teams.exists(id)) {
return teams[id].life + Lambda.fold(teams[id].players, function(p, c) return c + p.life, 0);