diff --git a/src/client/haxe/ru/m/tankz/Client.hx b/src/client/haxe/ru/m/tankz/Client.hx index 79cbc5f..4257b0e 100755 --- a/src/client/haxe/ru/m/tankz/Client.hx +++ b/src/client/haxe/ru/m/tankz/Client.hx @@ -1,5 +1,7 @@ package ru.m.tankz; +import ru.m.tankz.game.ClassicGame; +import ru.m.tankz.game.Game; import flash.ui.Keyboard; import flash.events.KeyboardEvent; import flash.text.Font; @@ -10,8 +12,6 @@ import haxework.resources.IResources; import haxework.gui.VGroupView; import haxework.gui.ViewBuilder; import haxework.gui.ButtonView; -import flash.display.Sprite; -import haxework.gui.IGroupView; import ru.m.tankz.PacketBuilder; import haxework.log.JSLogger; import haxework.gui.frame.IFrameSwitcher; @@ -74,6 +74,8 @@ class Client implements IConnectionHandler { view.switcher.change(StartFrame.ID); } }); + + Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE); } public function onPress(view:ButtonView):Void { diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx index cc7084e..08fbd75 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -1,6 +1,6 @@ package ru.m.tankz.view.frames; -import ru.m.tankz.config.Config; +import ru.m.tankz.game.GameState; import ru.m.tankz.control.PlayerControl; import flash.events.Event; import haxe.Timer; @@ -9,8 +9,6 @@ import haxework.gui.ViewBuilder; import haxework.provider.Provider; import protohx.Message; import ru.m.connect.IConnection; -import ru.m.tankz.config.ConfigBundle; -import ru.m.tankz.game.ClassicGame; import ru.m.tankz.game.Game; import ru.m.tankz.proto.pack.GameUpdateResponse; @@ -29,15 +27,15 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand } public function onShow():Void { - var config:Config = Provider.get(Config); - game = switch (config.type) { - case ClassicGame.TYPE: new ClassicGame(config); - case x: throw 'Unsupported game type "${x}"'; + var state:GameState = Provider.get(GameState); + game = Provider.build(Game, state.type); + if (game == null) { + throw 'Unsupported game type "${state.type}"'; } game.engine.listeners.push(render); - game.start(); - for (index in 0...game.config.getTeam('human').size) { - game.setControl({team:'human', index:index}, PlayerControl.forPlayer(index)); + game.start(state); + for (human in state.players['human'].iterator()) { + game.setControl({team:'human', index:human.index}, PlayerControl.forPlayer(human.index)); } content.addEventListener(Event.ENTER_FRAME, redraw); Provider.get(IConnection).packetHandler.addListener(this); diff --git a/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx index 51486b9..3bb257b 100644 --- a/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx @@ -1,14 +1,14 @@ package ru.m.tankz.view.frames; -import ru.m.tankz.config.Config; +import ru.m.tankz.game.GameState; import ru.m.tankz.game.ClassicGame; -import ru.m.tankz.config.ConfigBundle; import haxework.gui.frame.IFrameSwitcher; import haxework.provider.Provider; import haxework.gui.ButtonView; import haxework.gui.ViewBuilder; import haxework.gui.VGroupView; + @:template("layout/frames/start.json", "layout/styles.json") class StartFrame extends VGroupView implements ViewBuilder { @@ -28,11 +28,8 @@ class StartFrame extends VGroupView implements ViewBuilder { } } - private function startGame(players:Int):Void { - var config = ConfigBundle.get(ClassicGame.TYPE, 0); - config.getTeam('human').size = players; - config.getTeam('bot').size = 2 + 2 * players; - Provider.set(Config, config); + private function startGame(humans:Int):Void { + Provider.set(GameState, ClassicGame.buildState(0, humans)); Provider.get(IFrameSwitcher).change(GameFrame.ID); } } diff --git a/src/client/resources/config/classic.yaml b/src/client/resources/config/classic.yaml index 622c7cf..498e7bd 100644 --- a/src/client/resources/config/classic.yaml +++ b/src/client/resources/config/classic.yaml @@ -3,7 +3,6 @@ map: cellHeight: 22 gridWidth: 26 gridHeight: 26 - bricks: [] bricks: # border @@ -37,7 +36,6 @@ bricks: teams: - id: human - size: 2 spawnInterval: 0 points: - type: eagle @@ -56,7 +54,6 @@ teams: y: 24 direction: top - id: bot - size: 6 spawnInterval: 3000 points: - type: tank diff --git a/src/client/resources/fonts/8-BIT WONDER.TTF b/src/client/resources/fonts/8-BIT WONDER.TTF deleted file mode 100644 index 6d9b397..0000000 Binary files a/src/client/resources/fonts/8-BIT WONDER.TTF and /dev/null differ diff --git a/src/client/resources/fonts/8-BIT WONDER.eot b/src/client/resources/fonts/8-BIT WONDER.eot deleted file mode 100644 index e2c9e2a..0000000 Binary files a/src/client/resources/fonts/8-BIT WONDER.eot and /dev/null differ diff --git a/src/client/resources/fonts/8-BIT WONDER.svg b/src/client/resources/fonts/8-BIT WONDER.svg deleted file mode 100644 index 266614a..0000000 --- a/src/client/resources/fonts/8-BIT WONDER.svg +++ /dev/null @@ -1,161 +0,0 @@ - - -!"#$%&'()*+,-./0123456789:;å<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` abcdefghijklmnopqrstuvwxyz|{}~ \ No newline at end of file diff --git a/src/client/resources/fonts/8-BIT WONDER.woff b/src/client/resources/fonts/8-BIT WONDER.woff deleted file mode 100644 index 5902288..0000000 Binary files a/src/client/resources/fonts/8-BIT WONDER.woff and /dev/null differ diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index d0b1a2d..de6b6cd 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -14,7 +14,6 @@ typedef MapConfig = { var cellHeight:Float; var gridWidth:Int; var gridHeight:Int; - var bricks:Array; } typedef BrickConfig = { @@ -31,9 +30,12 @@ typedef BulletConfig = { } -typedef TankConfig = { +typedef TankType = { var group:String; var type:String; +} + +typedef TankConfig = { > TankType, var width:Float; var height:Float; var speed:Float; diff --git a/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx b/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx index 64827d3..dde0f4c 100644 --- a/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx +++ b/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx @@ -20,20 +20,10 @@ class ConfigBundle { return raw; } - public static function get(type:String, level:Int):Config { + public static function get(type:String):Config { switch (type) { case ClassicGame.TYPE: var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/${type}.yaml'), Parser.options().useObjects())); - var bricksData:String = Assets.getText('resources/levels/level00${level}.txt'); - var bricks:Array = []; - for (line in ~/\s+/g.split(bricksData)) { - for (c in line.split('')) { - if (c.length > 0) { - bricks.push(source.bricks[Std.parseInt(c) + 1]); - } - } - } - source.map.bricks = bricks; var tanks:Array = []; for (group in Reflect.fields(source.tanks)) { var data:Array = Reflect.field(source.tanks, group); diff --git a/src/common/haxe/ru/m/tankz/config/MapBundle.hx b/src/common/haxe/ru/m/tankz/config/MapBundle.hx new file mode 100644 index 0000000..66f1f12 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/config/MapBundle.hx @@ -0,0 +1,22 @@ +package ru.m.tankz.config; + +import openfl.Assets; +import ru.m.tankz.game.Game; +import ru.m.tankz.config.Config; + + +class MapBundle { + + public static function get(type:GameType, config:Config, level:Int):Array { + var bricksData:String = Assets.getText('resources/levels/level00${level}.txt'); + var bricks:Array = []; + for (line in ~/\s+/g.split(bricksData)) { + for (c in line.split('')) { + if (c.length > 0) { + bricks.push(config.bricks[Std.parseInt(c) + 1]); + } + } + } + return bricks; + } +} diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 1a49ac8..ea449ae 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -122,7 +122,8 @@ class Engine implements ControlHandler { case TankAction.MOVE(direction): tank.move(direction); case TankAction.LEVEL_UP(level): - tank.config = config.getTank('player', Std.string(Std.int(Math.min(Std.parseInt(tank.config.type) + level, 3)))); + // ToDo: + tank.config = config.getTank('human', Std.string(Std.int(Math.min(Std.parseInt(tank.config.type) + level, 3)))); case TankAction.STOP: tank.stop(); case TankAction.SHOT: diff --git a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx index 9a13d7c..ff2cbe2 100644 --- a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx +++ b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx @@ -1,5 +1,6 @@ package ru.m.tankz.game; +import ru.m.tankz.game.GameState.PlayerState; import ru.m.tankz.bot.Bot; import ru.m.tankz.game.Game; import ru.m.tankz.config.Config; @@ -9,14 +10,43 @@ class ClassicGame extends Game { public static var TYPE(default, never):GameType = 'classic'; - public function new(config:Config) { - super(TYPE, config); + public static var HUMAN(default, never):TeamId = 'human'; + public static var BOT(default, never):TeamId = 'bot'; + + public function new() { + super(TYPE); } - override public function start():Void { - super.start(); - for (player in teams.get('bot').players) { + override public function start(state:GameState):Void { + super.start(state); + for (player in teams.get(BOT).players) { setControl(player.id, new Bot()); } } + + public static function buildState(level:Int, humans:Int):GameState { + var state = new GameState(); + state.type = TYPE; + state.level = level; + state.players[HUMAN] = new Map(); + state.players[BOT] = new Map(); + for (i in 0...humans) { + state.players[HUMAN][i] = { + index:i, + tank:{ + group: HUMAN, + type: '1' + }, + life:3, + }; + } + for (i in 0...humans*2+2) { + state.players[BOT][i] = { + index:i, + tank:null, + life:-1, + }; + } + return state; + } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index a7d0fb6..618609c 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -1,5 +1,7 @@ package ru.m.tankz.game; +import ru.m.tankz.config.ConfigBundle; +import ru.m.tankz.config.MapBundle; import ru.m.tankz.game.Spawner; import ru.m.tankz.core.Entity; import ru.m.tankz.core.Eagle; @@ -25,15 +27,16 @@ typedef PlayerId = { class Game implements EngineListener { public var type(default, null):GameType; + public var state(default, null):GameState; public var teams(default, null):Map; public var config(default, null):Config; public var engine(default, null):Engine; private var spawners:Map; - public function new(type:GameType, config:Config) { + public function new(type:GameType) { this.type = type; - this.config = config; + this.config = ConfigBundle.get(type); this.engine = new Engine(config); engine.listeners.push(this); } @@ -53,13 +56,16 @@ class Game implements EngineListener { entity.rect.direction = Direction.fromString(point.direction); } - public function start():Void { + public function start(state:GameState):Void { + this.state = state; + var bricks = MapBundle.get(type, config, state.level); + engine.map.setData(bricks); teams = new Map(); spawners = new Map(); for (teamConfig in config.teams) { var team = new Team(teamConfig); - for (index in 0...team.config.size) { - var player = new Player({team:team.id, index:index}); + for (playerState in state.players.get(team.id)) { + var player = new Player({team:team.id, index:playerState.index}); team.players.push(player); teams.set(team.id, team); } diff --git a/src/common/haxe/ru/m/tankz/game/GameState.hx b/src/common/haxe/ru/m/tankz/game/GameState.hx new file mode 100644 index 0000000..fbd018e --- /dev/null +++ b/src/common/haxe/ru/m/tankz/game/GameState.hx @@ -0,0 +1,24 @@ +package ru.m.tankz.game; + +import ru.m.tankz.game.Game; +import ru.m.tankz.config.Config; + + +typedef PlayerState = { + var index:Int; + var tank:TankType; + var life:Int; +} + + +class GameState { + public var type:GameType; + public var level:Int; + public var players:Map>; + + public function new() { + type = null; + level = -1; + players = new Map(); + } +} diff --git a/src/common/haxe/ru/m/tankz/map/LevelMap.hx b/src/common/haxe/ru/m/tankz/map/LevelMap.hx index 554353f..ec59d39 100755 --- a/src/common/haxe/ru/m/tankz/map/LevelMap.hx +++ b/src/common/haxe/ru/m/tankz/map/LevelMap.hx @@ -28,13 +28,18 @@ class LevelMap { gridWidth = config.gridWidth; gridHeight = config.gridHeight; bricksMap = new HashMap(); + bricks = []; grid = new Grid( Std.int(cellWidth / 2), Std.int(cellHeight / 2), Std.int(cellWidth * gridWidth), Std.int(cellHeight * gridHeight) ); - bricks = Lambda.array(Lambda.mapi(config.bricks, function(i:Int, brickConfig:BrickConfig):Brick { + } + + public function setData(data:Array):Void { + bricksMap = new HashMap(); + bricks = Lambda.array(Lambda.mapi(data, function(i:Int, brickConfig:BrickConfig):Brick { var cellX = Std.int(i % gridWidth); var cellY = Std.int(Math.floor(i / gridHeight)); var cells:HashMap = new HashMap();