From ce343148c755dba6138624cbdb09fde099ca242a Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 22 Jan 2018 12:41:40 +0300 Subject: [PATCH] [common] split player and control --- .../haxe/ru/m/tankz/view/frames/GameFrame.hx | 3 ++ src/common/haxe/ru/m/tankz/bot/Bot.hx | 45 ++++++++++--------- src/common/haxe/ru/m/tankz/control/Control.hx | 4 ++ .../haxe/ru/m/tankz/game/ClassicGame.hx | 7 ++- src/common/haxe/ru/m/tankz/game/Game.hx | 24 ++++++++-- src/common/haxe/ru/m/tankz/game/Player.hx | 35 +++++++++++++-- 6 files changed, 85 insertions(+), 33 deletions(-) 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 04be35f..b929ea0 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -1,5 +1,6 @@ package ru.m.tankz.view.frames; +import ru.m.tankz.control.PlayerControl; import flash.events.Event; import haxe.Timer; import haxework.gui.VGroupView; @@ -32,6 +33,8 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand humans: 1, bots: 3, }); + game.setControl('player', 0, PlayerControl.forPlayer(0)); + //game.setControl('player', 1, PlayerControl.forPlayer(1)); 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/Bot.hx index 8c24f36..ccb217b 100644 --- a/src/common/haxe/ru/m/tankz/bot/Bot.hx +++ b/src/common/haxe/ru/m/tankz/bot/Bot.hx @@ -1,7 +1,5 @@ package ru.m.tankz.bot; -import ru.m.tankz.game.Game; -import ru.m.tankz.game.Player; import ru.m.tankz.control.Control; import ru.m.geom.Direction; import haxe.Timer; @@ -10,13 +8,13 @@ import ru.m.tankz.core.Tank; import Type.ValueType; -class Bot extends Player { +class Bot extends Control { private var shotTimer:Timer; private var turnTimer:Timer; - public function new(team:TeamId, index:Int) { - super(team, index); + public function new() { + super(); } override public function onCollision(with:Dynamic):Void { @@ -27,12 +25,27 @@ class Bot extends Player { } } - public function start():Void { + override public function start():Void { action(TankAction.MOVE(Direction.BOTTOM)); - shotTimer = new Timer(1000); - shotTimer.run = shot; - turnTimer = new Timer(3000); - turnTimer.run = turn; + if (shotTimer == null) { + shotTimer = new Timer(1000); + shotTimer.run = shot; + } + if (turnTimer == null) { + turnTimer = new Timer(3000); + turnTimer.run = turn; + } + } + + override public function stop():Void { + if (shotTimer != null) { + shotTimer.stop(); + shotTimer = null; + } + if (turnTimer != null) { + turnTimer.stop(); + turnTimer = null; + } } public function shot():Void { @@ -51,16 +64,4 @@ class Bot extends Player { Direction.RIGHT, ][Math.floor(Math.random() * 4)]; } - - override public function dispose():Void { - super.dispose(); - if (shotTimer != null) { - shotTimer.stop(); - shotTimer = null; - } - if (turnTimer != null) { - turnTimer.stop(); - turnTimer = null; - } - } } diff --git a/src/common/haxe/ru/m/tankz/control/Control.hx b/src/common/haxe/ru/m/tankz/control/Control.hx index 94ea9ef..202630b 100644 --- a/src/common/haxe/ru/m/tankz/control/Control.hx +++ b/src/common/haxe/ru/m/tankz/control/Control.hx @@ -31,8 +31,12 @@ class Control { } + public function start():Void {} + + public function stop():Void {} public function dispose():Void { + stop(); listener = null; } } diff --git a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx index a2ab163..908b9f6 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.control.Control; import ru.m.tankz.bot.Bot; import ru.m.tankz.game.Game; import ru.m.tankz.config.Config; @@ -24,13 +25,12 @@ class ClassicGame extends Game { var humans = new Team('player', settings.humans, 3); for (index in 0...humans.size) { var player = new Player(humans.id, index); - player.bind(this); humans.players.push(player); } var bots = new Team('bot', settings.bots, 20); for (index in 0...bots.size) { - var bot = new Bot(bots.id, 0); - bot.bind(this); + var bot = new Player(bots.id, 0, new Bot()); + bot.control.bind(this); bots.players.push(bot); } teams = new Map(); @@ -50,7 +50,6 @@ class ClassicGame extends Game { var tank = buildTank(0, bots.id, config.getTank(bots.type, 0), point); engine.spawn(tank); player.tankId = tank.id; - cast(player, Bot).start(); } } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 2dbf112..f326772 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -15,13 +15,13 @@ typedef TeamId = Int; typedef GameSettings = {} -class Game implements EngineListener implements ControlListener { +class Game implements EngineListener implements ControlListener { public var type(default, null):GameType; public var teams(default, null):Map; public var config(default, null):Config; public var engine(default, null):Engine; - public var settings(default, null):G; + public var settings(default, null):S; public function new(type:GameType, config:Config) { @@ -37,10 +37,26 @@ class Game implements EngineListener implements ControlListener return tank; } - public function start(settings:G):Void { + public function start(settings:S):Void { this.settings = settings; } + public function setControl(teamType:String, index:Int, control:Control):Void { + for (team in teams.iterator()) { + if (team.type == teamType) { + L.w('XXX', 'players: ${team.players}'); + var player = team.players[index]; + if (player.control != null) { + player.control.dispose(); + } + player.control = control; + player.control.bind(this); + break; + } + } + } + + public function onSpawn(entity:EntityType):Void { } @@ -53,10 +69,12 @@ class Game implements EngineListener implements ControlListener } + public function onAction(tankId:Int, action:TankAction):Void { engine.action(tankId, action); } + public function dispose():Void { } diff --git a/src/common/haxe/ru/m/tankz/game/Player.hx b/src/common/haxe/ru/m/tankz/game/Player.hx index 81bd953..4dc81e3 100644 --- a/src/common/haxe/ru/m/tankz/game/Player.hx +++ b/src/common/haxe/ru/m/tankz/game/Player.hx @@ -1,17 +1,44 @@ package ru.m.tankz.game; -import ru.m.tankz.game.Game; import ru.m.tankz.control.Control; +import ru.m.tankz.game.Game; -class Player extends Control { +class Player { public var index(default, null):Int; public var team(default, null):TeamId; + public var tankId(default, set):Int; + public var control(default, set):Control; - public function new(team:TeamId, index:Int) { - super(); + public function new(team:TeamId, index:Int, control:Control=null) { this.team = team; this.index = index; + this.control = control; + } + + public function set_tankId(value:Int):Int { + tankId = value; + if (control != null) { + control.tankId = tankId; + if (tankId != 0) { + control.start(); + } else { + control.stop(); + } + } + return tankId; + } + + public function set_control(value:Control):Control { + if (control != null) control.dispose(); + control = value; + if (control != null) { + control.tankId = tankId; + if (tankId != 0) { + control.start(); + } + } + return control; } }