diff --git a/src/client/haxe/ru/m/tankz/control/PlayerControl.hx b/src/client/haxe/ru/m/tankz/control/HumanControl.hx similarity index 85% rename from src/client/haxe/ru/m/tankz/control/PlayerControl.hx rename to src/client/haxe/ru/m/tankz/control/HumanControl.hx index f9b96c1..0a7e287 100644 --- a/src/client/haxe/ru/m/tankz/control/PlayerControl.hx +++ b/src/client/haxe/ru/m/tankz/control/HumanControl.hx @@ -8,15 +8,20 @@ import flash.ui.Keyboard; import flash.events.KeyboardEvent; import flash.Lib; -class PlayerControl extends Control { - private var keyBinding:Map; +typedef KeyBinding = Map; + + +class HumanControl extends Control { + public static var TYPE(default, never):ControlType = 'human'; + + private var keyBinding:KeyBinding; private var moveQueue:Array; private var shotTimer:Timer; - public function new(keyBinding:Map) { - super(); - this.keyBinding = keyBinding; + public function new(index:Int) { + super({type:TYPE, index:index}); + this.keyBinding = resolve(index); moveQueue = new Array(); Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); @@ -78,26 +83,26 @@ class PlayerControl extends Control { action(TankAction.SHOT); } - public static function forPlayer(index:Int):PlayerControl { + private static function resolve(index:Int):KeyBinding { switch (index) { case 0: - return new PlayerControl([ + return [ Keyboard.A => TankAction.MOVE(Direction.LEFT), Keyboard.S => TankAction.MOVE(Direction.BOTTOM), Keyboard.W => TankAction.MOVE(Direction.TOP), Keyboard.D => TankAction.MOVE(Direction.RIGHT), Keyboard.SPACE => TankAction.SHOT - ]); + ]; case 1: - return new PlayerControl([ + return [ Keyboard.LEFT => TankAction.MOVE(Direction.LEFT), Keyboard.DOWN => TankAction.MOVE(Direction.BOTTOM), Keyboard.UP => TankAction.MOVE(Direction.TOP), Keyboard.RIGHT => TankAction.MOVE(Direction.RIGHT), Keyboard.NUMPAD_0 => TankAction.SHOT - ]); + ]; case _: - throw 'Invalid player index ${index}'; + throw 'Invalid control index ${index}'; } } } 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 08fbd75..e304d12 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -1,7 +1,6 @@ package ru.m.tankz.view.frames; import ru.m.tankz.game.GameState; -import ru.m.tankz.control.PlayerControl; import flash.events.Event; import haxe.Timer; import haxework.gui.VGroupView; @@ -34,9 +33,6 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand } game.engine.listeners.push(render); 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); render.draw(game.engine); diff --git a/src/common/haxe/ru/m/tankz/bot/Bot.hx b/src/common/haxe/ru/m/tankz/bot/BotControl.hx similarity index 78% rename from src/common/haxe/ru/m/tankz/bot/Bot.hx rename to src/common/haxe/ru/m/tankz/bot/BotControl.hx index 03b4bbf..1ad360a 100644 --- a/src/common/haxe/ru/m/tankz/bot/Bot.hx +++ b/src/common/haxe/ru/m/tankz/bot/BotControl.hx @@ -6,13 +6,14 @@ import ru.m.geom.Direction; import haxe.Timer; -class Bot extends Control { +class BotControl extends Control { + public static var TYPE(default, never):ControlType = 'bot'; private var shotTimer:Timer; private var turnTimer:Timer; - public function new() { - super(); + public function new(index:Int) { + super({type:TYPE, index:index}); } override public function onCollision(with:EntityType):Void { @@ -24,8 +25,9 @@ class Bot extends Control { } override public function start():Void { - var tank = handler.entities.get(tankId); - action(TankAction.MOVE(tank.rect.direction)); + //var tank = handler.entities.get(tankId); + //action(TankAction.MOVE(tank.rect.direction)); + action(TankAction.MOVE(Direction.BOTTOM)); // ToDo: hardcode bot start direction if (shotTimer == null) { shotTimer = new Timer(1000); shotTimer.run = shot; diff --git a/src/common/haxe/ru/m/tankz/control/Control.hx b/src/common/haxe/ru/m/tankz/control/Control.hx index 482ca90..45abfe7 100644 --- a/src/common/haxe/ru/m/tankz/control/Control.hx +++ b/src/common/haxe/ru/m/tankz/control/Control.hx @@ -13,11 +13,23 @@ enum TankAction { } +typedef ControlType = String; + + +typedef ControlId = { + var type:ControlType; + var index:Int; +} + + class Control { + public var id:ControlId; public var tankId(default, default):Int; private var handler:ControlHandler; - public function new() {} + public function new(id:ControlId) { + this.id = id; + } public function bind(handler:ControlHandler):Void { this.handler = handler; diff --git a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx index ff2cbe2..b520eb2 100644 --- a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx +++ b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx @@ -1,9 +1,7 @@ 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; class ClassicGame extends Game { @@ -17,13 +15,6 @@ class ClassicGame extends Game { super(TYPE); } - 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; @@ -37,13 +28,24 @@ class ClassicGame extends Game { group: HUMAN, type: '1' }, + control:{ + type: 'human', + index: i + }, life:3, }; } for (i in 0...humans*2+2) { state.players[BOT][i] = { index:i, - tank:null, + tank:{ + group: BOT, + type: '1' + }, + control:{ + type: 'bot', + index: i + }, life:-1, }; } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 618609c..eec14f8 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.bot.BotControl; +import ru.m.tankz.control.HumanControl; import ru.m.tankz.config.ConfigBundle; import ru.m.tankz.config.MapBundle; import ru.m.tankz.game.Spawner; @@ -68,6 +70,15 @@ class Game implements EngineListener { var player = new Player({team:team.id, index:playerState.index}); team.players.push(player); teams.set(team.id, team); + if (playerState.control != null) { + var control = switch (playerState.control.type) { + case HumanControl.TYPE: new HumanControl(playerState.control.index); + case BotControl.TYPE: new BotControl(playerState.control.index); + case _: throw 'Unsupported control type: "${playerState.control.type}"'; + } + player.control = control; + player.control.bind(engine); + } } spawners.set(team.id, new Spawner(team.config, spawn)); } diff --git a/src/common/haxe/ru/m/tankz/game/GameState.hx b/src/common/haxe/ru/m/tankz/game/GameState.hx index fbd018e..a619a6f 100644 --- a/src/common/haxe/ru/m/tankz/game/GameState.hx +++ b/src/common/haxe/ru/m/tankz/game/GameState.hx @@ -4,10 +4,20 @@ import ru.m.tankz.game.Game; import ru.m.tankz.config.Config; +typedef ControlType = String; + + +typedef ControId = { + var type:ControlType; + var index:Int; +} + + typedef PlayerState = { var index:Int; var tank:TankType; var life:Int; + var control:ControId; }