Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 94b19a1c26 | |||
| 50a0cee044 | |||
| 26ad5f47cd | |||
| 864d5f3335 | |||
| 4b426183e8 | |||
| a5722dfa9d | |||
| 7984d8282c | |||
| 17823ede11 | |||
| 5db3ae1af0 | |||
| bbe2aeb7fb | |||
| 09b188cf24 |
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@@ -0,0 +1,9 @@
|
||||
# Editor configuration, see http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
@@ -3,7 +3,9 @@ hash_behaviour = merge
|
||||
host_key_checking = False
|
||||
#callback_whitelist = profile_tasks
|
||||
display_skipped_hosts = False
|
||||
stdout_callback = yaml
|
||||
bin_ansible_callbacks = True
|
||||
|
||||
[ssh_connection]
|
||||
pipelining = True
|
||||
ssh_args = -o ForwardAgent=yes
|
||||
ssh_args = -o ForwardAgent=yes
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
remote_src: true
|
||||
|
||||
- name: "Gulp build"
|
||||
command: "/usr/local/lib/npm/bin/gulp default"
|
||||
command: "/usr/local/lib/npm/bin/gulp default --host shmyga.ru"
|
||||
args:
|
||||
chdir: "{{ deploy_release_dir }}"
|
||||
|
||||
16
gulpfile.js
16
gulpfile.js
@@ -4,7 +4,7 @@ const Config = require('./config.json');
|
||||
const packageInfo = require('./package.json');
|
||||
const {Sdk, Haxe, Project, FlashPlayer} = require('gulp-haxetool');
|
||||
const dateformat = require('dateformat');
|
||||
|
||||
const argv = require('yargs').argv;
|
||||
|
||||
// ToDo: update default in gulp-haxetool
|
||||
FlashPlayer.VERSION = '32';
|
||||
@@ -49,11 +49,13 @@ const config = new Project.Config({
|
||||
'src/common/resources'
|
||||
],
|
||||
macros: [
|
||||
//'yield.parser.Parser.auto()', // ToDo: bug with extraParams.hxml in yield library
|
||||
`CompilationOption.set('build','${dateformat(new Date(), 'yyyy-mm-dd HH:MM:ss')}')`,
|
||||
]
|
||||
});
|
||||
|
||||
const host = argv.host || 'localhost';
|
||||
const port = argv.port || 5000;
|
||||
|
||||
/**
|
||||
* client
|
||||
*/
|
||||
@@ -78,10 +80,15 @@ const client = new Project(
|
||||
height: 768,
|
||||
},
|
||||
flags: [
|
||||
//'proto_debug',
|
||||
//'dom',
|
||||
//'dev_layout',
|
||||
//'bitmap_text',
|
||||
],
|
||||
macros: [
|
||||
`CompilationOption.set('host','${host}')`,
|
||||
`CompilationOption.set('port',${port})`,
|
||||
]
|
||||
}),
|
||||
module.exports.generate
|
||||
).bind(module, gulp);
|
||||
@@ -125,7 +132,8 @@ const server = new Project(
|
||||
name: 'server',
|
||||
sources: ['src/server/haxe'],
|
||||
main: 'ru.m.tankz.server.Server',
|
||||
})
|
||||
}),
|
||||
module.exports.generate
|
||||
).bind(module, gulp);
|
||||
|
||||
/**
|
||||
@@ -144,5 +152,5 @@ module.exports.default = gulp.series(
|
||||
module.exports['editor:flash:html'],
|
||||
module.exports['editor:html5:build'],
|
||||
|
||||
//module.exports['server:neko:build'],
|
||||
module.exports['server:neko:build'],
|
||||
);
|
||||
|
||||
636
package-lock.json
generated
636
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,19 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.11.0",
|
||||
"version": "0.12.0",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"dateformat": "^3.0.3",
|
||||
"gulp": "^4.0.0",
|
||||
"gulp-add": "0.0.2",
|
||||
"gulp-clean": "^0.4.0",
|
||||
"gulp-haxetool": "^0.0.17"
|
||||
"gulp-haxetool": "^0.0.19",
|
||||
"yargs": "^13.2.4"
|
||||
},
|
||||
"haxeDependencies": {
|
||||
"haxework": "git@bitbucket.org:shmyga/haxework.git",
|
||||
"lime": "7.3.0",
|
||||
"openfl": "8.9.0",
|
||||
"lime": "7.5.0",
|
||||
"openfl": "8.9.1",
|
||||
"hxcpp": "4.0.8",
|
||||
"promhx": "1.1.0",
|
||||
"protohx": "0.4.6",
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import ru.m.tankz.view.ClientView;
|
||||
import flash.Lib;
|
||||
import haxework.animate.Animate;
|
||||
import haxework.view.Root;
|
||||
import haxework.log.TraceLogger;
|
||||
#if flash import haxework.log.JSLogger; #end
|
||||
#if debug import haxework.log.SocketLogger; #end
|
||||
import haxework.view.Root;
|
||||
import ru.m.tankz.view.ClientView;
|
||||
|
||||
class Client {
|
||||
private static inline var TAG = 'Tankz';
|
||||
private static inline var TAG = "Tankz";
|
||||
|
||||
public static function main() {
|
||||
L.push(new TraceLogger());
|
||||
#if flash
|
||||
L.push(new JSLogger());
|
||||
L.push(new haxework.log.JSLogger());
|
||||
#end
|
||||
#if debug
|
||||
//L.push(new SocketLogger());
|
||||
//L.push(new haxework.log.SocketLogger());
|
||||
#end
|
||||
Const.init();
|
||||
Init.init();
|
||||
@@ -32,5 +30,10 @@ class Client {
|
||||
var view:ClientView = new ClientView();
|
||||
Root.bind(view);
|
||||
view.launch();
|
||||
|
||||
#if debug
|
||||
var fps = new openfl.display.FPS(0, 0, 0x00ff00);
|
||||
Lib.current.addChild(fps);
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,6 @@ import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
import ru.m.tankz.storage.RecordStorage;
|
||||
import ru.m.tankz.storage.SettingsStorage;
|
||||
#if flash
|
||||
import flash.Lib;
|
||||
#elseif html5
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
class Init {
|
||||
|
||||
@@ -41,19 +36,16 @@ class Init {
|
||||
@:provide static var popupManager:PopupManager;
|
||||
@:provide static var connection:IConnection<Request, Response>;
|
||||
|
||||
private static function getHost():String {
|
||||
private static function buildConnection():IConnection<Request, Response> {
|
||||
var host:String = CompilationOption.get("host");
|
||||
var port:Int = CompilationOption.get("port");
|
||||
L.d("Init", 'connect to $host:$port');
|
||||
#if flash
|
||||
var url = Lib.current.loaderInfo.url;
|
||||
var r:EReg = ~/(app|https?):\/\/?([-_\w\d\.]+)(:\d+)?\/?/;
|
||||
if (r.match(url) && !(r.matched(1) == "app")) {
|
||||
return r.matched(2);
|
||||
} else {
|
||||
return "localhost";
|
||||
}
|
||||
return new ru.m.connect.flash.FlashConnection<Request, Response>(host, port, Response);
|
||||
#elseif html5
|
||||
return Browser.location.hostname;
|
||||
return new ru.m.connect.js.JsConnection<Request, Response>(host, port + (ru.m.connect.js.JsConnection.isSecured() ? 1 : 0), Response);
|
||||
#else
|
||||
return "localhost";
|
||||
return new ru.m.connect.desktop.DesktopConnection<Request, Response>(host, port, Response);
|
||||
#end
|
||||
}
|
||||
|
||||
@@ -72,15 +64,7 @@ class Init {
|
||||
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
||||
popupManager.closeAnimateFactory = function(v) return new FadeAnimate(v, 100);
|
||||
|
||||
var host:String = getHost();
|
||||
L.d('Init', 'host: ${host}');
|
||||
#if flash
|
||||
connection = new ru.m.connect.flash.FlashConnection<Request, Response>(host, 5000, Response);
|
||||
#elseif html5
|
||||
connection = new ru.m.connect.js.JsConnection<Request, Response>(host, 5000, Response);
|
||||
#else
|
||||
connection = new ru.m.connect.fake.FakeConnection<Request, Response>(Response);
|
||||
#end
|
||||
connection = buildConnection();
|
||||
networkManager = new NetworkManager();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,5 +146,7 @@ class Style {
|
||||
registerButton("close", "times-circle-solid.svg");
|
||||
registerButton("next", "arrow-alt-circle-right-solid.svg");
|
||||
registerButton("start", "play-circle-solid.svg");
|
||||
registerButton("login", "sign-in-solid.svg");
|
||||
registerButton("logout", "sign-out-solid.svg");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package ru.m.tankz.control;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.control.Control;
|
||||
|
||||
|
||||
class ClientNetworkControl extends HumanControl {
|
||||
|
||||
@:provide private var network:NetworkManager;
|
||||
|
||||
@@ -1,78 +1,90 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
import haxework.storage.IStorage;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.tankz.proto.pack.GameRequest;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.pack.StartGameRequest;
|
||||
import ru.m.tankz.proto.game.GameChangeProto;
|
||||
import ru.m.tankz.proto.game.GameActionTypeProto;
|
||||
import ru.m.tankz.proto.pack.GameUpdateRequest;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.game.GameChangeProto;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
import ru.m.tankz.proto.pack.ListGameRequest;
|
||||
import ru.m.tankz.proto.pack.LoginRequest;
|
||||
import ru.m.tankz.proto.pack.LogoutRequest;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.pack.StartGameRequest;
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
|
||||
typedef ClientConnection = IConnection<Request, Response>;
|
||||
|
||||
enum ConnectionState {
|
||||
OFFLINE;
|
||||
CONNECT;
|
||||
CONNECTED;
|
||||
LOGIN;
|
||||
ONLINE(user:User);
|
||||
ERROR(error:Dynamic);
|
||||
}
|
||||
|
||||
class NetworkManager {
|
||||
|
||||
public var state(default, null):String;
|
||||
public var stateSignal:Signal<String>;
|
||||
public var listGameSignal:Signal<Array<GameInfoProto>>;
|
||||
public var gameSignal:Signal<GameInfoProto>;
|
||||
public var state(default, null):ConnectionState;
|
||||
public var game(default, null):GameProto;
|
||||
public var user(default, null):UserProto;
|
||||
|
||||
public var stateSignal:Signal<ConnectionState>;
|
||||
public var listGameSignal:Signal<Array<GameProto>>;
|
||||
public var gameSignal:Signal<GameProto>;
|
||||
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
||||
public var user(default, null):User;
|
||||
public var game(default, set):NetworkGame;
|
||||
|
||||
@:provide private var connection:ClientConnection;
|
||||
@:provide private var storage:MultiplayerStorage;
|
||||
|
||||
public function new() {
|
||||
stateSignal = new Signal<String>();
|
||||
listGameSignal = new Signal<Array<GameInfoProto>>();
|
||||
gameSignal = new Signal<GameInfoProto>();
|
||||
gameUpdateSignal = new Signal<Array<GameChangeProto>>();
|
||||
updateState('offline');
|
||||
stateSignal = new Signal();
|
||||
listGameSignal = new Signal();
|
||||
gameSignal = new Signal();
|
||||
gameUpdateSignal = new Signal();
|
||||
updateState(OFFLINE);
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.receiveHandler.connect(onResponse);
|
||||
user = storage.user;
|
||||
if (user == null) {
|
||||
user = {name: 'User', uuid: null};
|
||||
var user = storage.user;
|
||||
if (user != null) {
|
||||
login(user.name, user.uuid);
|
||||
}
|
||||
}
|
||||
|
||||
private function updateState(value:String):Void {
|
||||
private function updateState(value:ConnectionState):Void {
|
||||
state = value;
|
||||
stateSignal.emit(value);
|
||||
}
|
||||
|
||||
public function login(name:String):Void {
|
||||
user.name = name;
|
||||
updateState('connect...');
|
||||
public function login(name:String, uuid:String = null):Void {
|
||||
updateState(CONNECT);
|
||||
connection.connect().then(function(c:ClientConnection) {
|
||||
updateState('login...');
|
||||
updateState(LOGIN);
|
||||
c.send(new Request().setLogin(
|
||||
new LoginRequest()
|
||||
.setUuid(user.uuid)
|
||||
.setName(user.name)
|
||||
.setUuid(uuid)
|
||||
.setName(name)
|
||||
));
|
||||
}).catchError(function(_) {});
|
||||
}).catchError(function(error) {
|
||||
updateState(ERROR(error));
|
||||
});
|
||||
}
|
||||
|
||||
public function logout():Void {
|
||||
connection.send(new Request().setLogout(new LogoutRequest()));
|
||||
}
|
||||
|
||||
public function listGame():Void {
|
||||
connection.send(new Request().setListGame(new ListGameRequest()));
|
||||
}
|
||||
|
||||
public function createGame(type:String):Void {
|
||||
connection.send(new Request().setCreateGame(new CreateGameRequest().setType(type)));
|
||||
public function createGame(type:String, level:Int):Void {
|
||||
connection.send(new Request().setCreateGame(new CreateGameRequest().setType(type).setLevel(level)));
|
||||
}
|
||||
|
||||
public function joinGame(gameId:Int):Void {
|
||||
@@ -88,70 +100,54 @@ class NetworkManager {
|
||||
}
|
||||
|
||||
public function action(action:TankAction):Void {
|
||||
var update:GameUpdateRequest = switch action {
|
||||
case TankAction.MOVE(direction):
|
||||
new GameUpdateRequest()
|
||||
.setType(GameActionTypeProto.MOVE)
|
||||
.setDirectionX(direction.x)
|
||||
.setDirectionY(direction.y);
|
||||
case TankAction.STOP:
|
||||
new GameUpdateRequest()
|
||||
.setType(GameActionTypeProto.STOP);
|
||||
case TankAction.SHOT:
|
||||
new GameUpdateRequest()
|
||||
.setType(GameActionTypeProto.SHOT);
|
||||
case _: null;
|
||||
}
|
||||
if (update != null) {
|
||||
connection.send(new Request().setUpdateGame(update));
|
||||
}
|
||||
// ToDo: network
|
||||
}
|
||||
|
||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||
updateState(switch (event) {
|
||||
case ConnectionEvent.CONNECTED:
|
||||
updateState(switch event {
|
||||
case CONNECTED:
|
||||
L.d('Network', '$event');
|
||||
'connected';
|
||||
case ConnectionEvent.DISCONNECTED:
|
||||
CONNECTED;
|
||||
case DISCONNECTED:
|
||||
L.d('Network', '$event');
|
||||
'offline';
|
||||
case ConnectionEvent.ERROR(error):
|
||||
OFFLINE;
|
||||
case ERROR(error):
|
||||
L.e('Network', '$error', error);
|
||||
'error';
|
||||
ERROR(error);
|
||||
});
|
||||
}
|
||||
|
||||
private function onResponse(packet:Response):Void {
|
||||
if (packet.hasLogin()) {
|
||||
user = {
|
||||
uuid: packet.login.user.uuid,
|
||||
name: packet.login.user.name,
|
||||
this.user = packet.login.user;
|
||||
var user = {
|
||||
uuid: this.user.uuid,
|
||||
name: this.user.name,
|
||||
};
|
||||
storage.user = user;
|
||||
updateState('online');
|
||||
updateState(ONLINE(user));
|
||||
} else if (packet.hasLogout()) {
|
||||
user = null;
|
||||
updateState('connected');
|
||||
storage.user = null;
|
||||
updateState(CONNECTED);
|
||||
} else if (packet.hasListGame()) {
|
||||
listGameSignal.emit(packet.listGame.games);
|
||||
} else if (packet.hasCreateGame()) {
|
||||
gameSignal.emit(packet.createGame.game);
|
||||
game = packet.createGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasJoinGame()) {
|
||||
gameSignal.emit(packet.joinGame.game);
|
||||
game = packet.joinGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasLeaveGame()) {
|
||||
gameSignal.emit(null);
|
||||
if (packet.leaveGame.user.uuid == user.uuid) {
|
||||
game = null;
|
||||
gameSignal.emit(null);
|
||||
} else {
|
||||
game = packet.leaveGame.game;
|
||||
gameSignal.emit(game);
|
||||
}
|
||||
} else if (packet.hasStartGame()) {
|
||||
gameSignal.emit(packet.startGame.game);
|
||||
} else if (packet.hasUpdateGame()) {
|
||||
gameUpdateSignal.emit(packet.updateGame.changes);
|
||||
} else if (packet.hasGame()) {
|
||||
game.load(packet.game.game);
|
||||
game = packet.startGame.game;
|
||||
gameSignal.emit(game);
|
||||
}
|
||||
}
|
||||
|
||||
private function set_game(value:NetworkGame):NetworkGame {
|
||||
this.game = value;
|
||||
connection.send(new Request().setGame(new GameRequest()));
|
||||
return this.game;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package ru.m.tankz.render;
|
||||
|
||||
import haxework.view.IView;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.game.IGame;
|
||||
|
||||
interface IRender extends IView<Dynamic> {
|
||||
public var game(default, set):IGame;
|
||||
interface IRender extends IView<Dynamic> extends GameListener {
|
||||
public var config(default, set):Config;
|
||||
public function draw():Void;
|
||||
public function reset():Void;
|
||||
}
|
||||
|
||||
@@ -14,17 +14,17 @@ import ru.m.geom.Point;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.render.item.BonusRenderItem;
|
||||
import ru.m.tankz.render.item.BrickRenderItem;
|
||||
import ru.m.tankz.render.item.BulletRenderItem;
|
||||
import ru.m.tankz.render.item.EagleRenderItem;
|
||||
import ru.m.tankz.render.item.IRenderItem;
|
||||
import ru.m.tankz.render.item.TankRenderItem;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class Render extends SpriteView implements IRender implements GameListener {
|
||||
class Render extends SpriteView implements IRender {
|
||||
|
||||
public var game(default, set):IGame;
|
||||
public var config(default, set):Config;
|
||||
|
||||
private var backgroundLayer:Sprite;
|
||||
private var groundLayer:Sprite;
|
||||
@@ -32,7 +32,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
private var upLayer:Sprite;
|
||||
private var upperLayer:Sprite;
|
||||
|
||||
private var background:Sprite;
|
||||
private var items:Map<Int, IRenderItem>;
|
||||
|
||||
public function new() {
|
||||
@@ -51,13 +50,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
reset();
|
||||
}
|
||||
|
||||
private function set_game(value:IGame):IGame {
|
||||
game = value;
|
||||
game.connect(this);
|
||||
return game;
|
||||
private function set_config(value:Config):Config {
|
||||
return config = value;
|
||||
}
|
||||
|
||||
private function drawBackground(config:Config):Void {
|
||||
private function drawBackground():Void {
|
||||
var width = config.map.cellWidth * config.map.gridWidth;
|
||||
var height = config.map.cellHeight * config.map.gridHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
@@ -72,9 +69,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
for (item in items) {
|
||||
item.update();
|
||||
}
|
||||
if (background == null) {
|
||||
drawBackground(game.config);
|
||||
}
|
||||
}
|
||||
|
||||
private function onEnterFrame(event:Event):Void {
|
||||
@@ -91,10 +85,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
item.dispose();
|
||||
}
|
||||
items = new Map();
|
||||
if (background != null) {
|
||||
backgroundLayer.removeChild(background);
|
||||
background = null;
|
||||
}
|
||||
clearLayer(entryLayer);
|
||||
clearLayer(groundLayer);
|
||||
clearLayer(upLayer);
|
||||
@@ -108,9 +98,10 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
case COMPLETE(_, _):
|
||||
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||
case SPAWN(BRICK(bricks)):
|
||||
drawBackground();
|
||||
for (brick in bricks) {
|
||||
var item:IRenderItem = new BrickRenderItem(brick.rect, brick.type);
|
||||
var config = game.config.getBrick(brick.type);
|
||||
var config = config.getBrick(brick.type);
|
||||
items[brick.id] = item;
|
||||
if (config.layer > 2) {
|
||||
upLayer.addChild(item.view);
|
||||
@@ -119,15 +110,15 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
}
|
||||
}
|
||||
case SPAWN(EAGLE(id, rect, teamId)):
|
||||
var item = new EagleRenderItem(rect);
|
||||
var item = new EagleRenderItem(rect, config.getColor(new PlayerId(teamId, -1)));
|
||||
items.set(id, item);
|
||||
entryLayer.addChild(item.view);
|
||||
item.update();
|
||||
case SPAWN(TANK(id, rect, playerId, info)):
|
||||
var item = new TankRenderItem(rect);
|
||||
var config = game.config.getTank(info.type);
|
||||
item.color = game.config.getColor(playerId);
|
||||
item.skin = config.skin;
|
||||
var tankConfig = config.getTank(info.type);
|
||||
item.color = config.getColor(playerId);
|
||||
item.skin = tankConfig.skin;
|
||||
item.hits = info.hits;
|
||||
item.bonus = info.bonus;
|
||||
items.set(id, item);
|
||||
@@ -144,6 +135,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
items.set(id, item);
|
||||
upperLayer.addChild(item.view);
|
||||
item.update();
|
||||
case MOVE(EAGLE(id, position)):
|
||||
if (items.exists(id)) {
|
||||
var item = items[id];
|
||||
item.move(position);
|
||||
}
|
||||
case MOVE(BULLET(id, position)):
|
||||
if (items.exists(id)) {
|
||||
var item = items[id];
|
||||
@@ -163,7 +159,7 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
case CHANGE(TANK(id, type, hits, bonus)):
|
||||
if (items.exists(id)) {
|
||||
var item:TankRenderItem = cast items[id];
|
||||
item.skin = game.config.getTank(type).skin;
|
||||
item.skin = config.getTank(type).skin;
|
||||
item.hits = hits;
|
||||
item.bonus = bonus;
|
||||
}
|
||||
@@ -177,6 +173,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
||||
var item:EagleRenderItem = cast items[id];
|
||||
item.protect = state;
|
||||
}
|
||||
case CHANGE(BRICK(id, type)):
|
||||
if (items.exists(id)) {
|
||||
var item:BrickRenderItem = cast items[id];
|
||||
item.type = type;
|
||||
}
|
||||
case DESTROY(TANK(id, shot)):
|
||||
if (items.exists(id)) {
|
||||
var item = items[id];
|
||||
|
||||
@@ -6,7 +6,8 @@ import ru.m.tankz.Type;
|
||||
class BonusRenderItem extends BitmapRenderItem {
|
||||
|
||||
public var type(default, set):BonusType;
|
||||
private var d = 0.1;
|
||||
private var d = -0.05;
|
||||
private var alpha = 1.0;
|
||||
|
||||
public function new(rect:Rectangle, type:BonusType) {
|
||||
super(rect);
|
||||
@@ -24,8 +25,9 @@ class BonusRenderItem extends BitmapRenderItem {
|
||||
|
||||
override public function update():Void {
|
||||
super.update();
|
||||
view.alpha += d;
|
||||
if (view.alpha < 0 || view.alpha > 1.5) {
|
||||
alpha += d;
|
||||
view.alpha = alpha;
|
||||
if (alpha <= 0 || alpha >= 1.5) {
|
||||
d = -d;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,14 +26,13 @@ class BrickRenderItem extends RenderItem {
|
||||
public function new(rect:Rectangle, type:BrickType) {
|
||||
super(rect);
|
||||
this.shape = new Shape();
|
||||
this.type = type;
|
||||
cells = [
|
||||
new Point(0, 0),
|
||||
new Point(0, 1),
|
||||
new Point(1, 0),
|
||||
new Point(1, 1),
|
||||
];
|
||||
redraw();
|
||||
this.type = type;
|
||||
move(rect.position);
|
||||
}
|
||||
|
||||
@@ -53,6 +52,7 @@ class BrickRenderItem extends RenderItem {
|
||||
if (type != value) {
|
||||
type = value;
|
||||
image = Assets.getBitmapData('resources/image/map/${type}.png');
|
||||
redraw();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
@@ -69,6 +69,7 @@ class BrickRenderItem extends RenderItem {
|
||||
|
||||
public function redraw():Void {
|
||||
shape.graphics.clear();
|
||||
if (type == "none") return;
|
||||
switch state {
|
||||
case UNBROKEN:
|
||||
shape.graphics.beginBitmapFill(image);
|
||||
|
||||
@@ -2,18 +2,22 @@ package ru.m.tankz.render.item;
|
||||
|
||||
import flash.display.DisplayObject;
|
||||
import flash.display.Sprite;
|
||||
import haxework.color.Color;
|
||||
import haxework.view.utils.BitmapUtil;
|
||||
import ru.m.animate.Animate;
|
||||
import ru.m.geom.Rectangle;
|
||||
|
||||
class EagleRenderItem extends BitmapRenderItem {
|
||||
public var color(default, default):Color;
|
||||
public var death(default, set):Bool = true;
|
||||
public var protect(default, set):Bool;
|
||||
|
||||
private var container:Sprite;
|
||||
private var protectView:Animate;
|
||||
|
||||
public function new(rect:Rectangle) {
|
||||
public function new(rect:Rectangle, color:Color) {
|
||||
super(rect);
|
||||
this.color = color;
|
||||
container = new Sprite();
|
||||
container.addChild(bitmap);
|
||||
protectView = AnimateBundle.tankProtect();
|
||||
@@ -45,6 +49,9 @@ class EagleRenderItem extends BitmapRenderItem {
|
||||
death = value;
|
||||
var suffix = death ? '-death' : '';
|
||||
image = 'resources/image/eagle/eagle${suffix}.png';
|
||||
if (!color.zero) {
|
||||
bitmap.bitmapData = bitmapData = BitmapUtil.colorize(bitmapData, color);
|
||||
}
|
||||
}
|
||||
return death;
|
||||
}
|
||||
|
||||
@@ -55,10 +55,7 @@ class TankRenderItem extends BitmapRenderItem {
|
||||
if (!color1.zero) {
|
||||
image2 = BitmapUtil.colorize(image2, color2);
|
||||
}
|
||||
images = [
|
||||
image1, image1, image1,
|
||||
image2, image2, image2,
|
||||
];
|
||||
images = [for (i in 0...6) image1].concat([for (i in 0...6) image2]);
|
||||
frame = 0;
|
||||
bitmap.bitmapData = images[frame];
|
||||
}
|
||||
|
||||
@@ -5,24 +5,30 @@ import flash.media.Sound;
|
||||
import flash.media.SoundChannel;
|
||||
import flash.media.SoundTransform;
|
||||
import openfl.utils.Assets;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame;
|
||||
|
||||
class SoundManager implements GameListener {
|
||||
private static var TAG(default, never):String = 'SoundManager';
|
||||
private static var TAG(default, never):String = "SoundManager";
|
||||
|
||||
#if flash
|
||||
private static var type:String = 'mp3';
|
||||
private static var type:String = "mp3";
|
||||
#else
|
||||
private static var type:String = 'ogg';
|
||||
private static var type:String = "ogg";
|
||||
#end
|
||||
|
||||
public var config(default, default):Config;
|
||||
public var volume(default, set):Float = 1;
|
||||
public var mute(default, set):Bool = false;
|
||||
|
||||
private var channels:Array<SoundChannel>;
|
||||
private var transform:SoundTransform;
|
||||
|
||||
private var humanTanks:Array<Int>;
|
||||
private var humanBullets:Array<Int>;
|
||||
private var liveBonuses:Array<Int>;
|
||||
|
||||
public function new() {
|
||||
channels = [];
|
||||
updateSoundTransform();
|
||||
@@ -77,30 +83,44 @@ class SoundManager implements GameListener {
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case START(_):
|
||||
play('start');
|
||||
case SPAWN(BULLET(_)):
|
||||
if (false /*ToDo: human tank*/) {
|
||||
play('shot');
|
||||
play("start");
|
||||
humanTanks = [];
|
||||
liveBonuses = [];
|
||||
humanBullets = [];
|
||||
case SPAWN(BULLET(id, _, playerId, _)):
|
||||
if (config.isHuman(playerId)) {
|
||||
play("shot");
|
||||
humanBullets.push(id);
|
||||
}
|
||||
case SPAWN(TANK(id, _, playerId, _)):
|
||||
if (config.isHuman(playerId)) {
|
||||
humanTanks.push(id);
|
||||
}
|
||||
case SPAWN(BONUS(id, _, type)):
|
||||
play("bonus_add");
|
||||
if (type == "live") {
|
||||
liveBonuses.push(id);
|
||||
}
|
||||
case SPAWN(BONUS(_, _)):
|
||||
play('bonus_add');
|
||||
case HIT(TANK(_, _)):
|
||||
play('bullet_hit');
|
||||
case DESTROY(TANK(_, _)):
|
||||
if (true /*ToDo: human tank*/) {
|
||||
play('boom_player');
|
||||
play("bullet_hit");
|
||||
case HIT(CELL(_, _, shot)):
|
||||
if (humanBullets.indexOf(shot.bulletId) > -1) {
|
||||
//play("bullet_block");
|
||||
}
|
||||
case DESTROY(TANK(id, _)):
|
||||
if (humanTanks.indexOf(id) > -1) {
|
||||
play("boom_player");
|
||||
} else {
|
||||
play('boom_bot');
|
||||
play("boom_bot");
|
||||
}
|
||||
case DESTROY(EAGLE(_, _)):
|
||||
play('boom_player');
|
||||
case DESTROY(BONUS(_, _)):
|
||||
// ToDo: bonus type
|
||||
play('bonus_get');
|
||||
if (false /*ToDo: bonus.type == 'life'*/) {
|
||||
play('live');
|
||||
play("boom_player");
|
||||
case DESTROY(BONUS(id, _)):
|
||||
play("bonus_get");
|
||||
if (liveBonuses.indexOf(id) > -1) {
|
||||
play("live");
|
||||
} else {
|
||||
play('bonus_get');
|
||||
play("bonus_get");
|
||||
}
|
||||
case _:
|
||||
}
|
||||
|
||||
@@ -15,3 +15,7 @@ views:
|
||||
$type: ru.m.tankz.view.SettingsFrame
|
||||
- id: record
|
||||
$type: ru.m.tankz.view.RecordFrame
|
||||
- id: game_list
|
||||
$type: ru.m.tankz.view.network.GameListFrame
|
||||
- id: game_room
|
||||
$type: ru.m.tankz.view.network.GameRoomFrame
|
||||
|
||||
@@ -47,35 +47,31 @@ import ru.m.tankz.view.game.GameView;
|
||||
}
|
||||
}
|
||||
|
||||
private function start(state:GameState):Void {
|
||||
private function buildGame(state:GameState):Void {
|
||||
gameView.type = state.type;
|
||||
game = new Game(state);
|
||||
gameView.render.game = game;
|
||||
soundManager.config = game.config;
|
||||
gameView.render.config = game.config;
|
||||
game.connect(gameView.render);
|
||||
game.connect(soundManager);
|
||||
game.connect(this);
|
||||
if (gameView.panel != null) {
|
||||
game.connect(gameView.panel);
|
||||
}
|
||||
//game.connect(new GameTracer());
|
||||
}
|
||||
|
||||
private function start(state:GameState):Void {
|
||||
buildGame(state);
|
||||
game.connect(this);
|
||||
recorder = new GameRecorder();
|
||||
game.connect(recorder);
|
||||
runner = new GameRunner(game);
|
||||
runner.start(state);
|
||||
gameView.render.draw();
|
||||
}
|
||||
|
||||
private function play(record:GameRecord):Void {
|
||||
gameView.type = record.info.type;
|
||||
game = new Game(record.state);
|
||||
gameView.render.game = game;
|
||||
game.connect(soundManager);
|
||||
//game.connect(this);
|
||||
if (gameView.panel != null) {
|
||||
game.connect(gameView.panel);
|
||||
}
|
||||
buildGame(record.state);
|
||||
player = new GamePlayer(game, record);
|
||||
player.start();
|
||||
gameView.render.draw();
|
||||
}
|
||||
|
||||
private function stop():Void {
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
package ru.m.tankz.view;
|
||||
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.frame.IFrameSwitcher;
|
||||
import haxework.view.IGroupView;
|
||||
import haxework.view.InputView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.list.ListView;
|
||||
import haxework.view.VGroupView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
import ru.m.tankz.proto.core.GameStateProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
|
||||
@:template class NetworkFrame extends VGroupView {
|
||||
public static var ID(default, never):String = "network";
|
||||
|
||||
@:view var frameSwitcher(default, null):IFrameSwitcher;
|
||||
|
||||
@:view var loginFrame(default, null):IGroupView;
|
||||
@:view var stateLabel(default, null):LabelView;
|
||||
@:view var nameInput(default, null):InputView;
|
||||
@:view var loginButton(default, null):ButtonView;
|
||||
|
||||
@:view var gameListFrame(default, null):IGroupView;
|
||||
@:view var createGameButton(default, null):ButtonView;
|
||||
@:view var gameList(default, null):ListView<GameInfoProto>;
|
||||
|
||||
@:view var gameFrame(default, null):IGroupView;
|
||||
@:view var leaveGameButton(default, null):ButtonView;
|
||||
@:view var startGameButton(default, null):ButtonView;
|
||||
@:view var userList(default, null):ListView<UserProto>;
|
||||
|
||||
@:provide var network:NetworkManager;
|
||||
@:provide var mainFrameSwitcher:IFrameSwitcher;
|
||||
|
||||
public function init():Void {
|
||||
loginButton.onPress = this;
|
||||
createGameButton.onPress = this;
|
||||
leaveGameButton.onPress = this;
|
||||
startGameButton.onPress = this;
|
||||
gameList.dispatcher.addListener({
|
||||
onListItemClick: function(item:IListItemView<GameInfoProto>):Void {
|
||||
network.joinGame(item.data.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
nameInput.text = network.user.name;
|
||||
onStateChange(network.state);
|
||||
network.stateSignal.connect(onStateChange);
|
||||
}
|
||||
|
||||
private function onStateChange(state:String):Void {
|
||||
loginButton.disabled = !(state == "offline" || state == "error");
|
||||
stateLabel.text = state;
|
||||
if (state == 'online') {
|
||||
gameList.data = [];
|
||||
frameSwitcher.change(gameListFrame.id);
|
||||
network.listGameSignal.connect(onListGame);
|
||||
network.gameSignal.connect(onGame);
|
||||
network.listGame();
|
||||
} else {
|
||||
frameSwitcher.change(loginFrame.id);
|
||||
}
|
||||
}
|
||||
|
||||
private function onListGame(games:Array<GameInfoProto>):Void {
|
||||
gameList.data = games;
|
||||
}
|
||||
|
||||
private function onGame(game:GameInfoProto):Void {
|
||||
if (game != null) {
|
||||
userList.data = game.players;
|
||||
frameSwitcher.change(gameFrame.id);
|
||||
if (game.state == GameStateProto.STARTED) {
|
||||
Provider.set(GameState, new GameState(ClassicGame.TYPE, ClassicGame.PLAYER1));
|
||||
mainFrameSwitcher.change(GameFrame.ID);
|
||||
}
|
||||
} else {
|
||||
frameSwitcher.change(gameListFrame.id);
|
||||
}
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.listGameSignal.disconnect(onListGame);
|
||||
network.gameSignal.disconnect(onGame);
|
||||
network.stateSignal.disconnect(onStateChange);
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
switch (view.id) {
|
||||
case 'loginButton':
|
||||
network.login(nameInput.text);
|
||||
case 'createGameButton':
|
||||
network.createGame('classic');
|
||||
case 'leaveGameButton':
|
||||
network.leaveGame();
|
||||
case 'startGameButton':
|
||||
network.startGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
---
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: frameSwitcher
|
||||
$type: haxework.view.frame.FrameSwitcher
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
# login
|
||||
- id: loginFrame
|
||||
$type: haxework.view.VGroupView
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: stateLabel
|
||||
$type: haxework.view.LabelView
|
||||
$style: label
|
||||
text: offline
|
||||
width: 200
|
||||
height: 50
|
||||
- id: nameInput
|
||||
$type: haxework.view.InputView
|
||||
$style: label
|
||||
width: 200
|
||||
height: 50
|
||||
text: User
|
||||
skin:
|
||||
$type: haxework.view.skin.ColorSkin
|
||||
color: '#444444'
|
||||
- id: loginButton
|
||||
$type: haxework.view.ButtonView
|
||||
$style: button
|
||||
text: Login
|
||||
# game list
|
||||
- id: gameListFrame
|
||||
$type: haxework.view.VGroupView
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: createGameButton
|
||||
$type: haxework.view.ButtonView
|
||||
$style: button
|
||||
text: Create
|
||||
- id: gameList
|
||||
$type: haxework.view.list.VListView<ru.m.tankz.proto.core.GameInfoProto>
|
||||
factory: "@class:ru.m.tankz.view.network.GameItemView"
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
paddings: 10
|
||||
scroll:
|
||||
$type: haxework.view.list.VScrollView
|
||||
width: 10
|
||||
pHeight: 100
|
||||
skin:
|
||||
$type: haxework.view.list.VScrollSkin
|
||||
skin:
|
||||
$type: haxework.view.skin.ColorSkin
|
||||
color: "#000000"
|
||||
alpha: 0
|
||||
# game
|
||||
- id: gameFrame
|
||||
$type: haxework.view.VGroupView
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: startGameButton
|
||||
$type: haxework.view.ButtonView
|
||||
$style: button
|
||||
text: Start
|
||||
- id: leaveGameButton
|
||||
$type: haxework.view.ButtonView
|
||||
$style: button
|
||||
text: Leave
|
||||
- id: userList
|
||||
$type: haxework.view.list.VListView<ru.m.tankz.proto.core.UserProto>
|
||||
factory: "@class:ru.m.tankz.view.network.UserItemView"
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
paddings: 10
|
||||
scroll:
|
||||
$type: haxework.view.list.VScrollView
|
||||
width: 10
|
||||
pHeight: 100
|
||||
skin:
|
||||
$type: haxework.view.list.VScrollSkin
|
||||
skin:
|
||||
$type: haxework.view.skin.ColorSkin
|
||||
color: "#000000"
|
||||
alpha: 0
|
||||
@@ -1,25 +1,69 @@
|
||||
package ru.m.tankz.view;
|
||||
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.Type.GameType;
|
||||
import ru.m.tankz.view.popup.FontPopup;
|
||||
import ru.m.tankz.view.popup.LoginPopup;
|
||||
|
||||
@:template class StartFrame extends VGroupView {
|
||||
|
||||
public static var ID(default, never):String = "start";
|
||||
|
||||
@:view var username:LabelView;
|
||||
@:view("login") var loginButton:ButtonView;
|
||||
@:view("logout") var logoutButton:ButtonView;
|
||||
@:view("network") var networkButton:ButtonView;
|
||||
|
||||
@:provide var state:GameState;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
private var fontPopup:FontPopup;
|
||||
|
||||
public function onShow():Void {
|
||||
onConnectionState(network.state);
|
||||
network.stateSignal.connect(onConnectionState);
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.stateSignal.disconnect(onConnectionState);
|
||||
}
|
||||
|
||||
private function onConnectionState(state:ConnectionState):Void {
|
||||
trace("state", state);
|
||||
setUser(switch state {
|
||||
case ONLINE(user): user;
|
||||
case _: null;
|
||||
});
|
||||
}
|
||||
|
||||
private function setUser(value:User):Void {
|
||||
username.text = value == null ? "" : value.name;
|
||||
loginButton.visible = value == null;
|
||||
logoutButton.visible = value != null;
|
||||
networkButton.disabled = value == null;
|
||||
}
|
||||
|
||||
private function startGame(type:GameType):Void {
|
||||
state = new GameState(type);
|
||||
switcher.change(LevelFrame.ID);
|
||||
}
|
||||
|
||||
private function login():Void {
|
||||
LoginPopup.instance.show().then(function(user:User):Void {
|
||||
L.d("Login", 'user: $user');
|
||||
});
|
||||
}
|
||||
|
||||
private function logout():Void {
|
||||
network.logout();
|
||||
}
|
||||
|
||||
private function choiceFont():Void {
|
||||
if (fontPopup == null) {
|
||||
fontPopup = new FontPopup();
|
||||
|
||||
@@ -1,40 +1,63 @@
|
||||
---
|
||||
views:
|
||||
- $type: haxework.view.VGroupView
|
||||
skinId: container
|
||||
layout.margin: 10
|
||||
views:
|
||||
- $type: haxework.view.LabelView
|
||||
text: Tank'z
|
||||
skinId: font
|
||||
fontSize: 100
|
||||
geometry.margin.bottom: 30
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('classic')
|
||||
text: Classic
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('dota')
|
||||
text: DotA
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('death')
|
||||
text: DeathMatch
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:switcher.change('record')
|
||||
text: Records
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- id: settings
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.settings
|
||||
+onPress: $code:switcher.change('settings')
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.width: 100%
|
||||
- $type: haxework.view.LabelView
|
||||
geometry.padding: [20, 5]
|
||||
skinId: text.box
|
||||
text: $r:text:version
|
||||
- $type: haxework.view.VGroupView
|
||||
skinId: container
|
||||
layout.margin: 10
|
||||
views:
|
||||
- $type: haxework.view.LabelView
|
||||
text: Tank'z
|
||||
skinId: font
|
||||
fontSize: 100
|
||||
geometry.margin.bottom: 30
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('classic')
|
||||
text: Classic
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('dota')
|
||||
text: DotA
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:startGame('death')
|
||||
text: DeathMatch
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:switcher.change('record')
|
||||
text: Records
|
||||
- id: network
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:switcher.change('game_list')
|
||||
text: Network
|
||||
disabled: true
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- id: settings
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.settings
|
||||
+onPress: $code:switcher.change('settings')
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.width: 100%
|
||||
- id: username
|
||||
$type: haxework.view.LabelView
|
||||
skinId: text
|
||||
geometry.margin.right: 10
|
||||
- id: login
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.login
|
||||
+onPress: $code:login()
|
||||
- id: logout
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.logout
|
||||
+onPress: $code:logout()
|
||||
visible: false
|
||||
- $type: haxework.view.LabelView
|
||||
geometry.hAlign: right
|
||||
geometry.vAlign: top
|
||||
geometry.padding: [20, 5]
|
||||
geometry.position: absolute
|
||||
geometry.margin: [0, 20, 20, 0]
|
||||
skinId: text.box
|
||||
text: $r:text:version
|
||||
|
||||
19
src/client/haxe/ru/m/tankz/view/network/GameItemView.hx
Executable file → Normal file
19
src/client/haxe/ru/m/tankz/view/network/GameItemView.hx
Executable file → Normal file
@@ -1,21 +1,24 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.list.ListView;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
|
||||
@:template class GameItemView extends HGroupView implements IListItemView<GameInfoProto> {
|
||||
@:template class GameItemView extends HGroupView implements IListItemView<GameProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):GameInfoProto;
|
||||
public var data(default, set):GameProto;
|
||||
|
||||
@:view var label(default, null):LabelView;
|
||||
@:view var label:LabelView;
|
||||
|
||||
private function set_data(value:GameInfoProto):GameInfoProto {
|
||||
private function set_data(value:GameProto):GameProto {
|
||||
data = value;
|
||||
label.text = '${data.type}';
|
||||
label.text = '[${value.creator.name}] ${value.type} ${value.level} (${value.players.length})';
|
||||
return data;
|
||||
}
|
||||
|
||||
public static function factory():GameItemView {
|
||||
return new GameItemView();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
---
|
||||
width: 440
|
||||
height: 44
|
||||
margins: 5
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 48
|
||||
views:
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
$style: label
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
text: ""
|
||||
skin:
|
||||
$type: haxework.view.skin.ColorSkin
|
||||
color: "#000000"
|
||||
alpha: 0.2
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.stretch: true
|
||||
skinId: text.box
|
||||
|
||||
54
src/client/haxe/ru/m/tankz/view/network/GameListFrame.hx
Normal file
54
src/client/haxe/ru/m/tankz/view/network/GameListFrame.hx
Normal file
@@ -0,0 +1,54 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.list.VListView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
@:template class GameListFrame extends VGroupView {
|
||||
public static inline var ID = "game_list";
|
||||
|
||||
@:view var games:VListView<GameProto>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
public function onShow():Void {
|
||||
network.listGameSignal.connect(onGameList);
|
||||
network.gameSignal.connect(onGame);
|
||||
network.stateSignal.connect(onState);
|
||||
network.listGame();
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.listGameSignal.disconnect(onGameList);
|
||||
network.gameSignal.disconnect(onGame);
|
||||
network.stateSignal.disconnect(onState);
|
||||
}
|
||||
|
||||
private function create():Void {
|
||||
network.createGame("classic", 0);
|
||||
}
|
||||
|
||||
private function onState(state:ConnectionState):Void {
|
||||
switch state {
|
||||
case ONLINE(_):
|
||||
case _: switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function onGameList(data:Array<GameProto>):Void {
|
||||
games.data = data;
|
||||
}
|
||||
|
||||
private function onGame(game:GameProto):Void {
|
||||
if (game != null) {
|
||||
switcher.change(GameRoomFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function selectGame(game:GameProto):Void {
|
||||
network.joinGame(game.id);
|
||||
}
|
||||
}
|
||||
27
src/client/haxe/ru/m/tankz/view/network/GameListFrame.yaml
Normal file
27
src/client/haxe/ru/m/tankz/view/network/GameListFrame.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
views:
|
||||
- $type: haxework.view.VGroupView
|
||||
skinId: container
|
||||
geometry.padding: 20
|
||||
views:
|
||||
- id: header
|
||||
$type: haxework.view.LabelView
|
||||
geometry.margin.bottom: 20
|
||||
skinId: text.header
|
||||
text: Games List
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.simple
|
||||
text: Create
|
||||
+onPress: $code:create()
|
||||
- id: games
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $code:ru.m.tankz.view.network.GameItemView.factory
|
||||
geometry.margin: 10
|
||||
+onItemSelect: $code:function(item) selectGame(item.data)
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:switcher.change('start')
|
||||
53
src/client/haxe/ru/m/tankz/view/network/GameRoomFrame.hx
Normal file
53
src/client/haxe/ru/m/tankz/view/network/GameRoomFrame.hx
Normal file
@@ -0,0 +1,53 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.list.VListView;
|
||||
import haxework.view.TextView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
@:template class GameRoomFrame extends VGroupView {
|
||||
|
||||
public static inline var ID = "game_room";
|
||||
|
||||
@:view var info:TextView;
|
||||
@:view var players:VListView<UserProto>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
private function refresh(game:GameProto):Void {
|
||||
if (game != null) {
|
||||
info.text = '[${game.creator.name}] ${game.type} ${game.level} (${game.players.length})';
|
||||
players.data = game.players;
|
||||
} else {
|
||||
Timer.delay(function() switcher.change(GameListFrame.ID), 1);
|
||||
}
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
refresh(network.game);
|
||||
network.gameSignal.connect(onGame);
|
||||
network.stateSignal.connect(onState);
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.gameSignal.disconnect(onGame);
|
||||
network.stateSignal.disconnect(onState);
|
||||
network.leaveGame();
|
||||
}
|
||||
|
||||
private function onGame(game:GameProto):Void {
|
||||
refresh(game);
|
||||
}
|
||||
|
||||
private function onState(state:ConnectionState):Void {
|
||||
switch state {
|
||||
case ONLINE(_):
|
||||
case _: switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
src/client/haxe/ru/m/tankz/view/network/GameRoomFrame.yaml
Normal file
25
src/client/haxe/ru/m/tankz/view/network/GameRoomFrame.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
views:
|
||||
- $type: haxework.view.VGroupView
|
||||
skinId: container
|
||||
geometry.padding: 20
|
||||
views:
|
||||
- id: header
|
||||
$type: haxework.view.LabelView
|
||||
geometry.margin.bottom: 20
|
||||
skinId: text.header
|
||||
text: Game Room
|
||||
- id: info
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
skinId: text.box
|
||||
- id: players
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $code:ru.m.tankz.view.network.PlayerItemView.factory
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:network.leaveGame()
|
||||
11
src/client/haxe/ru/m/tankz/view/network/UserItemView.hx → src/client/haxe/ru/m/tankz/view/network/PlayerItemView.hx
Executable file → Normal file
11
src/client/haxe/ru/m/tankz/view/network/UserItemView.hx → src/client/haxe/ru/m/tankz/view/network/PlayerItemView.hx
Executable file → Normal file
@@ -5,17 +5,20 @@ import haxework.view.LabelView;
|
||||
import haxework.view.list.ListView;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
|
||||
@:template class UserItemView extends HGroupView implements IListItemView<UserProto> {
|
||||
@:template class PlayerItemView extends HGroupView implements IListItemView<UserProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):UserProto;
|
||||
|
||||
@:view var label(default, null):LabelView;
|
||||
@:view var label:LabelView;
|
||||
|
||||
private function set_data(value:UserProto):UserProto {
|
||||
data = value;
|
||||
label.text = '${data.uuid} -- ${data.name}';
|
||||
label.text = '${value.name}';
|
||||
return data;
|
||||
}
|
||||
|
||||
public static function factory():PlayerItemView {
|
||||
return new PlayerItemView();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 48
|
||||
views:
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.stretch: true
|
||||
skinId: text.box
|
||||
@@ -1,15 +0,0 @@
|
||||
---
|
||||
width: 440
|
||||
height: 44
|
||||
margins: 5
|
||||
views:
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
$style: label
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
text: ""
|
||||
skin:
|
||||
$type: haxework.view.skin.ColorSkin
|
||||
color: "#000000"
|
||||
alpha: 0.2
|
||||
@@ -1,27 +1,27 @@
|
||||
---
|
||||
view:
|
||||
$type: haxework.view.VGroupView
|
||||
geometry.size.width: 400
|
||||
geometry.size.height: 80%
|
||||
geometry.padding: 10
|
||||
geometry.hAlign: center
|
||||
geometry.vAlign: middle
|
||||
skinId: dark
|
||||
views:
|
||||
- id: fonts
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $this:fontViewFactory
|
||||
+onItemSelect: $code:function(item) close(item.data)
|
||||
scroll:
|
||||
$type: haxework.view.list.VScrollBarView
|
||||
skinId: scroll.vertical
|
||||
- $type: haxework.view.HGroupView
|
||||
geometry.size.width: 100%
|
||||
geometry.margin.top: 10
|
||||
layout.hAlign: right
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.simple
|
||||
text: Cancel
|
||||
+onPress: $code:reject('cancel')
|
||||
$type: haxework.view.VGroupView
|
||||
geometry.size.width: 400
|
||||
geometry.size.height: 80%
|
||||
geometry.padding: 10
|
||||
geometry.hAlign: center
|
||||
geometry.vAlign: middle
|
||||
skinId: dark
|
||||
views:
|
||||
- id: fonts
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $this:fontViewFactory
|
||||
+onItemSelect: $code:function(item) close(item.data)
|
||||
scroll:
|
||||
$type: haxework.view.list.VScrollBarView
|
||||
skinId: scroll.vertical
|
||||
- $type: haxework.view.HGroupView
|
||||
geometry.size.width: 100%
|
||||
geometry.margin.top: 10
|
||||
layout.hAlign: right
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.simple
|
||||
text: Cancel
|
||||
+onPress: $code:reject('cancel')
|
||||
|
||||
@@ -2,34 +2,34 @@
|
||||
layout.hAlign: center
|
||||
layout.vAlign: middle
|
||||
view:
|
||||
$type: haxework.view.VGroupView
|
||||
layout.hAlign: center
|
||||
geometry.size.width: 400
|
||||
geometry.size.height: 400
|
||||
skinId: window
|
||||
views:
|
||||
- $type: haxework.view.HGroupView
|
||||
geometry.size.width: 100%
|
||||
geometry.padding: 10
|
||||
layout.vAlign: middle
|
||||
views:
|
||||
- id: name
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
geometry.margin.left: 10
|
||||
layout.hAlign: left
|
||||
skinId: text
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: window.close
|
||||
+onPress: $code:reject('close')
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.height: 100%
|
||||
- id: presets
|
||||
$type: haxework.view.DataView
|
||||
factory: $this:presetViewFactory
|
||||
+onDataSelect: $this:onPresetSelect
|
||||
layout:
|
||||
$type: haxework.view.layout.HorizontalLayout
|
||||
hAlign: center
|
||||
margin: 5
|
||||
skinId: panel
|
||||
$type: haxework.view.VGroupView
|
||||
layout.hAlign: center
|
||||
geometry.size.width: 400
|
||||
geometry.size.height: 400
|
||||
skinId: window
|
||||
views:
|
||||
- $type: haxework.view.HGroupView
|
||||
geometry.size.width: 100%
|
||||
geometry.padding: 10
|
||||
layout.vAlign: middle
|
||||
views:
|
||||
- id: name
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
geometry.margin.left: 10
|
||||
layout.hAlign: left
|
||||
skinId: text
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: window.close
|
||||
+onPress: $code:reject('close')
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.height: 100%
|
||||
- id: presets
|
||||
$type: haxework.view.DataView
|
||||
factory: $this:presetViewFactory
|
||||
+onDataSelect: $this:onPresetSelect
|
||||
layout:
|
||||
$type: haxework.view.layout.HorizontalLayout
|
||||
hAlign: center
|
||||
margin: 5
|
||||
skinId: panel
|
||||
|
||||
47
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.hx
Normal file
47
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.hx
Normal file
@@ -0,0 +1,47 @@
|
||||
package ru.m.tankz.view.popup;
|
||||
|
||||
import haxework.log.BaseLogger.LoggerUtil;
|
||||
import haxework.view.InputView;
|
||||
import haxework.view.popup.PopupView;
|
||||
import haxework.view.TextView;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
|
||||
@:template class LoginPopup extends PopupView<User> {
|
||||
|
||||
@:view var username:InputView;
|
||||
@:view var password:InputView;
|
||||
@:view var error:TextView;
|
||||
|
||||
@:provide static var network:NetworkManager;
|
||||
|
||||
override private function onShow():Void {
|
||||
super.onShow();
|
||||
network.stateSignal.connect(onStateChange);
|
||||
}
|
||||
|
||||
override private function onClose():Void {
|
||||
super.onClose();
|
||||
network.stateSignal.disconnect(onStateChange);
|
||||
}
|
||||
|
||||
private function onStateChange(state:ConnectionState):Void {
|
||||
switch state {
|
||||
case ONLINE(user): close(user);
|
||||
case ERROR(error): this.error.text = LoggerUtil.printError(error);
|
||||
case _: this.error.text = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function submit():Void {
|
||||
network.login(username.text);
|
||||
}
|
||||
|
||||
public static var instance(get, null):LoginPopup;
|
||||
|
||||
private static function get_instance():LoginPopup {
|
||||
if (instance == null) {
|
||||
instance = new LoginPopup();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
64
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.yaml
Normal file
64
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.yaml
Normal file
@@ -0,0 +1,64 @@
|
||||
---
|
||||
layout.hAlign: center
|
||||
layout.vAlign: middle
|
||||
view:
|
||||
$type: haxework.view.VGroupView
|
||||
layout.hAlign: center
|
||||
geometry.size.width: 400
|
||||
# geometry.size.height: 400
|
||||
skinId: window
|
||||
views:
|
||||
- $type: haxework.view.HGroupView
|
||||
geometry.size.width: 100%
|
||||
geometry.padding: 10
|
||||
layout.vAlign: middle
|
||||
views:
|
||||
- id: name
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
geometry.margin.left: 10
|
||||
layout.hAlign: left
|
||||
skinId: text
|
||||
text: Login
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: window.close
|
||||
+onPress: $code:reject('close')
|
||||
- $type: haxework.view.VGroupView
|
||||
geometry.size.width: 100%
|
||||
# geometry.size.stretch: true
|
||||
geometry.padding: 20
|
||||
layout.margin: 5
|
||||
views:
|
||||
- $type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
skinId: text
|
||||
text: Username
|
||||
- id: username
|
||||
$type: haxework.view.InputView
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 28
|
||||
skinId: text.box
|
||||
- $type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
skinId: text
|
||||
text: Password
|
||||
- id: password
|
||||
$type: haxework.view.InputView
|
||||
textField.displayAsPassword: true
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 28
|
||||
skinId: text.box
|
||||
- id: error
|
||||
$type: haxework.view.TextView
|
||||
geometry.size.width: 100%
|
||||
skinId: text
|
||||
fill: false
|
||||
- $type: haxework.view.HGroupView
|
||||
layout.hAlign: center
|
||||
layout.margin: 5
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.simple
|
||||
text: Submit
|
||||
+onPress: $code:submit()
|
||||
1
src/client/resources/image/icon/sign-in-solid.svg
Normal file
1
src/client/resources/image/icon/sign-in-solid.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="sign-in" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="svg-inline--fa fa-sign-in fa-w-16 fa-2x"><path fill="currentColor" d="M137.2 110.3l21.9-21.9c9.3-9.3 24.5-9.4 33.9-.1L344.9 239c9.5 9.4 9.5 24.7 0 34.1L193 423.7c-9.4 9.3-24.5 9.3-33.9-.1l-21.9-21.9c-9.7-9.7-9.3-25.4.8-34.7l77.6-71.1H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h191.5l-77.6-71.1c-10-9.1-10.4-24.9-.7-34.5zM512 352V160c0-53-43-96-96-96h-84c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h84c17.7 0 32 14.3 32 32v192c0 17.7-14.3 32-32 32h-84c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h84c53 0 96-43 96-96z" class=""></path></svg>
|
||||
|
After Width: | Height: | Size: 698 B |
1
src/client/resources/image/icon/sign-out-solid.svg
Normal file
1
src/client/resources/image/icon/sign-out-solid.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="sign-out" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="svg-inline--fa fa-sign-out fa-w-16 fa-2x"><path fill="currentColor" d="M180 448H96c-53 0-96-43-96-96V160c0-53 43-96 96-96h84c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12H96c-17.7 0-32 14.3-32 32v192c0 17.7 14.3 32 32 32h84c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm117.9-303.1l77.6 71.1H184c-13.3 0-24 10.7-24 24v32c0 13.3 10.7 24 24 24h191.5l-77.6 71.1c-10.1 9.2-10.4 25-.8 34.7l21.9 21.9c9.3 9.3 24.5 9.4 33.9.1l152-150.8c9.5-9.4 9.5-24.7 0-34.1L353 88.3c-9.4-9.3-24.5-9.3-33.9.1l-21.9 21.9c-9.7 9.6-9.3 25.4.7 34.6z" class=""></path></svg>
|
||||
|
After Width: | Height: | Size: 695 B |
60
src/common/haxe/ru/m/Timer.hx
Normal file
60
src/common/haxe/ru/m/Timer.hx
Normal file
@@ -0,0 +1,60 @@
|
||||
package ru.m;
|
||||
|
||||
#if neko
|
||||
|
||||
import haxe.Log;
|
||||
import haxe.PosInfos;
|
||||
import neko.vm.Thread;
|
||||
|
||||
class Timer {
|
||||
|
||||
private var sleep:Float;
|
||||
private var stopped:Bool;
|
||||
|
||||
public function new(time_ms:Int) {
|
||||
this.sleep = time_ms / 1000.0;
|
||||
this.stopped = false;
|
||||
Thread.create(function() {
|
||||
while (!stopped) {
|
||||
Sys.sleep(sleep);
|
||||
try {
|
||||
run();
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function stop() {
|
||||
stopped = true;
|
||||
}
|
||||
|
||||
public dynamic function run() {}
|
||||
|
||||
public static function delay(f:Void -> Void, time_ms:Int) {
|
||||
var t = new Timer(time_ms);
|
||||
t.run = function() {
|
||||
t.stop();
|
||||
f();
|
||||
};
|
||||
return t;
|
||||
}
|
||||
|
||||
public static function measure<T>(f:Void -> T, ?pos:PosInfos):T {
|
||||
var t0 = stamp();
|
||||
var r = f();
|
||||
Log.trace((stamp() - t0) + "s", pos);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static inline function stamp():Float {
|
||||
return Sys.time();
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
typedef Timer = haxe.Timer;
|
||||
|
||||
#end
|
||||
@@ -7,7 +7,6 @@ import promhx.Promise;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
|
||||
class BaseConnection<O:Message, I:Message> implements IConnection<O, I> {
|
||||
public var handler(default, null):Signal<ConnectionEvent>;
|
||||
public var sendHandler(default, null):Signal<O>;
|
||||
@@ -17,12 +16,11 @@ class BaseConnection<O:Message, I:Message> implements IConnection<O, I> {
|
||||
|
||||
private var connectDeferred:Deferred<IConnection<O, I>>;
|
||||
|
||||
public function new(i:Class<I>) {
|
||||
queue = new PacketQueue<I>(i);
|
||||
public function new(inputFactory:Class<I>) {
|
||||
queue = new PacketQueue<I>(inputFactory);
|
||||
handler = new Signal<ConnectionEvent>();
|
||||
sendHandler = new Signal<O>();
|
||||
receiveHandler = new Signal<I>();
|
||||
connectDeferred = new Deferred();
|
||||
}
|
||||
|
||||
public function connect():Promise<IConnection<O, I>> {
|
||||
|
||||
@@ -5,7 +5,6 @@ import haxe.io.Bytes;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
|
||||
|
||||
enum ConnectionEvent {
|
||||
CONNECTED;
|
||||
DISCONNECTED;
|
||||
|
||||
20
src/common/haxe/ru/m/connect/PacketUtil.hx
Normal file
20
src/common/haxe/ru/m/connect/PacketUtil.hx
Normal file
@@ -0,0 +1,20 @@
|
||||
package ru.m.connect;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
|
||||
class PacketUtil {
|
||||
|
||||
public static function fromBytes<P:Message>(bytes:Bytes, factory:Class<P>):P {
|
||||
var packet:P = Type.createInstance(factory, []);
|
||||
packet.mergeFrom(bytes);
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static function toBytes<P:Message>(packet:P):Bytes {
|
||||
var out = new BytesOutput();
|
||||
packet.writeTo(out);
|
||||
return out.getBytes();
|
||||
}
|
||||
}
|
||||
74
src/common/haxe/ru/m/connect/desktop/DesktopConnection.hx
Normal file
74
src/common/haxe/ru/m/connect/desktop/DesktopConnection.hx
Normal file
@@ -0,0 +1,74 @@
|
||||
package ru.m.connect.desktop;
|
||||
|
||||
import cpp.vm.Thread;
|
||||
import haxe.Timer;
|
||||
import promhx.Deferred;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import sys.net.Host;
|
||||
import sys.net.Socket;
|
||||
|
||||
class DesktopConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
private var reader:Thread;
|
||||
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
socket = new Socket();
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
sendHandler.connect(_send);
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
connectDeferred = new Deferred();
|
||||
try {
|
||||
if (connected) {
|
||||
connectDeferred.resolve(this);
|
||||
} else {
|
||||
socket.connect(new Host(host), port);
|
||||
connected = true;
|
||||
reader = Thread.create(_read);
|
||||
connectDeferred.resolve(this);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
Timer.delay(function() connectDeferred.throwError(error), 1);
|
||||
}
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(DISCONNECTED);
|
||||
}
|
||||
|
||||
private function _read():Void {
|
||||
try {
|
||||
while (connected) {
|
||||
socket.waitForRead();
|
||||
var size = socket.input.readUInt16();
|
||||
var data = socket.input.read(size);
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receiveHandler.emit(packet);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ERROR(error));
|
||||
}
|
||||
}
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import ru.m.connect.IConnection.ConnectionEvent;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
|
||||
|
||||
class FakeConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package ru.m.connect.flash;
|
||||
|
||||
import ru.m.connect.IConnection.ConnectionEvent;
|
||||
import promhx.Promise;
|
||||
import flash.utils.Endian;
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
import haxe.io.Bytes;
|
||||
import flash.events.ErrorEvent;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.events.Event;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import flash.net.Socket;
|
||||
|
||||
import flash.utils.Endian;
|
||||
import haxe.io.Bytes;
|
||||
import promhx.Deferred;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
@@ -20,8 +19,8 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
|
||||
public function new(host:String, port:Int, i:Class<I>) {
|
||||
super(i);
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
@@ -37,14 +36,15 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
socket.connect(host, port);
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
if (socket.connected) {
|
||||
socket.close();
|
||||
//connected = false;
|
||||
//handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,13 +52,19 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
connectDeferred.throwError(event);
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.throwError(event);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
connectDeferred.resolve(this);
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.resolve(this);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
@@ -69,21 +75,19 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
try {
|
||||
var b = new flash.utils.ByteArray();
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
pushData(bs);
|
||||
var data = new flash.utils.ByteArray();
|
||||
socket.readBytes(data);
|
||||
var bytes = Bytes.ofData(data);
|
||||
pushData(bytes);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
}
|
||||
}
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
var out = new BytesOutput();
|
||||
packet.writeTo(out);
|
||||
var bytes = out.getBytes();
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.writeShort(bytes.length);
|
||||
socket.writeBytes(cast bytes.getData());
|
||||
socket.writeBytes(bytes.getData());
|
||||
socket.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package ru.m.connect.js;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import js.Browser;
|
||||
import js.html.BinaryType;
|
||||
import js.html.WebSocket;
|
||||
import promhx.Deferred;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
import ru.m.Base64;
|
||||
import ru.m.connect.IConnection.ConnectionEvent;
|
||||
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
@@ -14,35 +15,36 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
private var port:Int;
|
||||
private var socket:WebSocket;
|
||||
|
||||
public function new(host:String, port:Int, i:Class<I>) {
|
||||
super(i);
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
}
|
||||
|
||||
public static function isSecured():Bool {
|
||||
return Browser.location.protocol == "https:";
|
||||
}
|
||||
|
||||
private function buildSocket(host:String, port:Int):WebSocket {
|
||||
var protocol = switch Browser.location.protocol {
|
||||
case 'http:': 'ws:';
|
||||
case 'https:': 'wss:';
|
||||
case _: 'ws:';
|
||||
}
|
||||
var protocol = isSecured() ? "wss:" : "ws:";
|
||||
return new WebSocket('$protocol//$host:$port');
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
var self = this;
|
||||
var decodeBytes = Base64.decodeBase64;
|
||||
socket = buildSocket(host, port);
|
||||
socket.binaryType = BinaryType.ARRAYBUFFER;
|
||||
socket.onopen = this.onConnect;
|
||||
socket.onclose = this.onClose;
|
||||
socket.onerror = this.onError;
|
||||
socket.onmessage = this.onSocketData;
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
}
|
||||
|
||||
@@ -66,8 +68,8 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
try {
|
||||
var data:String = event.data;
|
||||
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
||||
var bytes = Bytes.ofData(event.data);
|
||||
var packet:I = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
@@ -76,6 +78,7 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
override public function send(packet:O):Void {
|
||||
super.send(packet);
|
||||
socket.send(WebSocketTools.packet2string(packet));
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.send(bytes.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package ru.m.connect.neko;
|
||||
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
import sys.net.Socket;
|
||||
|
||||
|
||||
class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
public var socket(default, null):Socket;
|
||||
@@ -20,9 +18,7 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
try {
|
||||
var b = new BytesOutput();
|
||||
packet.writeTo(b);
|
||||
var bytes = b.getBytes();
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
@@ -30,4 +26,4 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package ru.m.connect.neko;
|
||||
|
||||
import haxe.crypto.BaseCode;
|
||||
import haxe.crypto.Sha1;
|
||||
import protohx.Message;
|
||||
import haxe.io.Bytes;
|
||||
import haxe.io.BytesBuffer;
|
||||
import protohx.Message;
|
||||
import sys.net.Socket;
|
||||
|
||||
|
||||
class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
|
||||
private var opened:Bool;
|
||||
|
||||
@@ -17,12 +17,8 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
}
|
||||
|
||||
override private function _send(packet:O):Void {
|
||||
try {
|
||||
var data = WebSocketTools.packet2string(packet);
|
||||
writeData(data, socket);
|
||||
} catch (error:Dynamic) {
|
||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
||||
}
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
}
|
||||
|
||||
override public function pushData(bytes:Bytes):Void {
|
||||
@@ -37,13 +33,12 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
} else {
|
||||
var data = parseData(bytes);
|
||||
if (data != null) {
|
||||
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function encodeBase64(content:String):String {
|
||||
var suffix = switch (content.length % 3)
|
||||
{
|
||||
@@ -74,8 +69,8 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
socket.output.writeString(s);
|
||||
}
|
||||
|
||||
private function writeData(data:String, socket:sys.net.Socket, isServer = true):Void {
|
||||
socket.output.writeByte(0x81);
|
||||
private function writeData(data:Bytes, socket:sys.net.Socket, isServer = true):Void {
|
||||
socket.output.writeByte(130);
|
||||
|
||||
var len = 0;
|
||||
if (data.length < 126) len = data.length;
|
||||
@@ -98,7 +93,7 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
}
|
||||
|
||||
if (isServer) {
|
||||
socket.output.writeString(data);
|
||||
socket.output.writeBytes(data, 0, data.length);
|
||||
}
|
||||
else {
|
||||
var mask = [ Std.random(256), Std.random(256), Std.random(256), Std.random(256) ];
|
||||
@@ -106,29 +101,29 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
socket.output.writeByte(mask[1]);
|
||||
socket.output.writeByte(mask[2]);
|
||||
socket.output.writeByte(mask[3]);
|
||||
var maskedData = new StringBuf();
|
||||
var maskedData = new BytesBuffer();
|
||||
for (i in 0...data.length) {
|
||||
maskedData.addChar(data.charCodeAt(i) ^ mask[i % 4]);
|
||||
maskedData.addByte(data.get(i) ^ mask[i % 4]);
|
||||
}
|
||||
socket.output.writeString(maskedData.toString());
|
||||
socket.output.writeBytes(maskedData.getBytes(), 0, maskedData.length);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseData(bytes:Bytes):String {
|
||||
private function parseData(bytes:Bytes):Bytes {
|
||||
var p = 0;
|
||||
var opcode = bytes.get(p++);
|
||||
|
||||
if (opcode == 0x00) {
|
||||
var s = "";
|
||||
var data = new BytesBuffer();
|
||||
var b:Int;
|
||||
while ((b = bytes.get(p++)) != 0xFF) {
|
||||
s += String.fromCharCode(b);
|
||||
data.addByte(b);
|
||||
}
|
||||
return s;
|
||||
return data.getBytes();
|
||||
}
|
||||
|
||||
// 0x81 = fin & text
|
||||
if (opcode == 0x81) {
|
||||
// 130 = binary data
|
||||
if (opcode == 130) {
|
||||
var len = bytes.get(p++);
|
||||
|
||||
// mask
|
||||
@@ -159,13 +154,13 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
|
||||
//Lib.println("mask = " + mask);
|
||||
|
||||
var data = new StringBuf();
|
||||
var data = new BytesBuffer();
|
||||
for (i in 0...len) {
|
||||
data.addChar(bytes.get(p++) ^ mask[i % 4]);
|
||||
data.addByte(bytes.get(p++) ^ mask[i % 4]);
|
||||
}
|
||||
|
||||
//Lib.println("readed = " + data.toString());
|
||||
return data.toString();
|
||||
return data.getBytes();
|
||||
} else {
|
||||
throw "Expected masked data.";
|
||||
}
|
||||
@@ -180,4 +175,4 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package ru.m.tankz.config;
|
||||
|
||||
import haxework.color.Color;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
typedef CompleteRule = {
|
||||
@@ -222,4 +223,12 @@ class Config {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isHuman(playerId:PlayerId) {
|
||||
var controller:Controller = AController.fromString(getPlayer(playerId).control);
|
||||
return switch controller {
|
||||
case HUMAN(_): true;
|
||||
case _: false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package ru.m.tankz.control;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
interface IControlFactory {
|
||||
public function build(id:PlayerId, controller:Controller):Control;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package ru.m.tankz.control;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
class NoneControlFactory implements IControlFactory {
|
||||
|
||||
public function new() {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.m.tankz.core;
|
||||
|
||||
import haxe.Timer;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.Timer;
|
||||
|
||||
class Modificator extends Signal2<Int, Bool> {
|
||||
|
||||
|
||||
@@ -19,12 +19,14 @@ typedef EntityPoint = {
|
||||
class EntityBuilder {
|
||||
|
||||
private var config:Config;
|
||||
private var entityId:Int;
|
||||
|
||||
public function new(config:Config) {
|
||||
this.config = config;
|
||||
this.entityId = 0;
|
||||
}
|
||||
|
||||
private function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
||||
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
||||
return new Rectangle(
|
||||
(point.x + 1) * config.map.cellWidth - width / 2,
|
||||
(point.y + 1) * config.map.cellHeight - height / 2,
|
||||
@@ -34,35 +36,37 @@ class EntityBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public function buildEagle(id:Int, point:EntityPoint, teamId:TeamId):Eagle {
|
||||
public function buildEagle(point:EntityPoint, teamId:TeamId):Eagle {
|
||||
var eageleConfig = config.getTeam(teamId).eagle;
|
||||
var eagle = new Eagle(id, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), teamId, eageleConfig);
|
||||
var eagle = new Eagle(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), teamId, eageleConfig);
|
||||
eagle.color = config.getColor(new PlayerId(teamId, -1));
|
||||
return eagle;
|
||||
}
|
||||
|
||||
public function buildTank(id:Int, point:EntityPoint, playerId:PlayerId, type:TankType):Tank {
|
||||
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, bonusOff:Bool = false):Tank {
|
||||
var playerConfig = config.getPlayer(playerId);
|
||||
var tankConfig = config.getTank(type);
|
||||
var tank = new Tank(id, 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.bonus = Math.random() < playerConfig.bonus;
|
||||
if (!bonusOff) {
|
||||
tank.bonus = Math.random() < playerConfig.bonus;
|
||||
}
|
||||
if (playerConfig.protect > 0) {
|
||||
tank.protect.on(playerConfig.protect);
|
||||
}
|
||||
return tank;
|
||||
}
|
||||
|
||||
public function buildBullet(id:Int, point:Point, direction:Direction, playerId:PlayerId, type:TankType):Bullet {
|
||||
public function buildBullet(point:Point, direction:Direction, playerId:PlayerId, type:TankType):Bullet {
|
||||
var tankConfig = config.getTank(type);
|
||||
var bulletConfig = tankConfig.bullet;
|
||||
var bullet = new Bullet(id, new Rectangle(point.x - bulletConfig.width / 2, point.y - bulletConfig.height / 2, bulletConfig.width, bulletConfig.height, direction), playerId, bulletConfig);
|
||||
var bullet = new Bullet(++entityId, new Rectangle(point.x - bulletConfig.width / 2, point.y - bulletConfig.height / 2, bulletConfig.width, bulletConfig.height, direction), playerId, bulletConfig);
|
||||
return bullet;
|
||||
}
|
||||
|
||||
public function buildBonus(id:Int, point:EntityPoint, type:BonusType):Bonus {
|
||||
public function buildBonus(point:EntityPoint, type:BonusType):Bonus {
|
||||
var bonusConfig = config.getBonus(type);
|
||||
var bonus = new Bonus(id, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), bonusConfig);
|
||||
var bonus = new Bonus(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), bonusConfig);
|
||||
return bonus;
|
||||
}
|
||||
}
|
||||
|
||||
50
src/common/haxe/ru/m/tankz/game/EventUtil.hx
Normal file
50
src/common/haxe/ru/m/tankz/game/EventUtil.hx
Normal file
@@ -0,0 +1,50 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.core.EntityType.EntityTypeResolver;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import ru.m.tankz.core.Tank;
|
||||
import ru.m.tankz.map.Brick;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.map.LevelMap;
|
||||
|
||||
class EventUtil {
|
||||
|
||||
public static function buildBricksSpawn(map:LevelMap):GameEvent {
|
||||
var bricks = map.bricks.map(function(item:Brick):BrickInfo {
|
||||
return {
|
||||
id: item.id,
|
||||
x: item.cellY,
|
||||
y: item.cellY,
|
||||
rect: item.rect,
|
||||
type: item.config.type,
|
||||
}
|
||||
});
|
||||
return GameEvent.SPAWN(BRICK(bricks));
|
||||
}
|
||||
|
||||
public static function buildEagleSpawn(eagle:Eagle):GameEvent {
|
||||
return GameEvent.SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team));
|
||||
}
|
||||
|
||||
public static function buildTankSpawn(tank:Tank):GameEvent {
|
||||
return GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
||||
type:tank.config.type,
|
||||
hits:tank.hits,
|
||||
bonus:tank.bonus
|
||||
}));
|
||||
}
|
||||
|
||||
public static function buildMove(entity:Entity):GameEvent {
|
||||
return switch EntityTypeResolver.of(entity) {
|
||||
case EAGLE(eagle):
|
||||
GameEvent.MOVE(EAGLE(entity.id, entity.rect.position));
|
||||
case TANK(tank):
|
||||
GameEvent.MOVE(TANK(entity.id, entity.rect.position));
|
||||
case BULLET(bullet):
|
||||
GameEvent.MOVE(BULLET(entity.id, entity.rect.position));
|
||||
case _:
|
||||
null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ enum DestroyEvent {
|
||||
}
|
||||
|
||||
enum MoveEvent {
|
||||
EAGLE(id:Int, position:Position);
|
||||
TANK(id:Int, position:Position);
|
||||
BULLET(id:Int, position:Position);
|
||||
}
|
||||
@@ -65,6 +66,7 @@ enum ChangeEvent {
|
||||
PLAYER_LIFE(playerId:PlayerId, value:Int);
|
||||
TEAM_SCORE(teamId:TeamId, value:Int);
|
||||
TEAM_LIFE(teamId:TeamId, value:Int);
|
||||
BRICK(id:Int, type:BrickType);
|
||||
}
|
||||
|
||||
enum GameEvent {
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxe.ds.Option;
|
||||
import haxe.Timer;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.geom.Direction;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.geom.Position;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
@@ -19,15 +16,14 @@ import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.Spawner;
|
||||
import ru.m.tankz.map.Brick;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.Timer;
|
||||
|
||||
class GameRunner implements EngineListener implements GameListener {
|
||||
@:provide var controlFactory:IControlFactory;
|
||||
|
||||
private var game(default, null):IGame;
|
||||
private var gameEventSignal(get, null):Signal<GameEvent>;
|
||||
private var entityId:Int;
|
||||
private var timer:Timer;
|
||||
private var builder:EntityBuilder;
|
||||
|
||||
@@ -36,7 +32,6 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
this.builder = new EntityBuilder(this.game.config);
|
||||
this.game.connect(this);
|
||||
this.game.engine.connect(this);
|
||||
this.entityId = 0;
|
||||
}
|
||||
|
||||
private inline function get_gameEventSignal():Signal<GameEvent> {
|
||||
@@ -56,14 +51,6 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
game.engine.disconnect(this);
|
||||
}
|
||||
|
||||
private function pointToPosition(point:{x:Int, y:Int, direction:String}):Position {
|
||||
return {
|
||||
x: (point.x + 1) * game.config.map.cellWidth,
|
||||
y: (point.y + 1) * game.config.map.cellHeight,
|
||||
direction: Direction.fromString(point.direction),
|
||||
}
|
||||
}
|
||||
|
||||
public function start(state:GameState):Void {
|
||||
for (team in game.teams.iterator()) {
|
||||
for (player in team.players.iterator()) {
|
||||
@@ -80,24 +67,16 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
}
|
||||
if (team.config.eagle != null) {
|
||||
var point = game.config.getPoint(team.id, "eagle");
|
||||
var eagle = builder.buildEagle(++entityId, point, team.id);
|
||||
var point = team.spawner.getPoint("eagle");
|
||||
var eagle = builder.buildEagle(point, team.id);
|
||||
game.engine.spawn(eagle);
|
||||
gameEventSignal.emit(GameEvent.SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team)));
|
||||
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
||||
eagle.protect.connect(onEagleProtectChange);
|
||||
}
|
||||
}
|
||||
var bricks = game.engine.map.bricks.map(function(item:Brick):BrickInfo {
|
||||
return {
|
||||
id: item.id,
|
||||
x: item.cellY,
|
||||
y: item.cellY,
|
||||
rect: item.rect,
|
||||
type: item.config.type,
|
||||
}
|
||||
});
|
||||
gameEventSignal.emit(GameEvent.SPAWN(BRICK(bricks)));
|
||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(game.engine.map));
|
||||
gameEventSignal.emit(GameEvent.START(state));
|
||||
//for (i in 0...10) spawnBonus();
|
||||
}
|
||||
|
||||
public function next():Option<GameState> {
|
||||
@@ -112,9 +91,9 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
private function spawn(task:SpawnTask):Void {
|
||||
var tank = builder.buildTank(++entityId, task.point, task.playerId, task.tankType);
|
||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType);
|
||||
game.engine.spawn(tank);
|
||||
gameEventSignal.emit(GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {type:tank.config.type, hits:tank.hits, bonus:tank.bonus})));
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
tank.protect.connect(onTankProtectChange);
|
||||
tank.freezing.connect(onTankFreezingChange);
|
||||
}
|
||||
@@ -265,7 +244,7 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
|
||||
direction: "right",
|
||||
}
|
||||
var bonus = builder.buildBonus(++entityId, point, type);
|
||||
var bonus = builder.buildBonus(point, type);
|
||||
game.engine.spawn(bonus);
|
||||
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
|
||||
}
|
||||
@@ -353,7 +332,7 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
|
||||
var rect = tank.rect;
|
||||
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
||||
var bullet = builder.buildBullet(++entityId, point, rect.direction, player.id, tank.config.type);
|
||||
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
|
||||
bullet.tank = tank;
|
||||
bullet.move(bullet.rect.direction);
|
||||
game.engine.spawn(bullet);
|
||||
|
||||
@@ -13,7 +13,10 @@ class State {
|
||||
public var hits:Int;
|
||||
|
||||
public function new() {
|
||||
|
||||
score = 0;
|
||||
frags = 0;
|
||||
shots = 0;
|
||||
hits = 0;
|
||||
}
|
||||
|
||||
public function add(state:State) {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxe.Timer;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
import ru.m.Timer;
|
||||
|
||||
typedef SpawnTask = {
|
||||
var point:SpawnPoint;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
class NetworkGame extends Game {
|
||||
|
||||
private static var TAG(default, never):String = 'NetworkGame';
|
||||
|
||||
public function load(proto:GameProto):Void {
|
||||
L.w(TAG, 'load: ${proto}');
|
||||
// ToDo:
|
||||
}
|
||||
|
||||
public function export():GameProto {
|
||||
return new GameProto();
|
||||
}
|
||||
}
|
||||
@@ -14,24 +14,11 @@ enum GameStateProto {
|
||||
ENDED = 2;
|
||||
}
|
||||
|
||||
message BrickProto {
|
||||
string type = 1;
|
||||
}
|
||||
|
||||
message EntityProto {
|
||||
string type = 1;
|
||||
}
|
||||
|
||||
message GameInfoProto {
|
||||
message GameProto {
|
||||
int32 id = 1;
|
||||
string type = 2;
|
||||
UserProto creator = 3;
|
||||
repeated UserProto players = 4;
|
||||
GameStateProto state = 5;
|
||||
int32 level = 3;
|
||||
UserProto creator = 4;
|
||||
repeated UserProto players = 5;
|
||||
GameStateProto state = 6;
|
||||
}
|
||||
|
||||
message GameProto {
|
||||
GameInfoProto info = 1;
|
||||
repeated BrickProto map = 2;
|
||||
repeated EntityProto entities = 3;
|
||||
}
|
||||
@@ -30,16 +30,17 @@ message LogoutResponse {}
|
||||
message ListGameRequest {}
|
||||
|
||||
message ListGameResponse {
|
||||
repeated ru.m.tankz.proto.core.GameInfoProto games = 1;
|
||||
repeated ru.m.tankz.proto.core.GameProto games = 1;
|
||||
}
|
||||
|
||||
// Create Game
|
||||
message CreateGameRequest {
|
||||
string type = 1;
|
||||
int32 level = 2;
|
||||
}
|
||||
|
||||
message CreateGameResponse {
|
||||
ru.m.tankz.proto.core.GameInfoProto game = 1;
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
}
|
||||
|
||||
// Join Game
|
||||
@@ -48,41 +49,25 @@ message JoinGameRequest {
|
||||
}
|
||||
|
||||
message JoinGameResponse {
|
||||
ru.m.tankz.proto.core.GameInfoProto game = 1;
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
ru.m.tankz.proto.core.UserProto user = 2;
|
||||
}
|
||||
|
||||
// Leave Game
|
||||
message LeaveGameRequest {}
|
||||
|
||||
message LeaveGameResponse {
|
||||
ru.m.tankz.proto.core.GameInfoProto game = 1;
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
ru.m.tankz.proto.core.UserProto user = 2;
|
||||
}
|
||||
|
||||
// Start Game
|
||||
message StartGameRequest {}
|
||||
|
||||
message StartGameResponse {
|
||||
ru.m.tankz.proto.core.GameInfoProto game = 1;
|
||||
}
|
||||
|
||||
// Game
|
||||
message GameRequest {}
|
||||
|
||||
message GameResponse {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
}
|
||||
|
||||
// Game Update
|
||||
message GameUpdateRequest {
|
||||
ru.m.tankz.proto.game.GameActionTypeProto type = 1;
|
||||
int32 directionX = 2;
|
||||
int32 directionY = 3;
|
||||
}
|
||||
|
||||
message GameUpdateResponse {
|
||||
repeated ru.m.tankz.proto.game.GameChangeProto changes = 1;
|
||||
}
|
||||
|
||||
// Request
|
||||
message Request {
|
||||
oneof content {
|
||||
@@ -93,8 +78,6 @@ message Request {
|
||||
JoinGameRequest joinGame = 5;
|
||||
LeaveGameRequest leaveGame = 6;
|
||||
StartGameRequest startGame = 7;
|
||||
GameRequest game = 8;
|
||||
GameUpdateRequest updateGame = 9;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +91,7 @@ message Response {
|
||||
JoinGameResponse joinGame = 5;
|
||||
LeaveGameResponse leaveGame = 6;
|
||||
StartGameResponse startGame = 7;
|
||||
GameResponse game = 8;
|
||||
GameUpdateResponse updateGame = 9;
|
||||
|
||||
ErrorResponse error = 999;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,126 +1,65 @@
|
||||
package ru.m.tankz.editor.level;
|
||||
|
||||
import flash.display.DisplayObjectContainer;
|
||||
import flash.display.Graphics;
|
||||
import flash.display.Sprite;
|
||||
import flash.events.MouseEvent;
|
||||
import haxework.color.Color;
|
||||
import haxework.view.SpriteView;
|
||||
import haxework.view.utils.BitmapUtil;
|
||||
import openfl.Assets;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.game.EntityBuilder;
|
||||
import ru.m.tankz.game.EventUtil;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.map.Brick;
|
||||
import ru.m.tankz.map.LevelMap;
|
||||
import ru.m.tankz.render.RenderItem;
|
||||
import ru.m.tankz.render.Render;
|
||||
import ru.m.tankz.Type.PlayerId;
|
||||
|
||||
class SpawnPointEntity extends Entity {
|
||||
|
||||
public var point(default, null):SpawnPoint;
|
||||
|
||||
public function new(id:Int, point:SpawnPoint, rect:Rectangle) {
|
||||
super(id, rect);
|
||||
this.point = point;
|
||||
}
|
||||
}
|
||||
|
||||
class SpawnPointItem extends BitmapItem<SpawnPointEntity> {
|
||||
|
||||
private var cellX:Int = -1;
|
||||
private var cellY:Int = -1;
|
||||
private var src:String;
|
||||
private var color:Color;
|
||||
|
||||
public function new(value:SpawnPoint, config:Config) {
|
||||
src = getSrc(value, config);
|
||||
super(new SpawnPointEntity(0, value, new Rectangle(
|
||||
value.x * config.map.cellWidth,
|
||||
value.y * config.map.cellHeight,
|
||||
config.map.cellWidth * 2,
|
||||
config.map.cellHeight * 2
|
||||
)));
|
||||
color = config.getColor(new PlayerId(value.team, value.index));
|
||||
}
|
||||
|
||||
public static function getSrc(value:SpawnPoint, config:Config):String {
|
||||
var preset:GamePreset = config.presets[config.presets.length - 1];
|
||||
var tankConfig:TankConfig = null;
|
||||
if (value.type == 'tank') {
|
||||
var player = config.getPlayer(new PlayerId(value.team, value.index < 0 ? 0 : value.index));
|
||||
var tankType = player.tanks[0];
|
||||
tankConfig = config.getTank(tankType.type);
|
||||
}
|
||||
return switch(value.type) {
|
||||
case 'eagle': 'resources/image/eagle/eagle.png';
|
||||
case 'tank': 'resources/image/tank/${tankConfig.skin}-0.png';
|
||||
case _: 'resources/image/eagle/eagle-death.png';
|
||||
}
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
super.update();
|
||||
if (cellX != value.point.x || cellY != value.point.y) {
|
||||
var image = Assets.getBitmapData(getImage());
|
||||
cellX = value.point.x;
|
||||
cellY = value.point.y;
|
||||
value.rect.x = cellX * (value.rect.width / 2) + (value.rect.width - image.width) / 2;
|
||||
value.rect.y = cellY * (value.rect.height / 2) + (value.rect.height - image.height) / 2;
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
override public function redraw():Void {
|
||||
var image = Assets.getBitmapData(getImage());
|
||||
if (!color.zero) {
|
||||
image = BitmapUtil.colorize(image, color);
|
||||
}
|
||||
view.bitmapData = image;
|
||||
}
|
||||
|
||||
override private function getImage():String {
|
||||
return src;
|
||||
}
|
||||
}
|
||||
|
||||
enum Brush {
|
||||
POINT(point:SpawnPoint);
|
||||
BRICK(brick:BrickConfig);
|
||||
}
|
||||
|
||||
//ToDo: copy paste from ru.m.tankz.render.Render
|
||||
class MapEditView extends SpriteView {
|
||||
@:dispatcher(GameListener) class MapEditView extends Render {
|
||||
|
||||
public var config(default, set):Config;
|
||||
public var data(get, set):LevelConfig;
|
||||
public var map(default, null):LevelMap;
|
||||
public var brush(default, default):Brush;
|
||||
|
||||
private var items:Map<String, RenderItem<Dynamic, Dynamic>>;
|
||||
|
||||
private var backgroundLayer:Sprite;
|
||||
private var upLayer:Sprite;
|
||||
private var groundLayer:Sprite;
|
||||
private var spawnLayer:Sprite;
|
||||
private var builder:EntityBuilder;
|
||||
private var entityId:Int;
|
||||
private var pointEntities:Map<String, Entity>;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
items = new Map();
|
||||
content.mouseChildren = false;
|
||||
map = null;
|
||||
backgroundLayer = new Sprite();
|
||||
upLayer = new Sprite();
|
||||
groundLayer = new Sprite();
|
||||
spawnLayer = new Sprite();
|
||||
content.addChild(backgroundLayer);
|
||||
content.addChild(groundLayer);
|
||||
content.addChild(upLayer);
|
||||
content.addChild(spawnLayer);
|
||||
pointEntities = null;
|
||||
content.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
||||
connect(this);
|
||||
reset();
|
||||
}
|
||||
|
||||
override private function set_config(value:Config):Config {
|
||||
builder = new EntityBuilder(value);
|
||||
return super.set_config(value);
|
||||
}
|
||||
|
||||
override private function drawBackground():Void {
|
||||
super.drawBackground();
|
||||
var mapWidth = map.gridWidth * map.cellWidth;
|
||||
var mapHeight = map.gridHeight * map.cellHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
g.lineStyle(1, 0x007700);
|
||||
for (x in 0...map.gridWidth) {
|
||||
g.moveTo(x * map.cellWidth, 0);
|
||||
g.lineTo(x * map.cellWidth, mapHeight);
|
||||
}
|
||||
for (y in 0...map.gridHeight) {
|
||||
g.moveTo(0, y * map.cellHeight);
|
||||
g.lineTo(mapWidth, y * map.cellHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private function onMouseDown(event:MouseEvent):Void {
|
||||
content.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
||||
content.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
||||
@@ -137,92 +76,21 @@ class MapEditView extends SpriteView {
|
||||
if (b != null) {
|
||||
switch (brush) {
|
||||
case Brush.POINT(point):
|
||||
for (p in config.points) {
|
||||
if (p.team == point.team && p.type == point.type && p.index == point.index) {
|
||||
p.x = b.cellX;
|
||||
p.y = b.cellY;
|
||||
drawMap();
|
||||
drawMap();
|
||||
break;
|
||||
}
|
||||
}
|
||||
case Brush.BRICK(brick):
|
||||
b.config = brick;
|
||||
drawMap();
|
||||
var entity = pointEntities[pointKey(point)];
|
||||
point.x = b.cellX;
|
||||
point.y = b.cellY;
|
||||
var rect = builder.buildRect(point, entity.rect.width, entity.rect.height);
|
||||
entity.rect.position = rect.position;
|
||||
gameEventSignal.emit(EventUtil.buildMove(entity));
|
||||
case Brush.BRICK(brickConfig):
|
||||
b.config = config.getBrick(brickConfig.type);
|
||||
gameEventSignal.emit(GameEvent.CHANGE(BRICK(b.id, brickConfig.type)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function clearLayer(layer:DisplayObjectContainer) {
|
||||
while (layer.numChildren > 0) layer.removeChildAt(0);
|
||||
}
|
||||
|
||||
public function reset():Void {
|
||||
for (item in items.iterator()) {
|
||||
item.dispose();
|
||||
}
|
||||
items = new Map();
|
||||
clearLayer(groundLayer);
|
||||
clearLayer(upLayer);
|
||||
clearLayer(spawnLayer);
|
||||
}
|
||||
|
||||
private function drawBackground():Void {
|
||||
var mapWidth = map.gridWidth * map.cellWidth;
|
||||
var mapHeight = map.gridHeight * map.cellHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
g.clear();
|
||||
g.beginFill(0x000000);
|
||||
g.drawRect(0, 0, mapWidth, mapHeight);
|
||||
g.endFill();
|
||||
g.lineStyle(1, 0x007700);
|
||||
for (x in 0...map.gridWidth) {
|
||||
g.moveTo(x * map.cellWidth, 0);
|
||||
g.lineTo(x * map.cellWidth, mapHeight);
|
||||
}
|
||||
for (y in 0...map.gridHeight) {
|
||||
g.moveTo(0, y * map.cellHeight);
|
||||
g.lineTo(mapWidth, y * map.cellHeight);
|
||||
}
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
if (this.map != null) {
|
||||
drawBackground();
|
||||
drawMap();
|
||||
}
|
||||
super.update();
|
||||
}
|
||||
|
||||
private function drawMap() {
|
||||
for (brick in map.bricks) if (brick.config.index > 0) {
|
||||
var key = Std.string(brick.id);
|
||||
if (!items.exists(key)) {
|
||||
items[key] = new BrickItem(brick);
|
||||
if (brick.config.layer > 2) {
|
||||
upLayer.addChild(items[key].view);
|
||||
} else {
|
||||
groundLayer.addChild(items[key].view);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (point in config.points) {
|
||||
var key = '${point.team}:${point.type}:${point.index}';
|
||||
if (!items.exists(key)) {
|
||||
items[key] = new SpawnPointItem(point, config);
|
||||
spawnLayer.addChild(items[key].view);
|
||||
}
|
||||
}
|
||||
for (item in items) {
|
||||
item.update();
|
||||
}
|
||||
}
|
||||
|
||||
private function set_config(value:Config):Config {
|
||||
config = value;
|
||||
map = new LevelMap(config.map);
|
||||
setContentSize(map.gridWidth * map.cellWidth, map.gridHeight * map.cellHeight, "map");
|
||||
return config;
|
||||
private static function pointKey(point:SpawnPoint):String {
|
||||
return '${point.type}:${point.team}:${point.index}';
|
||||
}
|
||||
|
||||
private function get_data():LevelConfig {
|
||||
@@ -234,16 +102,35 @@ class MapEditView extends SpriteView {
|
||||
|
||||
private function set_data(value:LevelConfig):LevelConfig {
|
||||
reset();
|
||||
pointEntities = new Map();
|
||||
map = new LevelMap(config.map);
|
||||
setContentSize(map.gridWidth * map.cellWidth, map.gridHeight * map.cellHeight, "map");
|
||||
map.setData(value.data);
|
||||
if (value.points != null) for (point in value.points) {
|
||||
for (p in config.points) {
|
||||
if (p.team == point.team && p.type == point.type && p.index == point.index) {
|
||||
p.x = point.x;
|
||||
p.y = point.y;
|
||||
break;
|
||||
}
|
||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(map));
|
||||
for (point in config.points) {
|
||||
switch point.type {
|
||||
case "eagle":
|
||||
var eagle = builder.buildEagle(point, point.team);
|
||||
pointEntities[pointKey(point)] = eagle;
|
||||
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
||||
case "tank":
|
||||
var playerId = new PlayerId(point.team, point.index < 0 ? 0 : point.index);
|
||||
var player = config.getPlayer(playerId);
|
||||
var tankSpawn = player.tanks[0];
|
||||
var tank = builder.buildTank(point, playerId, tankSpawn.type, true);
|
||||
pointEntities[pointKey(point)] = tank;
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
}
|
||||
}
|
||||
if (value.points != null) {
|
||||
for (point in value.points) {
|
||||
var entity = pointEntities[pointKey(point)];
|
||||
var rect = builder.buildRect(point, entity.rect.width, entity.rect.height);
|
||||
entity.rect.position = rect.position;
|
||||
gameEventSignal.emit(EventUtil.buildMove(entity));
|
||||
}
|
||||
}
|
||||
draw();
|
||||
toUpdate();
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package ru.m.tankz.editor.level;
|
||||
|
||||
import haxework.color.Color;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.editor.level.MapEditView;
|
||||
import ru.m.tankz.Type.PlayerId;
|
||||
|
||||
class SpawnPointView extends BrushView<SpawnPoint> {
|
||||
@@ -15,7 +13,18 @@ class SpawnPointView extends BrushView<SpawnPoint> {
|
||||
}
|
||||
|
||||
override private function resolveSrc(value:SpawnPoint):String {
|
||||
return SpawnPointItem.getSrc(value, Provider.get(Config));
|
||||
var preset:GamePreset = config.presets[config.presets.length - 1];
|
||||
var tankConfig:TankConfig = null;
|
||||
if (value.type == 'tank') {
|
||||
var player = config.getPlayer(new PlayerId(value.team, value.index < 0 ? 0 : value.index));
|
||||
var tankType = player.tanks[0];
|
||||
tankConfig = config.getTank(tankType.type);
|
||||
}
|
||||
return switch(value.type) {
|
||||
case 'eagle': 'resources/image/eagle/eagle.png';
|
||||
case 'tank': 'resources/image/tank/${tankConfig.skin}-0.png';
|
||||
case _: 'resources/image/eagle/eagle-death.png';
|
||||
}
|
||||
}
|
||||
|
||||
public static inline function factory(index:Int, value:SpawnPoint) {
|
||||
|
||||
@@ -1,67 +1,61 @@
|
||||
package ru.m.tankz.server;
|
||||
|
||||
import ru.m.tankz.control.NoneControlFactory;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import haxe.io.Bytes;
|
||||
import haxework.log.TraceLogger;
|
||||
import haxework.provider.Provider;
|
||||
import neko.net.ThreadServer;
|
||||
import ru.m.connect.IConnection.ConnectionEvent;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
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.ServerLevelBundle;
|
||||
import ru.m.tankz.server.session.Session;
|
||||
import ru.m.tankz.server.game.GameManager;
|
||||
import ru.m.tankz.server.game.IGameManager;
|
||||
import ru.m.tankz.server.session.GameSession;
|
||||
import sys.net.Socket;
|
||||
#if debug import haxework.log.SocketLogger; #end
|
||||
|
||||
|
||||
class Server extends ThreadServer<Session, Bytes> {
|
||||
class Server extends ThreadServer<GameSession, Bytes> {
|
||||
|
||||
private static inline var TAG = 'Server';
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
override public function clientConnected(s:Socket):Session {
|
||||
var session = new Session(s);
|
||||
override public function clientConnected(socket:Socket):GameSession {
|
||||
var session = new GameSession(socket);
|
||||
L.d(TAG, 'Client connected');
|
||||
return session;
|
||||
}
|
||||
|
||||
override public function clientDisconnected(session:Session) {
|
||||
override public function clientDisconnected(session:GameSession) {
|
||||
L.d(TAG, 'Client disconnected');
|
||||
session.connection.handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
session.disconnect();
|
||||
}
|
||||
|
||||
override public function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||
override public function readClientMessage(session:GameSession, buf:Bytes, pos:Int, len:Int) {
|
||||
//L.d(TAG, 'Client message: ${buf}');
|
||||
return {msg: buf.sub(pos, len), bytes: len};
|
||||
}
|
||||
|
||||
override public function clientMessage(session:Session, bytes:Bytes) {
|
||||
try {
|
||||
session.pushData(bytes);
|
||||
} catch (error:Dynamic) {
|
||||
L.e(TAG, 'error', error);
|
||||
}
|
||||
override public function clientMessage(session:GameSession, bytes:Bytes) {
|
||||
session.pushData(bytes);
|
||||
}
|
||||
|
||||
@:provide static var gameManager:IGameManager;
|
||||
|
||||
public static function main() {
|
||||
L.push(new TraceLogger());
|
||||
#if debug
|
||||
L.push(new SocketLogger());
|
||||
L.push(new haxework.log.SocketLogger());
|
||||
#end
|
||||
L.d(TAG, 'Running');
|
||||
L.i(TAG, 'Build: ${CompilationOption.get("build")}');
|
||||
Provider.setFactory(IConfigBundle, ServerConfigBundle);
|
||||
Provider.setFactory(ILevelBundle, ServerLevelBundle);
|
||||
Provider.setFactory(IControlFactory, NoneControlFactory);
|
||||
var host:String = Sys.args().length > 0 ? Sys.args()[0] : "localhost";
|
||||
gameManager = new GameManager();
|
||||
var host:String = Sys.args().length > 0 ? Sys.args()[0] : "0.0.0.0";
|
||||
var port:Int = Sys.args().length > 1 ? Std.parseInt(Sys.args()[1]) : 5000;
|
||||
var wserver = new Server();
|
||||
L.i(TAG, 'Start on ${host}:${port}');
|
||||
wserver.run(host, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import sys.io.File;
|
||||
import yaml.Parser;
|
||||
import yaml.Yaml;
|
||||
|
||||
|
||||
class ServerConfigBundle implements IConfigBundle {
|
||||
|
||||
public function new() {}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package ru.m.tankz.server.db;
|
||||
|
||||
import ru.m.tankz.db.Orm;
|
||||
import ru.m.tankz.proto.Person;
|
||||
import ru.m.tankz.proto.Account;
|
||||
|
||||
class DbProvider {
|
||||
|
||||
private var orm:Orm;
|
||||
|
||||
public function new() {
|
||||
var db = new orm.Db("mysql://shmyga:password@localhost:3306/armageddon");
|
||||
orm = new Orm(db);
|
||||
}
|
||||
|
||||
public function getAccount(login:String, password:String):Null<Account> {
|
||||
var account = null;
|
||||
var accountModel = orm.account.getByLogin(login);
|
||||
if (accountModel != null && accountModel.password == password) {
|
||||
account = new Account().setLogin(accountModel.login);
|
||||
var personsModels = orm.person.getByAccount_id(accountModel.id);
|
||||
for (personModel in personsModels) {
|
||||
account.addPersons(new Person().setId(personModel.id).setName(personModel.name));
|
||||
}
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
public function getPerson(person_id:Int):Null<Person> {
|
||||
var person = null;
|
||||
var personModel = orm.person.get(person_id);
|
||||
if (personModel != null) {
|
||||
person = new Person().setId(personModel.id).setName(personModel.name);
|
||||
}
|
||||
return person;
|
||||
}
|
||||
}
|
||||
@@ -1,147 +1,78 @@
|
||||
package ru.m.tankz.server.game;
|
||||
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkGame;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.proto.pack.StartGameResponse;
|
||||
import ru.m.tankz.proto.pack.LeaveGameResponse;
|
||||
import ru.m.tankz.proto.pack.JoinGameResponse;
|
||||
import ru.m.tankz.proto.pack.ListGameResponse;
|
||||
import ru.m.tankz.proto.pack.CreateGameResponse;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.GameStateProto;
|
||||
import ru.m.tankz.proto.core.GameInfoProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.server.session.Thread;
|
||||
import ru.m.tankz.server.session.Session;
|
||||
import ru.m.tankz.server.game.IGameManager;
|
||||
|
||||
@:dispatcher(GameManagerListener) class GameManager implements IGameManager {
|
||||
public var games(default, null):Array<ServerGame>;
|
||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
||||
public var gamesByCreator(default, null):Map<String, ServerGame>;
|
||||
public var gamesByUser(default, null):Map<String, ServerGame>;
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
class NekoTimer {
|
||||
private var counter:Int;
|
||||
|
||||
private var sleep:Float;
|
||||
private var stopped:Bool;
|
||||
|
||||
public function new(time_ms:Int) {
|
||||
this.sleep = time_ms / 1000.0;
|
||||
this.stopped = false;
|
||||
Thread.create(function() {
|
||||
while (!stopped) {
|
||||
Sys.sleep(sleep);
|
||||
try {
|
||||
run();
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
public function new() {
|
||||
counter = 0;
|
||||
games = [];
|
||||
gamesById = new Map();
|
||||
gamesByCreator = new Map();
|
||||
gamesByUser = new Map();
|
||||
}
|
||||
|
||||
public dynamic function run() {}
|
||||
public function create(user:UserProto, type:String, level:Int):ServerGame {
|
||||
if (gamesByCreator.exists(user.uuid)) {
|
||||
delete(gamesByCreator[user.uuid].proto.id);
|
||||
}
|
||||
var proto = new GameProto()
|
||||
.setId(++counter)
|
||||
.setCreator(user)
|
||||
.setType(type)
|
||||
.setLevel(level)
|
||||
.setPlayers([user]);
|
||||
var game = new ServerGame(proto);
|
||||
games.push(game);
|
||||
gamesById[game.proto.id] = game;
|
||||
gamesByCreator[game.proto.creator.uuid] = game;
|
||||
createSignal.emit(game);
|
||||
return game;
|
||||
}
|
||||
|
||||
public function stop() {
|
||||
stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
class GameManager {
|
||||
|
||||
public static var byGameId:Map<Int, GameManager> = new Map<Int, GameManager>();
|
||||
public static var byPersonId:Map<String, GameManager> = new Map<String, GameManager>();
|
||||
public static var subscribers:Map<String, Bool> = new Map<String, Bool>();
|
||||
|
||||
private static var idCounter:Int = 0;
|
||||
|
||||
public var gameInfo(default, null):GameInfoProto;
|
||||
public var game(default, null):NetworkGame;
|
||||
|
||||
private var timer:NekoTimer;
|
||||
|
||||
//private var changes:Array<GameChange> = new Array<GameChange>();
|
||||
|
||||
public function new(creator:UserProto) {
|
||||
gameInfo = new GameInfoProto()
|
||||
.setId(idCounter++)
|
||||
.setState(GameStateProto.READY)
|
||||
.setCreator(creator);
|
||||
gameInfo.addPlayers(creator);
|
||||
byGameId.set(gameInfo.id, this);
|
||||
byPersonId.set(creator.uuid, this);
|
||||
broadcast(new Response().setCreateGame(new CreateGameResponse().setGame(gameInfo)));
|
||||
broadcastGames();
|
||||
}
|
||||
|
||||
public static function getReadyGames():Array<GameInfoProto> {
|
||||
return Lambda.array(Lambda.filter(Lambda.map(GameManager.byGameId, function(gm) return gm.gameInfo), function(game) return game.state == GameStateProto.READY));
|
||||
}
|
||||
|
||||
public function broadcastGames() {
|
||||
var packet = new Response().setListGame(new ListGameResponse().setGames(getReadyGames()));
|
||||
for (personId in subscribers.keys()) {
|
||||
var session:Session = Session.sessions.get(personId);
|
||||
session.send(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public function broadcast(packet:Response) {
|
||||
for (player in gameInfo.players) {
|
||||
var session:Session = Session.sessions.get(player.uuid);
|
||||
session.send(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public function join(user:UserProto) {
|
||||
gameInfo.addPlayers(user);
|
||||
byPersonId.set(user.uuid, this);
|
||||
broadcast(new Response().setJoinGame(new JoinGameResponse().setGame(gameInfo)));
|
||||
}
|
||||
|
||||
public function leave(user:UserProto) {
|
||||
gameInfo.setPlayers(gameInfo.players.filter(function(p:UserProto) return p.uuid != user.uuid));
|
||||
byPersonId.remove(user.uuid);
|
||||
var packet = new Response().setLeaveGame(new LeaveGameResponse().setGame(gameInfo));
|
||||
Session.sessions.get(user.uuid).send(packet);
|
||||
if (gameInfo.players.length == 0/* || person.id == game.creator.id*/) {
|
||||
stop();
|
||||
} else {
|
||||
broadcast(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public function start() {
|
||||
gameInfo.setState(GameStateProto.STARTED);
|
||||
game = new NetworkGame(ClassicGame.TYPE);
|
||||
game.start(new GameState(ClassicGame.TYPE,ClassicGame.PLAYER1));
|
||||
timer = new NekoTimer(30);
|
||||
timer.run = update;
|
||||
broadcast(new Response().setStartGame(new StartGameResponse().setGame(gameInfo)));
|
||||
}
|
||||
|
||||
public function stop() {
|
||||
gameInfo.setState(GameStateProto.ENDED);
|
||||
gameInfo.setPlayers([]);
|
||||
byGameId.remove(gameInfo.id);
|
||||
for (p in gameInfo.players) byPersonId.remove(p.uuid);
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
broadcast(new Response().setLeaveGame(new LeaveGameResponse().setGame(gameInfo)));
|
||||
broadcastGames();
|
||||
}
|
||||
|
||||
private function update() {
|
||||
game.engine.update();
|
||||
/*var changes = engine.update();
|
||||
changes = this.changes.concat(changes);
|
||||
this.changes = [];
|
||||
if (changes.length > 0) {
|
||||
broadcast(new GameUpdateResponse().setChanges(changes));
|
||||
}*/
|
||||
public function join(gameId:Int, user:UserProto):Void {
|
||||
if (gamesById.exists(gameId)) {
|
||||
var game = gamesById[gameId];
|
||||
game.proto.players.push(user);
|
||||
gamesByUser[user.uuid] = game;
|
||||
changeSignal.emit(game, JOIN(user));
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(gameId:Int):Void {
|
||||
if (gamesById.exists(gameId)) {
|
||||
var game = gamesById[gameId];
|
||||
games.remove(game);
|
||||
gamesById.remove(game.proto.id);
|
||||
gamesByCreator.remove(game.proto.creator.uuid);
|
||||
deleteSignal.emit(game);
|
||||
}
|
||||
}
|
||||
|
||||
public function leave(user:UserProto):Void {
|
||||
if (gamesByCreator.exists(user.uuid)) {
|
||||
delete(gamesByCreator[user.uuid].proto.id);
|
||||
} else if (gamesByUser.exists(user.uuid)) {
|
||||
var game = gamesByUser[user.uuid];
|
||||
game.proto.setPlayers(game.proto.players.filter(function(player) return player.uuid != user.uuid));
|
||||
changeSignal.emit(game, LEAVE(user));
|
||||
}
|
||||
}
|
||||
|
||||
public function start(gameId:Int):Void {
|
||||
if (gamesById.exists(gameId)) {
|
||||
var game = gamesById[gameId];
|
||||
game.proto.setState(GameStateProto.STARTED);
|
||||
changeSignal.emit(game, START);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
src/server/haxe/ru/m/tankz/server/game/IGameManager.hx
Normal file
35
src/server/haxe/ru/m/tankz/server/game/IGameManager.hx
Normal file
@@ -0,0 +1,35 @@
|
||||
package ru.m.tankz.server.game;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
enum GameChange {
|
||||
JOIN(user:UserProto);
|
||||
LEAVE(user:UserProto);
|
||||
START();
|
||||
}
|
||||
|
||||
interface GameManagerListener {
|
||||
public function onCreate(game:ServerGame):Void;
|
||||
public function onChange(game:ServerGame, change:GameChange):Void;
|
||||
public function onDelete(game:ServerGame):Void;
|
||||
}
|
||||
|
||||
interface IGameManager {
|
||||
public var games(default, null):Array<ServerGame>;
|
||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
||||
public var gamesByCreator(default, null):Map<String, ServerGame>;
|
||||
|
||||
private var createSignal(default, null):Signal<ServerGame>;
|
||||
private var changeSignal(default, null):Signal2<ServerGame, GameChange>;
|
||||
private var deleteSignal(default, null):Signal<ServerGame>;
|
||||
|
||||
public function connect(listener:GameManagerListener):Void;
|
||||
public function disconnect(listener:GameManagerListener):Void;
|
||||
|
||||
public function create(user:UserProto, type:String, level:Int):ServerGame;
|
||||
public function delete(gameId:Int):Void;
|
||||
public function join(gameId:Int, user:UserProto):Void;
|
||||
public function leave(user:UserProto):Void;
|
||||
public function start(gameId:Int):Void;
|
||||
}
|
||||
18
src/server/haxe/ru/m/tankz/server/game/ServerGame.hx
Normal file
18
src/server/haxe/ru/m/tankz/server/game/ServerGame.hx
Normal file
@@ -0,0 +1,18 @@
|
||||
package ru.m.tankz.server.game;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
class ServerGame extends Game {
|
||||
|
||||
public var runner(default, null):GameRunner;
|
||||
public var proto(default, null):GameProto;
|
||||
|
||||
public function new(proto:GameProto) {
|
||||
super(new GameState(proto.type, 0, proto.level));
|
||||
this.proto = proto;
|
||||
runner = new GameRunner(this);
|
||||
}
|
||||
}
|
||||
151
src/server/haxe/ru/m/tankz/server/session/GameSession.hx
Normal file
151
src/server/haxe/ru/m/tankz/server/session/GameSession.hx
Normal file
@@ -0,0 +1,151 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import com.hurlant.crypto.extra.UUID;
|
||||
import com.hurlant.crypto.prng.Random;
|
||||
import haxework.log.BaseLogger.LoggerUtil;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.pack.CreateGameResponse;
|
||||
import ru.m.tankz.proto.pack.ErrorResponse;
|
||||
import ru.m.tankz.proto.pack.JoinGameResponse;
|
||||
import ru.m.tankz.proto.pack.LeaveGameResponse;
|
||||
import ru.m.tankz.proto.pack.ListGameResponse;
|
||||
import ru.m.tankz.proto.pack.LoginResponse;
|
||||
import ru.m.tankz.proto.pack.LogoutResponse;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.pack.StartGameResponse;
|
||||
import ru.m.tankz.server.game.IGameManager;
|
||||
import ru.m.tankz.server.game.ServerGame;
|
||||
import sys.net.Socket;
|
||||
|
||||
class GameSession extends ProtoSession<Response, Request> implements GameManagerListener {
|
||||
private static inline var TAG = "Session";
|
||||
|
||||
@:provide static var gameManager:IGameManager;
|
||||
|
||||
public var user(default, null):UserProto;
|
||||
public var gameId(default, null):Int;
|
||||
|
||||
private var tag(get, never):String;
|
||||
|
||||
private function get_tag():String {
|
||||
return '[${id}|${user == null ? '-' : user.name}|${gameId == -1 ? '-' : Std.string(gameId)}]';
|
||||
}
|
||||
|
||||
public function new(socket:Socket) {
|
||||
super(socket, Request);
|
||||
gameId = -1;
|
||||
}
|
||||
|
||||
private function sendError(code:Int, message:String):Void {
|
||||
send(new Response().setError(new ErrorResponse().setCode(code).setMessage(message)));
|
||||
}
|
||||
|
||||
private function listGame():ListGameResponse {
|
||||
var games = gameManager.games;
|
||||
return new ListGameResponse().setGames([for (game in games) game.proto]);
|
||||
}
|
||||
|
||||
override public function send(packet:Response):Void {
|
||||
L.d(TAG, '$tag send: ${packet}');
|
||||
try {
|
||||
super.send(packet);
|
||||
} catch (error:Dynamic) {
|
||||
L.e(TAG, '$tag send ', error);
|
||||
}
|
||||
}
|
||||
|
||||
private function logout():Void {
|
||||
gameId = -1;
|
||||
gameManager.disconnect(this);
|
||||
if (user != null) {
|
||||
gameManager.leave(user);
|
||||
user = null;
|
||||
}
|
||||
}
|
||||
|
||||
override private function onRequest(request:Request):Void {
|
||||
L.d(TAG, '$tag onRequest: ${request}');
|
||||
try {
|
||||
if (!request.hasLogin() && user == null) {
|
||||
throw "Not Authorized";
|
||||
}
|
||||
// login
|
||||
if (request.hasLogin()) {
|
||||
user = new UserProto()
|
||||
.setUuid(request.login.uuid != null ? request.login.uuid : UUID.generateRandom(new Random()).toString())
|
||||
.setName(request.login.name);
|
||||
gameManager.connect(this);
|
||||
send(new Response().setLogin(new LoginResponse().setUser(user)));
|
||||
// logout
|
||||
} else if (request.hasLogout()) {
|
||||
logout();
|
||||
send(new Response().setLogout(new LogoutResponse()));
|
||||
// create
|
||||
} else if (request.hasCreateGame()) {
|
||||
var game = gameManager.create(user, request.createGame.type, request.createGame.level);
|
||||
gameId = game.proto.id;
|
||||
send(new Response().setCreateGame(new CreateGameResponse().setGame(game.proto)));
|
||||
// list
|
||||
} else if (request.hasListGame()) {
|
||||
send(new Response().setListGame(listGame()));
|
||||
// join
|
||||
} else if (request.hasJoinGame()) {
|
||||
gameId = request.joinGame.gameId;
|
||||
gameManager.join(request.joinGame.gameId, user);
|
||||
// leave
|
||||
} else if (request.hasLeaveGame()) {
|
||||
gameManager.leave(user);
|
||||
// start
|
||||
} else if (request.hasStartGame()) {
|
||||
gameManager.start(gameId);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
L.e(TAG, '$tag onRequest ', error);
|
||||
sendError(500, LoggerUtil.printError(error));
|
||||
}
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
L.d(TAG, '$tag disconnect');
|
||||
logout();
|
||||
super.disconnect();
|
||||
}
|
||||
|
||||
public function onCreate(game:ServerGame):Void {
|
||||
if (gameId == -1) {
|
||||
send(new Response().setListGame(listGame()));
|
||||
}
|
||||
}
|
||||
|
||||
public function onChange(game:ServerGame, change:GameChange):Void {
|
||||
if (gameId == game.proto.id) {
|
||||
switch change {
|
||||
case JOIN(user):
|
||||
if (user.uuid == this.user.uuid) {
|
||||
gameId = game.proto.id;
|
||||
}
|
||||
send(new Response().setJoinGame(new JoinGameResponse().setGame(game.proto).setUser(user)));
|
||||
case LEAVE(user):
|
||||
if (user.uuid == this.user.uuid) {
|
||||
gameId = -1;
|
||||
}
|
||||
send(new Response().setLeaveGame(new LeaveGameResponse().setGame(game.proto).setUser(user)));
|
||||
case START:
|
||||
send(new Response().setStartGame(new StartGameResponse().setGame(game.proto)));
|
||||
}
|
||||
} else {
|
||||
// ToDo: change game event
|
||||
send(new Response().setListGame(listGame()));
|
||||
}
|
||||
}
|
||||
|
||||
public function onDelete(game:ServerGame):Void {
|
||||
if (gameId == -1) {
|
||||
send(new Response().setListGame(listGame()));
|
||||
} else if (gameId == game.proto.id) {
|
||||
gameId = -1;
|
||||
send(new Response().setLeaveGame(new LeaveGameResponse().setGame(game.proto).setUser(user)));
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/server/haxe/ru/m/tankz/server/session/ISession.hx
Normal file
9
src/server/haxe/ru/m/tankz/server/session/ISession.hx
Normal file
@@ -0,0 +1,9 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
interface ISession {
|
||||
public var id(default, null):Int;
|
||||
public function pushData(bytes:Bytes):Void;
|
||||
public function disconnect():Void;
|
||||
}
|
||||
72
src/server/haxe/ru/m/tankz/server/session/ProtoSession.hx
Normal file
72
src/server/haxe/ru/m/tankz/server/session/ProtoSession.hx
Normal file
@@ -0,0 +1,72 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.connect.neko.NekoConnection;
|
||||
import ru.m.connect.neko.NekoWSConnection;
|
||||
import sys.net.Socket;
|
||||
|
||||
class ProtoSession<O:Message, I:Message> implements ISession {
|
||||
private static inline var TAG = "Session";
|
||||
|
||||
private static var POLICY_FILE:String = [
|
||||
"<?xml version=\"1.0\"?>",
|
||||
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">",
|
||||
"<cross-domain-policy>",
|
||||
"<site-control permitted-cross-domain-policies=\"master-only\"/>",
|
||||
"<allow-access-from domain=\"*\" to-ports=\"*\"/>",
|
||||
"</cross-domain-policy>"
|
||||
].join("\n");
|
||||
|
||||
private static var idCounter:Int = 0;
|
||||
|
||||
public var id(default, null):Int;
|
||||
public var connection(default, null):IConnection<O, I>;
|
||||
private var socket:Socket;
|
||||
private var request:Class<I>;
|
||||
|
||||
public function new(socket:Socket, request:Class<I>) {
|
||||
this.id = ++idCounter;
|
||||
this.socket = socket;
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
private function buildConnection(bytes:Bytes):IConnection<O, I> {
|
||||
var str:String = bytes.getString(0, bytes.length);
|
||||
if (str == "<policy-file-request/>" + String.fromCharCode(0)) {
|
||||
L.d(TAG, "policy-file-request");
|
||||
socket.output.writeString(POLICY_FILE + String.fromCharCode(0));
|
||||
socket.output.flush();
|
||||
return null;
|
||||
}
|
||||
if (StringTools.startsWith(str, "GET")) {
|
||||
connection = new NekoWSConnection<O, I>(socket, request);
|
||||
} else {
|
||||
connection = new NekoConnection<O, I>(socket, request);
|
||||
}
|
||||
connection.receiveHandler.connect(onRequest);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public function send(packet:O):Void {
|
||||
connection.send(packet);
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
if (connection == null) {
|
||||
connection = buildConnection(bytes);
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.pushData(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public function disconnect():Void {
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
private function onRequest(request:I):Void {
|
||||
L.d(TAG, 'onRequest: ${request}');
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import ru.m.tankz.proto.pack.GameResponse;
|
||||
import ru.m.tankz.proto.pack.GameRequest;
|
||||
import com.hurlant.crypto.extra.UUID;
|
||||
import com.hurlant.crypto.prng.Random;
|
||||
import haxe.io.Bytes;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.connect.neko.NekoConnection;
|
||||
import ru.m.connect.neko.NekoWebConnection;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
import ru.m.tankz.proto.pack.CreateGameResponse;
|
||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
||||
import ru.m.tankz.proto.pack.JoinGameResponse;
|
||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||
import ru.m.tankz.proto.pack.LeaveGameResponse;
|
||||
import ru.m.tankz.proto.pack.ListGameRequest;
|
||||
import ru.m.tankz.proto.pack.ListGameResponse;
|
||||
import ru.m.tankz.proto.pack.LoginRequest;
|
||||
import ru.m.tankz.proto.pack.LoginResponse;
|
||||
import ru.m.tankz.proto.pack.LogoutRequest;
|
||||
import ru.m.tankz.proto.pack.LogoutResponse;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.pack.StartGameRequest;
|
||||
import ru.m.tankz.proto.pack.StartGameResponse;
|
||||
import ru.m.tankz.server.game.GameManager;
|
||||
import sys.net.Socket;
|
||||
|
||||
|
||||
typedef ServerConnection = IConnection<Response, Request>;
|
||||
|
||||
class Session {
|
||||
private static inline var TAG = 'Session';
|
||||
|
||||
private static var POLICY_FILE:String = [
|
||||
'<?xml version="1.0"?>',
|
||||
'<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">',
|
||||
'<cross-domain-policy>',
|
||||
'<site-control permitted-cross-domain-policies="master-only"/>',
|
||||
'<allow-access-from domain="*" to-ports="*"/>',
|
||||
'</cross-domain-policy>'
|
||||
].join('\n');
|
||||
|
||||
public static var sessions:Map<String, Session> = new Map<String, Session>();
|
||||
|
||||
public var user(default, null):UserProto;
|
||||
public var gameId(default, null):Int = -1;
|
||||
|
||||
public var connection(default, null):ServerConnection;
|
||||
|
||||
private var socket:Socket;
|
||||
|
||||
public function new(socket:Socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public function send(packet:Response):Void {
|
||||
connection.send(packet);
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
if (connection != null) {
|
||||
connection.pushData(bytes);
|
||||
} else {
|
||||
var str:String = bytes.getString(0, bytes.length);
|
||||
if (str == '<policy-file-request/>' + String.fromCharCode(0)) {
|
||||
L.d(TAG, 'policy-file-request');
|
||||
socket.output.writeString(POLICY_FILE + String.fromCharCode(0));
|
||||
socket.output.flush();
|
||||
return;
|
||||
}
|
||||
if (StringTools.startsWith(str, "GET")) {
|
||||
connection = new NekoWebConnection<Response, Request>(socket, Request);
|
||||
} else {
|
||||
connection = new NekoConnection<Response, Request>(socket, Request);
|
||||
}
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.receiveHandler.connect(onRequest);
|
||||
connection.pushData(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||
L.d(TAG, '${event}');
|
||||
}
|
||||
|
||||
public function onRequest(request:Request):Void {
|
||||
if (request.hasLogin()) {
|
||||
send(new Response().setLogin(login(request.login)));
|
||||
} else if (request.hasLogout()) {
|
||||
send(new Response().setLogout(logout(request.logout)));
|
||||
} else if (request.hasListGame()) {
|
||||
send(new Response().setListGame(listGame(request.listGame)));
|
||||
} else if (request.hasCreateGame()) {
|
||||
send(new Response().setCreateGame(createGame(request.createGame)));
|
||||
} else if (request.hasJoinGame()) {
|
||||
send(new Response().setJoinGame(joinGame(request.joinGame)));
|
||||
} else if (request.hasLeaveGame()) {
|
||||
send(new Response().setLeaveGame(leaveGame(request.leaveGame)));
|
||||
} else if (request.hasStartGame()) {
|
||||
send(new Response().setStartGame(startGame(request.startGame)));
|
||||
}else if (request.hasGame()) {
|
||||
send(new Response().setGame(game(request.game)));
|
||||
}
|
||||
}
|
||||
|
||||
private function login(request:LoginRequest):LoginResponse {
|
||||
user = new UserProto()
|
||||
.setUuid(request.uuid != null ? request.uuid : UUID.generateRandom(new Random()).toString())
|
||||
.setName(request.name);
|
||||
sessions.set(user.uuid, this);
|
||||
GameManager.subscribers.set(user.uuid, true);
|
||||
return new LoginResponse().setUser(user);
|
||||
}
|
||||
|
||||
private function logout(request:LogoutRequest):LogoutResponse {
|
||||
GameManager.subscribers.remove(user.uuid);
|
||||
user = null;
|
||||
return new LogoutResponse();
|
||||
}
|
||||
|
||||
private function listGame(request:ListGameRequest):ListGameResponse {
|
||||
return new ListGameResponse().setGames(GameManager.getReadyGames());
|
||||
}
|
||||
|
||||
private function createGame(request:CreateGameRequest):CreateGameResponse {
|
||||
var gameManager:GameManager = new GameManager(user);
|
||||
return new CreateGameResponse().setGame(gameManager.gameInfo);
|
||||
}
|
||||
|
||||
private function joinGame(request:JoinGameRequest):JoinGameResponse {
|
||||
var gameManager:GameManager = GameManager.byGameId.get(request.gameId);
|
||||
gameManager.join(user);
|
||||
return new JoinGameResponse().setGame(gameManager.gameInfo);
|
||||
}
|
||||
|
||||
private function leaveGame(request:LeaveGameRequest):LeaveGameResponse {
|
||||
var gameManager:GameManager = GameManager.byPersonId.get(user.uuid);
|
||||
gameManager.leave(user);
|
||||
return new LeaveGameResponse().setGame(gameManager.gameInfo);
|
||||
}
|
||||
|
||||
private function startGame(request:StartGameRequest):StartGameResponse {
|
||||
var gameManager:GameManager = GameManager.byPersonId.get(user.uuid);
|
||||
gameManager.start();
|
||||
return new StartGameResponse().setGame(gameManager.gameInfo);
|
||||
}
|
||||
|
||||
private function game(request:GameRequest):GameResponse {
|
||||
var gameManager:GameManager = GameManager.byPersonId.get(user.uuid);
|
||||
return new GameResponse().setGame(gameManager.game.export());
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
enum ThreadHandle {
|
||||
}
|
||||
|
||||
class Thread {
|
||||
|
||||
var handle:ThreadHandle;
|
||||
|
||||
function new(h) {
|
||||
handle = h;
|
||||
}
|
||||
|
||||
/**
|
||||
Send a message to the thread queue. This message can be readed by using [readMessage].
|
||||
**/
|
||||
public function sendMessage(msg:Dynamic) {
|
||||
thread_send(handle, msg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Returns the current thread.
|
||||
**/
|
||||
public static function current() {
|
||||
return new Thread(thread_current());
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a new thread that will execute the [callb] function, then exit.
|
||||
**/
|
||||
public static function create(callb:Void -> Void) {
|
||||
return new Thread(thread_create(function(_) { return callb(); }, null));
|
||||
}
|
||||
|
||||
/**
|
||||
Reads a message from the thread queue. If [block] is true, the function
|
||||
blocks until a message is available. If [block] is false, the function
|
||||
returns [null] if no message is available.
|
||||
**/
|
||||
public static function readMessage(block:Bool):Dynamic {
|
||||
return thread_read_message(block);
|
||||
}
|
||||
|
||||
@:keep function __compare(t) {
|
||||
return untyped __dollar__compare(handle, t.handle);
|
||||
}
|
||||
|
||||
/**
|
||||
Starts an OS message loop after [osInitialize] has been done.
|
||||
In that state, the UI handled by this thread will be updated and
|
||||
[sync] calls can be performed. The loop returns when [exitLoop] is
|
||||
called for this thread.
|
||||
**
|
||||
public static function osLoop() {
|
||||
if( os_loop == null ) throw "Please call osInitialize() first";
|
||||
os_loop();
|
||||
}
|
||||
|
||||
/**
|
||||
The function [f] will be called by this thread if it's in [osLoop].
|
||||
[sync] returns immediatly. See [osInitialize] remarks.
|
||||
**
|
||||
public function sync( f : Void -> Void ) {
|
||||
os_sync(handle,f);
|
||||
}
|
||||
|
||||
/**
|
||||
The function [f] will be called by this thread and the calling thread
|
||||
will wait until the result is available then return its value.
|
||||
**
|
||||
public function syncResult<T>( f : Void -> T ) : T {
|
||||
if( this == current() )
|
||||
return f();
|
||||
var v = new neko.vm.Lock();
|
||||
var r = null;
|
||||
sync(function() {
|
||||
r = f();
|
||||
v.release();
|
||||
});
|
||||
v.wait();
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
Exit from [osLoop].
|
||||
**
|
||||
public function exitLoop() {
|
||||
os_loop_stop(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
If you want to use the [osLoop], [sync] and [syncResult] methods, you
|
||||
need to call [osInitialize] before creating any thread or calling [current].
|
||||
This will load [os.ndll] library and initialize UI methods for each thread.
|
||||
**
|
||||
public static function osInitialize() {
|
||||
os_loop = neko.Lib.load("os","os_loop",0);
|
||||
os_loop_stop = neko.Lib.load("os","os_loop_stop",1);
|
||||
os_sync = neko.Lib.load("os","os_sync",2);
|
||||
}
|
||||
|
||||
static var os_loop = null;
|
||||
static var os_loop_stop = null;
|
||||
static var os_sync = null;
|
||||
*/
|
||||
|
||||
static var thread_create = neko.Lib.load("std", "thread_create", 2);
|
||||
static var thread_current = neko.Lib.load("std", "thread_current", 0);
|
||||
static var thread_send = neko.Lib.load("std", "thread_send", 2);
|
||||
static var thread_read_message = neko.Lib.load("std", "thread_read_message", 1);
|
||||
}
|
||||
Reference in New Issue
Block a user