[common] GameRunner extend from Game
This commit is contained in:
25
src/client/haxe/ru/m/tankz/network/NetworkGame.hx
Normal file
25
src/client/haxe/ru/m/tankz/network/NetworkGame.hx
Normal file
@@ -0,0 +1,25 @@
|
||||
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,10 +1,10 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.control.Control;
|
||||
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.JoinGameRequest;
|
||||
|
||||
@@ -4,7 +4,6 @@ import flash.events.KeyboardEvent;
|
||||
import flash.ui.Keyboard;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.IView;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
|
||||
@:template class ClientView extends FrameSwitcher {
|
||||
@@ -16,7 +15,6 @@ import ru.m.tankz.sound.SoundManager;
|
||||
public function init():Void {
|
||||
resources.text.put('version', '${Const.VERSION}');
|
||||
switcher = this;
|
||||
onSwitch.connect(onFrameSwitch);
|
||||
}
|
||||
|
||||
public function launch():Void {
|
||||
@@ -31,10 +29,4 @@ import ru.m.tankz.sound.SoundManager;
|
||||
});
|
||||
change(StartFrame.ID);
|
||||
}
|
||||
|
||||
private function onFrameSwitch(frame:IView<Dynamic>):Void {
|
||||
if (frame.id == StartFrame.ID) {
|
||||
soundManager.stopAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,7 @@ package ru.m.tankz.view;
|
||||
import haxe.ds.Option;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.record.GamePlayer;
|
||||
@@ -15,6 +13,7 @@ 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;
|
||||
|
||||
@:template class GameFrame extends VGroupView implements GameListener {
|
||||
@@ -28,28 +27,16 @@ import ru.m.tankz.view.game.GameView;
|
||||
@:provide var soundManager:SoundManager;
|
||||
@:provide var state:GameState;
|
||||
@:provide var record:GameRecord;
|
||||
@:provide("result") var result:GameState;
|
||||
@:provide("next") var nextState:GameState;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var gameStorage:GameStorage;
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
|
||||
private var game:IGame;
|
||||
private var runner:GameRunner;
|
||||
@:provide var game:IGame;
|
||||
private var recorder:GameRecorder;
|
||||
private var player:GamePlayer;
|
||||
|
||||
public function onShow():Void {
|
||||
if (record != null) {
|
||||
play(record);
|
||||
record = null;
|
||||
} else {
|
||||
start(state);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildGame(state:GameState):Void {
|
||||
gameView.type = state.type;
|
||||
game = new Game(state);
|
||||
gameView.type = game.type;
|
||||
soundManager.config = game.config;
|
||||
gameView.render.config = game.config;
|
||||
game.connect(gameView.render);
|
||||
@@ -57,28 +44,16 @@ import ru.m.tankz.view.game.GameView;
|
||||
if (gameView.panel != null) {
|
||||
game.connect(gameView.panel);
|
||||
}
|
||||
}
|
||||
|
||||
private function start(state:GameState):Void {
|
||||
buildGame(state);
|
||||
// ToDo:
|
||||
if (!Std.is(game, GamePlayer)) {
|
||||
recorder = new GameRecorder();
|
||||
game.connect(recorder);
|
||||
}
|
||||
game.connect(this);
|
||||
recorder = new GameRecorder();
|
||||
game.connect(recorder);
|
||||
runner = new GameRunner(game);
|
||||
runner.start(state);
|
||||
}
|
||||
|
||||
private function play(record:GameRecord):Void {
|
||||
buildGame(record.state);
|
||||
player = new GamePlayer(game, record);
|
||||
player.start();
|
||||
game.start();
|
||||
}
|
||||
|
||||
private function stop():Void {
|
||||
if (runner != null) {
|
||||
runner.dispose();
|
||||
runner = null;
|
||||
}
|
||||
if (game != null) {
|
||||
game.dispose();
|
||||
game = null;
|
||||
@@ -88,19 +63,21 @@ import ru.m.tankz.view.game.GameView;
|
||||
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.COMPLETE(state, _):
|
||||
case GameEvent.COMPLETE(state, winner):
|
||||
// ToDo:
|
||||
recordStorage.save(recorder.record);
|
||||
result = state;
|
||||
this.state = switch runner.next() {
|
||||
if (recorder != null) {
|
||||
recordStorage.save(recorder.record);
|
||||
}
|
||||
this.state = state;
|
||||
nextState = switch next(winner) {
|
||||
case Some(s):
|
||||
// ToDo:
|
||||
var progress = gameStorage.get(game.type);
|
||||
progress.completeLevel(result.levelId, result.presetId);
|
||||
progress.completeLevel(state.levelId, state.presetId);
|
||||
gameStorage.set(progress);
|
||||
s;
|
||||
case None:
|
||||
new GameState(state.type, state.presetId, 0);
|
||||
null;
|
||||
}
|
||||
stop();
|
||||
switcher.change(ResultFrame.ID);
|
||||
@@ -108,8 +85,22 @@ import ru.m.tankz.view.game.GameView;
|
||||
}
|
||||
}
|
||||
|
||||
// ToDo:
|
||||
private function next(winner:TeamId):Option<GameState> {
|
||||
for (rule in game.config.game.complete) {
|
||||
if (rule.team != null && rule.team != winner) {
|
||||
return Option.None;
|
||||
}
|
||||
}
|
||||
var level = state.levelId + 1;
|
||||
if (level >= game.config.game.levels) level = 0;
|
||||
return Option.Some(new GameState(game.type, state.presetId, level, state));
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
stop();
|
||||
soundManager.stopAll();
|
||||
recorder = null;
|
||||
}
|
||||
|
||||
public function close():Void {
|
||||
|
||||
@@ -7,7 +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.storage.GameStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.popup.LevelPopup;
|
||||
@@ -19,6 +21,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
@:view var levels:DataView<LevelId, ButtonView>;
|
||||
|
||||
@:provide var state:GameState;
|
||||
@:provide var game:IGame;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var levelBundle:ILevelBundle;
|
||||
@:provide var storage:GameStorage;
|
||||
@@ -33,6 +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);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@ import haxework.view.DataView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.view.common.LifeView;
|
||||
|
||||
@:template class ResultFrame extends VGroupView {
|
||||
@@ -17,28 +19,32 @@ import ru.m.tankz.view.common.LifeView;
|
||||
|
||||
@:provide var frames:FrameSwitcher;
|
||||
@:provide var state:GameState;
|
||||
@:provide("result") var resultState:GameState;
|
||||
@:provide("next") var nextState:GameState;
|
||||
@:provide var game:IGame;
|
||||
|
||||
private function playerViewFactory(index:Int, player:PlayerState) {
|
||||
var view = new LifeView();
|
||||
var playerConfig = resultState.config.getPlayer(player.id);
|
||||
var playerConfig = state.config.getPlayer(player.id);
|
||||
var tankType = playerConfig.tanks[0].type;
|
||||
var tankConfig = resultState.config.getTank(tankType);
|
||||
var tankConfig = state.config.getTank(tankType);
|
||||
view.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||
view.color = resultState.getPlayerColor(player.id);
|
||||
view.color = state.getPlayerColor(player.id);
|
||||
view.life = player.frags;
|
||||
view.score = player.score;
|
||||
return view;
|
||||
}
|
||||
|
||||
public function onShow() {
|
||||
resultView.data = Lambda.array(resultState.players);
|
||||
levelLabel.text = 'Level ${resultState.levelId}';
|
||||
nextButton.visible = state != null;
|
||||
resultView.data = Lambda.array(state.players);
|
||||
levelLabel.text = 'Level ${state.levelId}';
|
||||
nextButton.visible = nextState != null;
|
||||
}
|
||||
|
||||
private function next() {
|
||||
frames.change(GameFrame.ID);
|
||||
if (nextState != null) {
|
||||
game = new GameRunner(nextState);
|
||||
frames.change(GameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function close() {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.view.common;
|
||||
|
||||
import ru.m.tankz.game.record.GamePlayer;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.LabelView;
|
||||
@@ -18,7 +20,7 @@ import ru.m.tankz.storage.RecordStorage;
|
||||
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var record:GameRecord;
|
||||
@:provide var game:IGame;
|
||||
|
||||
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
||||
if (data != value) {
|
||||
@@ -32,7 +34,8 @@ import ru.m.tankz.storage.RecordStorage;
|
||||
}
|
||||
|
||||
private function play():Void {
|
||||
record = recordStorage.read(data.id);
|
||||
var record = recordStorage.read(data.id);
|
||||
game = new GamePlayer(record);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,38 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
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.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.GameStateProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
@:template class GameRoomFrame extends VGroupView {
|
||||
|
||||
public static inline var ID = "game_room";
|
||||
|
||||
@:view var start:ButtonView;
|
||||
@:view var info:TextView;
|
||||
@:view var players:VListView<UserProto>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
@:provide var game:IGame;
|
||||
|
||||
private function refresh(game:GameProto):Void {
|
||||
if (game != null) {
|
||||
start.visible = game.creator.uuid == network.user.uuid;
|
||||
info.text = '[${game.creator.name}] ${game.type} ${game.level} (${game.players.length})';
|
||||
players.data = game.players;
|
||||
if (game.state == GameStateProto.STARTED) {
|
||||
this.game = new NetworkGame(network);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
} else {
|
||||
Timer.delay(function() switcher.change(GameListFrame.ID), 1);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,12 @@ views:
|
||||
geometry.margin.bottom: 20
|
||||
skinId: text.header
|
||||
text: Game Room
|
||||
- id: start
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.simple
|
||||
text: Start
|
||||
+onPress: $code:network.startGame()
|
||||
visible: false
|
||||
- id: info
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
|
||||
Reference in New Issue
Block a user