diff --git a/package.json b/package.json index f43a628..72207a1 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tankz", - "version": "0.7.4", + "version": "0.7.5", "private": true, "devDependencies": { "dateformat": "^3.0.3", diff --git a/src/client/haxe/ru/m/tankz/Client.hx b/src/client/haxe/ru/m/tankz/Client.hx index a388919..c223680 100755 --- a/src/client/haxe/ru/m/tankz/Client.hx +++ b/src/client/haxe/ru/m/tankz/Client.hx @@ -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); } diff --git a/src/client/haxe/ru/m/tankz/Client.yaml b/src/client/haxe/ru/m/tankz/ClientView.yaml similarity index 100% rename from src/client/haxe/ru/m/tankz/Client.yaml rename to src/client/haxe/ru/m/tankz/ClientView.yaml diff --git a/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx b/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx index 58eba24..8011569 100644 --- a/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx +++ b/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx @@ -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}"'; } } } diff --git a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx index 55461a4..0f5e976 100755 --- a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx @@ -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; + private var panels:Map; @: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); } } diff --git a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx index 19366f0..6b9a6a3 100644 --- a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx @@ -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 { diff --git a/src/client/haxe/ru/m/tankz/frame/SettingsFrame.hx b/src/client/haxe/ru/m/tankz/frame/SettingsFrame.hx index 3f13ccb..cc79a94 100644 --- a/src/client/haxe/ru/m/tankz/frame/SettingsFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/SettingsFrame.hx @@ -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"; diff --git a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx index f897fb2..6bc8809 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx @@ -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); } } diff --git a/src/client/haxe/ru/m/tankz/frame/StartGameFrame.hx b/src/client/haxe/ru/m/tankz/frame/StartGameFrame.hx index 63bc3b7..6338d90 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartGameFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/StartGameFrame.hx @@ -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; + @:view var players(default, null):ListView; @:view var levels(default, null):ListView; - @: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 = []; + var players:Array = []; 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):Void { - Provider.get(GameSave).state.level = item.data; + state.level = item.data; Provider.get(IFrameSwitcher).change(GameFrame.ID); } } diff --git a/src/client/haxe/ru/m/tankz/frame/StartGameFrame.yaml b/src/client/haxe/ru/m/tankz/frame/StartGameFrame.yaml index cf4449c..69ec914 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartGameFrame.yaml +++ b/src/client/haxe/ru/m/tankz/frame/StartGameFrame.yaml @@ -11,7 +11,7 @@ views: pHeight: 100 views: - id: players - $type: haxework.gui.list.VListView + $type: haxework.gui.list.VListView factory: "@class:ru.m.tankz.frame.level.PlayerView" pWidth: 50 pHeight: 100 diff --git a/src/client/haxe/ru/m/tankz/frame/game/ClassicGamePanel.hx b/src/client/haxe/ru/m/tankz/frame/game/ClassicGamePanel.hx index 78d3c49..daaef8c 100644 --- a/src/client/haxe/ru/m/tankz/frame/game/ClassicGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/frame/game/ClassicGamePanel.hx @@ -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; diff --git a/src/client/haxe/ru/m/tankz/frame/game/DotaGamePanel.hx b/src/client/haxe/ru/m/tankz/frame/game/DotaGamePanel.hx index 87bcbbc..c04af67 100644 --- a/src/client/haxe/ru/m/tankz/frame/game/DotaGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/frame/game/DotaGamePanel.hx @@ -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 = []; 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}'); diff --git a/src/client/haxe/ru/m/tankz/frame/level/LevelView.hx b/src/client/haxe/ru/m/tankz/frame/level/LevelView.hx index 4a2fc4d..3c23e21 100755 --- a/src/client/haxe/ru/m/tankz/frame/level/LevelView.hx +++ b/src/client/haxe/ru/m/tankz/frame/level/LevelView.hx @@ -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 { +@:template class LevelView extends HGroupView implements IListItemView { public var item_index(default, default):Int; public var data(default, set):Int; diff --git a/src/client/haxe/ru/m/tankz/frame/level/PlayerView.hx b/src/client/haxe/ru/m/tankz/frame/level/PlayerView.hx index d5ac87b..ffbbc4b 100644 --- a/src/client/haxe/ru/m/tankz/frame/level/PlayerView.hx +++ b/src/client/haxe/ru/m/tankz/frame/level/PlayerView.hx @@ -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 { +@:template class PlayerView extends HGroupView implements IListItemView { 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; } } } \ No newline at end of file diff --git a/src/client/haxe/ru/m/tankz/frame/network/GameItemView.hx b/src/client/haxe/ru/m/tankz/frame/network/GameItemView.hx index 6d2bed4..b8d3bc2 100755 --- a/src/client/haxe/ru/m/tankz/frame/network/GameItemView.hx +++ b/src/client/haxe/ru/m/tankz/frame/network/GameItemView.hx @@ -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 { +@:template class GameItemView extends HGroupView implements IListItemView { public var item_index(default, default):Int; public var data(default, set):GameInfoProto; diff --git a/src/client/haxe/ru/m/tankz/frame/network/UserItemView.hx b/src/client/haxe/ru/m/tankz/frame/network/UserItemView.hx index befada3..bec63c0 100755 --- a/src/client/haxe/ru/m/tankz/frame/network/UserItemView.hx +++ b/src/client/haxe/ru/m/tankz/frame/network/UserItemView.hx @@ -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 { +@:template class UserItemView extends HGroupView implements IListItemView { public var item_index(default, default):Int; public var data(default, set):UserProto; diff --git a/src/client/haxe/ru/m/tankz/frame/settings/ActionView.hx b/src/client/haxe/ru/m/tankz/frame/settings/ActionView.hx index 2c12630..7d12f62 100755 --- a/src/client/haxe/ru/m/tankz/frame/settings/ActionView.hx +++ b/src/client/haxe/ru/m/tankz/frame/settings/ActionView.hx @@ -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 { +@:template class ActionView extends HGroupView implements IListItemView { public var item_index(default, default):Int; public var data(default, set):ActionItem; diff --git a/src/client/haxe/ru/m/tankz/frame/settings/SettingsEditor.hx b/src/client/haxe/ru/m/tankz/frame/settings/SettingsEditor.hx index 4b4992a..b6b5ecd 100644 --- a/src/client/haxe/ru/m/tankz/frame/settings/SettingsEditor.hx +++ b/src/client/haxe/ru/m/tankz/frame/settings/SettingsEditor.hx @@ -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; diff --git a/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx b/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx index e69a10f..9dab478 100644 --- a/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx +++ b/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx @@ -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 { + public function read(type:GameType):Null { 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(); } } diff --git a/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx b/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx index e18055e..ee8e828 100644 --- a/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx +++ b/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx @@ -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 = [ @@ -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}, + ]); } diff --git a/src/common/haxe/ru/m/tankz/Type.hx b/src/common/haxe/ru/m/tankz/Type.hx index 31a03a1..9c160e9 100644 --- a/src/common/haxe/ru/m/tankz/Type.hx +++ b/src/common/haxe/ru/m/tankz/Type.hx @@ -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; diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 73058b0..ce837af 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -71,7 +71,6 @@ typedef TankSpawn = { typedef PlayerConfig = { var index:Int; - var control:ControlType; var tanks:Array; @:optional var bonus:Float; @:optional var protect:Float; diff --git a/src/common/haxe/ru/m/tankz/control/IControlFactory.hx b/src/common/haxe/ru/m/tankz/control/IControlFactory.hx index 74e978b..7be2192 100644 --- a/src/common/haxe/ru/m/tankz/control/IControlFactory.hx +++ b/src/common/haxe/ru/m/tankz/control/IControlFactory.hx @@ -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; } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 68bc9eb..74eb412 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -33,6 +33,7 @@ class Game { public var teams(default, null):Map; public var config(default, null):Config; public var engine(default, null):Engine; + public var loser(default, null):Null; private var points:Array; private var deferred:Deferred; @@ -78,8 +79,9 @@ class Game { entity.rect.direction = Direction.fromString(point.direction); } - public function start(save:GameSave):Stream { - this.state = save.state; + public function start(state:GameState):Stream { + 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 { 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; } } diff --git a/src/common/haxe/ru/m/tankz/game/GameSave.hx b/src/common/haxe/ru/m/tankz/game/GameSave.hx deleted file mode 100644 index 166db53..0000000 --- a/src/common/haxe/ru/m/tankz/game/GameSave.hx +++ /dev/null @@ -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; - public var server:GameServer; - - public function new(state:GameState, ?players:Array, ?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); - } -} diff --git a/src/common/haxe/ru/m/tankz/game/GameState.hx b/src/common/haxe/ru/m/tankz/game/GameState.hx index 04031a0..f3f48dd 100644 --- a/src/common/haxe/ru/m/tankz/game/GameState.hx +++ b/src/common/haxe/ru/m/tankz/game/GameState.hx @@ -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; + public var players(default, null):HashMap; + 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; + } +} \ No newline at end of file diff --git a/src/common/haxe/ru/m/tankz/game/Player.hx b/src/common/haxe/ru/m/tankz/game/Player.hx index bfd21b1..459b9e8 100644 --- a/src/common/haxe/ru/m/tankz/game/Player.hx +++ b/src/common/haxe/ru/m/tankz/game/Player.hx @@ -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, diff --git a/src/common/haxe/ru/m/tankz/game/Team.hx b/src/common/haxe/ru/m/tankz/game/Team.hx index 9e23f04..e691b64 100644 --- a/src/common/haxe/ru/m/tankz/game/Team.hx +++ b/src/common/haxe/ru/m/tankz/game/Team.hx @@ -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, ?save:GameSave) { + public function new(config:TeamConfig, points:Array, ?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; } diff --git a/src/common/resources/classic/config.yaml b/src/common/resources/classic/config.yaml index 810883d..b02b3ca 100644 --- a/src/common/resources/classic/config.yaml +++ b/src/common/resources/classic/config.yaml @@ -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: diff --git a/src/common/resources/dota/config.yaml b/src/common/resources/dota/config.yaml index 93f4b7a..49c9e26 100644 --- a/src/common/resources/dota/config.yaml +++ b/src/common/resources/dota/config.yaml @@ -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}