diff --git a/gen.sh b/gen.sh index aa7f9e8..df4fe7d 100755 --- a/gen.sh +++ b/gen.sh @@ -1,6 +1,6 @@ #!/bin/bash haxelib run protohx generate protohx.json -haxelib run orm mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon \ +haxelib run orm mysql://shmyga:password@localhost:3306/armageddon \ -s src-gen/haxe \ -c ru.m.tankz.db \ - -a ru.m.tankz.db \ No newline at end of file + -a ru.m.tankz.db diff --git a/package.json b/package.json index 5c3decd..7946e2c 100755 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "gulp": "^4.0.0", "gulp-add": "0.0.2", "gulp-clean": "^0.4.0", - "gulp-haxetool": "^0.0.14" + "gulp-haxetool": "^0.0.15" }, "haxeDependencies": { "haxework": "git@bitbucket.org:shmyga/haxework.git", diff --git a/src/client/haxe/ru/m/tankz/Init.hx b/src/client/haxe/ru/m/tankz/Init.hx index a31495f..cc4af88 100644 --- a/src/client/haxe/ru/m/tankz/Init.hx +++ b/src/client/haxe/ru/m/tankz/Init.hx @@ -15,7 +15,6 @@ import ru.m.tankz.network.NetworkManager; import ru.m.tankz.proto.pack.Request; import ru.m.tankz.proto.pack.Response; import ru.m.tankz.sound.SoundManager; -import ru.m.tankz.storage.SaveStorage; import ru.m.tankz.storage.UserStorage; #if flash import flash.Lib; @@ -46,7 +45,6 @@ class Init { Provider.setFactory(IResources, Resources); Provider.setFactory(ILevelBundle, LevelBundle); Provider.setFactory(IConfigBundle, ConfigBundle); - Provider.setFactory(SaveStorage, SaveStorage); Provider.setFactory(UserStorage, UserStorage); Provider.setFactory(SettingsStorage, SettingsStorage); Provider.setFactory(SoundManager, SoundManager); diff --git a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx index df59b55..b521ab6 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx @@ -5,14 +5,12 @@ import ru.m.tankz.frame.dota.DotaLevelFrame; import haxework.gui.ButtonView; import haxework.gui.frame.FrameSwitcher; import haxework.gui.VGroupView; -import ru.m.tankz.storage.SaveStorage; @:template class StartFrame extends VGroupView { public static var ID(default, never):String = "start"; @:provide var frameSwitcher:FrameSwitcher; - @:provide var storage:SaveStorage; public function onPress(view:ButtonView):Void { switch (view.id) { diff --git a/src/client/haxe/ru/m/tankz/frame/classic/ClassicGamePanel.hx b/src/client/haxe/ru/m/tankz/frame/classic/ClassicGamePanel.hx index dccf966..d253288 100644 --- a/src/client/haxe/ru/m/tankz/frame/classic/ClassicGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/frame/classic/ClassicGamePanel.hx @@ -15,12 +15,15 @@ import ru.m.tankz.preset.ClassicGame; public var game:Game; private function updateViews():Void { - bot.count.text = '${game.teams[ClassicGame.BOT].life}'; - player1.count.text = '${game.teams[ClassicGame.HUMAN].players[0].state.life}'; + bot.live.text = '${game.teams[ClassicGame.BOT].life}'; + player1.live.text = '${game.teams[ClassicGame.HUMAN].players[0].state.life}'; + player1.score.text = '${game.teams[ClassicGame.HUMAN].players[0].state.score}'; if (game.teams[ClassicGame.HUMAN].players[1] != null) { - player2.count.text = '${game.teams[ClassicGame.HUMAN].players[1].state.life}'; + player2.live.text = '${game.teams[ClassicGame.HUMAN].players[1].state.life}'; + player2.score.text = '${game.teams[ClassicGame.HUMAN].players[1].state.score}'; } else { - player2.count.text = ""; + player2.live.text = ""; + player2.score.text = ""; } } diff --git a/src/client/haxe/ru/m/tankz/frame/common/GameFrame.hx b/src/client/haxe/ru/m/tankz/frame/common/GameFrame.hx index f2b35d0..154e3a5 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/common/GameFrame.hx @@ -7,11 +7,10 @@ import haxework.gui.frame.FrameSwitcher; import haxework.gui.GroupView; import ru.m.tankz.frame.common.IGamePanel; import ru.m.tankz.game.Game; -import ru.m.tankz.game.GameState; +import ru.m.tankz.game.GameStart; import ru.m.tankz.network.NetworkManager; import ru.m.tankz.render.Render; import ru.m.tankz.sound.SoundManager; -import ru.m.tankz.storage.SaveStorage; class GameFrame extends GroupView { @@ -22,8 +21,7 @@ class GameFrame extends GroupView { @:provide var network:NetworkManager; @:provide var sound:SoundManager; - @:provide var state:GameState; - @:provide var storage:SaveStorage; + @:provide var gameStart:GameStart; @:provide var switcher:FrameSwitcher; private var game:Game; @@ -38,14 +36,14 @@ class GameFrame extends GroupView { } public function onShow():Void { - start(state); + start(gameStart); } - private function start(state:GameState):Void { - game = new Game(state.type); + private function start(start:GameStart):Void { + game = new Game(start.type); game.engine.connect(render); game.engine.connect(sound); - game.start(state).then(onGameStateChange).endThen(onGameComplete); + game.start(start).then(onGameStateChange).endThen(onGameComplete); timer = new Timer(10); timer.run = updateEngine; panel.game = game; @@ -67,24 +65,15 @@ class GameFrame extends GroupView { render.reset(); } - private function onGameStateChange(s:GameState):GameState { + private function onGameStateChange(_):Void { panel.toUpdate(); - return s; } - private function onGameComplete(result:Option):Void { - switch (result) { - case Option.Some(s): - panel.toUpdate(); - case Option.None: - } + private function onGameComplete(_):Void { switch (game.next()) { case Option.Some(s): - var state = game.save(); - this.state = state; - storage.write(state); stop(); - start(state); + start(s); case Option.None: switcher.change(StartFrame.ID); } diff --git a/src/client/haxe/ru/m/tankz/frame/common/LevelFrame.hx b/src/client/haxe/ru/m/tankz/frame/common/LevelFrame.hx index fc2b80c..c2a2428 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/LevelFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/common/LevelFrame.hx @@ -6,8 +6,7 @@ import haxework.resources.IResources; import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.config.Config; import ru.m.tankz.control.Control; -import ru.m.tankz.game.GameState; -import ru.m.tankz.storage.SaveStorage; +import ru.m.tankz.game.GameStart; import ru.m.tankz.Type; class LevelFrame extends GroupView { @@ -18,12 +17,11 @@ class LevelFrame extends GroupView { private var config(default, null):Config; @:provide var configBundle:IConfigBundle; - @:provide var state:GameState; - @:provide var storage:SaveStorage; + @:provide var start:GameStart; @:provide var resources:IResources; private function set_gameType(value:GameType):GameType { - if (gameType != value) { + if (gameType != value || true) { // ToDo: gameType = value; config = configBundle.get(gameType); preset = config.presets[0]; @@ -32,13 +30,13 @@ class LevelFrame extends GroupView { } private function set_preset(value:GamePreset):GamePreset { - if (preset != value) { + if (preset != value || true) { // ToDo: preset = value; - state = new GameState(gameType, preset.id); + start = new GameStart(gameType, preset.id); for (team in value.teams) { for (player in team.players) { var playerId = new PlayerId(team.id, player.index); - state.control.set(playerId, player.control != null ? player.control : Control.BOT); + start.players.push({playerId: playerId, control: player.control != null ? player.control : Control.BOT}); } } } @@ -46,8 +44,8 @@ class LevelFrame extends GroupView { } private function set_level(value:Int):Int { - state.level = value; - return state.level; + start.level = value; + return start.level; } private function levelViewFactory(index:Int, level:Int):ButtonView { diff --git a/src/client/haxe/ru/m/tankz/frame/common/LifeView.hx b/src/client/haxe/ru/m/tankz/frame/common/LifeView.hx index dfc14ac..8457379 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/LifeView.hx +++ b/src/client/haxe/ru/m/tankz/frame/common/LifeView.hx @@ -6,5 +6,6 @@ import haxework.gui.HGroupView; @:template class LifeView extends HGroupView { @:view public var image:ImageView; - @:view public var count:LabelView; + @:view public var live:LabelView; + @:view public var score:LabelView; } diff --git a/src/client/haxe/ru/m/tankz/frame/common/LifeView.yaml b/src/client/haxe/ru/m/tankz/frame/common/LifeView.yaml index f00879f..ab4d68c 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/LifeView.yaml +++ b/src/client/haxe/ru/m/tankz/frame/common/LifeView.yaml @@ -3,7 +3,11 @@ layout.margin: 5 views: - id: image $type: haxework.gui.ImageView - - id: count + - id: live $type: haxework.gui.LabelView skinId: text.box geometry.size.fixed: [50, 38] + - id: score + $type: haxework.gui.LabelView + skinId: text.box + geometry.size.fixed: [100, 38] diff --git a/src/client/haxe/ru/m/tankz/frame/common/PlayerView.hx b/src/client/haxe/ru/m/tankz/frame/common/PlayerView.hx index 990080d..2da2cd4 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/PlayerView.hx +++ b/src/client/haxe/ru/m/tankz/frame/common/PlayerView.hx @@ -1,65 +1,93 @@ package ru.m.tankz.frame.common; -import haxework.color.Color; -import haxework.gui.ButtonView; +import haxework.gui.DataView; import haxework.gui.HGroupView; import haxework.gui.LabelView; -import haxework.gui.list.ListView; -import haxework.gui.skin.Skin; -import openfl.Assets; -import ru.m.tankz.bundle.IConfigBundle; -import ru.m.tankz.control.Control; -import ru.m.tankz.game.GameState; -import ru.m.tankz.Type; +import haxework.gui.skin.ISkin; +import haxework.gui.ToggleButtonView; +import ru.m.tankz.game.GameStart; +import ru.m.tankz.Type.PlayerId; +import ru.m.tankz.Type.TeamId; -@:template class PlayerView extends HGroupView implements IListItemView { +class TeamButton extends ToggleButtonView { + public var team(default, set):TeamId; - public var item_index(default, default):Int; - public var data(default, set):PlayerId; - - @:view("index") var indexLabel(default, null):LabelView; - @:view var control(default, null):ButtonView; - - @:provide var state:GameState; - @:provide var configBundle:IConfigBundle; - - private function set_data(value:PlayerId):PlayerId { - data = value; - indexLabel.text = '${value.team} ${Std.string(data.index + 1)}'; - var color = 0xffffff; - var config = configBundle.get(state.type); - var preset = config.getPreset(state.presetId); - for (team in preset.teams) { - if (team.id == data.team) { - color = team.color; - for (player in team.players) { - if (player.index == data.index) { - if (!player.color.zero) { - color = player.color; - } - break; - } - } - preset.teams; - } + private function set_team(value:TeamId):TeamId { + if (team != value) { + team = value; + text = team.substr(0, 1).toUpperCase() + team.substr(1); } - indexLabel.fontColor = color; - var controlType = state.control.get(value); - var image = Assets.getBitmapData('resources/image/ui/control/${controlType}.png'); - control.skin = [Skin.buttonBitmap(image)]; - indexLabel.update(); + return team; + } +} + +class TeamSkin implements ISkin { + + public var color(default, default):Int; + + public function new(color:Int) { + this.color = color; + } + + public function draw(view:TeamButton):Void { + view.fontColor = view.on ? 0x000000 : 0xcccccc; + var graphics = view.content.graphics; + graphics.beginFill(view.on ? color : 0x333333); + graphics.lineStyle(1, view.on ? 0x333333 : color); + graphics.drawRect(0, 0, view.width, view.height); + graphics.endFill(); + graphics.lineStyle(); + } +} + +@:template class PlayerView extends HGroupView { + private static inline var NONE:TeamId = "none"; + + public var item_index(default, set):Int; + public var data(default, set):PlayerStart; + + @:view var label(default, null):LabelView; + @:view var teams(default, null):DataView; + + @:provide var start:GameStart; + + private function teamViewFactory(index:Int, team:TeamId) { + var view = new TeamButton(); + view.skin = [new TeamSkin(getTeamColor(team))]; + view.geometry.padding = [10, 5]; + view.team = team; + view.on = team == NONE; + return view; + } + + private function set_data(value:PlayerStart):PlayerStart { + data = value; + teams.data = [NONE].concat([for (team in start.preset.teams) team.id]); return data; } - public function toggleControl():Void { - if (data != null && data.index > -1) { - var controlType = switch state.control.get(data) { - case Control.BOT: Control.HUMAN; - case Control.HUMAN: Control.BOT; - case _: Control.BOT; + private function getTeamColor(teamId:TeamId):Int { + var color = 0xcccccc; + for (team in start.preset.teams) { + if (team.id == teamId) { + if (!team.color.zero) color = team.color; + break; } - state.control.set(data, controlType); - this.data = data; + } + return color; + } + + private function set_item_index(value:Int):Int { + item_index = value; + label.text = 'Player ${item_index}'; + return item_index; + } + + private function onTeamSelect(team:TeamId) { + data.playerId = new PlayerId(team, item_index); + for (view in teams.views) { + var button = cast(view, TeamButton); + button.on = team == button.team; } } } diff --git a/src/client/haxe/ru/m/tankz/frame/common/PlayerView.yaml b/src/client/haxe/ru/m/tankz/frame/common/PlayerView.yaml index 94d721f..fbf68fa 100644 --- a/src/client/haxe/ru/m/tankz/frame/common/PlayerView.yaml +++ b/src/client/haxe/ru/m/tankz/frame/common/PlayerView.yaml @@ -1,18 +1,15 @@ --- -geometry.size.width: 200 -geometry.size.height: 44 geometry.margin: 5 layout.margin: 10 +layout.vAlign: middle views: - - id: index + - id: label $type: haxework.gui.LabelView - geometry.size.stretch: true - skin: - - $type: haxework.gui.skin.ColorSkin - color: 0x000000 - alpha: 0.2 - shadow: true - shadowColor: 0x000000 - - id: control - $type: haxework.gui.ButtonView - +onPress: $code:toggleControl() + skinId: text + - id: teams + $type: haxework.gui.DataView + factory: $this:teamViewFactory + layout: + $type: haxework.gui.layout.HorizontalLayout + margin: 3 + +onDataSelect: $this:onTeamSelect diff --git a/src/client/haxe/ru/m/tankz/frame/dota/DotaGamePanel.hx b/src/client/haxe/ru/m/tankz/frame/dota/DotaGamePanel.hx index 183801a..8f8d14c 100644 --- a/src/client/haxe/ru/m/tankz/frame/dota/DotaGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/frame/dota/DotaGamePanel.hx @@ -1,10 +1,10 @@ package ru.m.tankz.frame.dota; -import ru.m.tankz.preset.DotaGame; -import ru.m.tankz.frame.common.LifeView; -import ru.m.tankz.frame.common.IGamePanel; import haxework.gui.HGroupView; +import ru.m.tankz.frame.common.IGamePanel; +import ru.m.tankz.frame.common.LifeView; import ru.m.tankz.game.Game; +import ru.m.tankz.preset.DotaGame; @:template class DotaGamePanel extends HGroupView implements IGamePanel { @@ -13,31 +13,11 @@ import ru.m.tankz.game.Game; public var game:Game; - private function stateString(game:Game):String { - if (game == null) { - return ''; - } - var result:Array = []; - result.push('Level: ${game.state.level}'); - for (team in game.teams) { - if (game.loser == team.id) { - result.push('${team.id}: LOSE'); - } else if (team.life > 0) { - result.push('${team.id}: ${team.life}'); - } else { - for (player in team.players) { - if (player.state.life > 0) { - result.push('${player.id.team}${player.id.index}: ${player.state.life}'); - } - } - } - } - return '[ ${result.join(' | ')} ]'; - } - private function updateViews():Void { - radiant.count.text = '${game.teams[DotaGame.RADIANT].life}'; - dire.count.text = '${game.teams[DotaGame.DIRE].life}'; + radiant.live.text = '${game.teams[DotaGame.RADIANT].life}'; + radiant.score.text = '0'; // ToDO + dire.live.text = '${game.teams[DotaGame.DIRE].life}'; + dire.score.text = '0'; // ToDO } override public function update():Void { diff --git a/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.hx b/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.hx index 132d92c..25148fe 100644 --- a/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.hx @@ -3,16 +3,17 @@ package ru.m.tankz.frame.dota; import haxework.gui.ButtonView; import haxework.gui.DataView; import haxework.gui.frame.FrameSwitcher; +import ru.m.tankz.control.Control; import ru.m.tankz.frame.common.LevelFrame; import ru.m.tankz.frame.common.PlayerView; +import ru.m.tankz.game.GameStart.PlayerStart; import ru.m.tankz.preset.DotaGame; -import ru.m.tankz.Type; @:template class DotaLevelFrame extends LevelFrame { public static inline var ID = "dota.level"; @:view var levels(default, null):DataView; - @:view var players(default, null):DataView; + @:view var players(default, null):DataView; @:provide var frames:FrameSwitcher; @@ -20,15 +21,17 @@ import ru.m.tankz.Type; gameType = DotaGame.TYPE; levels.data = [for (i in 0...config.game.levels) i]; var data = []; - for (team in preset.teams) { - for (p in team.players) { - data.push(new PlayerId(team.id, p.index)); - } + for (i in 0...2) { + data.push({ + playerId: null, + control: Control.HUMAN, + }); } + start.players = data; players.data = data; } - private function playerViewFactory(index:Int, player:PlayerId):PlayerView { + private function playerViewFactory(index:Int, player:PlayerStart):PlayerView { var view = new PlayerView(); view.item_index = index; view.data = player; diff --git a/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.yaml b/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.yaml index 7afda59..0052582 100644 --- a/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.yaml +++ b/src/client/haxe/ru/m/tankz/frame/dota/DotaLevelFrame.yaml @@ -6,21 +6,19 @@ views: - $type: haxework.gui.LabelView skinId: text.header text: DotA - - $type: haxework.gui.HGroupView - views: - - id: players - $type: haxework.gui.DataView - layout: - $type: haxework.gui.layout.VerticalLayout - hAlign: center - factory: $this:playerViewFactory - geometry.padding: 10 - - id: levels - $type: haxework.gui.DataView - layout: - $type: haxework.gui.layout.TailLayout - rowSize: 5 - margin: 5 - factory: $this:levelViewFactory - +onDataSelect: $code:function(value) level = value - geometry.padding: 10 + - id: players + $type: haxework.gui.DataView + layout: + $type: haxework.gui.layout.VerticalLayout + hAlign: center + factory: $this:playerViewFactory + geometry.padding: 10 + - id: levels + $type: haxework.gui.DataView + layout: + $type: haxework.gui.layout.TailLayout + rowSize: 5 + margin: 5 + factory: $this:levelViewFactory + +onDataSelect: $code:function(value) level = value + geometry.padding: 10 diff --git a/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx b/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx deleted file mode 100644 index 9dab478..0000000 --- a/src/client/haxe/ru/m/tankz/storage/SaveStorage.hx +++ /dev/null @@ -1,33 +0,0 @@ -package ru.m.tankz.storage; - -import flash.net.SharedObject; -import ru.m.tankz.game.GameState; -import ru.m.tankz.Type; - - -class SaveStorage { - - private static var TAG(default, never):String = 'SaveStorage'; - - private var so:SharedObject; - - public function new() { - so = SharedObject.getLocal('tankz'); - } - - public function read(type:GameType):Null { - var data:String = Reflect.getProperty(so.data, type); - L.d(TAG, 'read: ${data}'); - if (data != null) { - return GameState.fromYaml(data); - } - return null; - } - - public function write(save:GameState):Void { - var data:String = save.toYaml(); - L.d(TAG, 'write: ${data}'); - so.setProperty(save.type, data); - so.flush(); - } -} diff --git a/src/client/resources/image/ui/control/bot.png b/src/client/resources/image/ui/control/bot.png deleted file mode 100644 index b559495..0000000 Binary files a/src/client/resources/image/ui/control/bot.png and /dev/null differ diff --git a/src/client/resources/image/ui/control/human.png b/src/client/resources/image/ui/control/human.png deleted file mode 100644 index 7121610..0000000 Binary files a/src/client/resources/image/ui/control/human.png and /dev/null differ diff --git a/src/client/resources/image/ui/control/null.png b/src/client/resources/image/ui/control/null.png deleted file mode 100644 index de831b2..0000000 Binary files a/src/client/resources/image/ui/control/null.png and /dev/null differ diff --git a/src/common/haxe/ru/m/tankz/Type.hx b/src/common/haxe/ru/m/tankz/Type.hx index 9c160e9..19f20a2 100644 --- a/src/common/haxe/ru/m/tankz/Type.hx +++ b/src/common/haxe/ru/m/tankz/Type.hx @@ -1,8 +1,5 @@ package ru.m.tankz; -import haxe.io.Bytes; -import haxe.crypto.Crc32; - typedef Type = Dynamic; typedef GameType = String; @@ -17,20 +14,20 @@ typedef TankType = String; typedef BonusType = String; -class PlayerId { - public var team(default, null):TeamId; - public var index(default, null):Int; - - private var _hashCode(default, null):Int; +abstract PlayerId(Array) { + public var team(get, never):TeamId; + public var index(get, never):Int; public function new(team:TeamId, index:Int) { - this.team = team; - this.index = index; - this._hashCode = Crc32.make(Bytes.ofString('${team}-${index}')); + this = [team, index]; } - public function hashCode():Int { - return _hashCode; + private inline function get_team():TeamId return this[0]; + + private inline function get_index():Int return this[1]; + + @:to public inline function toString():String { + return '${team}:${index}'; } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 68abf04..fa96a9f 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -5,7 +5,6 @@ import haxe.Timer; import haxework.provider.Provider; import promhx.Deferred; import promhx.Stream; -import ru.m.geom.Direction; import ru.m.geom.Point; import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.bundle.ILevelBundle; @@ -18,17 +17,17 @@ import ru.m.tankz.core.Entity; import ru.m.tankz.core.EntityType; import ru.m.tankz.core.Tank; import ru.m.tankz.engine.Engine; -import ru.m.tankz.game.GameState; +import ru.m.tankz.game.GameStart; import ru.m.tankz.game.Spawner; import ru.m.tankz.Type; - class Game { private static var TAG(default, never):String = 'Game'; public var type(default, null):GameType; - public var state(default, null):GameState; + public var level(default, null):Int; + public var players(default, null):Array; public var preset(default, null):GamePreset; public var teams(default, null):Map; public var config(default, null):Config; @@ -36,10 +35,11 @@ class Game { public var loser(default, null):Null; private var points:Array; - private var deferred:Deferred; - private var stream:Stream; + private var deferred:Deferred; + private var stream:Stream; @:provide var configBundle:IConfigBundle; + @:provide var levelBundle:ILevelBundle; public function new(type:GameType) { this.type = type; @@ -81,29 +81,34 @@ class Game { entity.rect.direction = point.direction; } - public function start(state:GameState):Stream { - this.state = state; + public function start(start:GameStart):Stream { + this.players = start.players; + var players = players.slice(0); this.loser = null; - this.preset = config.getPreset(state.presetId); + this.preset = config.getPreset(start.presetId); + this.level = start.level; this.deferred = new Deferred(); - var level:LevelConfig = Provider.get(ILevelBundle).get(type, config, state.level); + var level:LevelConfig = levelBundle.get(type, config, start.level); points = level.points != null ? level.points : config.points; engine.map.setData(level.data); teams = new Map(); 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, state); + var team:Team = new Team(teamConfig, teamPoints); teams[team.id] = team; for (player in team.players.iterator()) { - 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; - player.control.bind(engine); - } + var controlType:ControlType = Control.BOT; + var nextPlayer = Lambda.find(players, function(p) return p.playerId != null && p.playerId.team == team.id); + if (nextPlayer != null) { + players.remove(nextPlayer); + controlType = nextPlayer.control; + } + var control = controlFactory.build(player.id, controlType); + L.d(TAG, 'control(${player.id} - ${control})'); + if (control != null) { + player.control = control; + player.control.bind(engine); } } team.spawner.runner = spawn; @@ -140,7 +145,7 @@ class Game { } else if (!team.isAlive) { lose(team.id); } - deferred.resolve(state); + deferred.resolve(null); } private function complete():Void { @@ -151,7 +156,7 @@ class Game { } } Timer.delay(function() { - deferred.resolve(state); + deferred.resolve(null); stream.end(); }, 5000); } @@ -160,6 +165,8 @@ class Game { switch (entity) { case EntityType.TANK(tank): getPlayer(tank.playerId).control.start(); + case EntityType.BULLET(bullet): + getPlayer(bullet.playerId).state.shots++; case _: } } @@ -169,7 +176,7 @@ class Game { case [EntityType.EAGLE(eagle), EntityChange.DEATH]: if (eagle.death) { lose(eagle.team); - deferred.resolve(state); + deferred.resolve(null); } case [EntityType.TANK(tank), EntityChange.HIT]: if (tank.bonus) { @@ -192,6 +199,8 @@ class Game { switch [entity, with] { case [EntityType.TANK(tank), EntityType.BONUS(bonus)]: applyBonus(tank, bonus); + case [EntityType.BULLET(bullet), EntityType.TANK(tank)]: + getPlayer(bullet.playerId).state.hits++; case _: } } @@ -214,14 +223,15 @@ class Game { spawnBonus(); } if (tank.config.score > 0 && playerId != null) { + getPlayer(playerId).state.frags++; getPlayer(playerId).state.score += tank.config.score; } - deferred.resolve(state); + deferred.resolve(null); case EntityType.BONUS(bonus): if (bonus.config.score > 0 && playerId != null) { getPlayer(playerId).state.score += bonus.config.score; } - deferred.resolve(state); + deferred.resolve(null); case _: } } @@ -235,15 +245,15 @@ class Game { engine.action(tankId, action); } - public function next():Option { + public function next():Option { for (rule in config.game.complete) { if (rule.team != null && rule.team == loser) { return Option.None; } } - state.level++; - if (state.level >= config.game.levels) state.level = 0; - return Option.Some(state); + var level = this.level + 1; + if (level >= config.game.levels) level = 0; + return Option.Some(new GameStart(type, preset.id, level, players)); } public function dispose():Void { @@ -298,8 +308,4 @@ class Game { engine.destroy(tank); // :-D } } - - public function save():GameState { - return state; - } } diff --git a/src/common/haxe/ru/m/tankz/game/GameStart.hx b/src/common/haxe/ru/m/tankz/game/GameStart.hx new file mode 100644 index 0000000..cdb4d26 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/game/GameStart.hx @@ -0,0 +1,34 @@ +package ru.m.tankz.game; + +import ru.m.tankz.config.Config; +import ru.m.tankz.bundle.IConfigBundle; +import ru.m.tankz.Type; + +typedef PlayerStart = { + var playerId: PlayerId; + var control:ControlType; +} + +class GameStart { + @:provide private var configBundle:IConfigBundle; + + public var type(default, default):GameType; + public var presetId(default, default):PresetId; + public var level(default, default):Int; + public var players(default, default):Array; + + public var preset(get, null):GamePreset; + + public function new(type:GameType, presetId:PresetId, level:Int = 0, players:Array = null) { + this.type = type; + this.presetId = presetId; + this.level = level; + this.players = players == null ? [] : players; + } + + private function get_preset():GamePreset { + var config = configBundle.get(type); + var preset = config.getPreset(presetId); + return preset; + } +} diff --git a/src/common/haxe/ru/m/tankz/game/GameState.hx b/src/common/haxe/ru/m/tankz/game/GameState.hx deleted file mode 100644 index f3f48dd..0000000 --- a/src/common/haxe/ru/m/tankz/game/GameState.hx +++ /dev/null @@ -1,33 +0,0 @@ -package ru.m.tankz.game; - -import ru.m.tankz.game.Player.PlayerState; -import haxe.ds.HashMap; -import ru.m.tankz.Type; - - -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 459b9e8..1ff0499 100644 --- a/src/common/haxe/ru/m/tankz/game/Player.hx +++ b/src/common/haxe/ru/m/tankz/game/Player.hx @@ -4,11 +4,13 @@ import ru.m.tankz.config.Config; import ru.m.tankz.control.Control; import ru.m.tankz.Type; - typedef PlayerState = { var tank:TankType; var life:Int; var score:Int; + var shots:Int; + var hits:Int; + var frags:Int; } class Player { @@ -27,6 +29,9 @@ class Player { life: config.life, tank: null, score: 0, + shots: 0, + hits: 0, + frags: 0, } } diff --git a/src/common/haxe/ru/m/tankz/game/Team.hx b/src/common/haxe/ru/m/tankz/game/Team.hx index e691b64..e63b4b6 100644 --- a/src/common/haxe/ru/m/tankz/game/Team.hx +++ b/src/common/haxe/ru/m/tankz/game/Team.hx @@ -3,7 +3,6 @@ package ru.m.tankz.game; import ru.m.tankz.Type; import ru.m.tankz.config.Config; - class Team { public var id(default, null):TeamId; @@ -14,14 +13,12 @@ class Team { public var isAlive(get, null):Bool; public var eagleId(default, default):Int; - public function new(config:TeamConfig, points:Array, ?state:GameState) { + public function new(config:TeamConfig, points:Array) { 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 = state.players.get(player.id); - if (state != null) player.state = state; this.players[playerConfig.index] = player; } this.life = config.life; diff --git a/src/server/haxe/ru/m/tankz/server/db/DbProvider.hx b/src/server/haxe/ru/m/tankz/server/db/DbProvider.hx index 4ea4642..0c31186 100755 --- a/src/server/haxe/ru/m/tankz/server/db/DbProvider.hx +++ b/src/server/haxe/ru/m/tankz/server/db/DbProvider.hx @@ -9,7 +9,7 @@ class DbProvider { private var orm:Orm; public function new() { - var db = new orm.Db("mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon"); + var db = new orm.Db("mysql://shmyga:password@localhost:3306/armageddon"); orm = new Orm(db); } @@ -34,4 +34,4 @@ class DbProvider { } return person; } -} \ No newline at end of file +}