[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);

View File

@@ -34,39 +34,6 @@ player:
- {type: bot2, rate: 0.27}
- {type: bot3, rate: 0.15}
presets:
# player1
- id: 1 Player
teams:
- id: human
players:
- {<<: *human, index: 0, color: 0xFFFF00, human: 1}
- id: bot
spawnInterval: 3000
life: 20
players:
- {<<: *bot, index: 0}
- {<<: *bot, index: 1}
- {<<: *bot, index: 2}
- {<<: *bot, index: 3}
# player2
- id: 2 Players
teams:
- id: human
players:
- {<<: *human, index: 0, color: 0xFFFF00, human: 1}
- {<<: *human, index: 1, color: 0x15C040, human: 2}
- id: bot
spawnInterval: 3000
life: 20
players:
- {<<: *bot, index: 0}
- {<<: *bot, index: 1}
- {<<: *bot, index: 2}
- {<<: *bot, index: 3}
- {<<: *bot, index: 4}
- {<<: *bot, index: 5}
points:
- {team: human, type: eagle, index: -1, direction: right, x: 12, y: 24}
- {team: human, type: tank, index: 0, direction: top, x: 8, y: 24}
@@ -116,7 +83,6 @@ tanks:
skin: pc
- type: human3
# upgrade: human3
downgrade: human2
width: 42
height: 38
@@ -181,3 +147,49 @@ bonuses:
- {score: 500, type: shovel, duration: 10}
- {score: 500, type: star}
- {score: 500, type: gun}
presets:
- id: 0
name: easy
teams:
- id: human
players:
- {<<: *human, index: 0, color: 0xFFFF00, control: human-0}
- id: bot
spawnInterval: 3000
life: 10
players:
- {<<: *bot, index: 0, control: bot-stupid}
- {<<: *bot, index: 1, control: bot-stupid}
- id: 1
name: normal
teams:
- id: human
players:
- {<<: *human, index: 0, color: 0xFFFF00, control: human-0}
- id: bot
spawnInterval: 3000
life: 20
players:
- {<<: *bot, index: 0, control: bot-stupid}
- {<<: *bot, index: 1, control: bot-stupid}
- {<<: *bot, index: 2, control: bot-hard}
- {<<: *bot, index: 3, control: bot-hard}
- id: 2
name: hard
teams:
- id: human
players:
- {<<: *human, index: 0, color: 0xFFFF00, control: human-0}
- id: bot
spawnInterval: 1000
life: 30
players:
- {<<: *bot, index: 0, control: bot-hard}
- {<<: *bot, index: 1, control: bot-hard}
- {<<: *bot, index: 2, control: bot-hard}
- {<<: *bot, index: 3, control: bot-hard}
- {<<: *bot, index: 4, control: bot-hard}
- {<<: *bot, index: 5, control: bot-hard}

View File

@@ -29,15 +29,16 @@ player:
team:
base: &team
players:
- {<<: *player, index: 0}
- {<<: *player, index: 0, control: bot-hard}
presets:
- id: default
- id: 0
name: default
teams:
- id: alpha
color: 0xFF4422
players:
- {<<: *player, index: 0, human: 1}
- {<<: *player, index: 0, control: human-0}
<<: *team
- id: beta
color: 0xFFD000

View File

@@ -41,24 +41,25 @@ team:
id: radiant
color: 0xff4422
players:
- {<<: *player-slow, index: 0, human: 1, color: 0xff8866}
- {<<: *player-fast, index: 1}
- {<<: *player-slow, index: 2}
- {<<: *player-fast, index: 3}
- {<<: *player-slow, index: 4}
- {<<: *player-slow, index: 0, control: human-0, color: 0xff8866}
- {<<: *player-fast, index: 1, control: bot-hard}
- {<<: *player-slow, index: 2, control: bot-hard}
- {<<: *player-fast, index: 3, control: bot-hard}
- {<<: *player-slow, index: 4, control: bot-hard}
dire: &dire
<<: *team
id: dire
color: 0x3284ff
players:
- {<<: *player-slow, index: 0}
- {<<: *player-fast, index: 1}
- {<<: *player-slow, index: 2}
- {<<: *player-fast, index: 3}
- {<<: *player-slow, index: 4}
- {<<: *player-slow, index: 0, control: bot-hard}
- {<<: *player-fast, index: 1, control: bot-hard}
- {<<: *player-slow, index: 2, control: bot-hard}
- {<<: *player-fast, index: 3, control: bot-hard}
- {<<: *player-slow, index: 4, control: bot-hard}
presets:
- id: default
- id: 0
name: default
teams:
- <<: *radiant
- <<: *dire