[common] remove GameSave; [client] update @:template macro use
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.7.4",
|
||||
"version": "0.7.5",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"dateformat": "^3.0.3",
|
||||
|
||||
@@ -13,7 +13,7 @@ import haxework.resources.IResources;
|
||||
#if flash import haxework.log.JSLogger; #end
|
||||
#if debug import haxework.log.SocketLogger; #end
|
||||
|
||||
|
||||
@:style("ru/m/tankz/Style.yaml")
|
||||
class Client {
|
||||
private static inline var TAG = 'Tankz';
|
||||
|
||||
@@ -36,16 +36,16 @@ class Client {
|
||||
}
|
||||
}
|
||||
|
||||
@:template('ru/m/tankz/Client.yaml', 'ru/m/tankz/Style.yaml')
|
||||
class ClientView extends VGroupView {
|
||||
@:view private var switcher(default, null):IFrameSwitcher;
|
||||
|
||||
@:template class ClientView extends VGroupView {
|
||||
private static inline var TAG = 'Tankz';
|
||||
|
||||
@:view private var switcher(default, null):IFrameSwitcher;
|
||||
@:provide var resources:IResources;
|
||||
|
||||
public function init():Void {
|
||||
var font:Font = Font.enumerateFonts()[0];
|
||||
Provider.get(IResources).text.put('font', 'Bookman Old Style');
|
||||
Provider.get(IResources).text.put('version', 'v${Const.VERSION} b${Const.BUILD}');
|
||||
resources.text.put('font', 'Bookman Old Style');
|
||||
resources.text.put('version', 'v${Const.VERSION} b${Const.BUILD}');
|
||||
Provider.set(IFrameSwitcher, switcher);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.bot.BotControl;
|
||||
|
||||
|
||||
@@ -12,12 +12,12 @@ class ClientControlFactory implements IControlFactory {
|
||||
humanControlIndex = 0;
|
||||
}
|
||||
|
||||
public function build(player:Player):Control {
|
||||
return switch (player.config.control) {
|
||||
case Control.HUMAN: new HumanControl(player.id, humanControlIndex++);
|
||||
case Control.BOT: new BotControl(player.id);
|
||||
public function build(id:PlayerId, type:ControlType):Control {
|
||||
return switch (type) {
|
||||
case Control.HUMAN: new HumanControl(id, humanControlIndex++);
|
||||
case Control.BOT: new BotControl(id);
|
||||
case Control.NONE: null;
|
||||
case _: throw 'Unsupported control type: "${player.config.control}"';
|
||||
case _: throw 'Unsupported control type: "${type}"';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,26 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import ru.m.tankz.frame.game.DotaGamePanel;
|
||||
import ru.m.tankz.frame.game.IGamePanel;
|
||||
import ru.m.tankz.frame.game.ClassicGamePanel;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.Type.GameType;
|
||||
import haxework.gui.View;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.network.NetworkGame;
|
||||
import flash.events.Event;
|
||||
import haxe.ds.Option;
|
||||
import haxe.Timer;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.frame.game.ClassicGamePanel;
|
||||
import ru.m.tankz.frame.game.DotaGamePanel;
|
||||
import ru.m.tankz.frame.game.IGamePanel;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkGame;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import ru.m.tankz.render.Render;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.Type.GameType;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/GameFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class GameFrame extends VGroupView {
|
||||
@:template class GameFrame extends VGroupView {
|
||||
|
||||
private static inline var TAG = "GameFrame";
|
||||
|
||||
@@ -36,9 +31,13 @@ class GameFrame extends VGroupView {
|
||||
@:view var dotaPanel(default, null):DotaGamePanel;
|
||||
@:view var classicPanel(default, null):ClassicGamePanel;
|
||||
|
||||
var panels:Map<GameType, IGamePanel>;
|
||||
private var panels:Map<GameType, IGamePanel>;
|
||||
|
||||
@:provide var network:NetworkManager;
|
||||
@:provide var sound:SoundManager;
|
||||
@:provide var state:GameState;
|
||||
@:provide var storage:SaveStorage;
|
||||
@:provide var switcher:IFrameSwitcher;
|
||||
|
||||
private var game:Game;
|
||||
private var timer:Timer;
|
||||
@@ -50,35 +49,36 @@ class GameFrame extends VGroupView {
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
start(Provider.get(GameSave));
|
||||
start(state);
|
||||
}
|
||||
|
||||
private function connectGame(game: Game) {
|
||||
game.engine.connect(render);
|
||||
game.engine.connect(Provider.get(SoundManager));
|
||||
game.engine.connect(sound);
|
||||
}
|
||||
|
||||
private function start(save:GameSave):Void {
|
||||
private function start(state:GameState):Void {
|
||||
for (type in panels.keys()) {
|
||||
panels.get(type).visible = type == save.state.type;
|
||||
panels.get(type).inLayout = type == save.state.type;
|
||||
panels.get(type).visible = type == state.type;
|
||||
panels.get(type).inLayout = type == state.type;
|
||||
}
|
||||
switch (save.server) {
|
||||
case GameServer.LOCAL:
|
||||
game = new Game(save.state.type);
|
||||
// ToDo: local / network game
|
||||
switch (1) {
|
||||
case 1:
|
||||
game = new Game(state.type);
|
||||
connectGame(game);
|
||||
game.start(save).then(onGameStateChange).endThen(onGameComplete);
|
||||
game.start(state).then(onGameStateChange).endThen(onGameComplete);
|
||||
timer = new Timer(10);
|
||||
timer.run = updateEngine;
|
||||
case GameServer.NETWORK:
|
||||
game = new NetworkGame(save.state.type);
|
||||
case 2:
|
||||
game = new NetworkGame(state.type);
|
||||
connectGame(game);
|
||||
network.game = cast game;
|
||||
}
|
||||
panels.get(save.state.type).game = game;
|
||||
panels.get(state.type).game = game;
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
render.draw(game.engine);
|
||||
Provider.get(SoundManager).play('start');
|
||||
sound.play('start');
|
||||
}
|
||||
|
||||
private function stop():Void {
|
||||
@@ -107,12 +107,13 @@ class GameFrame extends VGroupView {
|
||||
}
|
||||
switch (game.next()) {
|
||||
case Option.Some(s):
|
||||
var save = game.save();
|
||||
Provider.get(SaveStorage).write(save);
|
||||
var state = game.save();
|
||||
this.state = state;
|
||||
storage.write(state);
|
||||
stop();
|
||||
start(save);
|
||||
start(state);
|
||||
case Option.None:
|
||||
Provider.get(IFrameSwitcher).change(StartFrame.ID);
|
||||
switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import haxework.gui.LabelView;
|
||||
import haxework.gui.list.ListView;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
@@ -16,8 +16,7 @@ import ru.m.tankz.proto.core.GameStateProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/NetworkFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class NetworkFrame extends VGroupView {
|
||||
@:template class NetworkFrame extends VGroupView {
|
||||
public static var ID(default, never):String = "network";
|
||||
|
||||
@:view var frameSwitcher(default, null):IFrameSwitcher;
|
||||
@@ -80,7 +79,7 @@ class NetworkFrame extends VGroupView {
|
||||
userList.data = game.players;
|
||||
frameSwitcher.change(gameFrame.id);
|
||||
if (game.state == GameStateProto.STARTED) {
|
||||
Provider.set(GameSave, new GameSave({type: ClassicGame.TYPE, presetId: ClassicGame.PLAYER1}, null, GameServer.NETWORK));
|
||||
Provider.set(GameState, new GameState(ClassicGame.TYPE, ClassicGame.PLAYER1));
|
||||
mainFrameSwitcher.change(GameFrame.ID);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -5,8 +5,7 @@ import haxework.gui.ButtonView;
|
||||
import haxework.gui.VGroupView;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/SettingsFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class SettingsFrame extends VGroupView {
|
||||
@:template class SettingsFrame extends VGroupView {
|
||||
|
||||
public static var ID(default, never):String = "settings";
|
||||
|
||||
|
||||
@@ -3,16 +3,13 @@ package ru.m.tankz.frame;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/StartFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class StartFrame extends VGroupView {
|
||||
@:template class StartFrame extends VGroupView {
|
||||
|
||||
public static var ID(default, never):String = "start";
|
||||
|
||||
@@ -22,6 +19,7 @@ class StartFrame extends VGroupView {
|
||||
@:view var settings(default, null):ButtonView;
|
||||
|
||||
@:provide var frameSwitcher:IFrameSwitcher;
|
||||
@:provide var state:GameState;
|
||||
@:provide var storage:SaveStorage;
|
||||
|
||||
public function init():Void {
|
||||
@@ -45,7 +43,7 @@ class StartFrame extends VGroupView {
|
||||
}
|
||||
|
||||
private function startGame(type:GameType, presetId:PresetId):Void {
|
||||
Provider.set(GameSave, new GameSave({type: type, presetId: presetId}));
|
||||
state = new GameState(type, presetId);
|
||||
frameSwitcher.change(StartGameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import haxework.gui.list.ListView;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.frame.level.PresetsView;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/StartGameFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class StartGameFrame extends VGroupView {
|
||||
@:template class StartGameFrame extends VGroupView {
|
||||
public static inline var ID = "level";
|
||||
|
||||
@:view var presets(default, null):PresetsView;
|
||||
@:view var players(default, null):ListView<PlayerConfig>;
|
||||
@:view var players(default, null):ListView<PlayerId>;
|
||||
@:view var levels(default, null):ListView<Int>;
|
||||
|
||||
@:provide var save:GameSave;
|
||||
@:provide var state:GameState;
|
||||
@:provide var storage:SaveStorage;
|
||||
|
||||
public var gameType(default, set):GameType;
|
||||
@@ -43,7 +43,7 @@ class StartGameFrame extends VGroupView {
|
||||
presetId = null;
|
||||
presetId = config.presets[0].id;
|
||||
|
||||
var save:GameSave = storage.read(gameType);
|
||||
var save:GameState = storage.read(gameType);
|
||||
// ToDo: start save button
|
||||
}
|
||||
return gameType;
|
||||
@@ -53,13 +53,19 @@ class StartGameFrame extends VGroupView {
|
||||
if (presetId != value) {
|
||||
presetId = value;
|
||||
presets.selected = presetId;
|
||||
save.state.presetId = presetId;
|
||||
state = new GameState(gameType, presetId);
|
||||
if (presetId != null) {
|
||||
preset = config.getPreset(presetId);
|
||||
var players:Array<PlayerConfig> = [];
|
||||
var players:Array<PlayerId> = [];
|
||||
for (team in preset.teams) {
|
||||
players.push({index: -1, control: 'Team "${team.id}"', tanks: []});
|
||||
players = players.concat(team.players);
|
||||
var human = true;
|
||||
players.push(new PlayerId(team.id, -1));
|
||||
for (player in team.players) {
|
||||
var playerId = new PlayerId(team.id, player.index);
|
||||
state.control.set(playerId, human ? Control.HUMAN : Control.BOT);
|
||||
human = false;
|
||||
players.push(playerId);
|
||||
}
|
||||
}
|
||||
this.players.data = players;
|
||||
} else {
|
||||
@@ -70,14 +76,14 @@ class StartGameFrame extends VGroupView {
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
gameType = save.state.type;
|
||||
if (save.state.presetId != null) {
|
||||
presetId = save.state.presetId;
|
||||
gameType = state.type;
|
||||
if (state.presetId != null) {
|
||||
presetId = state.presetId;
|
||||
}
|
||||
}
|
||||
|
||||
public function onListItemClick(item:IListItemView<Int>):Void {
|
||||
Provider.get(GameSave).state.level = item.data;
|
||||
state.level = item.data;
|
||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ views:
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: players
|
||||
$type: haxework.gui.list.VListView<ru.m.tankz.config.Config.PlayerConfig>
|
||||
$type: haxework.gui.list.VListView<PlayerId>
|
||||
factory: "@class:ru.m.tankz.frame.level.PlayerView"
|
||||
pWidth: 50
|
||||
pHeight: 100
|
||||
|
||||
@@ -5,8 +5,7 @@ import haxework.gui.VGroupView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
|
||||
@:template("ru/m/tankz/frame/game/ClassicGamePanel.yaml", "ru/m/tankz/Style.yaml")
|
||||
class ClassicGamePanel extends VGroupView implements IGamePanel {
|
||||
@:template class ClassicGamePanel extends VGroupView implements IGamePanel {
|
||||
|
||||
@:view var botstate(default, null):LabelView;
|
||||
@:view var player1state(default, null):LabelView;
|
||||
|
||||
@@ -4,8 +4,7 @@ import haxework.gui.LabelView;
|
||||
import haxework.gui.HGroupView;
|
||||
import ru.m.tankz.game.Game;
|
||||
|
||||
@:template("ru/m/tankz/frame/game/DotaGamePanel.yaml", "ru/m/tankz/Style.yaml")
|
||||
class DotaGamePanel extends HGroupView implements IGamePanel {
|
||||
@:template class DotaGamePanel extends HGroupView implements IGamePanel {
|
||||
|
||||
@:view var state(default, null):LabelView;
|
||||
|
||||
@@ -18,7 +17,7 @@ class DotaGamePanel extends HGroupView implements IGamePanel {
|
||||
var result:Array<String> = [];
|
||||
result.push('Level: ${game.state.level}');
|
||||
for (team in game.teams) {
|
||||
if (game.state.loser == team.id) {
|
||||
if (game.loser == team.id) {
|
||||
result.push('${team.id}: LOSE');
|
||||
} else if (team.life > 0) {
|
||||
result.push('${team.id}: ${team.life}');
|
||||
|
||||
@@ -5,8 +5,7 @@ import haxework.gui.LabelView;
|
||||
import haxework.gui.list.ListView.IListItemView;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/level/LevelView.yaml", "ru/m/tankz/Style.yaml")
|
||||
class LevelView extends HGroupView implements IListItemView<Int> {
|
||||
@:template class LevelView extends HGroupView implements IListItemView<Int> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):Int;
|
||||
|
||||
@@ -4,39 +4,52 @@ import haxework.gui.ButtonView;
|
||||
import haxework.gui.HGroupView;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.list.ListView.IListItemView;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/level/PlayerView.yaml", "ru/m/tankz/Style.yaml")
|
||||
class PlayerView extends HGroupView implements IListItemView<PlayerConfig> {
|
||||
@:template class PlayerView extends HGroupView implements IListItemView<PlayerId> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):PlayerConfig;
|
||||
public var data(default, set):PlayerId;
|
||||
|
||||
@:view("index") var indexLabel(default, null):LabelView;
|
||||
@:view var control(default, null):ButtonView;
|
||||
|
||||
@:provide var state:GameState;
|
||||
|
||||
private function init():Void {
|
||||
control.onPress = this;
|
||||
}
|
||||
|
||||
private function set_data(value:PlayerConfig):PlayerConfig {
|
||||
private function set_data(value:PlayerId):PlayerId {
|
||||
data = value;
|
||||
indexLabel.text = data.index == -1 ? "" : Std.string(data.index);
|
||||
control.text = data.control;
|
||||
control.fontColor = data.index == -1 ? 0x00ff00 : 0xffffff;
|
||||
if (data.index == -1) {
|
||||
indexLabel.text = "";
|
||||
control.text = data.team;
|
||||
control.fontColor = 0x00ff00;
|
||||
} else {
|
||||
indexLabel.text = Std.string(data.index);
|
||||
var controlType = state.control.get(value);
|
||||
control.text = controlType;
|
||||
control.fontColor = switch controlType {
|
||||
case Control.HUMAN: 0xffff00;
|
||||
case _: 0xffffff;
|
||||
};
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
if (data != null && data.index > -1) {
|
||||
data.control = switch (data.control) {
|
||||
var controlType = switch state.control.get(data) {
|
||||
case Control.BOT: Control.HUMAN;
|
||||
case Control.HUMAN: Control.BOT;
|
||||
case _: Control.BOT;
|
||||
}
|
||||
control.text = data.control;
|
||||
state.control.set(data, controlType);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import haxework.gui.list.ListView;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/network/GameItemView.yaml", "ru/m/tankz/Style.yaml")
|
||||
class GameItemView extends HGroupView implements IListItemView<GameInfoProto> {
|
||||
@:template class GameItemView extends HGroupView implements IListItemView<GameInfoProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):GameInfoProto;
|
||||
|
||||
@@ -6,8 +6,7 @@ import haxework.gui.list.ListView;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/network/UserItemView.yaml", "ru/m/tankz/Style.yaml")
|
||||
class UserItemView extends HGroupView implements IListItemView<UserProto> {
|
||||
@:template class UserItemView extends HGroupView implements IListItemView<UserProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):UserProto;
|
||||
|
||||
@@ -38,8 +38,7 @@ class KeyboardMap {
|
||||
}
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/settings/ActionView.yaml", "ru/m/tankz/Style.yaml")
|
||||
class ActionView extends HGroupView implements IListItemView<ActionItem> {
|
||||
@:template class ActionView extends HGroupView implements IListItemView<ActionItem> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):ActionItem;
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package ru.m.tankz.frame.settings;
|
||||
|
||||
import haxework.gui.LabelView;
|
||||
import ru.m.tankz.control.ActionConfig;
|
||||
import promhx.Promise;
|
||||
import ru.m.tankz.storage.SettingsStorage;
|
||||
import haxework.gui.ButtonView;
|
||||
import ru.m.tankz.control.ActionConfig.ActionItem;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.list.ListView;
|
||||
import haxework.gui.VGroupView;
|
||||
import promhx.Promise;
|
||||
import ru.m.tankz.control.ActionConfig.ActionItem;
|
||||
import ru.m.tankz.control.ActionConfig;
|
||||
import ru.m.tankz.storage.SettingsStorage;
|
||||
|
||||
|
||||
@:template("ru/m/tankz/frame/settings/SettingsEditor.yaml", "ru/m/tankz/Style.yaml")
|
||||
class SettingsEditor extends VGroupView {
|
||||
@:template class SettingsEditor extends VGroupView {
|
||||
|
||||
public var controlIndex(default, set): Int;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz.storage;
|
||||
|
||||
import flash.net.SharedObject;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
|
||||
|
||||
class SaveStorage {
|
||||
@@ -15,19 +15,19 @@ class SaveStorage {
|
||||
so = SharedObject.getLocal('tankz');
|
||||
}
|
||||
|
||||
public function read(type:GameType):Null<GameSave> {
|
||||
public function read(type:GameType):Null<GameState> {
|
||||
var data:String = Reflect.getProperty(so.data, type);
|
||||
L.d(TAG, 'read: ${data}');
|
||||
if (data != null) {
|
||||
return GameSave.fromYaml(data);
|
||||
return GameState.fromYaml(data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function write(save:GameSave):Void {
|
||||
public function write(save:GameState):Void {
|
||||
var data:String = save.toYaml();
|
||||
L.d(TAG, 'write: ${data}');
|
||||
so.setProperty(save.state.type, data);
|
||||
so.setProperty(save.type, data);
|
||||
so.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class SettingsStorage {
|
||||
}
|
||||
|
||||
public static function getDefault(index: Int): ActionConfig {
|
||||
return defaults.get(index).clone();
|
||||
return (defaults.exists(index) ? defaults.get(index) : empty).clone();
|
||||
}
|
||||
|
||||
private static var defaults: Map<Int, ActionConfig> = [
|
||||
@@ -52,4 +52,12 @@ class SettingsStorage {
|
||||
{action:TankAction.SHOT, key:Keyboard.NUMPAD_0},
|
||||
]),
|
||||
];
|
||||
|
||||
private static var empty: ActionConfig = new ActionConfig([
|
||||
{action:TankAction.MOVE(Direction.TOP), key:-1},
|
||||
{action:TankAction.MOVE(Direction.LEFT), key:-1},
|
||||
{action:TankAction.MOVE(Direction.BOTTOM), key:-1},
|
||||
{action:TankAction.MOVE(Direction.RIGHT), key:-1},
|
||||
{action:TankAction.SHOT, key:-1},
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import haxe.crypto.Crc32;
|
||||
|
||||
typedef Type = Dynamic;
|
||||
|
||||
@@ -15,9 +17,21 @@ typedef TankType = String;
|
||||
|
||||
typedef BonusType = String;
|
||||
|
||||
typedef PlayerId = {
|
||||
var team:TeamId;
|
||||
var index:Int;
|
||||
class PlayerId {
|
||||
public var team(default, null):TeamId;
|
||||
public var index(default, null):Int;
|
||||
|
||||
private var _hashCode(default, null):Int;
|
||||
|
||||
public function new(team:TeamId, index:Int) {
|
||||
this.team = team;
|
||||
this.index = index;
|
||||
this._hashCode = Crc32.make(Bytes.ofString('${team}-${index}'));
|
||||
}
|
||||
|
||||
public function hashCode():Int {
|
||||
return _hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
typedef PresetId = String;
|
||||
|
||||
@@ -71,7 +71,6 @@ typedef TankSpawn = {
|
||||
|
||||
typedef PlayerConfig = {
|
||||
var index:Int;
|
||||
var control:ControlType;
|
||||
var tanks:Array<TankSpawn>;
|
||||
@:optional var bonus:Float;
|
||||
@:optional var protect:Float;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
interface IControlFactory {
|
||||
public function build(player:Player):Control;
|
||||
public function build(id:PlayerId, type:ControlType):Control;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class Game {
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):Engine;
|
||||
public var loser(default, null):Null<TeamId>;
|
||||
|
||||
private var points:Array<SpawnPoint>;
|
||||
private var deferred:Deferred<GameState>;
|
||||
@@ -78,8 +79,9 @@ class Game {
|
||||
entity.rect.direction = Direction.fromString(point.direction);
|
||||
}
|
||||
|
||||
public function start(save:GameSave):Stream<GameState> {
|
||||
this.state = save.state;
|
||||
public function start(state:GameState):Stream<GameState> {
|
||||
this.state = state;
|
||||
this.loser = null;
|
||||
this.preset = config.getPreset(state.presetId);
|
||||
this.deferred = new Deferred();
|
||||
var level:LevelConfig = Provider.get(ILevelBundle).get(type, config, state.level);
|
||||
@@ -89,11 +91,12 @@ class Game {
|
||||
var controlFactory:IControlFactory = Provider.build(IControlFactory);
|
||||
for (teamConfig in preset.teams) {
|
||||
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
||||
var team:Team = new Team(teamConfig, teamPoints, save);
|
||||
var team:Team = new Team(teamConfig, teamPoints, state);
|
||||
teams[team.id] = team;
|
||||
for (player in team.players.iterator()) {
|
||||
if (player.config.control != null) {
|
||||
var control = controlFactory.build(player);
|
||||
var controlType: ControlType = state.control.get(player.id);
|
||||
if (controlType != null) {
|
||||
var control = controlFactory.build(player.id, controlType);
|
||||
L.d(TAG, 'control(${player.id} - ${control})');
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
@@ -222,7 +225,7 @@ class Game {
|
||||
}
|
||||
|
||||
private function lose(teamId:TeamId):Void {
|
||||
state.loser = teamId;
|
||||
loser = teamId;
|
||||
complete();
|
||||
}
|
||||
|
||||
@@ -232,13 +235,13 @@ class Game {
|
||||
|
||||
public function next():Option<GameState> {
|
||||
for (rule in config.game.complete) {
|
||||
if (rule.team != null && rule.team == state.loser) {
|
||||
if (rule.team != null && rule.team == loser) {
|
||||
return Option.None;
|
||||
}
|
||||
}
|
||||
state.level++;
|
||||
if (state.level >= config.game.levels) state.level = 0;
|
||||
return Option.Some({type: state.type, presetId: preset.id, level: state.level});
|
||||
return Option.Some(state);
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
@@ -294,16 +297,7 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
public function save():GameSave {
|
||||
var save:GameSave = new GameSave(state);
|
||||
for (team in teams) {
|
||||
for (player in team.players) {
|
||||
if (player.config.control == Control.HUMAN) {
|
||||
player.state.life++;
|
||||
save.addPlayerState(player.id, player.state);
|
||||
}
|
||||
}
|
||||
}
|
||||
return save;
|
||||
public function save():GameState {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.Type;
|
||||
import yaml.Parser;
|
||||
import yaml.Renderer;
|
||||
import yaml.Yaml;
|
||||
|
||||
|
||||
typedef PlayerSave = {
|
||||
var id:PlayerId;
|
||||
var state:PlayerState;
|
||||
}
|
||||
|
||||
enum GameServer {
|
||||
LOCAL;
|
||||
NETWORK;
|
||||
}
|
||||
|
||||
class GameSave {
|
||||
|
||||
public var state:GameState;
|
||||
public var players:Array<PlayerSave>;
|
||||
public var server:GameServer;
|
||||
|
||||
public function new(state:GameState, ?players:Array<PlayerSave>, ?server:GameServer) {
|
||||
this.state = {
|
||||
type: state.type,
|
||||
presetId: state.presetId,
|
||||
level: state.level,
|
||||
};
|
||||
this.players = players != null ? players : [];
|
||||
this.server = server != null ? server : GameServer.LOCAL;
|
||||
}
|
||||
|
||||
public function addPlayerState(id:PlayerId, state:PlayerState):Void {
|
||||
players.push({id: id, state: state});
|
||||
}
|
||||
|
||||
public function getPlayerState(id:PlayerId):PlayerState {
|
||||
for (player in players) {
|
||||
if (player.id.team == id.team && player.id.index == id.index) {
|
||||
return player.state;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toYaml():String {
|
||||
return Yaml.render({state: state, players: players}, Renderer.options().setFlowLevel(0));
|
||||
}
|
||||
|
||||
public static function fromYaml(value:String):GameSave {
|
||||
var data:Dynamic = Yaml.parse(value, Parser.options().useObjects());
|
||||
return new GameSave(data.state, data.players);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,33 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Player.PlayerState;
|
||||
import haxe.ds.HashMap;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
typedef GameState = {
|
||||
var type:GameType;
|
||||
var presetId:PresetId;
|
||||
@:optional var level:Int;
|
||||
@:optional var loser:TeamId;
|
||||
}
|
||||
class GameState {
|
||||
public var type(default, null):GameType;
|
||||
public var presetId(default, null):PresetId;
|
||||
public var control(default, null):HashMap<PlayerId, ControlType>;
|
||||
public var players(default, null):HashMap<PlayerId, PlayerState>;
|
||||
public var level(default, default):Int;
|
||||
|
||||
public function new(type:GameType, presetId:PresetId) {
|
||||
this.type = type;
|
||||
this.presetId = presetId;
|
||||
this.control = new HashMap();
|
||||
this.players = new HashMap();
|
||||
this.level = 0;
|
||||
}
|
||||
|
||||
public function toYaml():String {
|
||||
//return Yaml.render(this, Renderer.options().setFlowLevel(0));
|
||||
return "";
|
||||
}
|
||||
|
||||
public static function fromYaml(value:String):GameState {
|
||||
//var data:Dynamic = Yaml.parse(value, Parser.options().useObjects());
|
||||
//return new GameState();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class Player {
|
||||
|
||||
public function new(teamId:TeamId, config:PlayerConfig) {
|
||||
this.config = config;
|
||||
this.id = {team:teamId, index:config.index};
|
||||
this.id = new PlayerId(teamId, config.index);
|
||||
this.control = null;
|
||||
this.state = {
|
||||
life: config.life,
|
||||
|
||||
@@ -14,13 +14,13 @@ class Team {
|
||||
public var isAlive(get, null):Bool;
|
||||
public var eagleId(default, default):Int;
|
||||
|
||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, ?save:GameSave) {
|
||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, ?state:GameState) {
|
||||
this.id = config.id;
|
||||
this.config = config;
|
||||
this.players = new Map();
|
||||
for (playerConfig in config.players) {
|
||||
var player:Player = new Player(id, playerConfig);
|
||||
var state = save.getPlayerState(player.id);
|
||||
var state = state.players.get(player.id);
|
||||
if (state != null) player.state = state;
|
||||
this.players[playerConfig.index] = player;
|
||||
}
|
||||
|
||||
@@ -21,13 +21,11 @@ bricks:
|
||||
|
||||
player:
|
||||
human: &human
|
||||
control: human
|
||||
life: 3
|
||||
protect: 5
|
||||
tanks:
|
||||
- {type: human0, rate: 1}
|
||||
bot: &bot
|
||||
control: bot
|
||||
color: 0xFFFFFF
|
||||
bonus: 0.25
|
||||
tanks:
|
||||
|
||||
@@ -20,29 +20,25 @@ bricks:
|
||||
|
||||
player:
|
||||
base: &player
|
||||
control: bot
|
||||
protect: 3
|
||||
fast: &player-fast
|
||||
<<: *player
|
||||
tanks:
|
||||
- {type: slow, rate: 0.5}
|
||||
- {type: fast, rate: 0.5}
|
||||
human1: &human1
|
||||
- {type: fast, rate: 1}
|
||||
slow: &player-slow
|
||||
<<: *player
|
||||
control: human
|
||||
color: 0xf055a0
|
||||
human2: &human2
|
||||
<<: *player
|
||||
control: human
|
||||
color: 0xa055f0
|
||||
tanks:
|
||||
- {type: slow, rate: 1}
|
||||
|
||||
team:
|
||||
base: &team
|
||||
life: 20
|
||||
players:
|
||||
- {<<: *player, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- {<<: *player-slow, index: 0}
|
||||
- {<<: *player-fast, index: 1}
|
||||
- {<<: *player-slow, index: 2}
|
||||
- {<<: *player-fast, index: 3}
|
||||
- {<<: *player-slow, index: 4}
|
||||
radiant: &radiant
|
||||
id: radiant
|
||||
color: 0xff4422
|
||||
@@ -53,45 +49,10 @@ team:
|
||||
<<: *team
|
||||
|
||||
presets:
|
||||
# player1
|
||||
- id: player1
|
||||
- id: default
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
# player2_coop
|
||||
- id: player2_coop
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *human1, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
# player2_vs
|
||||
- id: player2_vs
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
players:
|
||||
- {<<: *human2, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
|
||||
points:
|
||||
- {team: radiant, type: eagle, index: -1, direction: right, x: 0, y: 28}
|
||||
|
||||
Reference in New Issue
Block a user