From 2e856c1256270b0ce46f36c6dcd1c2de8c2f6333 Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 20 Feb 2018 17:23:53 +0300 Subject: [PATCH] [common] added PlayerState --- package.json | 2 +- src/client/haxe/ru/m/tankz/frame/GameFrame.hx | 8 +-- src/client/haxe/ru/m/tankz/render/Render.hx | 1 + src/client/resources/classic/config.yaml | 26 ++++---- src/client/resources/dota/config.yaml | 60 +++++++++--------- src/common/haxe/ru/m/tankz/config/Config.hx | 5 +- src/common/haxe/ru/m/tankz/core/Tank.hx | 2 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 4 +- src/common/haxe/ru/m/tankz/game/Game.hx | 61 ++++++++----------- src/common/haxe/ru/m/tankz/game/GameSave.hx | 26 ++++---- src/common/haxe/ru/m/tankz/game/Player.hx | 17 ++++-- src/common/haxe/ru/m/tankz/game/Spawner.hx | 5 +- src/common/haxe/ru/m/tankz/game/Team.hx | 23 ++++--- src/editor/haxe/ru/m/tankz/editor/Editor.hx | 7 +++ src/editor/haxe/ru/m/tankz/editor/Editor.yaml | 2 +- .../ru/m/tankz/editor/frame/LevelFrame.hx | 12 ++-- 16 files changed, 135 insertions(+), 126 deletions(-) diff --git a/package.json b/package.json index 8d6b5af..1a185e6 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tankz", - "version": "0.7.0", + "version": "0.7.1", "private": true, "devDependencies": { "ansi-colors": "^1.0.1", diff --git a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx index 89d8242..de8a2d3 100755 --- a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx @@ -69,7 +69,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand } private function stateString(game:Game):String { - if (game.state == null) { + if (game == null) { return ''; } var result:Array = []; @@ -77,12 +77,12 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand for (team in game.teams) { if (game.state.loser == team.id) { result.push('${team.id}: LOSE'); - } else if (team.life > -1) { + } else if (team.life > 0) { result.push('${team.id}: ${team.life}'); } else { for (player in team.players) { - if (player.life > -1) { - result.push('${player.id.team}${player.id.index}: ${player.life}'); + if (player.state.life > 0) { + result.push('${player.id.team}${player.id.index}: ${player.state.life}'); } } } diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index fe8fe54..bf46c50 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -96,6 +96,7 @@ class Render extends SpriteView implements EngineListener { clearLayer(entryLayer); clearLayer(groundLayer); clearLayer(upLayer); + clearLayer(upperLayer); } public function onSpawn(entity:EntityType):Void { diff --git a/src/client/resources/classic/config.yaml b/src/client/resources/classic/config.yaml index f829a82..f6c8b45 100644 --- a/src/client/resources/classic/config.yaml +++ b/src/client/resources/classic/config.yaml @@ -21,25 +21,26 @@ player: human: &human control: human life: 3 + protect: 5 tanks: - - {type: human0, rate: 1, protect: 5} + - {type: human0, rate: 1} bot: &bot control: bot color: 0xFFFFFF - life: -1 + bonus: 0.25 tanks: - - {type: bot0, rate: 0.25, bonus: 0.25} - - {type: bot1, rate: 0.25, bonus: 0.25} - - {type: bot2, rate: 0.25, bonus: 0.25} - - {type: bot3, rate: 0.25, bonus: 0.25} + - {type: bot0, rate: 0.25} + - {type: bot1, rate: 0.25} + - {type: bot2, rate: 0.25} + - {type: bot3, rate: 0.25} presets: + # player1 - id: player1 teams: - id: human - life: -1 players: - - {<<: *human, index: 0, color: 0xF5C040, life: 3} + - {<<: *human, index: 0, color: 0xFFFF00} - id: bot spawnInterval: 3000 life: 20 @@ -48,14 +49,13 @@ presets: - {<<: *bot, index: 1} - {<<: *bot, index: 2} - {<<: *bot, index: 3} - + # player2 - id: player2 teams: - id: human - life: -1 players: - - {<<: *human, index: 0, color: 0xF5C040, life: 3} - - {<<: *human, index: 1, color: 0x15C040, life: 3} + - {<<: *human, index: 0, color: 0xFFFF00} + - {<<: *human, index: 1, color: 0x15C040} - id: bot spawnInterval: 3000 life: 20 @@ -117,6 +117,7 @@ tanks: - type: human3 upgrade: human3 + downgrade: human2 width: 42 height: 38 speed: 2.9 @@ -125,7 +126,6 @@ tanks: speed: 9.0 piercing: 3 bullets: 2 - hits: 1 skin: pd - type: bot0 diff --git a/src/client/resources/dota/config.yaml b/src/client/resources/dota/config.yaml index 084c28d..47acc12 100644 --- a/src/client/resources/dota/config.yaml +++ b/src/client/resources/dota/config.yaml @@ -17,34 +17,42 @@ bricks: - {type: armor, index: 4, layer: 2, armor: 2} - {type: brick, index: 5, layer: 2, armor: 1} +player: + base: &player + control: bot + protect: 3 + tanks: + - {type: slow, rate: 0.5} + - {type: fast, rate: 0.5} + human1: &human1 + <<: *player + control: human + color: 0xf055a0 + human2: &human2 + <<: *player + control: human + color: 0xa055f0 + team: + base: &team + life: 20 + players: + - {<<: *player, index: 0} + - {<<: *player, index: 1} + - {<<: *player, index: 2} + - {<<: *player, index: 3} + - {<<: *player, index: 4} radiant: &radiant id: radiant color: 0xff4422 - life: 20 + <<: *team dire: &dire id: dire color: 0x2244ff - life: 20 - -player: &player - life: -1 - control: bot - tanks: - - {type: slow, rate: 0.5, protect: 3, bonus: 0} - - {type: fast, rate: 0.5, protect: 3, bonus: 0} - -human1: &human1 - <<: *player - control: human - color: 0xf055a0 - -human2: &human2 - <<: *player - control: human - color: 0xa055f0 + <<: *team presets: + # player1 - id: player1 teams: - <<: *radiant @@ -55,12 +63,7 @@ presets: - {<<: *player, index: 3} - {<<: *player, index: 4} - <<: *dire - players: - - {<<: *player, index: 0} - - {<<: *player, index: 1} - - {<<: *player, index: 2} - - {<<: *player, index: 3} - - {<<: *player, index: 4} + # player2_coop - id: player2_coop teams: - <<: *radiant @@ -71,12 +74,7 @@ presets: - {<<: *player, index: 3} - {<<: *player, index: 4} - <<: *dire - players: - - {<<: *player, index: 0} - - {<<: *player, index: 1} - - {<<: *player, index: 2} - - {<<: *player, index: 3} - - {<<: *player, index: 4} + # player2_vs - id: player2_vs teams: - <<: *radiant diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 5890d36..19c3925 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -49,6 +49,7 @@ typedef TankConfig = { var hits:Int; var skin:String; @:optinal var upgrade:TankType; + @:optinal var downgrade:TankType; } typedef BonusConfig = { @@ -59,14 +60,14 @@ typedef BonusConfig = { typedef TankSpawn = { var type:TankType; var rate:Float; - @:optional var bonus:Float; - @:optional var protect:Float; } typedef PlayerConfig = { var index:Int; var control:ControlType; var tanks:Array; + @:optional var bonus:Float; + @:optional var protect:Float; @:optional var life:Int; @:optional var color:Color; } diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index 1a0c6a3..a357596 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -26,7 +26,6 @@ class Tank extends MobileEntity { this.freezing = new Modificator(); this.playerId = playerId; this.config = config; - this.hits = config.hits; this.layer = 1; } @@ -36,6 +35,7 @@ class Tank extends MobileEntity { rect.direction = d; speed = value.speed; config = value; + hits = config.hits; return value; } diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index cf9369c..c305395 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -51,7 +51,9 @@ class CollisionProcessor implements EngineListener { // Nothing } else { if (!tank.protect.active) { - if (tank.hits > 0) { + if (tank.config.downgrade != null) { + tank.config = engine.config.getTank(tank.config.downgrade); + } else if (tank.hits > 0) { tank.hits--; engine.change(tank, EntityChange.HIT); } else { diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index b38a60c..3d220f6 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -55,17 +55,22 @@ class Game implements EngineListener { return teams[playerId.team].players[playerId.index]; } - private function buildTank(playerId:PlayerId, point:SpawnPoint):Tank { - var player = getPlayer(playerId); - var spawns:Array = player.config.tanks; - var spawn:TankSpawn = spawns[Math.floor(Math.random() * spawns.length)]; - var tankConfig:TankConfig = config.getTank(spawn.type); - var tank = new Tank(playerId, tankConfig); - tank.color = player.config.color.zero ? teams[playerId.team].config.color : player.config.color; - tank.bonus = Math.random() < spawn.bonus; - applyPoint(tank, point); - if (spawn.protect > 0) { - tank.protect.on(spawn.protect); + private function buildTank(task:SpawnTask):Tank { + var player = getPlayer(task.playerId); + var tankType:TankType = if (task.tankType != null) { + task.tankType; + } else { + var spawns:Array = player.config.tanks; + var spawn:TankSpawn = spawns[Math.floor(Math.random() * spawns.length)]; + spawn.type; + } + var tankConfig:TankConfig = config.getTank(tankType); + var tank = new Tank(task.playerId, tankConfig); + tank.color = player.config.color.zero ? teams[task.playerId.team].config.color : player.config.color; + tank.bonus = Math.random() < player.config.bonus; + applyPoint(tank, task.point); + if (player.config.protect > 0) { + tank.protect.on(player.config.protect); } return tank; } @@ -86,19 +91,9 @@ class Game implements EngineListener { var humanControlIndex = 0; for (teamConfig in preset.teams) { var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id); - var team:Team = new Team(teamConfig, teamPoints); + var team:Team = new Team(teamConfig, teamPoints, save); teams[team.id] = team; for (player in team.players.iterator()) { - var playerSave:PlayerSave = save.getPlayer(player.id); - if (playerSave != null) { - player.life = playerSave.life; - if (playerSave.tank != null) { - player.config.tanks = [{ - type: playerSave.tank, - rate: 1, - }]; - } - } if (player.config.control != null) { var control = switch (player.config.control) { case Control.HUMAN: new HumanControl(player.id, humanControlIndex++); @@ -119,7 +114,7 @@ class Game implements EngineListener { for (team in teams.iterator()) { for (player in team.players.iterator()) { if (team.trySpawn(player.id)) { - team.spawner.push(player.id); + team.spawner.push(player.id, player.state.tank); } } var eaglePoint = team.spawner.getPoint('eagle'); @@ -140,8 +135,9 @@ class Game implements EngineListener { var player = getPlayer(task.playerId); player.tankId = 0; if (getTeam(task.playerId.team).trySpawn(task.playerId, true)) { - var tank = buildTank(task.playerId, task.point); + var tank = buildTank(task); player.tankId = tank.id; + player.state.tank = tank.config.type; engine.spawn(tank); } else if (!team.isAlive) { lose(team.id); @@ -257,7 +253,7 @@ class Game implements EngineListener { private function applyBonus(tank:Tank, bonus:Bonus):Void { switch (bonus.config.type) { case 'life': - getPlayer(tank.playerId).life++; + getPlayer(tank.playerId).state.life++; case 'star': if (tank.config.upgrade != null) { tank.config = config.getTank(tank.config.upgrade); @@ -291,23 +287,14 @@ class Game implements EngineListener { } public function save():GameSave { - var players:Array = []; + var save:GameSave = new GameSave(state); for (team in teams) { for (player in team.players) { if (player.config.control == Control.HUMAN) { - var tank:Tank = EntityTypeResolver.as(engine.entities[player.tankId], Tank); - players.push({ - id: player.id, - life: player.life + (player.tankId > 0 ? 1 : 0), - tank: tank != null ? tank.config.type : null - }); + save.addPlayerState(player.id, player.state); } } } - return new GameSave({ - type: state.type, - presetId: state.presetId, - level: state.level - }, players); + return save; } } diff --git a/src/common/haxe/ru/m/tankz/game/GameSave.hx b/src/common/haxe/ru/m/tankz/game/GameSave.hx index e3f4aa2..9e5a3ad 100644 --- a/src/common/haxe/ru/m/tankz/game/GameSave.hx +++ b/src/common/haxe/ru/m/tankz/game/GameSave.hx @@ -1,42 +1,46 @@ 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; -import ru.m.tankz.Type; typedef PlayerSave = { var id:PlayerId; - var tank:TankType; - var life:Int; + var state:PlayerState; } - class GameSave { public var state:GameState; public var players:Array; public function new(state:GameState, ?players:Array) { - this.state = state; + this.state = { + type: state.type, + presetId: state.presetId, + level: state.level, + }; this.players = players != null ? players : []; } - public function getPlayer(id:PlayerId):PlayerSave { + 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; + return player.state; } } return null; } public function toYaml():String { - return Yaml.render({ - state: state, - players: players, - }, Renderer.options().setFlowLevel(0)); + return Yaml.render({state: state, players: players}, Renderer.options().setFlowLevel(0)); } public static function fromYaml(value:String):GameSave { diff --git a/src/common/haxe/ru/m/tankz/game/Player.hx b/src/common/haxe/ru/m/tankz/game/Player.hx index 3270a21..bfd21b1 100644 --- a/src/common/haxe/ru/m/tankz/game/Player.hx +++ b/src/common/haxe/ru/m/tankz/game/Player.hx @@ -5,20 +5,29 @@ import ru.m.tankz.control.Control; import ru.m.tankz.Type; -class Player { +typedef PlayerState = { + var tank:TankType; + var life:Int; + var score:Int; +} +class Player { public var config(default, null):PlayerConfig; public var id(default, null):PlayerId; public var tankId(default, set):Int; public var control(default, set):Control; - public var life(default, default):Int; public var isAlive(get, null):Bool; + public var state(default, default):PlayerState; public function new(teamId:TeamId, config:PlayerConfig) { this.config = config; this.id = {team:teamId, index:config.index}; this.control = null; - this.life = config.life; + this.state = { + life: config.life, + tank: null, + score: 0, + } } private function set_tankId(value:Int):Int { @@ -39,6 +48,6 @@ class Player { } private function get_isAlive():Bool { - return tankId > 0 || life > 0; + return tankId > 0 || state.life > 0; } } diff --git a/src/common/haxe/ru/m/tankz/game/Spawner.hx b/src/common/haxe/ru/m/tankz/game/Spawner.hx index 070286c..404a42b 100644 --- a/src/common/haxe/ru/m/tankz/game/Spawner.hx +++ b/src/common/haxe/ru/m/tankz/game/Spawner.hx @@ -8,6 +8,7 @@ import ru.m.tankz.Type; typedef SpawnTask = { var point:SpawnPoint; var playerId:PlayerId; + var tankType:TankType; } class Spawner { @@ -51,7 +52,7 @@ class Spawner { return null; } - public function push(playerId:PlayerId):Void { + public function push(playerId:PlayerId, ?tankType:TankType):Void { var point:SpawnPoint = null; if (indexedPoints.exists(playerId.index)) { point = indexedPoints.get(playerId.index); @@ -60,7 +61,7 @@ class Spawner { if (index >= anyPoints.length) index = 0; } if (point != null) { - queue.push({playerId:playerId, point:point}); + queue.push({playerId:playerId, point:point, tankType:tankType}); run(); } } diff --git a/src/common/haxe/ru/m/tankz/game/Team.hx b/src/common/haxe/ru/m/tankz/game/Team.hx index 3074cd3..9e23f04 100644 --- a/src/common/haxe/ru/m/tankz/game/Team.hx +++ b/src/common/haxe/ru/m/tankz/game/Team.hx @@ -14,12 +14,15 @@ class Team { public var isAlive(get, null):Bool; public var eagleId(default, default):Int; - public function new(config:TeamConfig, points:Array) { + public function new(config:TeamConfig, points:Array, ?save:GameSave) { this.id = config.id; this.config = config; this.players = new Map(); for (playerConfig in config.players) { - this.players[playerConfig.index] = new Player(id, playerConfig); + var player:Player = new Player(id, playerConfig); + var state = save.getPlayerState(player.id); + if (state != null) player.state = state; + this.players[playerConfig.index] = player; } this.life = config.life; this.spawner = new Spawner(config, points); @@ -28,16 +31,12 @@ class Team { public function trySpawn(playerId:PlayerId, spawn:Bool = false):Bool { var player:Player = players[playerId.index]; var result = false; - if (player.life > -1) { - if (player.life > 0) { - if (spawn) player.life--; - result = true; - } - } else if (life > -1) { - if (life > 0) { - if (spawn) life--; - result = true; - } + if (player.state.life > 0) { + if (spawn) player.state.life--; + result = true; + } else if (life > 0) { + if (spawn) life--; + result = true; } return result; } diff --git a/src/editor/haxe/ru/m/tankz/editor/Editor.hx b/src/editor/haxe/ru/m/tankz/editor/Editor.hx index 7e209e4..317b203 100644 --- a/src/editor/haxe/ru/m/tankz/editor/Editor.hx +++ b/src/editor/haxe/ru/m/tankz/editor/Editor.hx @@ -1,5 +1,9 @@ package ru.m.tankz.editor; +import ru.m.tankz.bundle.LevelBundle; +import ru.m.tankz.bundle.ILevelBundle; +import ru.m.tankz.bundle.ConfigBundle; +import ru.m.tankz.bundle.IConfigBundle; import flash.text.Font; import haxework.gui.ButtonView; import haxework.gui.frame.FrameSwitcher; @@ -54,6 +58,9 @@ class Editor { Root.bind(view); view.content.stage.stageFocusRect = false; + Provider.setFactory(IConfigBundle, ConfigBundle); + Provider.setFactory(ILevelBundle, LevelBundle); + view.switcher.change(LevelFrame.ID); } diff --git a/src/editor/haxe/ru/m/tankz/editor/Editor.yaml b/src/editor/haxe/ru/m/tankz/editor/Editor.yaml index 36042df..44f7216 100644 --- a/src/editor/haxe/ru/m/tankz/editor/Editor.yaml +++ b/src/editor/haxe/ru/m/tankz/editor/Editor.yaml @@ -4,7 +4,7 @@ pWidth: 100 pHeight: 100 skin: $type: haxework.gui.skin.BitmapSkin - image: "@asset:image:resources/images/background.png" + image: "@asset:image:resources/image/ui/background.png" fillType: REPEAT views: # Tabs diff --git a/src/editor/haxe/ru/m/tankz/editor/frame/LevelFrame.hx b/src/editor/haxe/ru/m/tankz/editor/frame/LevelFrame.hx index 888c144..a2054b8 100644 --- a/src/editor/haxe/ru/m/tankz/editor/frame/LevelFrame.hx +++ b/src/editor/haxe/ru/m/tankz/editor/frame/LevelFrame.hx @@ -1,5 +1,7 @@ package ru.m.tankz.editor.frame; +import ru.m.tankz.util.LevelUtil; +import ru.m.tankz.bundle.IConfigBundle; import haxework.gui.ButtonView; import haxework.gui.LabelView; import haxework.gui.list.ListView; @@ -8,8 +10,6 @@ import haxework.gui.VGroupView; import haxework.gui.ViewBuilder; import haxework.provider.Provider; import ru.m.tankz.config.Config; -import ru.m.tankz.config.ConfigBundle; -import ru.m.tankz.config.LevelBundle; import ru.m.tankz.editor.FileUtil; import ru.m.tankz.editor.level.BrickView; import ru.m.tankz.editor.level.MapEditView; @@ -73,11 +73,11 @@ class LevelFrame extends VGroupView implements ViewBuilder implements LevelFrame } private function setGameType(type:GameType):Void { - config = ConfigBundle.get(type); + config = Provider.get(IConfigBundle).get(type); Provider.set(Config, config); mapView.config = config; - mapView.data = LevelBundle.empty(config); + mapView.data = LevelUtil.empty(config); brickList.data = config.bricks.filter(function(brick) return brick.index > -1); spawnPointList.data = config.points; @@ -96,13 +96,13 @@ class LevelFrame extends VGroupView implements ViewBuilder implements LevelFrame L.d(TAG, 'OPEN'); FileUtil.browse().then(function(content:FileContent) { fileNameLabel.text = content.name; - mapView.data = LevelBundle.loads(config, content.content); + mapView.data = LevelUtil.loads(config, content.content); }); case 'saveButton': L.d(TAG, 'SAVE'); FileUtil.save({ name: fileNameLabel.text, - content: LevelBundle.dumps(config, mapView.data), + content: LevelUtil.dumps(config, mapView.data), }); case _: }