Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 01417f5bbd | |||
| abf944418e | |||
| c7946ef1da | |||
| 241499bc2d | |||
| 74297dd9c7 | |||
| 31cb20bf85 | |||
| 1d95de02e1 | |||
| ba13111a8e |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tankz",
|
"name": "tankz",
|
||||||
"version": "0.12.0",
|
"version": "0.13.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"dateformat": "^3.0.3",
|
"dateformat": "^3.0.3",
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import ru.m.tankz.bundle.ConfigBundle;
|
|||||||
import ru.m.tankz.bundle.IConfigBundle;
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
import ru.m.tankz.bundle.ILevelBundle;
|
import ru.m.tankz.bundle.ILevelBundle;
|
||||||
import ru.m.tankz.bundle.LevelBundle;
|
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.network.NetworkManager;
|
||||||
import ru.m.tankz.proto.pack.Request;
|
import ru.m.tankz.proto.pack.Request;
|
||||||
import ru.m.tankz.proto.pack.Response;
|
import ru.m.tankz.proto.pack.Response;
|
||||||
@@ -32,7 +30,6 @@ class Init {
|
|||||||
@:provide static var recordStorage:RecordStorage;
|
@:provide static var recordStorage:RecordStorage;
|
||||||
@:provide static var soundManager:SoundManager;
|
@:provide static var soundManager:SoundManager;
|
||||||
@:provide static var networkManager:NetworkManager;
|
@:provide static var networkManager:NetworkManager;
|
||||||
@:provide static var controlFactory:IControlFactory;
|
|
||||||
@:provide static var popupManager:PopupManager;
|
@:provide static var popupManager:PopupManager;
|
||||||
@:provide static var connection:IConnection<Request, Response>;
|
@:provide static var connection:IConnection<Request, Response>;
|
||||||
|
|
||||||
@@ -58,7 +55,6 @@ class Init {
|
|||||||
gameStorage = new GameStorage();
|
gameStorage = new GameStorage();
|
||||||
recordStorage = new RecordStorage();
|
recordStorage = new RecordStorage();
|
||||||
soundManager = new SoundManager();
|
soundManager = new SoundManager();
|
||||||
controlFactory = new ClientControlFactory();
|
|
||||||
popupManager = new PopupManager();
|
popupManager = new PopupManager();
|
||||||
|
|
||||||
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class ConfigBundle implements IConfigBundle {
|
|||||||
public function get(type:GameType):Config {
|
public function get(type:GameType):Config {
|
||||||
if (!_cache.exists(type)) {
|
if (!_cache.exists(type)) {
|
||||||
var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects());
|
var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects());
|
||||||
_cache.set(type, new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses));
|
_cache.set(type, Config.fromSource(type, source));
|
||||||
}
|
}
|
||||||
return _cache.get(type);
|
return _cache.get(type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package ru.m.tankz.control;
|
|
||||||
|
|
||||||
import ru.m.tankz.bot.StupidBotControl;
|
|
||||||
import ru.m.tankz.bot.HardBotControl;
|
|
||||||
import ru.m.tankz.Type;
|
|
||||||
|
|
||||||
class ClientControlFactory implements IControlFactory {
|
|
||||||
|
|
||||||
public function new() {}
|
|
||||||
|
|
||||||
public function build(id:PlayerId, controller:Controller):Control {
|
|
||||||
return switch controller {
|
|
||||||
case HUMAN(index): new HumanControl(id, index);
|
|
||||||
case BOT(type): switch type {
|
|
||||||
case StupidBotControl.BOT_TYPE: new StupidBotControl(id);
|
|
||||||
case HardBotControl.BOT_TYPE: new HardBotControl(id);
|
|
||||||
case _: null;
|
|
||||||
}
|
|
||||||
case NONE: null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
10
src/client/haxe/ru/m/tankz/control/LocalControlFactory.hx
Normal file
10
src/client/haxe/ru/m/tankz/control/LocalControlFactory.hx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
class LocalControlFactory extends BaseControlFactory {
|
||||||
|
|
||||||
|
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return new HumanControl(id, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@ package ru.m.tankz.control;
|
|||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
|
|
||||||
class ClientNetworkControl extends HumanControl {
|
class NetworkControl extends HumanControl {
|
||||||
|
|
||||||
@:provide private var network:NetworkManager;
|
@:provide private var network:NetworkManager;
|
||||||
|
|
||||||
override public function action(action:TankAction):Void {
|
override public function action(action:TankAction):Void {
|
||||||
network.action(action);
|
if (tankId > -1) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
29
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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);
|
||||||
|
recorder.onGameEvent(event); //ToDo:
|
||||||
|
recordStorage.save(recorder.record);
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
41
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
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 time = game.time;
|
||||||
|
var eventStr = game.event;
|
||||||
|
var event:GameEvent = Unserializer.run(eventStr);
|
||||||
|
gameEventSignal.emit(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function start():Void {
|
||||||
|
var player = Lambda.find(network.game.players, function(player) return player.user.uuid == network.user.uuid);
|
||||||
|
if (player != null) {
|
||||||
|
state.controls.push({playerId: [player.team, player.index], control: "human-0"});
|
||||||
|
}
|
||||||
|
super.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function dispose():Void {
|
||||||
|
super.dispose();
|
||||||
|
network.gameEventSignal.disconnect(onGameEventProto);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
package ru.m.tankz.network;
|
package ru.m.tankz.network;
|
||||||
|
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
import haxe.Serializer;
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.proto.core.GameProto;
|
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.game.GameChangeProto;
|
||||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
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.JoinGameRequest;
|
||||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||||
import ru.m.tankz.proto.pack.ListGameRequest;
|
import ru.m.tankz.proto.pack.ListGameRequest;
|
||||||
@@ -38,6 +42,7 @@ class NetworkManager {
|
|||||||
public var listGameSignal:Signal<Array<GameProto>>;
|
public var listGameSignal:Signal<Array<GameProto>>;
|
||||||
public var gameSignal:Signal<GameProto>;
|
public var gameSignal:Signal<GameProto>;
|
||||||
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
||||||
|
public var gameEventSignal:Signal<GameEventResponse>;
|
||||||
|
|
||||||
@:provide private var connection:ClientConnection;
|
@:provide private var connection:ClientConnection;
|
||||||
@:provide private var storage:MultiplayerStorage;
|
@:provide private var storage:MultiplayerStorage;
|
||||||
@@ -47,6 +52,7 @@ class NetworkManager {
|
|||||||
listGameSignal = new Signal();
|
listGameSignal = new Signal();
|
||||||
gameSignal = new Signal();
|
gameSignal = new Signal();
|
||||||
gameUpdateSignal = new Signal();
|
gameUpdateSignal = new Signal();
|
||||||
|
gameEventSignal = new Signal();
|
||||||
updateState(OFFLINE);
|
updateState(OFFLINE);
|
||||||
connection.handler.connect(onConnectionEvent);
|
connection.handler.connect(onConnectionEvent);
|
||||||
connection.receiveHandler.connect(onResponse);
|
connection.receiveHandler.connect(onResponse);
|
||||||
@@ -99,8 +105,10 @@ class NetworkManager {
|
|||||||
connection.send(new Request().setStartGame(new StartGameRequest()));
|
connection.send(new Request().setStartGame(new StartGameRequest()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function action(action:TankAction):Void {
|
public function action(tankId:Int, action:TankAction):Void {
|
||||||
// ToDo: network
|
connection.send(new Request().setGameEvent(new GameEventRequest().setTime(0).setEvent(
|
||||||
|
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||||
@@ -148,6 +156,8 @@ class NetworkManager {
|
|||||||
} else if (packet.hasStartGame()) {
|
} else if (packet.hasStartGame()) {
|
||||||
game = packet.startGame.game;
|
game = packet.startGame.game;
|
||||||
gameSignal.emit(game);
|
gameSignal.emit(game);
|
||||||
|
} else if (packet.hasGameEvent()) {
|
||||||
|
gameEventSignal.emit(packet.gameEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ class Render extends SpriteView implements IRender {
|
|||||||
case SPAWN(TANK(id, rect, playerId, info)):
|
case SPAWN(TANK(id, rect, playerId, info)):
|
||||||
var item = new TankRenderItem(rect);
|
var item = new TankRenderItem(rect);
|
||||||
var tankConfig = config.getTank(info.type);
|
var tankConfig = config.getTank(info.type);
|
||||||
item.color = config.getColor(playerId);
|
item.color = info.color;
|
||||||
item.skin = tankConfig.skin;
|
item.skin = tankConfig.skin;
|
||||||
item.hits = info.hits;
|
item.hits = info.hits;
|
||||||
item.bonus = info.bonus;
|
item.bonus = info.bonus;
|
||||||
@@ -224,6 +224,9 @@ class Render extends SpriteView implements IRender {
|
|||||||
item.destroy();
|
item.destroy();
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
|
#if cpp
|
||||||
|
flash.Lib.current.stage.invalidate();
|
||||||
|
#end
|
||||||
}
|
}
|
||||||
|
|
||||||
private function playAnimate(point:Point, animate:OnceAnimate):Promise<Dynamic> {
|
private function playAnimate(point:Point, animate:OnceAnimate):Promise<Dynamic> {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import flash.events.KeyboardEvent;
|
|||||||
import flash.ui.Keyboard;
|
import flash.ui.Keyboard;
|
||||||
import haxework.resources.IResources;
|
import haxework.resources.IResources;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.IView;
|
|
||||||
import ru.m.tankz.sound.SoundManager;
|
import ru.m.tankz.sound.SoundManager;
|
||||||
|
|
||||||
@:template class ClientView extends FrameSwitcher {
|
@:template class ClientView extends FrameSwitcher {
|
||||||
@@ -16,7 +15,6 @@ import ru.m.tankz.sound.SoundManager;
|
|||||||
public function init():Void {
|
public function init():Void {
|
||||||
resources.text.put('version', '${Const.VERSION}');
|
resources.text.put('version', '${Const.VERSION}');
|
||||||
switcher = this;
|
switcher = this;
|
||||||
onSwitch.connect(onFrameSwitch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function launch():Void {
|
public function launch():Void {
|
||||||
@@ -31,10 +29,4 @@ import ru.m.tankz.sound.SoundManager;
|
|||||||
});
|
});
|
||||||
change(StartFrame.ID);
|
change(StartFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onFrameSwitch(frame:IView<Dynamic>):Void {
|
|
||||||
if (frame.id == StartFrame.ID) {
|
|
||||||
soundManager.stopAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,14 @@ package ru.m.tankz.view;
|
|||||||
import haxe.ds.Option;
|
import haxe.ds.Option;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
import ru.m.tankz.game.Game;
|
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.GameRunner;
|
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
import ru.m.tankz.game.IGame;
|
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.GameRecord;
|
||||||
import ru.m.tankz.game.record.GameRecorder;
|
|
||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.sound.SoundManager;
|
import ru.m.tankz.sound.SoundManager;
|
||||||
import ru.m.tankz.storage.GameStorage;
|
import ru.m.tankz.storage.GameStorage;
|
||||||
import ru.m.tankz.storage.RecordStorage;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.tankz.view.game.GameView;
|
import ru.m.tankz.view.game.GameView;
|
||||||
|
|
||||||
@:template class GameFrame extends VGroupView implements GameListener {
|
@:template class GameFrame extends VGroupView implements GameListener {
|
||||||
@@ -28,28 +24,14 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
@:provide var soundManager:SoundManager;
|
@:provide var soundManager:SoundManager;
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
@:provide var record:GameRecord;
|
@:provide var record:GameRecord;
|
||||||
@:provide("result") var result:GameState;
|
@:provide("next") var nextState:GameState;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var gameStorage:GameStorage;
|
@:provide var gameStorage:GameStorage;
|
||||||
@:provide var recordStorage:RecordStorage;
|
|
||||||
|
|
||||||
private var game:IGame;
|
@:provide var game:IGame;
|
||||||
private var runner:GameRunner;
|
|
||||||
private var recorder:GameRecorder;
|
|
||||||
private var player:GamePlayer;
|
|
||||||
|
|
||||||
public function onShow():Void {
|
public function onShow():Void {
|
||||||
if (record != null) {
|
gameView.type = game.type;
|
||||||
play(record);
|
|
||||||
record = null;
|
|
||||||
} else {
|
|
||||||
start(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function buildGame(state:GameState):Void {
|
|
||||||
gameView.type = state.type;
|
|
||||||
game = new Game(state);
|
|
||||||
soundManager.config = game.config;
|
soundManager.config = game.config;
|
||||||
gameView.render.config = game.config;
|
gameView.render.config = game.config;
|
||||||
game.connect(gameView.render);
|
game.connect(gameView.render);
|
||||||
@@ -57,28 +39,11 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
if (gameView.panel != null) {
|
if (gameView.panel != null) {
|
||||||
game.connect(gameView.panel);
|
game.connect(gameView.panel);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private function start(state:GameState):Void {
|
|
||||||
buildGame(state);
|
|
||||||
game.connect(this);
|
game.connect(this);
|
||||||
recorder = new GameRecorder();
|
game.start();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function stop():Void {
|
private function stop():Void {
|
||||||
if (runner != null) {
|
|
||||||
runner.dispose();
|
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
if (game != null) {
|
if (game != null) {
|
||||||
game.dispose();
|
game.dispose();
|
||||||
game = null;
|
game = null;
|
||||||
@@ -88,19 +53,17 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.COMPLETE(state, _):
|
case GameEvent.COMPLETE(state, winner):
|
||||||
// ToDo:
|
this.state = state;
|
||||||
recordStorage.save(recorder.record);
|
nextState = switch next(winner) {
|
||||||
result = state;
|
|
||||||
this.state = switch runner.next() {
|
|
||||||
case Some(s):
|
case Some(s):
|
||||||
// ToDo:
|
// ToDo:
|
||||||
var progress = gameStorage.get(game.type);
|
var progress = gameStorage.get(game.type);
|
||||||
progress.completeLevel(result.levelId, result.presetId);
|
progress.completeLevel(state.levelId, state.presetId);
|
||||||
gameStorage.set(progress);
|
gameStorage.set(progress);
|
||||||
s;
|
s;
|
||||||
case None:
|
case None:
|
||||||
new GameState(state.type, state.presetId, 0);
|
null;
|
||||||
}
|
}
|
||||||
stop();
|
stop();
|
||||||
switcher.change(ResultFrame.ID);
|
switcher.change(ResultFrame.ID);
|
||||||
@@ -108,8 +71,21 @@ 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 {
|
public function onHide():Void {
|
||||||
stop();
|
stop();
|
||||||
|
soundManager.stopAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function close():Void {
|
public function close():Void {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import haxework.view.VGroupView;
|
|||||||
import ru.m.tankz.bundle.ILevelBundle;
|
import ru.m.tankz.bundle.ILevelBundle;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameState;
|
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.storage.GameStorage;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.tankz.view.popup.LevelPopup;
|
import ru.m.tankz.view.popup.LevelPopup;
|
||||||
@@ -19,6 +21,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
@:view var levels:DataView<LevelId, ButtonView>;
|
@:view var levels:DataView<LevelId, ButtonView>;
|
||||||
|
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
|
@:provide var game:IGame;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var levelBundle:ILevelBundle;
|
@:provide var levelBundle:ILevelBundle;
|
||||||
@:provide var storage:GameStorage;
|
@:provide var storage:GameStorage;
|
||||||
@@ -30,9 +33,11 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
levels.data = [for (i in 0...state.config.game.levels) i];
|
levels.data = [for (i in 0...state.config.game.levels) i];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function start(level:LevelConfig, preset:GamePreset):Void {
|
private function start(level:LevelConfig, preset:GamePreset, control:ControlPreset):Void {
|
||||||
state.levelId = level.id;
|
state.levelId = level.id;
|
||||||
state.presetId = preset.id;
|
state.presetId = preset.id;
|
||||||
|
state.controls = control.values;
|
||||||
|
game = new LocalGame(state);
|
||||||
switcher.change(GameFrame.ID);
|
switcher.change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +62,9 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
levelPopup.setData(
|
levelPopup.setData(
|
||||||
level,
|
level,
|
||||||
state.config.presets,
|
state.config.presets,
|
||||||
|
state.config.controls,
|
||||||
storage.get(state.type)
|
storage.get(state.type)
|
||||||
);
|
);
|
||||||
levelPopup.show().then(function(preset) preset != null ? start(level, preset) : {});
|
levelPopup.show().then(function(result) result != null ? start(level, result.preset, result.control) : {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import haxework.view.DataView;
|
|||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
|
import ru.m.tankz.game.GameRunner;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.view.common.LifeView;
|
import ru.m.tankz.view.common.LifeView;
|
||||||
|
|
||||||
@:template class ResultFrame extends VGroupView {
|
@:template class ResultFrame extends VGroupView {
|
||||||
@@ -17,28 +19,32 @@ import ru.m.tankz.view.common.LifeView;
|
|||||||
|
|
||||||
@:provide var frames:FrameSwitcher;
|
@:provide var frames:FrameSwitcher;
|
||||||
@:provide var state:GameState;
|
@: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) {
|
private function playerViewFactory(index:Int, player:PlayerState) {
|
||||||
var view = new LifeView();
|
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 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.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||||
view.color = resultState.getPlayerColor(player.id);
|
view.color = state.getPlayerColor(player.id);
|
||||||
view.life = player.frags;
|
view.life = player.frags;
|
||||||
view.score = player.score;
|
view.score = player.score;
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onShow() {
|
public function onShow() {
|
||||||
resultView.data = Lambda.array(resultState.players);
|
resultView.data = Lambda.array(state.players);
|
||||||
levelLabel.text = 'Level ${resultState.levelId}';
|
levelLabel.text = 'Level ${state.levelId}';
|
||||||
nextButton.visible = state != null;
|
nextButton.visible = nextState != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function next() {
|
private function next() {
|
||||||
frames.change(GameFrame.ID);
|
if (nextState != null) {
|
||||||
|
game = new GameRunner(nextState);
|
||||||
|
frames.change(GameFrame.ID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function close() {
|
private function close() {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.view.common;
|
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.frame.FrameSwitcher;
|
||||||
import haxework.view.HGroupView;
|
import haxework.view.HGroupView;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
@@ -18,7 +20,7 @@ import ru.m.tankz.storage.RecordStorage;
|
|||||||
|
|
||||||
@:provide var recordStorage:RecordStorage;
|
@:provide var recordStorage:RecordStorage;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var record:GameRecord;
|
@:provide var game:IGame;
|
||||||
|
|
||||||
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
||||||
if (data != value) {
|
if (data != value) {
|
||||||
@@ -32,7 +34,8 @@ import ru.m.tankz.storage.RecordStorage;
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function play():Void {
|
private function play():Void {
|
||||||
record = recordStorage.read(data.id);
|
var record = recordStorage.read(data.id);
|
||||||
|
game = new GamePlayer(record);
|
||||||
switcher.change(GameFrame.ID);
|
switcher.change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,38 @@
|
|||||||
package ru.m.tankz.view.network;
|
package ru.m.tankz.view.network;
|
||||||
|
|
||||||
|
import haxework.view.ButtonView;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.list.VListView;
|
import haxework.view.list.VListView;
|
||||||
import haxework.view.TextView;
|
import haxework.view.TextView;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
|
import ru.m.tankz.game.NetworkGame;
|
||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.proto.core.GameProto;
|
import ru.m.tankz.proto.core.GameProto;
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
import ru.m.tankz.proto.core.GameStateProto;
|
||||||
|
import ru.m.tankz.proto.core.PlayerProto;
|
||||||
|
|
||||||
@:template class GameRoomFrame extends VGroupView {
|
@:template class GameRoomFrame extends VGroupView {
|
||||||
|
|
||||||
public static inline var ID = "game_room";
|
public static inline var ID = "game_room";
|
||||||
|
|
||||||
|
@:view var start:ButtonView;
|
||||||
@:view var info:TextView;
|
@:view var info:TextView;
|
||||||
@:view var players:VListView<UserProto>;
|
@:view var players:VListView<PlayerProto>;
|
||||||
|
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var network:NetworkManager;
|
@:provide var network:NetworkManager;
|
||||||
|
@:provide var game:IGame;
|
||||||
|
|
||||||
private function refresh(game:GameProto):Void {
|
private function refresh(game:GameProto):Void {
|
||||||
if (game != null) {
|
if (game != null) {
|
||||||
|
start.visible = game.creator.uuid == network.user.uuid;
|
||||||
info.text = '[${game.creator.name}] ${game.type} ${game.level} (${game.players.length})';
|
info.text = '[${game.creator.name}] ${game.type} ${game.level} (${game.players.length})';
|
||||||
players.data = game.players;
|
players.data = game.players;
|
||||||
|
if (game.state == GameStateProto.STARTED) {
|
||||||
|
this.game = new NetworkGame(network);
|
||||||
|
switcher.change(GameFrame.ID);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Timer.delay(function() switcher.change(GameListFrame.ID), 1);
|
Timer.delay(function() switcher.change(GameListFrame.ID), 1);
|
||||||
}
|
}
|
||||||
@@ -36,7 +47,8 @@ import ru.m.tankz.proto.core.UserProto;
|
|||||||
public function onHide():Void {
|
public function onHide():Void {
|
||||||
network.gameSignal.disconnect(onGame);
|
network.gameSignal.disconnect(onGame);
|
||||||
network.stateSignal.disconnect(onState);
|
network.stateSignal.disconnect(onState);
|
||||||
network.leaveGame();
|
// ToDo:
|
||||||
|
//network.leaveGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onGame(game:GameProto):Void {
|
private function onGame(game:GameProto):Void {
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ views:
|
|||||||
geometry.margin.bottom: 20
|
geometry.margin.bottom: 20
|
||||||
skinId: text.header
|
skinId: text.header
|
||||||
text: Game Room
|
text: Game Room
|
||||||
|
- id: start
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.simple
|
||||||
|
text: Start
|
||||||
|
+onPress: $code:network.startGame()
|
||||||
|
visible: false
|
||||||
- id: info
|
- id: info
|
||||||
$type: haxework.view.LabelView
|
$type: haxework.view.LabelView
|
||||||
geometry.size.width: 100%
|
geometry.size.width: 100%
|
||||||
|
|||||||
@@ -3,18 +3,18 @@ package ru.m.tankz.view.network;
|
|||||||
import haxework.view.HGroupView;
|
import haxework.view.HGroupView;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
import haxework.view.list.ListView;
|
import haxework.view.list.ListView;
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
import ru.m.tankz.proto.core.PlayerProto;
|
||||||
|
|
||||||
@:template class PlayerItemView extends HGroupView implements IListItemView<UserProto> {
|
@:template class PlayerItemView extends HGroupView implements IListItemView<PlayerProto> {
|
||||||
|
|
||||||
public var item_index(default, default):Int;
|
public var item_index(default, default):Int;
|
||||||
public var data(default, set):UserProto;
|
public var data(default, set):PlayerProto;
|
||||||
|
|
||||||
@:view var label:LabelView;
|
@:view var label:LabelView;
|
||||||
|
|
||||||
private function set_data(value:UserProto):UserProto {
|
private function set_data(value:PlayerProto):PlayerProto {
|
||||||
data = value;
|
data = value;
|
||||||
label.text = '${value.name}';
|
label.text = '${value.user.name}';
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.view.popup;
|
package ru.m.tankz.view.popup;
|
||||||
|
|
||||||
|
import haxework.view.ToggleButtonView;
|
||||||
import haxework.view.ButtonView;
|
import haxework.view.ButtonView;
|
||||||
import haxework.view.DataView;
|
import haxework.view.DataView;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
@@ -7,19 +8,28 @@ import haxework.view.popup.PopupView;
|
|||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameProgress;
|
import ru.m.tankz.game.GameProgress;
|
||||||
|
|
||||||
@:template class LevelPopup extends PopupView<GamePreset> {
|
typedef Result = {
|
||||||
|
var control:ControlPreset;
|
||||||
|
var preset:GamePreset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:template class LevelPopup extends PopupView<Result> {
|
||||||
|
|
||||||
private var level:LevelConfig;
|
private var level:LevelConfig;
|
||||||
private var progress:GameProgress;
|
private var progress:GameProgress;
|
||||||
|
|
||||||
@:view var name:LabelView;
|
@:view var name:LabelView;
|
||||||
@:view("presets") var presetsView:DataView<GamePreset, ButtonView>;
|
@:view("presets") var presetsView:DataView<GamePreset, ButtonView>;
|
||||||
|
@:view("controls") var controlsView:DataView<ControlPreset, ToggleButtonView>;
|
||||||
|
private var control:ControlPreset;
|
||||||
|
|
||||||
public function setData(level:LevelConfig, presets:Array<GamePreset>, progress:GameProgress):Void {
|
public function setData(level:LevelConfig, presets:Array<GamePreset>, controls:Array<ControlPreset>, progress:GameProgress):Void {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.progress = progress;
|
this.progress = progress;
|
||||||
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
||||||
presetsView.data = presets;
|
presetsView.data = presets;
|
||||||
|
control = controls[0];
|
||||||
|
controlsView.data = controls;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function presetViewFactory(index:Int, value:GamePreset):ButtonView {
|
private function presetViewFactory(index:Int, value:GamePreset):ButtonView {
|
||||||
@@ -32,7 +42,22 @@ import ru.m.tankz.game.GameProgress;
|
|||||||
|
|
||||||
private function onPresetSelect(value:GamePreset):Void {
|
private function onPresetSelect(value:GamePreset):Void {
|
||||||
if (progress.isPresetAvailable(level.id, value.id)) {
|
if (progress.isPresetAvailable(level.id, value.id)) {
|
||||||
close(value);
|
close({control: control, preset: value});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function controlViewFactory(index:Int, value:ControlPreset):ToggleButtonView {
|
||||||
|
var result = new ToggleButtonView();
|
||||||
|
result.skinId = 'button.simple.active';
|
||||||
|
result.on = control == value;
|
||||||
|
result.text = value.name;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onControlSelect(index:Int, value:ControlPreset, view:ToggleButtonView):Void {
|
||||||
|
control = value;
|
||||||
|
for (v in controlsView.dataViews) {
|
||||||
|
v.on = v == view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,15 @@ view:
|
|||||||
+onPress: $code:reject('close')
|
+onPress: $code:reject('close')
|
||||||
- $type: haxework.view.SpriteView
|
- $type: haxework.view.SpriteView
|
||||||
geometry.size.height: 100%
|
geometry.size.height: 100%
|
||||||
|
- id: controls
|
||||||
|
$type: haxework.view.DataView
|
||||||
|
factory: $this:controlViewFactory
|
||||||
|
+onItemSelect: $this:onControlSelect
|
||||||
|
layout:
|
||||||
|
$type: haxework.view.layout.HorizontalLayout
|
||||||
|
hAlign: center
|
||||||
|
margin: 5
|
||||||
|
skinId: panel
|
||||||
- id: presets
|
- id: presets
|
||||||
$type: haxework.view.DataView
|
$type: haxework.view.DataView
|
||||||
factory: $this:presetViewFactory
|
factory: $this:presetViewFactory
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class Timer {
|
|||||||
try {
|
try {
|
||||||
run();
|
run();
|
||||||
} catch (error:Dynamic) {
|
} catch (error:Dynamic) {
|
||||||
trace(error);
|
trace(haxework.log.BaseLogger.LoggerUtil.printError(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,21 +1,22 @@
|
|||||||
package ru.m.connect;
|
package ru.m.connect;
|
||||||
|
|
||||||
|
import Reflect;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import haxe.io.BytesBuffer;
|
import haxe.io.BytesBuffer;
|
||||||
import haxe.io.BytesInput;
|
import haxe.io.BytesInput;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
|
|
||||||
class PacketQueue<P:Message> {
|
class PacketQueue<P:Message> {
|
||||||
|
|
||||||
public var packetClass(default, null):Class<P>;
|
public var packetClass(default, null):Class<P>;
|
||||||
|
|
||||||
private var bytesBuff:Bytes;
|
private var buffer:BytesBuffer;
|
||||||
private var msgs:List<P>;
|
private var msgs:List<P>;
|
||||||
|
|
||||||
public function new(packetClass:Class<P>) {
|
public function new(packetClass:Class<P>) {
|
||||||
this.packetClass = packetClass;
|
this.packetClass = packetClass;
|
||||||
msgs = new List<P>();
|
msgs = new List<P>();
|
||||||
|
buffer = new BytesBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public inline function hasMsg():Bool {
|
public inline function hasMsg():Bool {
|
||||||
@@ -30,47 +31,41 @@ class PacketQueue<P:Message> {
|
|||||||
msgs.add(msg);
|
msgs.add(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addBytes(bytes:Bytes):Void {
|
private function readPackage():Null<P> {
|
||||||
if (bytes == null) {
|
var bytes = buffer.getBytes();
|
||||||
return;
|
var input = new BytesInput(bytes);
|
||||||
}
|
input.bigEndian = false;
|
||||||
if (bytesBuff == null) {
|
if (input.length > 1) {
|
||||||
bytesBuff = bytes;
|
var packetSize = input.readUInt16();
|
||||||
} else {
|
if (input.length >= packetSize + 2) {
|
||||||
var buffer = new BytesBuffer();
|
|
||||||
buffer.add(bytesBuff);
|
|
||||||
buffer.add(bytes);
|
|
||||||
bytesBuff = buffer.getBytes();
|
|
||||||
}
|
|
||||||
if (bytesBuff == null || bytesBuff.length < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var available = bytesBuff.length;
|
|
||||||
var bi = new BytesInput(bytesBuff);
|
|
||||||
bi.bigEndian = false;
|
|
||||||
while (available > 0) {
|
|
||||||
var packetSize = bi.readUInt16();
|
|
||||||
available -= 2;
|
|
||||||
if (packetSize <= available) {
|
|
||||||
var msgBytes = bi.read(packetSize);
|
|
||||||
var packet:P = Type.createInstance(packetClass, []);
|
var packet:P = Type.createInstance(packetClass, []);
|
||||||
packet.mergeFrom(msgBytes);
|
try {
|
||||||
addMsg(packet);
|
packet.mergeFrom(input.read(packetSize));
|
||||||
available -= packetSize;
|
buffer = new BytesBuffer();
|
||||||
} else {
|
buffer.add(input.read(input.length - (packetSize + 2)));
|
||||||
break;
|
return packet;
|
||||||
|
} catch (error:Dynamic) {
|
||||||
|
L.w("PacketQueue", "readPackage ", error);
|
||||||
|
buffer = new BytesBuffer();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (available == 0) {
|
buffer = new BytesBuffer();
|
||||||
bytesBuff = null;
|
buffer.add(bytes);
|
||||||
} else if (available > 0) {
|
return null;
|
||||||
if (bytesBuff.length != available) {
|
}
|
||||||
var pos = bytesBuff.length - available;
|
|
||||||
bytesBuff = bytesBuff.sub(pos, available);
|
public function addBytes(bytes:Bytes):Void {
|
||||||
}
|
buffer.add(bytes);
|
||||||
} else {
|
var packet = readPackage();
|
||||||
throw "Wrong available: " + available;
|
while (packet != null) {
|
||||||
|
msgs.add(packet);
|
||||||
|
packet = readPackage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public function clean():Void {
|
||||||
|
buffer = new BytesBuffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -74,14 +74,10 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function onSocketData(_):Void {
|
private function onSocketData(_):Void {
|
||||||
try {
|
var data = new flash.utils.ByteArray();
|
||||||
var data = new flash.utils.ByteArray();
|
socket.readBytes(data);
|
||||||
socket.readBytes(data);
|
var bytes = Bytes.ofData(data);
|
||||||
var bytes = Bytes.ofData(data);
|
pushData(bytes);
|
||||||
pushData(bytes);
|
|
||||||
} catch (error:Dynamic) {
|
|
||||||
handler.emit(ConnectionEvent.ERROR(error));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _send(packet:O):Void {
|
private function _send(packet:O):Void {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
this.port = port;
|
this.port = port;
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isSecured():Bool {
|
public static function isSecured():Bool {
|
||||||
return Browser.location.protocol == "https:";
|
return Browser.location.protocol == "https:";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
@@ -12,7 +12,7 @@ class BotControl extends Control {
|
|||||||
private var tank(get, null):Tank;
|
private var tank(get, null):Tank;
|
||||||
|
|
||||||
private inline function get_tank():Tank {
|
private inline function get_tank():Tank {
|
||||||
return handler == null ? null : handler.engine.getEntity(tankId);
|
return handler == null || tankId == -1 ? null : engine.getEntity(tankId);
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function stop():Void {
|
override public function stop():Void {
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import ru.m.geom.Direction;
|
|||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
class BotHelper {
|
class BotHelper {
|
||||||
|
|
||||||
public static function findEagle(team:TeamId, handler:IGame):Null<Eagle> {
|
public static function findEagle(team:TeamId, engine:IEngine):Null<Eagle> {
|
||||||
for (entity in handler.engine.entities) {
|
for (entity in engine.entities) {
|
||||||
switch (EntityTypeResolver.of(entity)) {
|
switch (EntityTypeResolver.of(entity)) {
|
||||||
case EntityType.EAGLE(eagle):
|
case EntityType.EAGLE(eagle):
|
||||||
if (eagle.team != team) {
|
if (eagle.team != team) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
@@ -56,7 +56,7 @@ class HardBotControl extends BotControl {
|
|||||||
}
|
}
|
||||||
var enemy:Tank = null;
|
var enemy:Tank = null;
|
||||||
var distance:Float = Math.POSITIVE_INFINITY;
|
var distance:Float = Math.POSITIVE_INFINITY;
|
||||||
for (entity in handler.engine.entities.iterator()) {
|
for (entity in engine.entities.iterator()) {
|
||||||
switch EntityTypeResolver.of(entity) {
|
switch EntityTypeResolver.of(entity) {
|
||||||
case TANK(t):
|
case TANK(t):
|
||||||
if (t.playerId.team != tank.playerId.team) {
|
if (t.playerId.team != tank.playerId.team) {
|
||||||
@@ -82,7 +82,7 @@ class HardBotControl extends BotControl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function calcTurn():Void {
|
private function calcTurn():Void {
|
||||||
var eagle:Eagle = BotHelper.findEagle(playerId.team, handler);
|
var eagle:Eagle = BotHelper.findEagle(playerId.team, engine);
|
||||||
if (eagle != null && Math.random() > 0.5) {
|
if (eagle != null && Math.random() > 0.5) {
|
||||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
@@ -45,7 +45,7 @@ class StupidBotControl extends BotControl {
|
|||||||
|
|
||||||
private function calcTurn():Void {
|
private function calcTurn():Void {
|
||||||
if (handler == null || tank == null) return;
|
if (handler == null || tank == null) return;
|
||||||
var eagle:Eagle = BotHelper.findEagle(playerId.team, handler);
|
var eagle:Eagle = BotHelper.findEagle(playerId.team, engine);
|
||||||
if (eagle != null && Math.random() > 0.5) {
|
if (eagle != null && Math.random() > 0.5) {
|
||||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,17 +3,6 @@ package ru.m.tankz.bundle;
|
|||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
|
||||||
typedef ConfigSource = {
|
|
||||||
var game:GameConfig;
|
|
||||||
var map: MapConfig;
|
|
||||||
var bricks: Array<BrickConfig>;
|
|
||||||
var presets: Array<GamePreset>;
|
|
||||||
var points: Array<SpawnPoint>;
|
|
||||||
var tanks: Array<TankConfig>;
|
|
||||||
var bonuses: Array<BonusConfig>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IConfigBundle {
|
interface IConfigBundle {
|
||||||
public function get(type:GameType):Config;
|
public function get(type:GameType):Config;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ typedef TankConfig = {
|
|||||||
var speed:Float;
|
var speed:Float;
|
||||||
var bullet:BulletConfig;
|
var bullet:BulletConfig;
|
||||||
var bullets:Int;
|
var bullets:Int;
|
||||||
var hits:Int;
|
|
||||||
var skin:String;
|
var skin:String;
|
||||||
|
@:optinal var hits:Int;
|
||||||
@:optinal var upgrade:TankType;
|
@:optinal var upgrade:TankType;
|
||||||
@:optinal var downgrade:TankType;
|
@:optinal var downgrade:TankType;
|
||||||
@:optinal var score:Int;
|
@:optinal var score:Int;
|
||||||
@@ -105,6 +105,29 @@ typedef LevelConfig = {
|
|||||||
@:optional var points:Array<SpawnPoint>;
|
@:optional var points:Array<SpawnPoint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef PlayerControl = {
|
||||||
|
var playerId:PlayerId;
|
||||||
|
var control:String;
|
||||||
|
@:optional var color:Color;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef ControlPreset = {
|
||||||
|
var id:Int;
|
||||||
|
var name:String;
|
||||||
|
var values:Array<PlayerControl>;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ConfigSource = {
|
||||||
|
var game:GameConfig;
|
||||||
|
var map: MapConfig;
|
||||||
|
var bricks: Array<BrickConfig>;
|
||||||
|
var presets: Array<GamePreset>;
|
||||||
|
var controls: Array<ControlPreset>;
|
||||||
|
var points: Array<SpawnPoint>;
|
||||||
|
var tanks: Array<TankConfig>;
|
||||||
|
var bonuses: Array<BonusConfig>;
|
||||||
|
}
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public var type(default, null):String;
|
public var type(default, null):String;
|
||||||
public var game(default, null):GameConfig;
|
public var game(default, null):GameConfig;
|
||||||
@@ -112,6 +135,7 @@ class Config {
|
|||||||
public var bricks(default, null):Array<BrickConfig>;
|
public var bricks(default, null):Array<BrickConfig>;
|
||||||
public var tanks(default, null):Array<TankConfig>;
|
public var tanks(default, null):Array<TankConfig>;
|
||||||
public var presets(default, null):Array<GamePreset>;
|
public var presets(default, null):Array<GamePreset>;
|
||||||
|
public var controls(default, null):Array<ControlPreset>;
|
||||||
public var points(default, null):Array<SpawnPoint>;
|
public var points(default, null):Array<SpawnPoint>;
|
||||||
public var bonuses(default, null):Array<BonusConfig>;
|
public var bonuses(default, null):Array<BonusConfig>;
|
||||||
|
|
||||||
@@ -123,12 +147,17 @@ class Config {
|
|||||||
private var teamsMap:Map<TeamId, TeamConfig>;
|
private var teamsMap:Map<TeamId, TeamConfig>;
|
||||||
private var playersMap:Map<String, PlayerConfig>;
|
private var playersMap:Map<String, PlayerConfig>;
|
||||||
|
|
||||||
|
public static function fromSource(type:GameType, source:ConfigSource):Config {
|
||||||
|
return new Config(type, source.game, source.map, source.bricks, source.presets, source.controls, source.points, source.tanks, source.bonuses);
|
||||||
|
}
|
||||||
|
|
||||||
public function new(
|
public function new(
|
||||||
type:String,
|
type:String,
|
||||||
game:GameConfig,
|
game:GameConfig,
|
||||||
map:MapConfig,
|
map:MapConfig,
|
||||||
bricks:Array<BrickConfig>,
|
bricks:Array<BrickConfig>,
|
||||||
presets:Array<GamePreset>,
|
presets:Array<GamePreset>,
|
||||||
|
controls:Array<ControlPreset>,
|
||||||
points:Array<SpawnPoint>,
|
points:Array<SpawnPoint>,
|
||||||
tanks:Array<TankConfig>,
|
tanks:Array<TankConfig>,
|
||||||
bonuses:Array<BonusConfig>
|
bonuses:Array<BonusConfig>
|
||||||
@@ -138,6 +167,7 @@ class Config {
|
|||||||
this.map = map;
|
this.map = map;
|
||||||
this.bricks = bricks;
|
this.bricks = bricks;
|
||||||
this.presets = presets;
|
this.presets = presets;
|
||||||
|
this.controls = controls;
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.tanks = tanks;
|
this.tanks = tanks;
|
||||||
this.bonuses = bonuses;
|
this.bonuses = bonuses;
|
||||||
|
|||||||
30
src/common/haxe/ru/m/tankz/control/BaseControlFactory.hx
Normal file
30
src/common/haxe/ru/m/tankz/control/BaseControlFactory.hx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.bot.HardBotControl;
|
||||||
|
import ru.m.tankz.bot.StupidBotControl;
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
class BaseControlFactory implements IControlFactory {
|
||||||
|
|
||||||
|
public function new() {}
|
||||||
|
|
||||||
|
public function build(id:PlayerId, controller:Controller):Control {
|
||||||
|
return switch controller {
|
||||||
|
case Controller.HUMAN(index): buildHuman(id, index);
|
||||||
|
case Controller.BOT(type): buildBot(id, type);
|
||||||
|
case Controller.NONE: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildBot(id:PlayerId, type:String):Control {
|
||||||
|
return switch type {
|
||||||
|
case StupidBotControl.BOT_TYPE: new StupidBotControl(id);
|
||||||
|
case HardBotControl.BOT_TYPE: new HardBotControl(id);
|
||||||
|
case _: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
package ru.m.tankz.control;
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
import ru.m.tankz.game.GameEvent;
|
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
|
import ru.m.tankz.engine.IEngine;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
@@ -17,17 +18,20 @@ class Control {
|
|||||||
public var playerId(default, null):PlayerId;
|
public var playerId(default, null):PlayerId;
|
||||||
public var tankId(default, default):Int;
|
public var tankId(default, default):Int;
|
||||||
private var handler:IGame;
|
private var handler:IGame;
|
||||||
|
private var engine:IEngine;
|
||||||
|
|
||||||
public function new(playerId:PlayerId) {
|
public function new(playerId:PlayerId) {
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
|
this.tankId = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bind(handler:IGame):Void {
|
public function bind(handler:IGame, engine:IEngine):Void {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.engine = engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function action(action:TankAction):Void {
|
public function action(action:TankAction):Void {
|
||||||
if (tankId != 0 && handler != null) {
|
if (tankId > -1 && handler != null) {
|
||||||
handler.gameEventSignal.emit(GameEvent.ACTION(tankId, action));
|
handler.gameEventSignal.emit(GameEvent.ACTION(tankId, action));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,5 +45,7 @@ class Control {
|
|||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
stop();
|
stop();
|
||||||
handler = null;
|
handler = null;
|
||||||
|
engine = null;
|
||||||
|
tankId = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import ru.m.geom.Direction;
|
|||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.core.Bullet;
|
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.MobileEntity;
|
import ru.m.tankz.core.MobileEntity;
|
||||||
@@ -21,14 +20,14 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
public var allEntities(default, null):Map<Int, Entity>;
|
public var allEntities(default, null):Map<Int, Entity>;
|
||||||
public var entities(default, null):Map<Int, Entity>;
|
public var entities(default, null):Map<Int, Entity>;
|
||||||
|
|
||||||
private var time:Float;
|
private var ticker:Ticker;
|
||||||
|
|
||||||
public function new(config:Config) {
|
public function new(config:Config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
map = new LevelMap(config.map);
|
map = new LevelMap(config.map);
|
||||||
allEntities = new Map();
|
allEntities = new Map();
|
||||||
entities = new Map();
|
entities = new Map();
|
||||||
time = Date.now().getTime();
|
ticker = new Ticker();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEntity<T:Entity>(entityId:Int):T {
|
public function getEntity<T:Entity>(entityId:Int):T {
|
||||||
@@ -68,9 +67,8 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function update():Void {
|
public function update():Void {
|
||||||
var newTime:Float = Date.now().getTime();
|
if (!ticker.running) ticker.start();
|
||||||
var d:Float = newTime - time;
|
var d:Float = ticker.passed;
|
||||||
time = newTime;
|
|
||||||
|
|
||||||
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
||||||
var entityType:EntityType = EntityTypeResolver.of(ent);
|
var entityType:EntityType = EntityTypeResolver.of(ent);
|
||||||
|
|||||||
42
src/common/haxe/ru/m/tankz/engine/Ticker.hx
Normal file
42
src/common/haxe/ru/m/tankz/engine/Ticker.hx
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package ru.m.tankz.engine;
|
||||||
|
|
||||||
|
class Ticker {
|
||||||
|
public var time(get, null):Int;
|
||||||
|
public var passed(get, null):Int;
|
||||||
|
public var running(default, null):Bool;
|
||||||
|
|
||||||
|
private var begin:Int;
|
||||||
|
private var last:Int;
|
||||||
|
|
||||||
|
private static var TIME = Timer.stamp();
|
||||||
|
|
||||||
|
private inline static function now():Int {
|
||||||
|
return Std.int((Timer.stamp() - TIME) * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
begin = 0;
|
||||||
|
last = 0;
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start():Void {
|
||||||
|
last = begin = now();
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stop():Void {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_time():Int {
|
||||||
|
return now() - begin;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_passed():Int {
|
||||||
|
var now = now();
|
||||||
|
var result = now - last;
|
||||||
|
last = now;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import haxework.color.Color;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
@@ -23,7 +24,7 @@ class EntityBuilder {
|
|||||||
|
|
||||||
public function new(config:Config) {
|
public function new(config:Config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.entityId = 0;
|
this.entityId = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
||||||
@@ -43,11 +44,11 @@ class EntityBuilder {
|
|||||||
return eagle;
|
return eagle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, bonusOff:Bool = false):Tank {
|
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, color:Color, bonusOff:Bool = false):Tank {
|
||||||
var playerConfig = config.getPlayer(playerId);
|
var playerConfig = config.getPlayer(playerId);
|
||||||
var tankConfig = config.getTank(type);
|
var tankConfig = config.getTank(type);
|
||||||
var tank = new Tank(++entityId, buildRect(point, tankConfig.width, tankConfig.height), playerId, tankConfig);
|
var tank = new Tank(++entityId, buildRect(point, tankConfig.width, tankConfig.height), playerId, tankConfig);
|
||||||
tank.color = config.getColor(playerId);
|
tank.color = color.zero ? config.getColor(playerId) : color;
|
||||||
if (!bonusOff) {
|
if (!bonusOff) {
|
||||||
tank.bonus = Math.random() < playerConfig.bonus;
|
tank.bonus = Math.random() < playerConfig.bonus;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ class EventUtil {
|
|||||||
return GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
return GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
||||||
type:tank.config.type,
|
type:tank.config.type,
|
||||||
hits:tank.hits,
|
hits:tank.hits,
|
||||||
bonus:tank.bonus
|
bonus:tank.bonus,
|
||||||
|
color:tank.color,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import ru.m.geom.Point;
|
|||||||
import ru.m.geom.Position;
|
import ru.m.geom.Position;
|
||||||
import ru.m.tankz.bundle.IConfigBundle;
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
import ru.m.tankz.config.Config;
|
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.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.engine.Engine;
|
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
@@ -20,34 +22,20 @@ import ru.m.tankz.Type;
|
|||||||
public var type(default, null):GameType;
|
public var type(default, null):GameType;
|
||||||
public var teams(default, null):Map<TeamId, Team>;
|
public var teams(default, null):Map<TeamId, Team>;
|
||||||
public var config(default, null):Config;
|
public var config(default, null):Config;
|
||||||
public var engine(default, null):IEngine;
|
|
||||||
public var winner(default, null):Null<TeamId>;
|
public var winner(default, null):Null<TeamId>;
|
||||||
|
|
||||||
public var state(default, null):GameState;
|
public var state(default, null):GameState;
|
||||||
|
public var engine(default, null):IEngine;
|
||||||
private var builder:EntityBuilder;
|
public var controlFactory(default, null):IControlFactory;
|
||||||
|
|
||||||
@:provide var configBundle:IConfigBundle;
|
@:provide var configBundle:IConfigBundle;
|
||||||
|
|
||||||
public function new(state:GameState) {
|
public function new(state:GameState) {
|
||||||
this.type = state.type;
|
this.type = state.type;
|
||||||
|
this.state = state;
|
||||||
this.teams = new Map();
|
this.teams = new Map();
|
||||||
this.config = configBundle.get(type);
|
this.config = configBundle.get(type);
|
||||||
this.engine = new Engine(config);
|
this.controlFactory = new NoneControlFactory();
|
||||||
this.builder = new EntityBuilder(config);
|
|
||||||
connect(this);
|
connect(this);
|
||||||
prepare(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function prepare(state:GameState):Void {
|
|
||||||
var level:LevelConfig = state.level;
|
|
||||||
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
|
|
||||||
engine.map.setData(level.data);
|
|
||||||
for (teamConfig in state.preset.teams) {
|
|
||||||
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
|
||||||
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
|
||||||
teams[team.id] = team;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function applyPosition(entity:Entity, position:Position):Void {
|
private function applyPosition(entity:Entity, position:Position):Void {
|
||||||
@@ -67,15 +55,15 @@ import ru.m.tankz.Type;
|
|||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.START(state):
|
case START(state):
|
||||||
this.state = state;
|
this.state = state;
|
||||||
case GameEvent.COMPLETE(state, winnerId):
|
case COMPLETE(state, winnerId):
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.winner = winnerId;
|
this.winner = winnerId;
|
||||||
case GameEvent.SPAWN(EAGLE(id, rect, teamId)):
|
case SPAWN(EAGLE(id, rect, teamId)):
|
||||||
var team = getTeam(teamId);
|
var team = getTeam(teamId);
|
||||||
team.eagleId = id;
|
team.eagleId = id;
|
||||||
case GameEvent.SPAWN(TANK(id, rect, playerId, info)):
|
case SPAWN(TANK(id, rect, playerId, info)):
|
||||||
var player = getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
player.tankId = id;
|
player.tankId = id;
|
||||||
player.state.tank = info.type;
|
player.state.tank = info.type;
|
||||||
@@ -83,8 +71,38 @@ import ru.m.tankz.Type;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function start():Void {
|
||||||
|
var level:LevelConfig = state.level;
|
||||||
|
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
|
||||||
|
for (teamConfig in state.preset.teams) {
|
||||||
|
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
||||||
|
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
||||||
|
teams[team.id] = team;
|
||||||
|
}
|
||||||
|
var controlsById:Map<String, PlayerControl> = new Map();
|
||||||
|
for (control in state.controls) {
|
||||||
|
controlsById[control.playerId] = control;
|
||||||
|
}
|
||||||
|
for (team in teams.iterator()) {
|
||||||
|
for (player in team.players.iterator()) {
|
||||||
|
var playerControl = controlsById.get(player.id);
|
||||||
|
if (playerControl != null && !playerControl.color.zero) {
|
||||||
|
player.state.color = playerControl.color;
|
||||||
|
}
|
||||||
|
var controlType:Controller = AController.fromString(playerControl != null ? playerControl.control : player.config.control);
|
||||||
|
var control = controlFactory.build(player.id, controlType);
|
||||||
|
if (control != null) {
|
||||||
|
player.control = control;
|
||||||
|
player.control.bind(this, engine);
|
||||||
|
} else {
|
||||||
|
// ToDo: remove player
|
||||||
|
player.state.life = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
gameEventSignal.dispose();
|
gameEventSignal.dispose();
|
||||||
engine.dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import haxework.color.Color;
|
||||||
import ru.m.geom.Position;
|
import ru.m.geom.Position;
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
@@ -9,6 +10,7 @@ typedef TankInfo = {
|
|||||||
var type:TankType;
|
var type:TankType;
|
||||||
var hits:Int;
|
var hits:Int;
|
||||||
var bonus:Bool;
|
var bonus:Bool;
|
||||||
|
var color:Color;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef BrickInfo = {
|
typedef BrickInfo = {
|
||||||
|
|||||||
@@ -1,65 +1,49 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
import haxe.ds.Option;
|
import haxework.color.Color;
|
||||||
import haxework.signal.Signal;
|
|
||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.tankz.control.Control;
|
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.Bonus;
|
||||||
import ru.m.tankz.core.Bullet;
|
import ru.m.tankz.core.Bullet;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
|
import ru.m.tankz.engine.Engine;
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.IGame;
|
|
||||||
import ru.m.tankz.game.Spawner;
|
import ru.m.tankz.game.Spawner;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.Timer;
|
import ru.m.Timer;
|
||||||
|
|
||||||
class GameRunner implements EngineListener implements GameListener {
|
class GameRunner extends Game implements EngineListener {
|
||||||
@:provide var controlFactory:IControlFactory;
|
|
||||||
|
|
||||||
private var game(default, null):IGame;
|
|
||||||
private var gameEventSignal(get, null):Signal<GameEvent>;
|
|
||||||
private var timer:Timer;
|
private var timer:Timer;
|
||||||
private var builder:EntityBuilder;
|
private var builder:EntityBuilder;
|
||||||
|
|
||||||
public function new(game:IGame) {
|
public function new(state:GameState) {
|
||||||
this.game = game;
|
super(state);
|
||||||
this.builder = new EntityBuilder(this.game.config);
|
this.builder = new EntityBuilder(config);
|
||||||
this.game.connect(this);
|
this.engine = new Engine(config);
|
||||||
this.game.engine.connect(this);
|
this.engine.connect(this);
|
||||||
}
|
|
||||||
|
|
||||||
private inline function get_gameEventSignal():Signal<GameEvent> {
|
|
||||||
return game.gameEventSignal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update():Void {
|
private function update():Void {
|
||||||
game.engine.update();
|
engine.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dispose():Void {
|
override public function dispose():Void {
|
||||||
|
super.dispose();
|
||||||
if (timer != null) {
|
if (timer != null) {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
game.disconnect(this);
|
engine.dispose();
|
||||||
game.engine.disconnect(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(state:GameState):Void {
|
override public function start():Void {
|
||||||
for (team in game.teams.iterator()) {
|
super.start();
|
||||||
for (player in team.players.iterator()) {
|
engine.map.setData(state.level.data);
|
||||||
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
|
for (team in teams.iterator()) {
|
||||||
if (control != null) {
|
|
||||||
player.control = control;
|
|
||||||
player.control.bind(game);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
team.spawner.runner = spawn;
|
team.spawner.runner = spawn;
|
||||||
for (player in team.players.iterator()) {
|
for (player in team.players.iterator()) {
|
||||||
if (team.tryRespawn(player.id)) {
|
if (team.tryRespawn(player.id)) {
|
||||||
@@ -69,53 +53,43 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
if (team.config.eagle != null) {
|
if (team.config.eagle != null) {
|
||||||
var point = team.spawner.getPoint("eagle");
|
var point = team.spawner.getPoint("eagle");
|
||||||
var eagle = builder.buildEagle(point, team.id);
|
var eagle = builder.buildEagle(point, team.id);
|
||||||
game.engine.spawn(eagle);
|
engine.spawn(eagle);
|
||||||
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
||||||
eagle.protect.connect(onEagleProtectChange);
|
eagle.protect.connect(onEagleProtectChange);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(game.engine.map));
|
gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map));
|
||||||
gameEventSignal.emit(GameEvent.START(state));
|
gameEventSignal.emit(START(state));
|
||||||
//for (i in 0...10) spawnBonus();
|
//for (i in 0...10) spawnBonus();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next():Option<GameState> {
|
|
||||||
for (rule in game.config.game.complete) {
|
|
||||||
if (rule.team != null && rule.team != game.winner) {
|
|
||||||
return Option.None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var level = game.state.levelId + 1;
|
|
||||||
if (level >= game.config.game.levels) level = 0;
|
|
||||||
return Option.Some(new GameState(game.type, game.state.presetId, level, game.state));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function spawn(task:SpawnTask):Void {
|
private function spawn(task:SpawnTask):Void {
|
||||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType);
|
var player = getPlayer(task.playerId);
|
||||||
game.engine.spawn(tank);
|
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color);
|
||||||
|
engine.spawn(tank);
|
||||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||||
tank.protect.connect(onTankProtectChange);
|
tank.protect.connect(onTankProtectChange);
|
||||||
tank.freezing.connect(onTankFreezingChange);
|
tank.freezing.connect(onTankFreezingChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onEagleProtectChange(id:Int, state:Bool):Void {
|
private function onEagleProtectChange(id:Int, state:Bool):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(EAGLE_PROTECT(id, state)));
|
gameEventSignal.emit(CHANGE(EAGLE_PROTECT(id, state)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onTankProtectChange(id:Int, state:Bool):Void {
|
private function onTankProtectChange(id:Int, state:Bool):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_PROTECT(id, state)));
|
gameEventSignal.emit(CHANGE(TANK_PROTECT(id, state)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onTankFreezingChange(id:Int, state:Bool):Void {
|
private function onTankFreezingChange(id:Int, state:Bool):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_FREEZE(id, state)));
|
gameEventSignal.emit(CHANGE(TANK_FREEZE(id, state)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkComplete():Void {
|
private function checkComplete():Void {
|
||||||
var actives:Array<TeamId> = [];
|
var actives:Array<TeamId> = [];
|
||||||
for (team in game.teams.iterator()) {
|
for (team in teams.iterator()) {
|
||||||
if (team.isAlive) {
|
if (team.isAlive) {
|
||||||
if (team.eagleId > 0) {
|
if (team.eagleId > -1) {
|
||||||
if (!cast(game.engine.entities[team.eagleId], Eagle).death) {
|
if (!cast(engine.entities[team.eagleId], Eagle).death) {
|
||||||
actives.push(team.id);
|
actives.push(team.id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -132,15 +106,17 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function complete(winner:TeamId):Void {
|
private function complete(winner:TeamId):Void {
|
||||||
for (team in game.teams.iterator()) {
|
for (team in teams.iterator()) {
|
||||||
for (player in team.players) {
|
for (player in team.players) {
|
||||||
player.control.action(TankAction.STOP);
|
if (player.control != null) {
|
||||||
player.control.dispose();
|
player.control.action(STOP);
|
||||||
|
player.control.dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Timer.delay(function() {
|
Timer.delay(function() {
|
||||||
gameEventSignal.emit(GameEvent.COMPLETE(game.state, winner));
|
gameEventSignal.emit(COMPLETE(state, winner));
|
||||||
}, 5000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onSpawn(entity:EntityType):Void {
|
public function onSpawn(entity:EntityType):Void {
|
||||||
@@ -155,17 +131,17 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline function emitTankMove(tank:Tank):Void {
|
private inline function emitTankMove(tank:Tank):Void {
|
||||||
gameEventSignal.emit(GameEvent.MOVE(TANK(tank.id, {x:tank.rect.x, y:tank.rect.y, direction:tank.rect.direction})));
|
gameEventSignal.emit(MOVE(TANK(tank.id, {x:tank.rect.x, y:tank.rect.y, direction:tank.rect.direction})));
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function emitTankChange(tank:Tank):Void {
|
private inline function emitTankChange(tank:Tank):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK(tank.id, tank.config.type, tank.hits, tank.bonus)));
|
gameEventSignal.emit(CHANGE(TANK(tank.id, tank.config.type, tank.hits, tank.bonus)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onCollision(entity:EntityType, with:EntityType):Void {
|
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||||
switch entity {
|
switch entity {
|
||||||
case EntityType.TANK(tank):
|
case EntityType.TANK(tank):
|
||||||
var control = game.getPlayer(tank.playerId).control;
|
var control = getPlayer(tank.playerId).control;
|
||||||
if (control != null) control.onCollision(with);
|
if (control != null) control.onCollision(with);
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
@@ -180,16 +156,16 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
tank.rect.lean(cell.rect);
|
tank.rect.lean(cell.rect);
|
||||||
emitTankMove(tank);
|
emitTankMove(tank);
|
||||||
case [BULLET(bullet), BULLET(other_bullet)]:
|
case [BULLET(bullet), BULLET(other_bullet)]:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(other_bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(other_bullet.id)));
|
||||||
case [BULLET(bullet), CELL(cell)]:
|
case [BULLET(bullet), CELL(cell)]:
|
||||||
bullet.rect.lean(cell.rect);
|
bullet.rect.lean(cell.rect);
|
||||||
gameEventSignal.emit(GameEvent.HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
gameEventSignal.emit(HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
case [TANK(tank), BONUS(bonus)]:
|
case [TANK(tank), BONUS(bonus)]:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BONUS(bonus.id, {tankId: tank.id, score: bonus.config.score})));
|
gameEventSignal.emit(DESTROY(BONUS(bonus.id, {tankId: tank.id, score: bonus.config.score})));
|
||||||
case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/:
|
case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/:
|
||||||
if (bullet.tankId == tank.id || (!game.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
|
if (bullet.tankId == tank.id || (!config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
|
||||||
// Nothing
|
// Nothing
|
||||||
} else {
|
} else {
|
||||||
if (!tank.protect.active) {
|
if (!tank.protect.active) {
|
||||||
@@ -199,27 +175,27 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
tank.bonus = false;
|
tank.bonus = false;
|
||||||
spawnBonus();
|
spawnBonus();
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||||
emitTankChange(tank);
|
emitTankChange(tank);
|
||||||
} else if (tank.config.downgrade != null) {
|
} else if (tank.config.downgrade != null) {
|
||||||
tank.config = game.config.getTank(tank.config.downgrade);
|
tank.config = config.getTank(tank.config.downgrade);
|
||||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||||
emitTankChange(tank);
|
emitTankChange(tank);
|
||||||
} else {
|
} else {
|
||||||
var score = tank.config.score;
|
var score = tank.config.score;
|
||||||
if (tank.playerId.team == bullet.playerId.team) {
|
if (tank.playerId.team == bullet.playerId.team) {
|
||||||
score = Math.round(score * -0.5);
|
score = Math.round(score * -0.5);
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, buildShot(bullet, score))));
|
gameEventSignal.emit(DESTROY(TANK(tank.id, buildShot(bullet, score))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
}
|
}
|
||||||
case [BULLET(bullet), EAGLE(eagle)]:
|
case [BULLET(bullet), EAGLE(eagle)]:
|
||||||
if (!eagle.protect.active) {
|
if (!eagle.protect.active) {
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
gameEventSignal.emit(DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,7 +205,7 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
case TANK(tank):
|
case TANK(tank):
|
||||||
emitTankMove(tank);
|
emitTankMove(tank);
|
||||||
case BULLET(bullet):
|
case BULLET(bullet):
|
||||||
gameEventSignal.emit(GameEvent.MOVE(BULLET(bullet.id, {x:bullet.rect.x, y:bullet.rect.y, direction:bullet.rect.direction})));
|
gameEventSignal.emit(MOVE(BULLET(bullet.id, {x:bullet.rect.x, y:bullet.rect.y, direction:bullet.rect.direction})));
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,15 +214,15 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function spawnBonus():Void {
|
private function spawnBonus():Void {
|
||||||
var type = game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)].type;
|
var type = config.bonuses[Math.floor(Math.random() * config.bonuses.length)].type;
|
||||||
var point = {
|
var point = {
|
||||||
x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)),
|
x: Math.floor(Math.random() * (engine.map.gridWidth - 1)),
|
||||||
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
|
y: Math.floor(Math.random() * (engine.map.gridHeight - 1)),
|
||||||
direction: "right",
|
direction: "right",
|
||||||
}
|
}
|
||||||
var bonus = builder.buildBonus(point, type);
|
var bonus = builder.buildBonus(point, type);
|
||||||
game.engine.spawn(bonus);
|
engine.spawn(bonus);
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
|
gameEventSignal.emit(SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function alienTank(team:TeamId):Tank->Bool {
|
private inline function alienTank(team:TeamId):Tank->Bool {
|
||||||
@@ -260,35 +236,35 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
case "star":
|
case "star":
|
||||||
upgradeTank(tank);
|
upgradeTank(tank);
|
||||||
case "grenade":
|
case "grenade":
|
||||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(t.id, {tankId: tank.id})));
|
gameEventSignal.emit(DESTROY(TANK(t.id, {tankId: tank.id})));
|
||||||
}
|
}
|
||||||
case "helmet":
|
case "helmet":
|
||||||
tank.protect.on(bonus.config.duration);
|
tank.protect.on(bonus.config.duration);
|
||||||
case "clock":
|
case "clock":
|
||||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||||
t.freezing.on(bonus.config.duration);
|
t.freezing.on(bonus.config.duration);
|
||||||
t.stop();
|
t.stop();
|
||||||
gameEventSignal.emit(GameEvent.STOP(TANK(t.id)));
|
gameEventSignal.emit(STOP(TANK(t.id)));
|
||||||
}
|
}
|
||||||
case "shovel":
|
case "shovel":
|
||||||
// ToDo: protect eagle/area
|
// ToDo: protect eagle/area
|
||||||
var team:Team = game.teams[tank.playerId.team];
|
var team:Team = teams[tank.playerId.team];
|
||||||
if (team.eagleId > 0) {
|
if (team.eagleId > 0) {
|
||||||
var eagle:Eagle = cast(game.engine.entities[team.eagleId], Eagle);
|
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
|
||||||
eagle.protect.on(bonus.config.duration);
|
eagle.protect.on(bonus.config.duration);
|
||||||
}
|
}
|
||||||
case "gun":
|
case "gun":
|
||||||
upgradeTank(tank, 5);
|
upgradeTank(tank, 5);
|
||||||
case _:
|
case _:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, {tankId: tank.id}))); // :-D
|
gameEventSignal.emit(DESTROY(TANK(tank.id, {tankId: tank.id}))); // :-D
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function upgradeTank(tank:Tank, level:Int = 1):Void {
|
private function upgradeTank(tank:Tank, level:Int = 1):Void {
|
||||||
if (tank.config.upgrade != null) {
|
if (tank.config.upgrade != null) {
|
||||||
while (level-- > 0 && tank.config.upgrade != null) {
|
while (level-- > 0 && tank.config.upgrade != null) {
|
||||||
tank.config = game.config.getTank(tank.config.upgrade);
|
tank.config = config.getTank(tank.config.upgrade);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tank.hits++;
|
tank.hits++;
|
||||||
@@ -297,70 +273,76 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function changeScore(playerId:PlayerId, score:Int):Void {
|
private function changeScore(playerId:PlayerId, score:Int):Void {
|
||||||
var player = game.getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
var team = game.getTeam(playerId.team);
|
var team = getTeam(playerId.team);
|
||||||
player.state.score += score;
|
player.state.score += score;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
gameEventSignal.emit(CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TEAM_SCORE(playerId.team, game.state.getTeamScore(team.id))));
|
gameEventSignal.emit(CHANGE(TEAM_SCORE(playerId.team, state.getTeamScore(team.id))));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function changeLife(playerId:PlayerId, life:Int):Void {
|
private function changeLife(playerId:PlayerId, life:Int):Void {
|
||||||
var player = game.getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
player.state.life += life;
|
player.state.life += life;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_LIFE(playerId, player.state.life)));
|
gameEventSignal.emit(CHANGE(PLAYER_LIFE(playerId, player.state.life)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function changeTeamLife(teamId:TeamId, life:Int):Void {
|
private function changeTeamLife(teamId:TeamId, life:Int):Void {
|
||||||
var team = game.getTeam(teamId);
|
var team = getTeam(teamId);
|
||||||
team.state.life += life;
|
team.state.life += life;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TEAM_LIFE(teamId, team.state.life)));
|
gameEventSignal.emit(CHANGE(TEAM_LIFE(teamId, team.state.life)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
override public function onGameEvent(event:GameEvent):Void {
|
||||||
|
super.onGameEvent(event);
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.START(_):
|
case START(_):
|
||||||
timer = new Timer(10);
|
timer = new Timer(30);
|
||||||
timer.run = update;
|
timer.run = update;
|
||||||
case GameEvent.COMPLETE(_, _):
|
case COMPLETE(_, _):
|
||||||
if (timer != null) {
|
if (timer != null) {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
case GameEvent.ACTION(tankId, SHOT):
|
case ACTION(tankId, SHOT):
|
||||||
var tank:Tank = cast game.engine.entities.get(tankId);
|
var tank:Tank = cast engine.entities.get(tankId);
|
||||||
var player = game.getPlayer(tank.playerId);
|
var player = getPlayer(tank.playerId);
|
||||||
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
|
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
|
||||||
var rect = tank.rect;
|
var rect = tank.rect;
|
||||||
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
||||||
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
|
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
|
||||||
bullet.tank = tank;
|
bullet.tank = tank;
|
||||||
bullet.move(bullet.rect.direction);
|
bullet.move(bullet.rect.direction);
|
||||||
game.engine.spawn(bullet);
|
engine.spawn(bullet);
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing)));
|
gameEventSignal.emit(SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing)));
|
||||||
}
|
}
|
||||||
case GameEvent.ACTION(tankId, MOVE(direction)):
|
case ACTION(tankId, MOVE(direction)):
|
||||||
game.engine.move(tankId, direction);
|
engine.move(tankId, direction);
|
||||||
case GameEvent.ACTION(tankId, STOP):
|
case ACTION(tankId, STOP):
|
||||||
gameEventSignal.emit(GameEvent.STOP(TANK(tankId)));
|
gameEventSignal.emit(STOP(TANK(tankId)));
|
||||||
game.engine.stop(tankId);
|
engine.stop(tankId);
|
||||||
case GameEvent.SPAWN(TANK(_, _, playerId, _)):
|
case SPAWN(TANK(_, _, playerId, _)):
|
||||||
game.getPlayer(playerId).control.start();
|
var control = getPlayer(playerId).control;
|
||||||
case GameEvent.SPAWN(BULLET(_, _, playerId, _)):
|
if (control != null) {
|
||||||
game.getPlayer(playerId).bullets++;
|
control.start();
|
||||||
case GameEvent.DESTROY(EAGLE(id, shot)):
|
}
|
||||||
var eagle:Eagle = game.engine.getEntity(id);
|
case SPAWN(BULLET(_, _, playerId, _)):
|
||||||
|
getPlayer(playerId).bullets++;
|
||||||
|
case DESTROY(EAGLE(id, shot)):
|
||||||
|
var eagle:Eagle = engine.getEntity(id);
|
||||||
eagle.death = true;
|
eagle.death = true;
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
var tank:Tank = engine.getEntity(shot.tankId);
|
||||||
changeScore(tank.playerId, shot.score);
|
changeScore(tank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
checkComplete();
|
checkComplete();
|
||||||
case GameEvent.DESTROY(TANK(id, shot)):
|
case DESTROY(TANK(id, shot)):
|
||||||
var tank:Tank = game.engine.getEntity(id);
|
var tank:Tank = engine.getEntity(id);
|
||||||
var team = game.getTeam(tank.playerId.team);
|
var team = getTeam(tank.playerId.team);
|
||||||
var player = game.getPlayer(tank.playerId);
|
var player = getPlayer(tank.playerId);
|
||||||
player.control.stop();
|
if (player.control != null) {
|
||||||
player.tankId = 0; //ToDo: ?
|
player.control.stop();
|
||||||
|
}
|
||||||
|
player.tankId = -1;
|
||||||
team.onDestroy(player.id);
|
team.onDestroy(player.id);
|
||||||
if (player.state.life > 0) {
|
if (player.state.life > 0) {
|
||||||
changeLife(player.id, -1);
|
changeLife(player.id, -1);
|
||||||
@@ -378,44 +360,44 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
spawnBonus();
|
spawnBonus();
|
||||||
}
|
}
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
var shooterTank:Tank = game.engine.getEntity(shot.tankId);
|
var shooterTank:Tank = engine.getEntity(shot.tankId);
|
||||||
changeScore(shooterTank.playerId, shot.score);
|
changeScore(shooterTank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case GameEvent.DESTROY(BONUS(id, shot)):
|
case DESTROY(BONUS(id, shot)):
|
||||||
var bonus:Bonus = game.engine.getEntity(id);
|
var bonus:Bonus = engine.getEntity(id);
|
||||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
var tank:Tank = engine.getEntity(shot.tankId);
|
||||||
applyBonus(tank, bonus);
|
applyBonus(tank, bonus);
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
changeScore(tank.playerId, shot.score);
|
changeScore(tank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case GameEvent.DESTROY(BULLET(id)):
|
case DESTROY(BULLET(id)):
|
||||||
var bullet:Bullet = game.engine.getEntity(id);
|
var bullet:Bullet = engine.getEntity(id);
|
||||||
var player = game.getPlayer(bullet.playerId);
|
var player = getPlayer(bullet.playerId);
|
||||||
player.bullets--;
|
player.bullets--;
|
||||||
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
|
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
|
||||||
// ToDo: move
|
// ToDo: move
|
||||||
new Point(bullet.rect.direction.x * 5, bullet.rect.direction.y * 5)
|
new Point(bullet.rect.direction.x * 5, bullet.rect.direction.y * 5)
|
||||||
);
|
);
|
||||||
var cells = game.engine.map.grid.getCells(side.setLength(game.engine.map.grid.cellWidth * 3));
|
var cells = engine.map.grid.getCells(side.setLength(engine.map.grid.cellWidth * 3));
|
||||||
for (cell in cells) {
|
for (cell in cells) {
|
||||||
if (cell.armor > 0) {
|
if (cell.armor > 0) {
|
||||||
var shot = buildShot(bullet);
|
var shot = buildShot(bullet);
|
||||||
if (cell.armor == bullet.config.piercing) {
|
if (cell.armor == bullet.config.piercing) {
|
||||||
game.engine.destroyCell(cell.cellX, cell.cellY);
|
engine.destroyCell(cell.cellX, cell.cellY);
|
||||||
var brick = game.engine.map.getBrick(cell.position);
|
var brick = engine.map.getBrick(cell.position);
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
gameEventSignal.emit(DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
||||||
} else if (cell.armor < bullet.config.piercing) {
|
} else if (cell.armor < bullet.config.piercing) {
|
||||||
var brick = game.engine.map.getBrick(cell.position);
|
var brick = engine.map.getBrick(cell.position);
|
||||||
for (cell in brick.cells) {
|
for (cell in brick.cells) {
|
||||||
game.engine.destroyCell(cell.cellX, cell.cellY);
|
engine.destroyCell(cell.cellX, cell.cellY);
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BRICK(brick.id, shot)));
|
gameEventSignal.emit(DESTROY(BRICK(brick.id, shot)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ class GameState {
|
|||||||
public var type:GameType;
|
public var type:GameType;
|
||||||
public var presetId:PresetId;
|
public var presetId:PresetId;
|
||||||
public var levelId:LevelId;
|
public var levelId:LevelId;
|
||||||
|
public var controls:Array<PlayerControl>;
|
||||||
public var players:Map<String, PlayerState>;
|
public var players:Map<String, PlayerState>;
|
||||||
public var teams:Map<TeamId, TeamState>;
|
public var teams:Map<TeamId, TeamState>;
|
||||||
public var preset(get, null):GamePreset;
|
public var preset(get, null):GamePreset;
|
||||||
@@ -81,10 +82,12 @@ class GameState {
|
|||||||
@:provide static private var configBundle:IConfigBundle;
|
@:provide static private var configBundle:IConfigBundle;
|
||||||
@:provide static private var levelBundle:ILevelBundle;
|
@:provide static private var levelBundle:ILevelBundle;
|
||||||
|
|
||||||
public function new(type:GameType, presetId:PresetId = 0, levelId:Int = 0, state:GameState = null) {
|
public function new(type:GameType, presetId:PresetId = 0, levelId:Int = 0, state:GameState = null, controls:Array<PlayerControl> = null) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.presetId = presetId;
|
this.presetId = presetId;
|
||||||
this.levelId = levelId;
|
this.levelId = levelId;
|
||||||
|
//this.controls = controls == null ? config.controls[0].values : controls;
|
||||||
|
this.controls = controls == null ? [] : controls;
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
this.teams = new Map();
|
this.teams = new Map();
|
||||||
this.players = new Map();
|
this.players = new Map();
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ package ru.m.tankz.game;
|
|||||||
|
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.control.IControlFactory;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
interface IGame extends GameListener {
|
interface IGame extends GameListener {
|
||||||
public var type(default, null):GameType;
|
public var type(default, null):GameType;
|
||||||
public var teams(default, null):Map<TeamId, Team>;
|
public var teams(default, null):Map<TeamId, Team>;
|
||||||
public var config(default, null):Config;
|
public var config(default, null):Config;
|
||||||
public var engine(default, null):IEngine;
|
|
||||||
public var winner(default, null):Null<TeamId>;
|
public var winner(default, null):Null<TeamId>;
|
||||||
public var state(default, null):GameState;
|
public var state(default, null):GameState;
|
||||||
|
public var controlFactory(default, null):IControlFactory;
|
||||||
|
|
||||||
public var gameEventSignal(default, null):Signal<GameEvent>;
|
public var gameEventSignal(default, null):Signal<GameEvent>;
|
||||||
|
|
||||||
@@ -24,6 +24,8 @@ interface IGame extends GameListener {
|
|||||||
public function getTeam(teamId:TeamId):Team;
|
public function getTeam(teamId:TeamId):Team;
|
||||||
|
|
||||||
public function getPlayer(playerId:PlayerId):Player;
|
public function getPlayer(playerId:PlayerId):Player;
|
||||||
|
|
||||||
|
public function start():Void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GameListener {
|
interface GameListener {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ class Player {
|
|||||||
this.state.reset();
|
this.state.reset();
|
||||||
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
||||||
this.bullets = 0;
|
this.bullets = 0;
|
||||||
|
this.tankId = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_tankId(value:Int):Int {
|
private function set_tankId(value:Int):Int {
|
||||||
@@ -42,6 +43,6 @@ class Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function get_isAlive():Bool {
|
private function get_isAlive():Bool {
|
||||||
return tankId > 0 || state.life > 0;
|
return tankId > -1 || state.life > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class Team {
|
|||||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, state:TeamState = null) {
|
public function new(config:TeamConfig, points:Array<SpawnPoint>, state:TeamState = null) {
|
||||||
this.id = config.id;
|
this.id = config.id;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.active = 0;
|
||||||
|
this.eagleId = -1;
|
||||||
this.players = new Map();
|
this.players = new Map();
|
||||||
for (playerConfig in config.players) {
|
for (playerConfig in config.players) {
|
||||||
var playerState = state == null ? null : state.players[playerConfig.index];
|
var playerState = state == null ? null : state.players[playerConfig.index];
|
||||||
|
|||||||
@@ -2,24 +2,25 @@ package ru.m.tankz.game.record;
|
|||||||
|
|
||||||
import flash.events.Event;
|
import flash.events.Event;
|
||||||
import flash.Lib;
|
import flash.Lib;
|
||||||
|
import ru.m.tankz.engine.Ticker;
|
||||||
import ru.m.tankz.game.record.GameRecord;
|
import ru.m.tankz.game.record.GameRecord;
|
||||||
|
|
||||||
class GamePlayer {
|
class GamePlayer extends Game {
|
||||||
|
|
||||||
private var frame:Int;
|
|
||||||
private var game:IGame;
|
|
||||||
private var record:GameRecord;
|
private var record:GameRecord;
|
||||||
private var data:Array<EventItem>;
|
private var data:Array<EventItem>;
|
||||||
|
private var ticker:Ticker;
|
||||||
|
|
||||||
public function new(game:IGame, record:GameRecord) {
|
public function new(record:GameRecord) {
|
||||||
frame = 0;
|
super(record.state);
|
||||||
this.game = game;
|
|
||||||
this.record = record;
|
this.record = record;
|
||||||
this.data = null;
|
this.data = null;
|
||||||
|
this.ticker = new Ticker();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start():Void {
|
override public function start():Void {
|
||||||
frame = 0;
|
super.start();
|
||||||
|
ticker.start();
|
||||||
data = record.events.slice(0);
|
data = record.events.slice(0);
|
||||||
Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
|
Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||||
}
|
}
|
||||||
@@ -29,12 +30,11 @@ class GamePlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function onEnterFrame(event:Event):Void {
|
private function onEnterFrame(event:Event):Void {
|
||||||
frame++;
|
|
||||||
var events = 0;
|
var events = 0;
|
||||||
for (event in data) {
|
for (event in data) {
|
||||||
if (event.frame <= frame) {
|
if (event.time <= ticker.time) {
|
||||||
events++;
|
events++;
|
||||||
game.gameEventSignal.emit(event.event);
|
gameEventSignal.emit(event.event);
|
||||||
switch event.event {
|
switch event.event {
|
||||||
case GameEvent.COMPLETE(_, _):
|
case GameEvent.COMPLETE(_, _):
|
||||||
stop();
|
stop();
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import com.hurlant.crypto.extra.UUID;
|
|||||||
import com.hurlant.crypto.prng.Random;
|
import com.hurlant.crypto.prng.Random;
|
||||||
|
|
||||||
typedef EventItem = {
|
typedef EventItem = {
|
||||||
frame:Int,
|
var time:Int;
|
||||||
event:GameEvent
|
var event:GameEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameRecordInfo {
|
class GameRecordInfo {
|
||||||
|
|||||||
@@ -1,44 +1,31 @@
|
|||||||
package ru.m.tankz.game.record;
|
package ru.m.tankz.game.record;
|
||||||
|
|
||||||
import flash.events.Event;
|
import ru.m.tankz.engine.Ticker;
|
||||||
import flash.Lib;
|
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
|
|
||||||
class GameRecorder implements GameListener {
|
class GameRecorder implements GameListener {
|
||||||
|
|
||||||
private var frame:Int;
|
|
||||||
public var record(default, null):GameRecord;
|
public var record(default, null):GameRecord;
|
||||||
|
|
||||||
|
private var ticker:Ticker;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
frame = 0;
|
ticker = new Ticker();
|
||||||
record = new GameRecord();
|
record = new GameRecord();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
|
if (!ticker.running) ticker.start();
|
||||||
|
record.events.push({time: ticker.time, event: event});
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.START(state):
|
case GameEvent.START(state):
|
||||||
record.info.type = state.type;
|
record.info.type = state.type;
|
||||||
record.info.presetId = state.presetId;
|
record.info.presetId = state.presetId;
|
||||||
record.info.levelId = state.levelId;
|
record.info.levelId = state.levelId;
|
||||||
start();
|
record.info.date = Date.now();
|
||||||
case GameEvent.COMPLETE(_, _):
|
case GameEvent.COMPLETE(_, _):
|
||||||
stop();
|
ticker.stop();
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
record.events.push({frame: frame, event: event});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function start():Void {
|
|
||||||
frame = 0;
|
|
||||||
record.info.date = Date.now();
|
|
||||||
Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stop():Void {
|
|
||||||
Lib.current.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function onEnterFrame(event:Event):Void {
|
|
||||||
frame++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package ru.m.tankz.proto.core;
|
package ru.m.tankz.proto.core;
|
||||||
|
|
||||||
|
|
||||||
message UserProto {
|
message UserProto {
|
||||||
string uuid = 1;
|
string uuid = 1;
|
||||||
string name = 2;
|
string name = 2;
|
||||||
@@ -14,11 +13,17 @@ enum GameStateProto {
|
|||||||
ENDED = 2;
|
ENDED = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message PlayerProto {
|
||||||
|
UserProto user = 1;
|
||||||
|
string team = 2;
|
||||||
|
int32 index = 3;
|
||||||
|
}
|
||||||
|
|
||||||
message GameProto {
|
message GameProto {
|
||||||
int32 id = 1;
|
int32 id = 1;
|
||||||
string type = 2;
|
string type = 2;
|
||||||
int32 level = 3;
|
int32 level = 3;
|
||||||
UserProto creator = 4;
|
UserProto creator = 4;
|
||||||
repeated UserProto players = 5;
|
repeated PlayerProto players = 5;
|
||||||
GameStateProto state = 6;
|
GameStateProto state = 6;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,17 @@ message StartGameResponse {
|
|||||||
ru.m.tankz.proto.core.GameProto game = 1;
|
ru.m.tankz.proto.core.GameProto game = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Game
|
||||||
|
message GameEventRequest {
|
||||||
|
int32 time = 1;
|
||||||
|
string event = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GameEventResponse {
|
||||||
|
int32 time = 1;
|
||||||
|
string event = 2;
|
||||||
|
}
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
message Request {
|
message Request {
|
||||||
oneof content {
|
oneof content {
|
||||||
@@ -78,6 +89,7 @@ message Request {
|
|||||||
JoinGameRequest joinGame = 5;
|
JoinGameRequest joinGame = 5;
|
||||||
LeaveGameRequest leaveGame = 6;
|
LeaveGameRequest leaveGame = 6;
|
||||||
StartGameRequest startGame = 7;
|
StartGameRequest startGame = 7;
|
||||||
|
GameEventRequest gameEvent = 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +103,7 @@ message Response {
|
|||||||
JoinGameResponse joinGame = 5;
|
JoinGameResponse joinGame = 5;
|
||||||
LeaveGameResponse leaveGame = 6;
|
LeaveGameResponse leaveGame = 6;
|
||||||
StartGameResponse startGame = 7;
|
StartGameResponse startGame = 7;
|
||||||
|
GameEventResponse gameEvent = 8;
|
||||||
|
|
||||||
ErrorResponse error = 999;
|
ErrorResponse error = 999;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,14 +38,15 @@ team:
|
|||||||
human: &team_human
|
human: &team_human
|
||||||
id: human
|
id: human
|
||||||
players:
|
players:
|
||||||
- {<<: *human, index: 0, color: 0xFFFF00, control: human-0}
|
- {<<: *human, index: 0, color: 0xFFFF00, control: 'null'}
|
||||||
|
- {<<: *human, index: 1, color: 0x15C040, control: 'null'}
|
||||||
eagle:
|
eagle:
|
||||||
score: 0
|
score: 0
|
||||||
|
|
||||||
points:
|
points:
|
||||||
- {team: human, type: eagle, index: -1, direction: right, x: 12, y: 24}
|
- {team: human, type: eagle, index: -1, direction: right, x: 12, y: 24}
|
||||||
- {team: human, type: tank, index: 0, direction: top, x: 8, y: 24}
|
- {team: human, type: tank, index: 0, direction: top, x: 8, y: 24}
|
||||||
#- {team: human, type: tank, index: 1, direction: top, x: 16, y: 24}
|
- {team: human, type: tank, index: 1, direction: top, x: 16, y: 24}
|
||||||
- {team: bot, type: tank, index: -1, direction: bottom, x: 0, y: 0}
|
- {team: bot, type: tank, index: -1, direction: bottom, x: 0, y: 0}
|
||||||
- {team: bot, type: tank, index: -2, direction: bottom, x: 12, y: 0}
|
- {team: bot, type: tank, index: -2, direction: bottom, x: 12, y: 0}
|
||||||
- {team: bot, type: tank, index: -3, direction: bottom, x: 24, y: 0}
|
- {team: bot, type: tank, index: -3, direction: bottom, x: 24, y: 0}
|
||||||
@@ -195,3 +196,17 @@ presets:
|
|||||||
- {<<: *bot, index: 3, control: bot-hard}
|
- {<<: *bot, index: 3, control: bot-hard}
|
||||||
- {<<: *bot, index: 4, control: bot-hard}
|
- {<<: *bot, index: 4, control: bot-hard}
|
||||||
- {<<: *bot, index: 5, control: bot-hard}
|
- {<<: *bot, index: 5, control: bot-hard}
|
||||||
|
|
||||||
|
controls:
|
||||||
|
- id: 0
|
||||||
|
name: 1 Player
|
||||||
|
values:
|
||||||
|
- playerId: [human, 0]
|
||||||
|
control: human-0
|
||||||
|
- id: 1
|
||||||
|
name: 2 Player
|
||||||
|
values:
|
||||||
|
- playerId: [human, 0]
|
||||||
|
control: human-0
|
||||||
|
- playerId: [human, 1]
|
||||||
|
control: human-1
|
||||||
|
|||||||
@@ -37,8 +37,6 @@ presets:
|
|||||||
teams:
|
teams:
|
||||||
- id: alpha
|
- id: alpha
|
||||||
color: 0xFF4422
|
color: 0xFF4422
|
||||||
players:
|
|
||||||
- {<<: *player, index: 0, control: human-0}
|
|
||||||
<<: *team
|
<<: *team
|
||||||
- id: beta
|
- id: beta
|
||||||
color: 0xFFD000
|
color: 0xFFD000
|
||||||
@@ -91,3 +89,17 @@ tanks:
|
|||||||
skin: pc
|
skin: pc
|
||||||
|
|
||||||
bonuses: []
|
bonuses: []
|
||||||
|
|
||||||
|
controls:
|
||||||
|
- id: 0
|
||||||
|
name: 1 Player
|
||||||
|
values:
|
||||||
|
- playerId: [alpha, 0]
|
||||||
|
control: human-0
|
||||||
|
- id: 1
|
||||||
|
name: 2 Player
|
||||||
|
values:
|
||||||
|
- playerId: [alpha, 0]
|
||||||
|
control: human-0
|
||||||
|
- playerId: [beta, 0]
|
||||||
|
control: human-1
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ team:
|
|||||||
id: radiant
|
id: radiant
|
||||||
color: 0xff4422
|
color: 0xff4422
|
||||||
players:
|
players:
|
||||||
- {<<: *player-slow, index: 0, control: human-0, color: 0xff8866}
|
- {<<: *player-slow, index: 0, control: bot-hard}
|
||||||
- {<<: *player-fast, index: 1, control: bot-hard}
|
- {<<: *player-fast, index: 1, control: bot-hard}
|
||||||
- {<<: *player-slow, index: 2, control: bot-hard}
|
- {<<: *player-slow, index: 2, control: bot-hard}
|
||||||
- {<<: *player-fast, index: 3, control: bot-hard}
|
- {<<: *player-fast, index: 3, control: bot-hard}
|
||||||
@@ -114,3 +114,29 @@ bonuses:
|
|||||||
- {type: life}
|
- {type: life}
|
||||||
- {type: shovel, duration: 10}
|
- {type: shovel, duration: 10}
|
||||||
- {type: star}
|
- {type: star}
|
||||||
|
|
||||||
|
controls:
|
||||||
|
- id: 0
|
||||||
|
name: 1 Player
|
||||||
|
values:
|
||||||
|
- playerId: [radiant, 0]
|
||||||
|
control: human-0
|
||||||
|
color: 0xff8866
|
||||||
|
- id: 1
|
||||||
|
name: 2 Player Coop
|
||||||
|
values:
|
||||||
|
- playerId: [radiant, 0]
|
||||||
|
control: human-0
|
||||||
|
color: 0xff8866
|
||||||
|
- playerId: [radiant, 1]
|
||||||
|
control: human-1
|
||||||
|
color: 0xff8866
|
||||||
|
- id: 2
|
||||||
|
name: 2 Player VS
|
||||||
|
values:
|
||||||
|
- playerId: [radiant, 0]
|
||||||
|
control: human-0
|
||||||
|
color: 0xff8866
|
||||||
|
- playerId: [dire, 0]
|
||||||
|
control: human-1
|
||||||
|
color: 0x4294ff
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ enum Brush {
|
|||||||
var playerId = new PlayerId(point.team, point.index < 0 ? 0 : point.index);
|
var playerId = new PlayerId(point.team, point.index < 0 ? 0 : point.index);
|
||||||
var player = config.getPlayer(playerId);
|
var player = config.getPlayer(playerId);
|
||||||
var tankSpawn = player.tanks[0];
|
var tankSpawn = player.tanks[0];
|
||||||
var tank = builder.buildTank(point, playerId, tankSpawn.type, true);
|
var tank = builder.buildTank(point, playerId, tankSpawn.type, 0, true);
|
||||||
pointEntities[pointKey(point)] = tank;
|
pointEntities[pointKey(point)] = tank;
|
||||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import haxework.provider.Provider;
|
|||||||
import neko.net.ThreadServer;
|
import neko.net.ThreadServer;
|
||||||
import ru.m.tankz.bundle.IConfigBundle;
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
import ru.m.tankz.bundle.ILevelBundle;
|
import ru.m.tankz.bundle.ILevelBundle;
|
||||||
import ru.m.tankz.control.IControlFactory;
|
|
||||||
import ru.m.tankz.control.NoneControlFactory;
|
|
||||||
import ru.m.tankz.server.bundle.ServerConfigBundle;
|
import ru.m.tankz.server.bundle.ServerConfigBundle;
|
||||||
import ru.m.tankz.server.bundle.ServerLevelBundle;
|
import ru.m.tankz.server.bundle.ServerLevelBundle;
|
||||||
import ru.m.tankz.server.game.GameManager;
|
import ru.m.tankz.server.game.GameManager;
|
||||||
@@ -50,7 +48,6 @@ class Server extends ThreadServer<GameSession, Bytes> {
|
|||||||
L.i(TAG, 'Build: ${CompilationOption.get("build")}');
|
L.i(TAG, 'Build: ${CompilationOption.get("build")}');
|
||||||
Provider.setFactory(IConfigBundle, ServerConfigBundle);
|
Provider.setFactory(IConfigBundle, ServerConfigBundle);
|
||||||
Provider.setFactory(ILevelBundle, ServerLevelBundle);
|
Provider.setFactory(ILevelBundle, ServerLevelBundle);
|
||||||
Provider.setFactory(IControlFactory, NoneControlFactory);
|
|
||||||
gameManager = new GameManager();
|
gameManager = new GameManager();
|
||||||
var host:String = Sys.args().length > 0 ? Sys.args()[0] : "0.0.0.0";
|
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;
|
var port:Int = Sys.args().length > 1 ? Std.parseInt(Sys.args()[1]) : 5000;
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ class ServerConfigBundle implements IConfigBundle {
|
|||||||
var path:String = FileSystem.absolutePath('./resources/${type}/config.yaml');
|
var path:String = FileSystem.absolutePath('./resources/${type}/config.yaml');
|
||||||
var data:String = File.getContent(path);
|
var data:String = File.getContent(path);
|
||||||
var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
|
var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
|
||||||
return new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses);
|
return Config.fromSource(type, source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package ru.m.tankz.server.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
import ru.m.tankz.control.Control;
|
||||||
|
import ru.m.tankz.control.BaseControlFactory;
|
||||||
|
|
||||||
|
class ClientControl extends Control {}
|
||||||
|
|
||||||
|
class ServerControlFactory extends BaseControlFactory {
|
||||||
|
|
||||||
|
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return new ClientControl(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,39 @@
|
|||||||
package ru.m.tankz.server.game;
|
package ru.m.tankz.server.game;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
|
import ru.m.tankz.game.IGame.GameListener;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.proto.core.GameProto;
|
import ru.m.tankz.proto.core.GameProto;
|
||||||
import ru.m.tankz.proto.core.GameStateProto;
|
import ru.m.tankz.proto.core.GameStateProto;
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
import ru.m.tankz.proto.core.UserProto;
|
||||||
import ru.m.tankz.server.game.IGameManager;
|
import ru.m.tankz.server.game.IGameManager;
|
||||||
|
|
||||||
|
class _GameListener implements GameListener {
|
||||||
|
private var game:ServerGame;
|
||||||
|
private var dispatcher:IGameManager;
|
||||||
|
|
||||||
|
public function new(game:ServerGame, dispatcher:IGameManager) {
|
||||||
|
this.game = game;
|
||||||
|
this.dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
|
dispatcher.dispatchEvent(game, event);
|
||||||
|
switch event {
|
||||||
|
case COMPLETE(_, _):
|
||||||
|
dispatcher.delete(game.proto.id);
|
||||||
|
dispose();
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dispose():Void {
|
||||||
|
game.disconnect(this);
|
||||||
|
game = null;
|
||||||
|
dispatcher = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@:dispatcher(GameManagerListener) class GameManager implements IGameManager {
|
@:dispatcher(GameManagerListener) class GameManager implements IGameManager {
|
||||||
public var games(default, null):Array<ServerGame>;
|
public var games(default, null):Array<ServerGame>;
|
||||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
public var gamesById(default, null):Map<Int, ServerGame>;
|
||||||
@@ -29,9 +58,9 @@ import ru.m.tankz.server.game.IGameManager;
|
|||||||
.setId(++counter)
|
.setId(++counter)
|
||||||
.setCreator(user)
|
.setCreator(user)
|
||||||
.setType(type)
|
.setType(type)
|
||||||
.setLevel(level)
|
.setLevel(level);
|
||||||
.setPlayers([user]);
|
|
||||||
var game = new ServerGame(proto);
|
var game = new ServerGame(proto);
|
||||||
|
game.joinUser(user);
|
||||||
games.push(game);
|
games.push(game);
|
||||||
gamesById[game.proto.id] = game;
|
gamesById[game.proto.id] = game;
|
||||||
gamesByCreator[game.proto.creator.uuid] = game;
|
gamesByCreator[game.proto.creator.uuid] = game;
|
||||||
@@ -42,7 +71,7 @@ import ru.m.tankz.server.game.IGameManager;
|
|||||||
public function join(gameId:Int, user:UserProto):Void {
|
public function join(gameId:Int, user:UserProto):Void {
|
||||||
if (gamesById.exists(gameId)) {
|
if (gamesById.exists(gameId)) {
|
||||||
var game = gamesById[gameId];
|
var game = gamesById[gameId];
|
||||||
game.proto.players.push(user);
|
game.joinUser(user);
|
||||||
gamesByUser[user.uuid] = game;
|
gamesByUser[user.uuid] = game;
|
||||||
changeSignal.emit(game, JOIN(user));
|
changeSignal.emit(game, JOIN(user));
|
||||||
}
|
}
|
||||||
@@ -63,16 +92,22 @@ import ru.m.tankz.server.game.IGameManager;
|
|||||||
delete(gamesByCreator[user.uuid].proto.id);
|
delete(gamesByCreator[user.uuid].proto.id);
|
||||||
} else if (gamesByUser.exists(user.uuid)) {
|
} else if (gamesByUser.exists(user.uuid)) {
|
||||||
var game = gamesByUser[user.uuid];
|
var game = gamesByUser[user.uuid];
|
||||||
game.proto.setPlayers(game.proto.players.filter(function(player) return player.uuid != user.uuid));
|
game.leaveUser(user);
|
||||||
changeSignal.emit(game, LEAVE(user));
|
changeSignal.emit(game, LEAVE(user));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(gameId:Int):Void {
|
public function start(gameId:Int):Void {
|
||||||
if (gamesById.exists(gameId)) {
|
if (gamesById.exists(gameId)) {
|
||||||
var game = gamesById[gameId];
|
var game:ServerGame = gamesById[gameId];
|
||||||
game.proto.setState(GameStateProto.STARTED);
|
game.proto.setState(GameStateProto.STARTED);
|
||||||
changeSignal.emit(game, START);
|
changeSignal.emit(game, START);
|
||||||
|
game.connect(new _GameListener(game, this));
|
||||||
|
game.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dispatchEvent(game:ServerGame, event:GameEvent):Void {
|
||||||
|
eventSignal.emit(game, event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.server.game;
|
package ru.m.tankz.server.game;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
import ru.m.tankz.proto.core.UserProto;
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ interface GameManagerListener {
|
|||||||
public function onCreate(game:ServerGame):Void;
|
public function onCreate(game:ServerGame):Void;
|
||||||
public function onChange(game:ServerGame, change:GameChange):Void;
|
public function onChange(game:ServerGame, change:GameChange):Void;
|
||||||
public function onDelete(game:ServerGame):Void;
|
public function onDelete(game:ServerGame):Void;
|
||||||
|
public function onEvent(game:ServerGame, event:GameEvent):Void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IGameManager {
|
interface IGameManager {
|
||||||
@@ -23,6 +25,9 @@ interface IGameManager {
|
|||||||
private var createSignal(default, null):Signal<ServerGame>;
|
private var createSignal(default, null):Signal<ServerGame>;
|
||||||
private var changeSignal(default, null):Signal2<ServerGame, GameChange>;
|
private var changeSignal(default, null):Signal2<ServerGame, GameChange>;
|
||||||
private var deleteSignal(default, null):Signal<ServerGame>;
|
private var deleteSignal(default, null):Signal<ServerGame>;
|
||||||
|
private var eventSignal(default, null):Signal2<ServerGame, GameEvent>;
|
||||||
|
|
||||||
|
public function dispatchEvent(game:ServerGame, event:GameEvent):Void;
|
||||||
|
|
||||||
public function connect(listener:GameManagerListener):Void;
|
public function connect(listener:GameManagerListener):Void;
|
||||||
public function disconnect(listener:GameManagerListener):Void;
|
public function disconnect(listener:GameManagerListener):Void;
|
||||||
|
|||||||
@@ -1,18 +1,39 @@
|
|||||||
package ru.m.tankz.server.game;
|
package ru.m.tankz.server.game;
|
||||||
|
|
||||||
import ru.m.tankz.game.Game;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameRunner;
|
import ru.m.tankz.game.GameRunner;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
import ru.m.tankz.proto.core.GameProto;
|
import ru.m.tankz.proto.core.GameProto;
|
||||||
|
import ru.m.tankz.proto.core.PlayerProto;
|
||||||
|
import ru.m.tankz.proto.core.UserProto;
|
||||||
|
import ru.m.tankz.server.control.ServerControlFactory;
|
||||||
|
|
||||||
class ServerGame extends Game {
|
class ServerGame extends GameRunner {
|
||||||
|
|
||||||
public var runner(default, null):GameRunner;
|
public var runner(default, null):GameRunner;
|
||||||
public var proto(default, null):GameProto;
|
public var proto(default, null):GameProto;
|
||||||
|
|
||||||
public function new(proto:GameProto) {
|
public function new(proto:GameProto) {
|
||||||
super(new GameState(proto.type, 0, proto.level));
|
super(new GameState(proto.type, 0, proto.level));
|
||||||
|
this.controlFactory = new ServerControlFactory();
|
||||||
this.proto = proto;
|
this.proto = proto;
|
||||||
runner = new GameRunner(this);
|
}
|
||||||
|
|
||||||
|
public function joinUser(user:UserProto):Void {
|
||||||
|
var index = proto.players.length;
|
||||||
|
var teamId = "human"; // ToDo:
|
||||||
|
proto.players.push(new PlayerProto().setUser(user).setTeam(teamId).setIndex(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function leaveUser(user:UserProto):Void {
|
||||||
|
proto.setPlayers(proto.players.filter(function(player) return player.user.uuid != user.uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function start():Void {
|
||||||
|
state.controls = proto.players.map(function(player):PlayerControl return {
|
||||||
|
playerId: [player.team, player.index],
|
||||||
|
control: 'human-0'
|
||||||
|
});
|
||||||
|
super.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package ru.m.tankz.server.session;
|
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;
|
||||||
import com.hurlant.crypto.extra.UUID;
|
import com.hurlant.crypto.extra.UUID;
|
||||||
import com.hurlant.crypto.prng.Random;
|
import com.hurlant.crypto.prng.Random;
|
||||||
import haxework.log.BaseLogger.LoggerUtil;
|
import haxework.log.BaseLogger.LoggerUtil;
|
||||||
@@ -47,7 +51,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
override public function send(packet:Response):Void {
|
override public function send(packet:Response):Void {
|
||||||
L.d(TAG, '$tag send: ${packet}');
|
#if proto_debug L.d(TAG, '$tag send: ${packet}'); #end
|
||||||
try {
|
try {
|
||||||
super.send(packet);
|
super.send(packet);
|
||||||
} catch (error:Dynamic) {
|
} catch (error:Dynamic) {
|
||||||
@@ -65,7 +69,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
override private function onRequest(request:Request):Void {
|
override private function onRequest(request:Request):Void {
|
||||||
L.d(TAG, '$tag onRequest: ${request}');
|
#if proto_debug L.d(TAG, '$tag onRequest: ${request}'); #end
|
||||||
try {
|
try {
|
||||||
if (!request.hasLogin() && user == null) {
|
if (!request.hasLogin() && user == null) {
|
||||||
throw "Not Authorized";
|
throw "Not Authorized";
|
||||||
@@ -99,6 +103,11 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
// start
|
// start
|
||||||
} else if (request.hasStartGame()) {
|
} else if (request.hasStartGame()) {
|
||||||
gameManager.start(gameId);
|
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) {
|
} catch (error:Dynamic) {
|
||||||
L.e(TAG, '$tag onRequest ', error);
|
L.e(TAG, '$tag onRequest ', error);
|
||||||
@@ -148,4 +157,10 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
send(new Response().setLeaveGame(new LeaveGameResponse().setGame(game.proto).setUser(user)));
|
send(new Response().setLeaveGame(new LeaveGameResponse().setGame(game.proto).setUser(user)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onEvent(game:ServerGame, event:GameEvent):Void {
|
||||||
|
if (gameId == game.proto.id) {
|
||||||
|
send(new Response().setGameEvent(new GameEventResponse().setTime(0).setEvent(Serializer.run(event))));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user