[common] refactor ControlFactory
This commit is contained in:
@@ -10,8 +10,6 @@ import ru.m.tankz.bundle.ConfigBundle;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.bundle.LevelBundle;
|
||||
import ru.m.tankz.control.ClientControlFactory;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
@@ -32,7 +30,6 @@ class Init {
|
||||
@:provide static var recordStorage:RecordStorage;
|
||||
@:provide static var soundManager:SoundManager;
|
||||
@:provide static var networkManager:NetworkManager;
|
||||
@:provide static var controlFactory:IControlFactory;
|
||||
@:provide static var popupManager:PopupManager;
|
||||
@:provide static var connection:IConnection<Request, Response>;
|
||||
|
||||
@@ -58,7 +55,6 @@ class Init {
|
||||
gameStorage = new GameStorage();
|
||||
recordStorage = new RecordStorage();
|
||||
soundManager = new SoundManager();
|
||||
controlFactory = new ClientControlFactory();
|
||||
popupManager = new PopupManager();
|
||||
|
||||
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
||||
|
||||
@@ -2,7 +2,7 @@ package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class ClientControlFactory extends BaseControlFactory {
|
||||
class LocalControlFactory extends BaseControlFactory {
|
||||
|
||||
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||
return new HumanControl(id, index);
|
||||
@@ -3,11 +3,11 @@ package ru.m.tankz.control;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.control.Control;
|
||||
|
||||
class ClientNetworkControl extends HumanControl {
|
||||
class NetworkControl extends HumanControl {
|
||||
|
||||
@:provide private var network:NetworkManager;
|
||||
|
||||
override public function action(action:TankAction):Void {
|
||||
network.action(action);
|
||||
network.action(tankId, action);
|
||||
}
|
||||
}
|
||||
10
src/client/haxe/ru/m/tankz/control/NetworkControlFactory.hx
Normal file
10
src/client/haxe/ru/m/tankz/control/NetworkControlFactory.hx
Normal file
@@ -0,0 +1,10 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class NetworkControlFactory extends BaseControlFactory {
|
||||
|
||||
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||
return new NetworkControl(id, index);
|
||||
}
|
||||
}
|
||||
28
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
28
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
@@ -0,0 +1,28 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.control.LocalControlFactory;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.record.GameRecorder;
|
||||
import ru.m.tankz.storage.RecordStorage;
|
||||
|
||||
class LocalGame extends GameRunner {
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
private var recorder:GameRecorder;
|
||||
|
||||
public function new(state:GameState) {
|
||||
super(state);
|
||||
controlFactory = new LocalControlFactory();
|
||||
recorder = new GameRecorder();
|
||||
connect(recorder);
|
||||
}
|
||||
|
||||
override public function onGameEvent(event:GameEvent):Void {
|
||||
super.onGameEvent(event);
|
||||
switch event {
|
||||
case GameEvent.COMPLETE(_, _):
|
||||
disconnect(recorder);
|
||||
recordStorage.save(recorder.record);
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
33
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
@@ -0,0 +1,33 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxe.Unserializer;
|
||||
import ru.m.tankz.control.NetworkControlFactory;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||
|
||||
class NetworkGame extends Game {
|
||||
|
||||
private var network:NetworkManager;
|
||||
|
||||
public function new(network:NetworkManager) {
|
||||
super(new GameState(network.game.type, 0, network.game.level));
|
||||
this.network = network;
|
||||
this.controlFactory = new NetworkControlFactory();
|
||||
network.gameEventSignal.connect(onGameEventProto);
|
||||
}
|
||||
|
||||
private function onGameEventProto(game:GameEventResponse):Void {
|
||||
var frame = game.frame;
|
||||
var eventStr = game.event;
|
||||
var event:GameEvent = Unserializer.run(eventStr);
|
||||
gameEventSignal.emit(event);
|
||||
}
|
||||
|
||||
override public function dispose():Void {
|
||||
super.dispose();
|
||||
network.gameEventSignal.disconnect(onGameEventProto);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.Game;
|
||||
|
||||
class NetworkGame extends Game {
|
||||
|
||||
private var network:NetworkManager;
|
||||
|
||||
public function new(network:NetworkManager) {
|
||||
super(new GameState(network.game.type, 0, network.game.level));
|
||||
this.network = network;
|
||||
network.gameSignal.connect(onGameChange);
|
||||
}
|
||||
|
||||
private function onGameChange(game:GameProto):Void {
|
||||
|
||||
}
|
||||
|
||||
override public function dispose():Void {
|
||||
super.dispose();
|
||||
network.gameSignal.disconnect(onGameChange);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.game.IGame;
|
||||
import haxe.Unserializer;
|
||||
import haxe.Serializer;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.control.Control;
|
||||
@@ -10,6 +9,8 @@ import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.game.GameChangeProto;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
import ru.m.tankz.proto.pack.GameEventRequest;
|
||||
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||
import ru.m.tankz.proto.pack.ListGameRequest;
|
||||
@@ -41,16 +42,17 @@ class NetworkManager {
|
||||
public var listGameSignal:Signal<Array<GameProto>>;
|
||||
public var gameSignal:Signal<GameProto>;
|
||||
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
||||
public var gameEventSignal:Signal<GameEventResponse>;
|
||||
|
||||
@:provide private var connection:ClientConnection;
|
||||
@:provide private var storage:MultiplayerStorage;
|
||||
@:provide private var _game:IGame;
|
||||
|
||||
public function new() {
|
||||
stateSignal = new Signal();
|
||||
listGameSignal = new Signal();
|
||||
gameSignal = new Signal();
|
||||
gameUpdateSignal = new Signal();
|
||||
gameEventSignal = new Signal();
|
||||
updateState(OFFLINE);
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.receiveHandler.connect(onResponse);
|
||||
@@ -103,8 +105,10 @@ class NetworkManager {
|
||||
connection.send(new Request().setStartGame(new StartGameRequest()));
|
||||
}
|
||||
|
||||
public function action(action:TankAction):Void {
|
||||
// ToDo: network
|
||||
public function action(tankId:Int, action:TankAction):Void {
|
||||
connection.send(new Request().setGameEvent(new GameEventRequest().setFrame(0).setEvent(
|
||||
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||
)));
|
||||
}
|
||||
|
||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||
@@ -153,10 +157,7 @@ class NetworkManager {
|
||||
game = packet.startGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasGameEvent()) {
|
||||
var frame = packet.gameEvent.frame;
|
||||
var eventStr = packet.gameEvent.event;
|
||||
var event:GameEvent = Unserializer.run(eventStr);
|
||||
_game.gameEventSignal.emit(event);
|
||||
gameEventSignal.emit(packet.gameEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,10 @@ import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.record.GamePlayer;
|
||||
import ru.m.tankz.game.record.GameRecord;
|
||||
import ru.m.tankz.game.record.GameRecorder;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.storage.RecordStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.game.GameView;
|
||||
|
||||
@@ -30,10 +27,8 @@ import ru.m.tankz.view.game.GameView;
|
||||
@:provide("next") var nextState:GameState;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var gameStorage:GameStorage;
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
|
||||
@:provide var game:IGame;
|
||||
private var recorder:GameRecorder;
|
||||
|
||||
public function onShow():Void {
|
||||
gameView.type = game.type;
|
||||
@@ -44,11 +39,6 @@ import ru.m.tankz.view.game.GameView;
|
||||
if (gameView.panel != null) {
|
||||
game.connect(gameView.panel);
|
||||
}
|
||||
// ToDo:
|
||||
if (!Std.is(game, GamePlayer)) {
|
||||
recorder = new GameRecorder();
|
||||
game.connect(recorder);
|
||||
}
|
||||
game.connect(this);
|
||||
game.start();
|
||||
}
|
||||
@@ -64,10 +54,6 @@ import ru.m.tankz.view.game.GameView;
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.COMPLETE(state, winner):
|
||||
// ToDo:
|
||||
if (recorder != null) {
|
||||
recordStorage.save(recorder.record);
|
||||
}
|
||||
this.state = state;
|
||||
nextState = switch next(winner) {
|
||||
case Some(s):
|
||||
@@ -100,7 +86,6 @@ import ru.m.tankz.view.game.GameView;
|
||||
public function onHide():Void {
|
||||
stop();
|
||||
soundManager.stopAll();
|
||||
recorder = null;
|
||||
}
|
||||
|
||||
public function close():Void {
|
||||
|
||||
@@ -7,9 +7,9 @@ import haxework.view.LabelView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.LocalGame;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.popup.LevelPopup;
|
||||
@@ -36,7 +36,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
private function start(level:LevelConfig, preset:GamePreset):Void {
|
||||
state.levelId = level.id;
|
||||
state.presetId = preset.id;
|
||||
game = new GameRunner(state);
|
||||
game = new LocalGame(state);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import haxework.view.list.VListView;
|
||||
import haxework.view.TextView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.network.NetworkGame;
|
||||
import ru.m.tankz.game.NetworkGame;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.GameStateProto;
|
||||
|
||||
@@ -4,8 +4,12 @@ import ru.m.geom.Point;
|
||||
import ru.m.geom.Position;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.control.NoneControlFactory;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
@@ -21,6 +25,8 @@ import ru.m.tankz.Type;
|
||||
public var winner(default, null):Null<TeamId>;
|
||||
public var state(default, null):GameState;
|
||||
public var frame(default, null):Int;
|
||||
public var engine(default, null):IEngine;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
@@ -30,6 +36,7 @@ import ru.m.tankz.Type;
|
||||
this.teams = new Map();
|
||||
this.config = configBundle.get(type);
|
||||
this.frame = 0;
|
||||
this.controlFactory = new NoneControlFactory();
|
||||
connect(this);
|
||||
}
|
||||
|
||||
@@ -74,6 +81,15 @@ import ru.m.tankz.Type;
|
||||
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
||||
teams[team.id] = team;
|
||||
}
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players.iterator()) {
|
||||
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
player.control.bind(this, engine);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
|
||||
@@ -3,8 +3,6 @@ package ru.m.tankz.game;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.core.Bonus;
|
||||
import ru.m.tankz.core.Bullet;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
@@ -18,10 +16,6 @@ import ru.m.tankz.Type;
|
||||
import ru.m.Timer;
|
||||
|
||||
class GameRunner extends Game implements EngineListener {
|
||||
@:provide var controlFactory:IControlFactory;
|
||||
|
||||
public var engine(default, null):IEngine;
|
||||
|
||||
private var timer:Timer;
|
||||
private var builder:EntityBuilder;
|
||||
|
||||
@@ -50,13 +44,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
super.start();
|
||||
engine.map.setData(state.level.data);
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players.iterator()) {
|
||||
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
player.control.bind(this, engine);
|
||||
}
|
||||
}
|
||||
team.spawner.runner = spawn;
|
||||
for (player in team.players.iterator()) {
|
||||
if (team.tryRespawn(player.id)) {
|
||||
@@ -120,10 +107,12 @@ class GameRunner extends Game implements EngineListener {
|
||||
private function complete(winner:TeamId):Void {
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players) {
|
||||
if (player.control != null) {
|
||||
player.control.action(TankAction.STOP);
|
||||
player.control.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Timer.delay(function() {
|
||||
gameEventSignal.emit(GameEvent.COMPLETE(state, winner));
|
||||
}, 3000);
|
||||
|
||||
@@ -2,6 +2,7 @@ package ru.m.tankz.game;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
interface IGame extends GameListener {
|
||||
@@ -11,6 +12,7 @@ interface IGame extends GameListener {
|
||||
public var winner(default, null):Null<TeamId>;
|
||||
public var state(default, null):GameState;
|
||||
public var frame(default, null):Int;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
|
||||
public var gameEventSignal(default, null):Signal<GameEvent>;
|
||||
|
||||
|
||||
@@ -6,10 +6,8 @@ import haxework.provider.Provider;
|
||||
import neko.net.ThreadServer;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.server.bundle.ServerConfigBundle;
|
||||
import ru.m.tankz.server.bundle.ServerLevelBundle;
|
||||
import ru.m.tankz.server.control.ServerControlFactory;
|
||||
import ru.m.tankz.server.game.GameManager;
|
||||
import ru.m.tankz.server.game.IGameManager;
|
||||
import ru.m.tankz.server.session.GameSession;
|
||||
@@ -50,7 +48,6 @@ class Server extends ThreadServer<GameSession, Bytes> {
|
||||
L.i(TAG, 'Build: ${CompilationOption.get("build")}');
|
||||
Provider.setFactory(IConfigBundle, ServerConfigBundle);
|
||||
Provider.setFactory(ILevelBundle, ServerLevelBundle);
|
||||
Provider.setFactory(IControlFactory, ServerControlFactory);
|
||||
gameManager = new GameManager();
|
||||
var host:String = Sys.args().length > 0 ? Sys.args()[0] : "0.0.0.0";
|
||||
var port:Int = Sys.args().length > 1 ? Std.parseInt(Sys.args()[1]) : 5000;
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
package ru.m.tankz.server.control;
|
||||
|
||||
import ru.m.tankz.bot.HardBotControl;
|
||||
import ru.m.tankz.control.BaseControlFactory;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.Type.PlayerId;
|
||||
|
||||
class ServerControlFactory extends BaseControlFactory {
|
||||
|
||||
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||
return new HardBotControl(id); // ToDo:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package ru.m.tankz.server.game;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.server.control.ServerControlFactory;
|
||||
|
||||
class ServerGame extends GameRunner {
|
||||
|
||||
@@ -11,6 +12,7 @@ class ServerGame extends GameRunner {
|
||||
|
||||
public function new(proto:GameProto) {
|
||||
super(new GameState(proto.type, 0, proto.level));
|
||||
this.controlFactory = new ServerControlFactory();
|
||||
this.proto = proto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import haxe.Unserializer;
|
||||
import haxe.Serializer;
|
||||
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
@@ -102,6 +103,11 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
// start
|
||||
} else if (request.hasStartGame()) {
|
||||
gameManager.start(gameId);
|
||||
} else if (request.hasGameEvent()) {
|
||||
if (gameManager.gamesById.exists(gameId)) {
|
||||
var event:GameEvent = Unserializer.run(request.gameEvent.event);
|
||||
gameManager.gamesById[gameId].gameEventSignal.emit(event);
|
||||
}
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
L.e(TAG, '$tag onRequest ', error);
|
||||
|
||||
Reference in New Issue
Block a user