Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| abd1b834d7 | |||
| 6ad9076987 | |||
| 78bbf5264e | |||
| 4c8ae66624 | |||
| 5a3d5b974e | |||
| fb9a28c126 | |||
| 7115ea8ac2 | |||
| 01417f5bbd | |||
| abf944418e | |||
| c7946ef1da | |||
| 241499bc2d | |||
| 74297dd9c7 | |||
| 31cb20bf85 | |||
| 1d95de02e1 | |||
| ba13111a8e | |||
| 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,6 +3,8 @@ hash_behaviour = merge
|
|||||||
host_key_checking = False
|
host_key_checking = False
|
||||||
#callback_whitelist = profile_tasks
|
#callback_whitelist = profile_tasks
|
||||||
display_skipped_hosts = False
|
display_skipped_hosts = False
|
||||||
|
stdout_callback = yaml
|
||||||
|
bin_ansible_callbacks = True
|
||||||
|
|
||||||
[ssh_connection]
|
[ssh_connection]
|
||||||
pipelining = True
|
pipelining = True
|
||||||
|
|||||||
@@ -6,6 +6,6 @@
|
|||||||
remote_src: true
|
remote_src: true
|
||||||
|
|
||||||
- name: "Gulp build"
|
- name: "Gulp build"
|
||||||
command: "/usr/local/lib/npm/bin/gulp default"
|
command: "/usr/local/lib/npm/bin/gulp default --host shmyga.ru"
|
||||||
args:
|
args:
|
||||||
chdir: "{{ deploy_release_dir }}"
|
chdir: "{{ deploy_release_dir }}"
|
||||||
|
|||||||
19
gulpfile.js
19
gulpfile.js
@@ -4,7 +4,7 @@ const Config = require('./config.json');
|
|||||||
const packageInfo = require('./package.json');
|
const packageInfo = require('./package.json');
|
||||||
const {Sdk, Haxe, Project, FlashPlayer} = require('gulp-haxetool');
|
const {Sdk, Haxe, Project, FlashPlayer} = require('gulp-haxetool');
|
||||||
const dateformat = require('dateformat');
|
const dateformat = require('dateformat');
|
||||||
|
const argv = require('yargs').argv;
|
||||||
|
|
||||||
// ToDo: update default in gulp-haxetool
|
// ToDo: update default in gulp-haxetool
|
||||||
FlashPlayer.VERSION = '32';
|
FlashPlayer.VERSION = '32';
|
||||||
@@ -48,12 +48,17 @@ const config = new Project.Config({
|
|||||||
assets: [
|
assets: [
|
||||||
'src/common/resources'
|
'src/common/resources'
|
||||||
],
|
],
|
||||||
|
flags: [
|
||||||
|
//'proto_debug',
|
||||||
|
],
|
||||||
macros: [
|
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')}')`,
|
`CompilationOption.set('build','${dateformat(new Date(), 'yyyy-mm-dd HH:MM:ss')}')`,
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const host = argv.host || 'localhost';
|
||||||
|
const port = argv.port || 5000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* client
|
* client
|
||||||
*/
|
*/
|
||||||
@@ -78,10 +83,15 @@ const client = new Project(
|
|||||||
height: 768,
|
height: 768,
|
||||||
},
|
},
|
||||||
flags: [
|
flags: [
|
||||||
|
//'proto_debug',
|
||||||
//'dom',
|
//'dom',
|
||||||
//'dev_layout',
|
//'dev_layout',
|
||||||
//'bitmap_text',
|
//'bitmap_text',
|
||||||
],
|
],
|
||||||
|
macros: [
|
||||||
|
`CompilationOption.set('host','${host}')`,
|
||||||
|
`CompilationOption.set('port',${port})`,
|
||||||
|
]
|
||||||
}),
|
}),
|
||||||
module.exports.generate
|
module.exports.generate
|
||||||
).bind(module, gulp);
|
).bind(module, gulp);
|
||||||
@@ -125,7 +135,8 @@ const server = new Project(
|
|||||||
name: 'server',
|
name: 'server',
|
||||||
sources: ['src/server/haxe'],
|
sources: ['src/server/haxe'],
|
||||||
main: 'ru.m.tankz.server.Server',
|
main: 'ru.m.tankz.server.Server',
|
||||||
})
|
}),
|
||||||
|
module.exports.generate
|
||||||
).bind(module, gulp);
|
).bind(module, gulp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,5 +155,5 @@ module.exports.default = gulp.series(
|
|||||||
module.exports['editor:flash:html'],
|
module.exports['editor:flash:html'],
|
||||||
module.exports['editor:html5:build'],
|
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",
|
"name": "tankz",
|
||||||
"version": "0.11.0",
|
"version": "0.14.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"dateformat": "^3.0.3",
|
"dateformat": "^3.0.3",
|
||||||
"gulp": "^4.0.0",
|
"gulp": "^4.0.0",
|
||||||
"gulp-add": "0.0.2",
|
"gulp-add": "0.0.2",
|
||||||
"gulp-clean": "^0.4.0",
|
"gulp-clean": "^0.4.0",
|
||||||
"gulp-haxetool": "^0.0.17"
|
"gulp-haxetool": "^0.0.19",
|
||||||
|
"yargs": "^13.2.4"
|
||||||
},
|
},
|
||||||
"haxeDependencies": {
|
"haxeDependencies": {
|
||||||
"haxework": "git@bitbucket.org:shmyga/haxework.git",
|
"haxework": "git@bitbucket.org:shmyga/haxework.git",
|
||||||
"lime": "7.3.0",
|
"lime": "7.5.0",
|
||||||
"openfl": "8.9.0",
|
"openfl": "8.9.1",
|
||||||
"hxcpp": "4.0.8",
|
"hxcpp": "4.0.8",
|
||||||
"promhx": "1.1.0",
|
"promhx": "1.1.0",
|
||||||
"protohx": "0.4.6",
|
"protohx": "0.4.6",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
"protoFiles": [
|
"protoFiles": [
|
||||||
"src/common/proto/core.proto",
|
"src/common/proto/core.proto",
|
||||||
"src/common/proto/game.proto",
|
"src/common/proto/game.proto",
|
||||||
|
"src/common/proto/room.proto",
|
||||||
"src/common/proto/pack.proto"
|
"src/common/proto/pack.proto"
|
||||||
],
|
],
|
||||||
"cleanOut": true,
|
"cleanOut": true,
|
||||||
|
|||||||
@@ -1,23 +1,21 @@
|
|||||||
package ru.m.tankz;
|
package ru.m.tankz;
|
||||||
|
|
||||||
import ru.m.tankz.view.ClientView;
|
|
||||||
import flash.Lib;
|
import flash.Lib;
|
||||||
import haxework.animate.Animate;
|
import haxework.animate.Animate;
|
||||||
import haxework.view.Root;
|
|
||||||
import haxework.log.TraceLogger;
|
import haxework.log.TraceLogger;
|
||||||
#if flash import haxework.log.JSLogger; #end
|
import haxework.view.Root;
|
||||||
#if debug import haxework.log.SocketLogger; #end
|
import ru.m.tankz.view.ClientView;
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
private static inline var TAG = 'Tankz';
|
private static inline var TAG = "Tankz";
|
||||||
|
|
||||||
public static function main() {
|
public static function main() {
|
||||||
L.push(new TraceLogger());
|
L.push(new TraceLogger());
|
||||||
#if flash
|
#if flash
|
||||||
L.push(new JSLogger());
|
L.push(new haxework.log.JSLogger());
|
||||||
#end
|
#end
|
||||||
#if debug
|
#if debug
|
||||||
//L.push(new SocketLogger());
|
//L.push(new haxework.log.SocketLogger());
|
||||||
#end
|
#end
|
||||||
Const.init();
|
Const.init();
|
||||||
Init.init();
|
Init.init();
|
||||||
@@ -32,5 +30,10 @@ class Client {
|
|||||||
var view:ClientView = new ClientView();
|
var view:ClientView = new ClientView();
|
||||||
Root.bind(view);
|
Root.bind(view);
|
||||||
view.launch();
|
view.launch();
|
||||||
|
|
||||||
|
#if debug
|
||||||
|
var fps = new openfl.display.FPS(0, 0, 0x00ff00);
|
||||||
|
Lib.current.addChild(fps);
|
||||||
|
#end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import ru.m.tankz.bundle.ConfigBundle;
|
|||||||
import ru.m.tankz.bundle.IConfigBundle;
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
import ru.m.tankz.bundle.ILevelBundle;
|
import ru.m.tankz.bundle.ILevelBundle;
|
||||||
import ru.m.tankz.bundle.LevelBundle;
|
import ru.m.tankz.bundle.LevelBundle;
|
||||||
import ru.m.tankz.control.ClientControlFactory;
|
|
||||||
import ru.m.tankz.control.IControlFactory;
|
|
||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.proto.pack.Request;
|
import ru.m.tankz.proto.pack.Request;
|
||||||
import ru.m.tankz.proto.pack.Response;
|
import ru.m.tankz.proto.pack.Response;
|
||||||
@@ -20,11 +18,6 @@ import ru.m.tankz.storage.GameStorage;
|
|||||||
import ru.m.tankz.storage.MultiplayerStorage;
|
import ru.m.tankz.storage.MultiplayerStorage;
|
||||||
import ru.m.tankz.storage.RecordStorage;
|
import ru.m.tankz.storage.RecordStorage;
|
||||||
import ru.m.tankz.storage.SettingsStorage;
|
import ru.m.tankz.storage.SettingsStorage;
|
||||||
#if flash
|
|
||||||
import flash.Lib;
|
|
||||||
#elseif html5
|
|
||||||
import js.Browser;
|
|
||||||
#end
|
|
||||||
|
|
||||||
class Init {
|
class Init {
|
||||||
|
|
||||||
@@ -37,23 +30,19 @@ class Init {
|
|||||||
@:provide static var recordStorage:RecordStorage;
|
@:provide static var recordStorage:RecordStorage;
|
||||||
@:provide static var soundManager:SoundManager;
|
@:provide static var soundManager:SoundManager;
|
||||||
@:provide static var networkManager:NetworkManager;
|
@:provide static var networkManager:NetworkManager;
|
||||||
@:provide static var controlFactory:IControlFactory;
|
|
||||||
@:provide static var popupManager:PopupManager;
|
@:provide static var popupManager:PopupManager;
|
||||||
@:provide static var connection:IConnection<Request, Response>;
|
@:provide static var connection:IConnection<Request, Response>;
|
||||||
|
|
||||||
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
|
#if flash
|
||||||
var url = Lib.current.loaderInfo.url;
|
return new ru.m.connect.flash.FlashConnection<Request, Response>(host, port, Response);
|
||||||
var r:EReg = ~/(app|https?):\/\/?([-_\w\d\.]+)(:\d+)?\/?/;
|
|
||||||
if (r.match(url) && !(r.matched(1) == "app")) {
|
|
||||||
return r.matched(2);
|
|
||||||
} else {
|
|
||||||
return "localhost";
|
|
||||||
}
|
|
||||||
#elseif html5
|
#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
|
#else
|
||||||
return "localhost";
|
return new ru.m.connect.desktop.DesktopConnection<Request, Response>(host, port, Response);
|
||||||
#end
|
#end
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,21 +55,12 @@ class Init {
|
|||||||
gameStorage = new GameStorage();
|
gameStorage = new GameStorage();
|
||||||
recordStorage = new RecordStorage();
|
recordStorage = new RecordStorage();
|
||||||
soundManager = new SoundManager();
|
soundManager = new SoundManager();
|
||||||
controlFactory = new ClientControlFactory();
|
|
||||||
popupManager = new PopupManager();
|
popupManager = new PopupManager();
|
||||||
|
|
||||||
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
||||||
popupManager.closeAnimateFactory = function(v) return new FadeAnimate(v, 100);
|
popupManager.closeAnimateFactory = function(v) return new FadeAnimate(v, 100);
|
||||||
|
|
||||||
var host:String = getHost();
|
connection = buildConnection();
|
||||||
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
|
|
||||||
networkManager = new NetworkManager();
|
networkManager = new NetworkManager();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ class Style {
|
|||||||
public static var darkColor = 0x777564;
|
public static var darkColor = 0x777564;
|
||||||
public static var textColor = 0xE7E0BB;
|
public static var textColor = 0xE7E0BB;
|
||||||
public static var activeColor = 0xFFFF00;
|
public static var activeColor = 0xFFFF00;
|
||||||
|
public static var borderColor = ColorUtil.multiply(lightColor, 1.5);
|
||||||
|
|
||||||
public static var baseFontSize = 18;
|
public static var baseFontSize = 18;
|
||||||
public static var bigFontSize = 22;
|
public static var bigFontSize = 22;
|
||||||
@@ -45,6 +46,14 @@ class Style {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function textBox(color:Int):SkinSet {
|
||||||
|
return [
|
||||||
|
Skin.color(0x000000, 0.1),
|
||||||
|
Skin.border(lightColor, 1, 2),
|
||||||
|
Skin.text(color, baseFontSize, fontFamily, fontEmbed),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public static function register(font:Font = null):Void {
|
public static function register(font:Font = null):Void {
|
||||||
resources.color.put("light", lightColor);
|
resources.color.put("light", lightColor);
|
||||||
resources.color.put("dark", darkColor);
|
resources.color.put("dark", darkColor);
|
||||||
@@ -111,7 +120,7 @@ class Style {
|
|||||||
Skin.geometry(new Geometry().setPadding([25, 8])),
|
Skin.geometry(new Geometry().setPadding([25, 8])),
|
||||||
]);
|
]);
|
||||||
resources.skin.put("border", [
|
resources.skin.put("border", [
|
||||||
Skin.border(ColorUtil.multiply(lightColor, 1.5), 1, 2),
|
Skin.border(borderColor, 1, 2),
|
||||||
]);
|
]);
|
||||||
resources.skin.put("scroll", [
|
resources.skin.put("scroll", [
|
||||||
Skin.scrollVertical(lightColor, ColorUtil.diff(lightColor, 128)),
|
Skin.scrollVertical(lightColor, ColorUtil.diff(lightColor, 128)),
|
||||||
@@ -134,17 +143,22 @@ class Style {
|
|||||||
]);
|
]);
|
||||||
resources.skin.put("window", [
|
resources.skin.put("window", [
|
||||||
Skin.color(darkColor),
|
Skin.color(darkColor),
|
||||||
Skin.border(ColorUtil.multiply(lightColor, 1.5), 1, 2),
|
Skin.border(borderColor, 1, 2),
|
||||||
Skin.geometry(new Geometry().setPadding(2)),
|
Skin.geometry(new Geometry().setPadding(2)),
|
||||||
]);
|
]);
|
||||||
resources.skin.put("window.close", [
|
resources.skin.put("window.close", [
|
||||||
Skin.size(36, 36),
|
Skin.size(36, 36),
|
||||||
new ButtonSVGSkin(Assets.getText("resources/image/icon/window-close-solid.svg"), lightColor),
|
new ButtonSVGSkin(Assets.getText("resources/image/icon/window-close-solid.svg"), lightColor),
|
||||||
]);
|
]);
|
||||||
|
resources.skin.put("line", [
|
||||||
|
Skin.color(borderColor),
|
||||||
|
]);
|
||||||
|
|
||||||
registerButton("settings", "cog-solid.svg");
|
registerButton("settings", "cog-solid.svg");
|
||||||
registerButton("close", "times-circle-solid.svg");
|
registerButton("close", "times-circle-solid.svg");
|
||||||
registerButton("next", "arrow-alt-circle-right-solid.svg");
|
registerButton("next", "arrow-alt-circle-right-solid.svg");
|
||||||
registerButton("start", "play-circle-solid.svg");
|
registerButton("start", "play-circle-solid.svg");
|
||||||
|
registerButton("login", "sign-in-solid.svg");
|
||||||
|
registerButton("logout", "sign-out-solid.svg");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class ConfigBundle implements IConfigBundle {
|
|||||||
public function get(type:GameType):Config {
|
public function get(type:GameType):Config {
|
||||||
if (!_cache.exists(type)) {
|
if (!_cache.exists(type)) {
|
||||||
var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects());
|
var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects());
|
||||||
_cache.set(type, new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses));
|
_cache.set(type, Config.fromSource(type, source));
|
||||||
}
|
}
|
||||||
return _cache.get(type);
|
return _cache.get(type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package ru.m.tankz.control;
|
|
||||||
|
|
||||||
import ru.m.tankz.bot.StupidBotControl;
|
|
||||||
import ru.m.tankz.bot.HardBotControl;
|
|
||||||
import ru.m.tankz.Type;
|
|
||||||
|
|
||||||
class ClientControlFactory implements IControlFactory {
|
|
||||||
|
|
||||||
public function new() {}
|
|
||||||
|
|
||||||
public function build(id:PlayerId, controller:Controller):Control {
|
|
||||||
return switch controller {
|
|
||||||
case HUMAN(index): new HumanControl(id, index);
|
|
||||||
case BOT(type): switch type {
|
|
||||||
case StupidBotControl.BOT_TYPE: new StupidBotControl(id);
|
|
||||||
case HardBotControl.BOT_TYPE: new HardBotControl(id);
|
|
||||||
case _: null;
|
|
||||||
}
|
|
||||||
case NONE: null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
10
src/client/haxe/ru/m/tankz/control/LocalControlFactory.hx
Normal file
10
src/client/haxe/ru/m/tankz/control/LocalControlFactory.hx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
class LocalControlFactory extends BaseControlFactory {
|
||||||
|
|
||||||
|
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return new HumanControl(id, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,13 @@ package ru.m.tankz.control;
|
|||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
|
|
||||||
|
class NetworkControl extends HumanControl {
|
||||||
class ClientNetworkControl extends HumanControl {
|
|
||||||
|
|
||||||
@:provide private var network:NetworkManager;
|
@:provide private var network:NetworkManager;
|
||||||
|
|
||||||
override public function action(action:TankAction):Void {
|
override public function action(action:TankAction):Void {
|
||||||
network.action(action);
|
if (tankId > -1) {
|
||||||
|
network.action(tankId, action);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
10
src/client/haxe/ru/m/tankz/control/NetworkControlFactory.hx
Normal file
10
src/client/haxe/ru/m/tankz/control/NetworkControlFactory.hx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
class NetworkControlFactory extends BaseControlFactory {
|
||||||
|
|
||||||
|
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return new NetworkControl(id, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
29
src/client/haxe/ru/m/tankz/game/LocalGame.hx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import ru.m.tankz.control.LocalControlFactory;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
|
import ru.m.tankz.game.record.GameRecorder;
|
||||||
|
import ru.m.tankz.storage.RecordStorage;
|
||||||
|
|
||||||
|
class LocalGame extends GameRunner {
|
||||||
|
@:provide var recordStorage:RecordStorage;
|
||||||
|
private var recorder:GameRecorder;
|
||||||
|
|
||||||
|
public function new(state:GameState) {
|
||||||
|
super(state);
|
||||||
|
controlFactory = new LocalControlFactory();
|
||||||
|
recorder = new GameRecorder();
|
||||||
|
connect(recorder);
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function onGameEvent(event:GameEvent):Void {
|
||||||
|
super.onGameEvent(event);
|
||||||
|
switch event {
|
||||||
|
case GameEvent.COMPLETE(_, _):
|
||||||
|
disconnect(recorder);
|
||||||
|
recorder.onGameEvent(event); //ToDo:
|
||||||
|
recordStorage.save(recorder.record);
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
54
src/client/haxe/ru/m/tankz/game/NetworkGame.hx
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import haxe.Unserializer;
|
||||||
|
import ru.m.tankz.control.NetworkControlFactory;
|
||||||
|
import ru.m.tankz.game.Game;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
|
import ru.m.tankz.game.GameState;
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
|
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||||
|
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||||
|
|
||||||
|
class NetworkGame extends Game {
|
||||||
|
|
||||||
|
private var network:NetworkManager;
|
||||||
|
|
||||||
|
public function new(network:NetworkManager) {
|
||||||
|
super(new GameState(network.room.game.type, 0, network.room.game.level));
|
||||||
|
this.network = network;
|
||||||
|
this.controlFactory = new NetworkControlFactory();
|
||||||
|
network.gameEventSignal.connect(onGameEventProto);
|
||||||
|
network.stateSignal.connect(onConnectionState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onGameEventProto(game:GameEventResponse):Void {
|
||||||
|
var time = game.time;
|
||||||
|
var eventStr = game.event;
|
||||||
|
var event:GameEvent = Unserializer.run(eventStr);
|
||||||
|
gameEventSignal.emit(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onConnectionState(state:ConnectionState):Void {
|
||||||
|
switch state {
|
||||||
|
case ONLINE(user):
|
||||||
|
if (network.room != null) {
|
||||||
|
network.joinGame(network.room.game.id);
|
||||||
|
}
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function start():Void {
|
||||||
|
var slot:RoomSlotProto = Lambda.find(network.room.slots, function(slot:RoomSlotProto) return slot.hasUser() && slot.user.uuid == network.user.uuid);
|
||||||
|
if (slot != null) {
|
||||||
|
state.controls.push({playerId: [slot.slot.team, slot.slot.index], control: "human-0"});
|
||||||
|
}
|
||||||
|
super.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function dispose():Void {
|
||||||
|
super.dispose();
|
||||||
|
network.gameEventSignal.disconnect(onGameEventProto);
|
||||||
|
network.stateSignal.disconnect(onConnectionState);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,157 +1,170 @@
|
|||||||
package ru.m.tankz.network;
|
package ru.m.tankz.network;
|
||||||
|
|
||||||
import ru.m.tankz.storage.MultiplayerStorage;
|
import ru.m.tankz.proto.room.SlotProto;
|
||||||
import haxework.storage.IStorage;
|
import ru.m.tankz.proto.room.SlotRequest;
|
||||||
|
import haxe.Serializer;
|
||||||
import haxework.signal.Signal;
|
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.tankz.control.Control;
|
|
||||||
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.connect.IConnection;
|
||||||
import ru.m.tankz.proto.core.GameInfoProto;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.proto.pack.ListGameRequest;
|
import ru.m.tankz.game.GameEvent;
|
||||||
|
import ru.m.tankz.proto.core.UserProto;
|
||||||
|
import ru.m.tankz.proto.pack.GameEventRequest;
|
||||||
|
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||||
import ru.m.tankz.proto.pack.LoginRequest;
|
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.Request;
|
||||||
import ru.m.tankz.proto.pack.Response;
|
import ru.m.tankz.proto.pack.Response;
|
||||||
|
import ru.m.tankz.proto.room.CreateRequest;
|
||||||
|
import ru.m.tankz.proto.room.JoinRequest;
|
||||||
|
import ru.m.tankz.proto.room.LeaveRequest;
|
||||||
|
import ru.m.tankz.proto.room.RoomListRequest;
|
||||||
|
import ru.m.tankz.proto.room.RoomProto;
|
||||||
|
import ru.m.tankz.proto.room.RoomRequest;
|
||||||
|
import ru.m.tankz.proto.room.StartRequest;
|
||||||
|
import ru.m.tankz.storage.MultiplayerStorage;
|
||||||
|
|
||||||
typedef ClientConnection = IConnection<Request, Response>;
|
typedef ClientConnection = IConnection<Request, Response>;
|
||||||
|
|
||||||
|
enum ConnectionState {
|
||||||
|
OFFLINE;
|
||||||
|
CONNECT;
|
||||||
|
CONNECTED;
|
||||||
|
LOGIN;
|
||||||
|
ONLINE(user:User);
|
||||||
|
ERROR(error:Dynamic);
|
||||||
|
}
|
||||||
|
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
|
|
||||||
public var state(default, null):String;
|
public var state(default, null):ConnectionState;
|
||||||
public var stateSignal:Signal<String>;
|
public var room(default, null):RoomProto;
|
||||||
public var listGameSignal:Signal<Array<GameInfoProto>>;
|
public var user(default, null):UserProto;
|
||||||
public var gameSignal:Signal<GameInfoProto>;
|
|
||||||
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
public var stateSignal:Signal<ConnectionState>;
|
||||||
public var user(default, null):User;
|
public var listRoomSignal:Signal<Array<RoomProto>>;
|
||||||
public var game(default, set):NetworkGame;
|
public var roomSignal:Signal<RoomProto>;
|
||||||
|
public var gameEventSignal:Signal<GameEventResponse>;
|
||||||
|
|
||||||
@:provide private var connection:ClientConnection;
|
@:provide private var connection:ClientConnection;
|
||||||
@:provide private var storage:MultiplayerStorage;
|
@:provide private var storage:MultiplayerStorage;
|
||||||
|
|
||||||
|
private var reconnectTimer:Timer;
|
||||||
|
private var reconnectDelay:Int;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
stateSignal = new Signal<String>();
|
reconnectDelay = 500;
|
||||||
listGameSignal = new Signal<Array<GameInfoProto>>();
|
stateSignal = new Signal();
|
||||||
gameSignal = new Signal<GameInfoProto>();
|
listRoomSignal = new Signal();
|
||||||
gameUpdateSignal = new Signal<Array<GameChangeProto>>();
|
roomSignal = new Signal();
|
||||||
updateState('offline');
|
gameEventSignal = new Signal();
|
||||||
|
updateState(OFFLINE);
|
||||||
connection.handler.connect(onConnectionEvent);
|
connection.handler.connect(onConnectionEvent);
|
||||||
connection.receiveHandler.connect(onResponse);
|
connection.receiveHandler.connect(onResponse);
|
||||||
user = storage.user;
|
connect();
|
||||||
if (user == null) {
|
|
||||||
user = {name: 'User', uuid: null};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateState(value:String):Void {
|
private function updateState(value:ConnectionState):Void {
|
||||||
state = value;
|
state = value;
|
||||||
stateSignal.emit(value);
|
stateSignal.emit(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function login(name:String):Void {
|
public function login(name:String, uuid:String = null):Void {
|
||||||
user.name = name;
|
updateState(LOGIN);
|
||||||
updateState('connect...');
|
connection.send(new Request().setLogin(
|
||||||
connection.connect().then(function(c:ClientConnection) {
|
|
||||||
updateState('login...');
|
|
||||||
c.send(new Request().setLogin(
|
|
||||||
new LoginRequest()
|
new LoginRequest()
|
||||||
.setUuid(user.uuid)
|
.setUuid(uuid)
|
||||||
.setName(user.name)
|
.setName(name)
|
||||||
));
|
));
|
||||||
}).catchError(function(_) {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listGame():Void {
|
public function logout():Void {
|
||||||
connection.send(new Request().setListGame(new ListGameRequest()));
|
connection.send(new Request().setLogout(new LogoutRequest()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createGame(type:String):Void {
|
public function listGame(subscribe:Bool):Void {
|
||||||
connection.send(new Request().setCreateGame(new CreateGameRequest().setType(type)));
|
connection.send(new Request().setRoomList(new RoomListRequest().setSubscribe(subscribe)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function joinGame(gameId:Int):Void {
|
public function createGame(type:String, level:Int):Void {
|
||||||
connection.send(new Request().setJoinGame(new JoinGameRequest().setGameId(gameId)));
|
connection.send(new Request().setRoom(new RoomRequest().setCreate(new CreateRequest().setType(type).setLevel(level))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function joinGame(gameId:Int, restore:Bool = false):Void {
|
||||||
|
connection.send(new Request().setRoom(new RoomRequest().setJoin(new JoinRequest().setGameId(gameId).setRestore(restore))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function selectSlot(slot:SlotProto):Void {
|
||||||
|
connection.send(new Request().setRoom(new RoomRequest().setSlot(new SlotRequest().setSlot(slot))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function leaveGame():Void {
|
public function leaveGame():Void {
|
||||||
connection.send(new Request().setLeaveGame(new LeaveGameRequest()));
|
connection.send(new Request().setRoom(new RoomRequest().setLeave(new LeaveRequest())));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function startGame():Void {
|
public function startGame():Void {
|
||||||
connection.send(new Request().setStartGame(new StartGameRequest()));
|
connection.send(new Request().setRoom(new RoomRequest().setStart(new StartRequest())));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function action(action:TankAction):Void {
|
public function action(tankId:Int, action:TankAction):Void {
|
||||||
var update:GameUpdateRequest = switch action {
|
connection.send(new Request().setGameEvent(new GameEventRequest().setTime(0).setEvent(
|
||||||
case TankAction.MOVE(direction):
|
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||||
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));
|
private function connect():Void {
|
||||||
|
if (reconnectTimer != null) {
|
||||||
|
reconnectTimer.stop();
|
||||||
|
reconnectTimer = null;
|
||||||
|
}
|
||||||
|
reconnectTimer = null;
|
||||||
|
updateState(CONNECT);
|
||||||
|
connection.connect().catchError(function(error) {});
|
||||||
|
}
|
||||||
|
|
||||||
|
private function reconnect():Void {
|
||||||
|
reconnectDelay = Std.int(Math.min(reconnectDelay * 2, 10000));
|
||||||
|
if (reconnectTimer == null) {
|
||||||
|
reconnectTimer = Timer.delay(connect, reconnectDelay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||||
updateState(switch (event) {
|
updateState(switch event {
|
||||||
case ConnectionEvent.CONNECTED:
|
case CONNECTED: CONNECTED;
|
||||||
L.d('Network', '$event');
|
case DISCONNECTED: OFFLINE;
|
||||||
'connected';
|
case ERROR(error): ERROR(error);
|
||||||
case ConnectionEvent.DISCONNECTED:
|
|
||||||
L.d('Network', '$event');
|
|
||||||
'offline';
|
|
||||||
case ConnectionEvent.ERROR(error):
|
|
||||||
L.e('Network', '$error', error);
|
|
||||||
'error';
|
|
||||||
});
|
});
|
||||||
|
switch event {
|
||||||
|
case CONNECTED:
|
||||||
|
reconnectDelay = 500;
|
||||||
|
var user = storage.user;
|
||||||
|
if (user != null) {
|
||||||
|
login(user.name, user.uuid);
|
||||||
|
}
|
||||||
|
case DISCONNECTED | ERROR(_):
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onResponse(packet:Response):Void {
|
private function onResponse(packet:Response):Void {
|
||||||
if (packet.hasLogin()) {
|
if (packet.hasLogin()) {
|
||||||
user = {
|
this.user = packet.login.user;
|
||||||
uuid: packet.login.user.uuid,
|
var user = {
|
||||||
name: packet.login.user.name,
|
uuid: this.user.uuid,
|
||||||
|
name: this.user.name,
|
||||||
};
|
};
|
||||||
storage.user = user;
|
storage.user = user;
|
||||||
updateState('online');
|
updateState(ONLINE(user));
|
||||||
} else if (packet.hasLogout()) {
|
} else if (packet.hasLogout()) {
|
||||||
user = null;
|
storage.user = null;
|
||||||
updateState('connected');
|
updateState(CONNECTED);
|
||||||
} else if (packet.hasListGame()) {
|
} else if (packet.hasRoomList()) {
|
||||||
listGameSignal.emit(packet.listGame.games);
|
listRoomSignal.emit(packet.roomList.rooms);
|
||||||
} else if (packet.hasCreateGame()) {
|
} else if (packet.hasRoom()) {
|
||||||
gameSignal.emit(packet.createGame.game);
|
room = packet.room.room;
|
||||||
} else if (packet.hasJoinGame()) {
|
roomSignal.emit(room);
|
||||||
gameSignal.emit(packet.joinGame.game);
|
} else if (packet.hasGameEvent()) {
|
||||||
} else if (packet.hasLeaveGame()) {
|
gameEventSignal.emit(packet.gameEvent);
|
||||||
gameSignal.emit(null);
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
package ru.m.tankz.render;
|
||||||
|
|
||||||
import haxework.view.IView;
|
import haxework.view.IView;
|
||||||
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
|
|
||||||
interface IRender extends IView<Dynamic> {
|
interface IRender extends IView<Dynamic> extends GameListener {
|
||||||
public var game(default, set):IGame;
|
public var config(default, set):Config;
|
||||||
public function draw():Void;
|
public function draw():Void;
|
||||||
public function reset():Void;
|
public function reset():Void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,17 @@ import ru.m.geom.Point;
|
|||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.game.GameEvent;
|
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.BonusRenderItem;
|
||||||
import ru.m.tankz.render.item.BrickRenderItem;
|
import ru.m.tankz.render.item.BrickRenderItem;
|
||||||
import ru.m.tankz.render.item.BulletRenderItem;
|
import ru.m.tankz.render.item.BulletRenderItem;
|
||||||
import ru.m.tankz.render.item.EagleRenderItem;
|
import ru.m.tankz.render.item.EagleRenderItem;
|
||||||
import ru.m.tankz.render.item.IRenderItem;
|
import ru.m.tankz.render.item.IRenderItem;
|
||||||
import ru.m.tankz.render.item.TankRenderItem;
|
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 backgroundLayer:Sprite;
|
||||||
private var groundLayer:Sprite;
|
private var groundLayer:Sprite;
|
||||||
@@ -32,7 +32,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
private var upLayer:Sprite;
|
private var upLayer:Sprite;
|
||||||
private var upperLayer:Sprite;
|
private var upperLayer:Sprite;
|
||||||
|
|
||||||
private var background:Sprite;
|
|
||||||
private var items:Map<Int, IRenderItem>;
|
private var items:Map<Int, IRenderItem>;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
@@ -51,13 +50,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_game(value:IGame):IGame {
|
private function set_config(value:Config):Config {
|
||||||
game = value;
|
return config = value;
|
||||||
game.connect(this);
|
|
||||||
return game;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function drawBackground(config:Config):Void {
|
private function drawBackground():Void {
|
||||||
var width = config.map.cellWidth * config.map.gridWidth;
|
var width = config.map.cellWidth * config.map.gridWidth;
|
||||||
var height = config.map.cellHeight * config.map.gridHeight;
|
var height = config.map.cellHeight * config.map.gridHeight;
|
||||||
var g:Graphics = backgroundLayer.graphics;
|
var g:Graphics = backgroundLayer.graphics;
|
||||||
@@ -72,9 +69,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
for (item in items) {
|
for (item in items) {
|
||||||
item.update();
|
item.update();
|
||||||
}
|
}
|
||||||
if (background == null) {
|
|
||||||
drawBackground(game.config);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onEnterFrame(event:Event):Void {
|
private function onEnterFrame(event:Event):Void {
|
||||||
@@ -91,10 +85,6 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
item.dispose();
|
item.dispose();
|
||||||
}
|
}
|
||||||
items = new Map();
|
items = new Map();
|
||||||
if (background != null) {
|
|
||||||
backgroundLayer.removeChild(background);
|
|
||||||
background = null;
|
|
||||||
}
|
|
||||||
clearLayer(entryLayer);
|
clearLayer(entryLayer);
|
||||||
clearLayer(groundLayer);
|
clearLayer(groundLayer);
|
||||||
clearLayer(upLayer);
|
clearLayer(upLayer);
|
||||||
@@ -108,9 +98,10 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
case COMPLETE(_, _):
|
case COMPLETE(_, _):
|
||||||
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
|
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||||
case SPAWN(BRICK(bricks)):
|
case SPAWN(BRICK(bricks)):
|
||||||
|
drawBackground();
|
||||||
for (brick in bricks) {
|
for (brick in bricks) {
|
||||||
var item:IRenderItem = new BrickRenderItem(brick.rect, brick.type);
|
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;
|
items[brick.id] = item;
|
||||||
if (config.layer > 2) {
|
if (config.layer > 2) {
|
||||||
upLayer.addChild(item.view);
|
upLayer.addChild(item.view);
|
||||||
@@ -119,15 +110,15 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case SPAWN(EAGLE(id, rect, teamId)):
|
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);
|
items.set(id, item);
|
||||||
entryLayer.addChild(item.view);
|
entryLayer.addChild(item.view);
|
||||||
item.update();
|
item.update();
|
||||||
case SPAWN(TANK(id, rect, playerId, info)):
|
case SPAWN(TANK(id, rect, playerId, info)):
|
||||||
var item = new TankRenderItem(rect);
|
var item = new TankRenderItem(rect);
|
||||||
var config = game.config.getTank(info.type);
|
var tankConfig = config.getTank(info.type);
|
||||||
item.color = game.config.getColor(playerId);
|
item.color = info.color;
|
||||||
item.skin = config.skin;
|
item.skin = tankConfig.skin;
|
||||||
item.hits = info.hits;
|
item.hits = info.hits;
|
||||||
item.bonus = info.bonus;
|
item.bonus = info.bonus;
|
||||||
items.set(id, item);
|
items.set(id, item);
|
||||||
@@ -144,6 +135,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
items.set(id, item);
|
items.set(id, item);
|
||||||
upperLayer.addChild(item.view);
|
upperLayer.addChild(item.view);
|
||||||
item.update();
|
item.update();
|
||||||
|
case MOVE(EAGLE(id, position)):
|
||||||
|
if (items.exists(id)) {
|
||||||
|
var item = items[id];
|
||||||
|
item.move(position);
|
||||||
|
}
|
||||||
case MOVE(BULLET(id, position)):
|
case MOVE(BULLET(id, position)):
|
||||||
if (items.exists(id)) {
|
if (items.exists(id)) {
|
||||||
var item = items[id];
|
var item = items[id];
|
||||||
@@ -163,7 +159,7 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
case CHANGE(TANK(id, type, hits, bonus)):
|
case CHANGE(TANK(id, type, hits, bonus)):
|
||||||
if (items.exists(id)) {
|
if (items.exists(id)) {
|
||||||
var item:TankRenderItem = cast items[id];
|
var item:TankRenderItem = cast items[id];
|
||||||
item.skin = game.config.getTank(type).skin;
|
item.skin = config.getTank(type).skin;
|
||||||
item.hits = hits;
|
item.hits = hits;
|
||||||
item.bonus = bonus;
|
item.bonus = bonus;
|
||||||
}
|
}
|
||||||
@@ -177,6 +173,11 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
var item:EagleRenderItem = cast items[id];
|
var item:EagleRenderItem = cast items[id];
|
||||||
item.protect = state;
|
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)):
|
case DESTROY(TANK(id, shot)):
|
||||||
if (items.exists(id)) {
|
if (items.exists(id)) {
|
||||||
var item = items[id];
|
var item = items[id];
|
||||||
@@ -223,6 +224,9 @@ class Render extends SpriteView implements IRender implements GameListener {
|
|||||||
item.destroy();
|
item.destroy();
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
|
#if cpp
|
||||||
|
flash.Lib.current.stage.invalidate();
|
||||||
|
#end
|
||||||
}
|
}
|
||||||
|
|
||||||
private function playAnimate(point:Point, animate:OnceAnimate):Promise<Dynamic> {
|
private function playAnimate(point:Point, animate:OnceAnimate):Promise<Dynamic> {
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import ru.m.tankz.Type;
|
|||||||
class BonusRenderItem extends BitmapRenderItem {
|
class BonusRenderItem extends BitmapRenderItem {
|
||||||
|
|
||||||
public var type(default, set):BonusType;
|
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) {
|
public function new(rect:Rectangle, type:BonusType) {
|
||||||
super(rect);
|
super(rect);
|
||||||
@@ -24,8 +25,9 @@ class BonusRenderItem extends BitmapRenderItem {
|
|||||||
|
|
||||||
override public function update():Void {
|
override public function update():Void {
|
||||||
super.update();
|
super.update();
|
||||||
view.alpha += d;
|
alpha += d;
|
||||||
if (view.alpha < 0 || view.alpha > 1.5) {
|
view.alpha = alpha;
|
||||||
|
if (alpha <= 0 || alpha >= 1.5) {
|
||||||
d = -d;
|
d = -d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,14 +26,13 @@ class BrickRenderItem extends RenderItem {
|
|||||||
public function new(rect:Rectangle, type:BrickType) {
|
public function new(rect:Rectangle, type:BrickType) {
|
||||||
super(rect);
|
super(rect);
|
||||||
this.shape = new Shape();
|
this.shape = new Shape();
|
||||||
this.type = type;
|
|
||||||
cells = [
|
cells = [
|
||||||
new Point(0, 0),
|
new Point(0, 0),
|
||||||
new Point(0, 1),
|
new Point(0, 1),
|
||||||
new Point(1, 0),
|
new Point(1, 0),
|
||||||
new Point(1, 1),
|
new Point(1, 1),
|
||||||
];
|
];
|
||||||
redraw();
|
this.type = type;
|
||||||
move(rect.position);
|
move(rect.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +52,7 @@ class BrickRenderItem extends RenderItem {
|
|||||||
if (type != value) {
|
if (type != value) {
|
||||||
type = value;
|
type = value;
|
||||||
image = Assets.getBitmapData('resources/image/map/${type}.png');
|
image = Assets.getBitmapData('resources/image/map/${type}.png');
|
||||||
|
redraw();
|
||||||
}
|
}
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@@ -69,6 +69,7 @@ class BrickRenderItem extends RenderItem {
|
|||||||
|
|
||||||
public function redraw():Void {
|
public function redraw():Void {
|
||||||
shape.graphics.clear();
|
shape.graphics.clear();
|
||||||
|
if (type == "none") return;
|
||||||
switch state {
|
switch state {
|
||||||
case UNBROKEN:
|
case UNBROKEN:
|
||||||
shape.graphics.beginBitmapFill(image);
|
shape.graphics.beginBitmapFill(image);
|
||||||
|
|||||||
@@ -2,18 +2,22 @@ package ru.m.tankz.render.item;
|
|||||||
|
|
||||||
import flash.display.DisplayObject;
|
import flash.display.DisplayObject;
|
||||||
import flash.display.Sprite;
|
import flash.display.Sprite;
|
||||||
|
import haxework.color.Color;
|
||||||
|
import haxework.view.utils.BitmapUtil;
|
||||||
import ru.m.animate.Animate;
|
import ru.m.animate.Animate;
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
|
|
||||||
class EagleRenderItem extends BitmapRenderItem {
|
class EagleRenderItem extends BitmapRenderItem {
|
||||||
|
public var color(default, default):Color;
|
||||||
public var death(default, set):Bool = true;
|
public var death(default, set):Bool = true;
|
||||||
public var protect(default, set):Bool;
|
public var protect(default, set):Bool;
|
||||||
|
|
||||||
private var container:Sprite;
|
private var container:Sprite;
|
||||||
private var protectView:Animate;
|
private var protectView:Animate;
|
||||||
|
|
||||||
public function new(rect:Rectangle) {
|
public function new(rect:Rectangle, color:Color) {
|
||||||
super(rect);
|
super(rect);
|
||||||
|
this.color = color;
|
||||||
container = new Sprite();
|
container = new Sprite();
|
||||||
container.addChild(bitmap);
|
container.addChild(bitmap);
|
||||||
protectView = AnimateBundle.tankProtect();
|
protectView = AnimateBundle.tankProtect();
|
||||||
@@ -45,6 +49,9 @@ class EagleRenderItem extends BitmapRenderItem {
|
|||||||
death = value;
|
death = value;
|
||||||
var suffix = death ? '-death' : '';
|
var suffix = death ? '-death' : '';
|
||||||
image = 'resources/image/eagle/eagle${suffix}.png';
|
image = 'resources/image/eagle/eagle${suffix}.png';
|
||||||
|
if (!color.zero) {
|
||||||
|
bitmap.bitmapData = bitmapData = BitmapUtil.colorize(bitmapData, color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return death;
|
return death;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,10 +55,7 @@ class TankRenderItem extends BitmapRenderItem {
|
|||||||
if (!color1.zero) {
|
if (!color1.zero) {
|
||||||
image2 = BitmapUtil.colorize(image2, color2);
|
image2 = BitmapUtil.colorize(image2, color2);
|
||||||
}
|
}
|
||||||
images = [
|
images = [for (i in 0...6) image1].concat([for (i in 0...6) image2]);
|
||||||
image1, image1, image1,
|
|
||||||
image2, image2, image2,
|
|
||||||
];
|
|
||||||
frame = 0;
|
frame = 0;
|
||||||
bitmap.bitmapData = images[frame];
|
bitmap.bitmapData = images[frame];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,24 +5,30 @@ import flash.media.Sound;
|
|||||||
import flash.media.SoundChannel;
|
import flash.media.SoundChannel;
|
||||||
import flash.media.SoundTransform;
|
import flash.media.SoundTransform;
|
||||||
import openfl.utils.Assets;
|
import openfl.utils.Assets;
|
||||||
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
|
|
||||||
class SoundManager implements GameListener {
|
class SoundManager implements GameListener {
|
||||||
private static var TAG(default, never):String = 'SoundManager';
|
private static var TAG(default, never):String = "SoundManager";
|
||||||
|
|
||||||
#if flash
|
#if flash
|
||||||
private static var type:String = 'mp3';
|
private static var type:String = "mp3";
|
||||||
#else
|
#else
|
||||||
private static var type:String = 'ogg';
|
private static var type:String = "ogg";
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
public var config(default, default):Config;
|
||||||
public var volume(default, set):Float = 1;
|
public var volume(default, set):Float = 1;
|
||||||
public var mute(default, set):Bool = false;
|
public var mute(default, set):Bool = false;
|
||||||
|
|
||||||
private var channels:Array<SoundChannel>;
|
private var channels:Array<SoundChannel>;
|
||||||
private var transform:SoundTransform;
|
private var transform:SoundTransform;
|
||||||
|
|
||||||
|
private var humanTanks:Array<Int>;
|
||||||
|
private var humanBullets:Array<Int>;
|
||||||
|
private var liveBonuses:Array<Int>;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
channels = [];
|
channels = [];
|
||||||
updateSoundTransform();
|
updateSoundTransform();
|
||||||
@@ -77,30 +83,44 @@ class SoundManager implements GameListener {
|
|||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
switch event {
|
switch event {
|
||||||
case START(_):
|
case START(_):
|
||||||
play('start');
|
play("start");
|
||||||
case SPAWN(BULLET(_)):
|
humanTanks = [];
|
||||||
if (false /*ToDo: human tank*/) {
|
liveBonuses = [];
|
||||||
play('shot');
|
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(_, _)):
|
case HIT(TANK(_, _)):
|
||||||
play('bullet_hit');
|
play("bullet_hit");
|
||||||
case DESTROY(TANK(_, _)):
|
case HIT(CELL(_, _, shot)):
|
||||||
if (true /*ToDo: human tank*/) {
|
if (humanBullets.indexOf(shot.bulletId) > -1) {
|
||||||
play('boom_player');
|
//play("bullet_block");
|
||||||
|
}
|
||||||
|
case DESTROY(TANK(id, _)):
|
||||||
|
if (humanTanks.indexOf(id) > -1) {
|
||||||
|
play("boom_player");
|
||||||
} else {
|
} else {
|
||||||
play('boom_bot');
|
play("boom_bot");
|
||||||
}
|
}
|
||||||
case DESTROY(EAGLE(_, _)):
|
case DESTROY(EAGLE(_, _)):
|
||||||
play('boom_player');
|
play("boom_player");
|
||||||
case DESTROY(BONUS(_, _)):
|
case DESTROY(BONUS(id, _)):
|
||||||
// ToDo: bonus type
|
play("bonus_get");
|
||||||
play('bonus_get');
|
if (liveBonuses.indexOf(id) > -1) {
|
||||||
if (false /*ToDo: bonus.type == 'life'*/) {
|
play("live");
|
||||||
play('live');
|
|
||||||
} else {
|
} else {
|
||||||
play('bonus_get');
|
play("bonus_get");
|
||||||
}
|
}
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,34 @@
|
|||||||
package ru.m.tankz.view;
|
package ru.m.tankz.view;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
import flash.events.KeyboardEvent;
|
import flash.events.KeyboardEvent;
|
||||||
import flash.ui.Keyboard;
|
import flash.ui.Keyboard;
|
||||||
import haxework.resources.IResources;
|
import haxework.resources.IResources;
|
||||||
|
import haxework.view.ButtonView;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.IView;
|
import haxework.view.LabelView;
|
||||||
|
import haxework.view.VGroupView;
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.sound.SoundManager;
|
import ru.m.tankz.sound.SoundManager;
|
||||||
|
import ru.m.tankz.view.popup.LoginPopup;
|
||||||
|
|
||||||
@:template class ClientView extends FrameSwitcher {
|
@:template class ClientView extends VGroupView {
|
||||||
|
@:view("switcher") var switcherView:FrameSwitcher;
|
||||||
|
@:view var username:LabelView;
|
||||||
|
@:view("login") var loginButton:ButtonView;
|
||||||
|
@:view("logout") var logoutButton:ButtonView;
|
||||||
|
|
||||||
|
@:provide var network:NetworkManager;
|
||||||
@:provide var resources:IResources;
|
@:provide var resources:IResources;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var soundManager:SoundManager;
|
@:provide var soundManager:SoundManager;
|
||||||
|
@:provide var game:IGame;
|
||||||
|
|
||||||
public function init():Void {
|
public function init():Void {
|
||||||
resources.text.put('version', '${Const.VERSION}');
|
resources.text.put('version', '${Const.VERSION}');
|
||||||
switcher = this;
|
switcher = switcherView;
|
||||||
onSwitch.connect(onFrameSwitch);
|
network.stateSignal.connect(onConnectionState);
|
||||||
|
onConnectionState(network.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function launch():Void {
|
public function launch():Void {
|
||||||
@@ -24,17 +36,50 @@ import ru.m.tankz.sound.SoundManager;
|
|||||||
content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||||
switch event.keyCode {
|
switch event.keyCode {
|
||||||
case Keyboard.ESCAPE:
|
case Keyboard.ESCAPE:
|
||||||
change(StartFrame.ID);
|
switcher.change(StartFrame.ID);
|
||||||
case Keyboard.M:
|
case Keyboard.M:
|
||||||
soundManager.mute = !soundManager.mute;
|
soundManager.mute = !soundManager.mute;
|
||||||
|
case Keyboard.P:
|
||||||
|
if (game != null) {
|
||||||
|
game.pause = !game.pause;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
change(StartFrame.ID);
|
switcher.change(StartFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onFrameSwitch(frame:IView<Dynamic>):Void {
|
private function onConnectionState(state:ConnectionState):Void {
|
||||||
if (frame.id == StartFrame.ID) {
|
L.d("ClientView", 'onConnectionState: ${state}');
|
||||||
soundManager.stopAll();
|
switch state {
|
||||||
|
case ONLINE(user):
|
||||||
|
username.text = user.name;
|
||||||
|
logoutButton.visible = true;
|
||||||
|
loginButton.visible = false;
|
||||||
|
case CONNECTED:
|
||||||
|
username.text = "";
|
||||||
|
logoutButton.visible = false;
|
||||||
|
loginButton.visible = true;
|
||||||
|
case ERROR(error):
|
||||||
|
//L.e("ClientView", 'onConnectionState: ERROR', error);
|
||||||
|
L.w("ClientView", 'onConnectionState: ERROR');
|
||||||
|
case _:
|
||||||
|
username.text = "";
|
||||||
|
logoutButton.visible = false;
|
||||||
|
loginButton.visible = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 close():Void {
|
||||||
|
switcher.change(StartFrame.ID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
---
|
---
|
||||||
$type: haxework.view.frame.FrameSwitcher
|
layout.overflow: true
|
||||||
|
views:
|
||||||
|
- $type: haxework.view.frame.FrameSwitcher
|
||||||
|
id: switcher
|
||||||
geometry.size.stretch: true
|
geometry.size.stretch: true
|
||||||
|
layout.overflow: true
|
||||||
skinId: dark
|
skinId: dark
|
||||||
views:
|
views:
|
||||||
- id: start
|
- id: start
|
||||||
@@ -15,3 +19,35 @@ views:
|
|||||||
$type: ru.m.tankz.view.SettingsFrame
|
$type: ru.m.tankz.view.SettingsFrame
|
||||||
- id: record
|
- id: record
|
||||||
$type: ru.m.tankz.view.RecordFrame
|
$type: ru.m.tankz.view.RecordFrame
|
||||||
|
- id: room_list
|
||||||
|
$type: ru.m.tankz.view.network.RoomListFrame
|
||||||
|
- id: room
|
||||||
|
$type: ru.m.tankz.view.network.RoomFrame
|
||||||
|
- $type: haxework.view.HGroupView
|
||||||
|
skinId: panel
|
||||||
|
layout.margin: 10
|
||||||
|
views:
|
||||||
|
- id: settings
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.settings
|
||||||
|
+onPress: $code:switcher.change('settings')
|
||||||
|
- $type: haxework.view.SpriteView
|
||||||
|
geometry.size.width: 50%
|
||||||
|
- id: username
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
skinId: text
|
||||||
|
- 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.SpriteView
|
||||||
|
geometry.size.width: 50%
|
||||||
|
- id: close
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.close
|
||||||
|
+onPress: $code:close()
|
||||||
|
|||||||
@@ -3,18 +3,14 @@ package ru.m.tankz.view;
|
|||||||
import haxe.ds.Option;
|
import haxe.ds.Option;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
import ru.m.tankz.game.Game;
|
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.GameRunner;
|
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.game.record.GamePlayer;
|
|
||||||
import ru.m.tankz.game.record.GameRecord;
|
import ru.m.tankz.game.record.GameRecord;
|
||||||
import ru.m.tankz.game.record.GameRecorder;
|
|
||||||
import ru.m.tankz.network.NetworkManager;
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.sound.SoundManager;
|
import ru.m.tankz.sound.SoundManager;
|
||||||
import ru.m.tankz.storage.GameStorage;
|
import ru.m.tankz.storage.GameStorage;
|
||||||
import ru.m.tankz.storage.RecordStorage;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.tankz.view.game.GameView;
|
import ru.m.tankz.view.game.GameView;
|
||||||
|
|
||||||
@:template class GameFrame extends VGroupView implements GameListener {
|
@:template class GameFrame extends VGroupView implements GameListener {
|
||||||
@@ -28,61 +24,26 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
@:provide var soundManager:SoundManager;
|
@:provide var soundManager:SoundManager;
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
@:provide var record:GameRecord;
|
@:provide var record:GameRecord;
|
||||||
@:provide("result") var result:GameState;
|
@:provide("next") var nextState:GameState;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var gameStorage:GameStorage;
|
@:provide var gameStorage:GameStorage;
|
||||||
@:provide var recordStorage:RecordStorage;
|
|
||||||
|
|
||||||
private var game:IGame;
|
@:provide var game:IGame;
|
||||||
private var runner:GameRunner;
|
|
||||||
private var recorder:GameRecorder;
|
|
||||||
private var player:GamePlayer;
|
|
||||||
|
|
||||||
public function onShow():Void {
|
public function onShow():Void {
|
||||||
if (record != null) {
|
gameView.type = game.type;
|
||||||
play(record);
|
soundManager.config = game.config;
|
||||||
record = null;
|
gameView.render.config = game.config;
|
||||||
} else {
|
game.connect(gameView.render);
|
||||||
start(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function start(state:GameState):Void {
|
|
||||||
gameView.type = state.type;
|
|
||||||
game = new Game(state);
|
|
||||||
gameView.render.game = game;
|
|
||||||
game.connect(soundManager);
|
game.connect(soundManager);
|
||||||
|
if (gameView.panel != null) {
|
||||||
|
game.connect(gameView.panel);
|
||||||
|
}
|
||||||
game.connect(this);
|
game.connect(this);
|
||||||
if (gameView.panel != null) {
|
game.start();
|
||||||
game.connect(gameView.panel);
|
|
||||||
}
|
|
||||||
//game.connect(new GameTracer());
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
player = new GamePlayer(game, record);
|
|
||||||
player.start();
|
|
||||||
gameView.render.draw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function stop():Void {
|
private function stop():Void {
|
||||||
if (runner != null) {
|
|
||||||
runner.dispose();
|
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
if (game != null) {
|
if (game != null) {
|
||||||
game.dispose();
|
game.dispose();
|
||||||
game = null;
|
game = null;
|
||||||
@@ -92,19 +53,17 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.COMPLETE(state, _):
|
case GameEvent.COMPLETE(state, winner):
|
||||||
// ToDo:
|
this.state = state;
|
||||||
recordStorage.save(recorder.record);
|
nextState = switch next(winner) {
|
||||||
result = state;
|
|
||||||
this.state = switch runner.next() {
|
|
||||||
case Some(s):
|
case Some(s):
|
||||||
// ToDo:
|
// ToDo:
|
||||||
var progress = gameStorage.get(game.type);
|
var progress = gameStorage.get(game.type);
|
||||||
progress.completeLevel(result.levelId, result.presetId);
|
progress.completeLevel(state.levelId, state.presetId);
|
||||||
gameStorage.set(progress);
|
gameStorage.set(progress);
|
||||||
s;
|
s;
|
||||||
case None:
|
case None:
|
||||||
new GameState(state.type, state.presetId, 0);
|
null;
|
||||||
}
|
}
|
||||||
stop();
|
stop();
|
||||||
switcher.change(ResultFrame.ID);
|
switcher.change(ResultFrame.ID);
|
||||||
@@ -112,8 +71,21 @@ import ru.m.tankz.view.game.GameView;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToDo:
|
||||||
|
private function next(winner:TeamId):Option<GameState> {
|
||||||
|
for (rule in game.config.game.complete) {
|
||||||
|
if (rule.team != null && rule.team != winner) {
|
||||||
|
return Option.None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var level = state.levelId + 1;
|
||||||
|
if (level >= game.config.game.levels) level = 0;
|
||||||
|
return Option.Some(new GameState(game.type, state.presetId, level, state));
|
||||||
|
}
|
||||||
|
|
||||||
public function onHide():Void {
|
public function onHide():Void {
|
||||||
stop();
|
stop();
|
||||||
|
soundManager.stopAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function close():Void {
|
public function close():Void {
|
||||||
|
|||||||
@@ -5,10 +5,3 @@ views:
|
|||||||
views:
|
views:
|
||||||
- id: game
|
- id: game
|
||||||
$type: ru.m.tankz.view.game.GameView
|
$type: ru.m.tankz.view.game.GameView
|
||||||
# - $type: haxework.view.HGroupView
|
|
||||||
# skinId: panel
|
|
||||||
# views:
|
|
||||||
# - id: close
|
|
||||||
# $type: haxework.view.ButtonView
|
|
||||||
# skinId: button.close
|
|
||||||
# +onPress: $code:close()
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import haxework.view.VGroupView;
|
|||||||
import ru.m.tankz.bundle.ILevelBundle;
|
import ru.m.tankz.bundle.ILevelBundle;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
|
import ru.m.tankz.game.LocalGame;
|
||||||
import ru.m.tankz.storage.GameStorage;
|
import ru.m.tankz.storage.GameStorage;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.tankz.view.popup.LevelPopup;
|
import ru.m.tankz.view.popup.LevelPopup;
|
||||||
@@ -19,6 +21,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
@:view var levels:DataView<LevelId, ButtonView>;
|
@:view var levels:DataView<LevelId, ButtonView>;
|
||||||
|
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
|
@:provide var game:IGame;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var levelBundle:ILevelBundle;
|
@:provide var levelBundle:ILevelBundle;
|
||||||
@:provide var storage:GameStorage;
|
@:provide var storage:GameStorage;
|
||||||
@@ -30,9 +33,11 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
levels.data = [for (i in 0...state.config.game.levels) i];
|
levels.data = [for (i in 0...state.config.game.levels) i];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function start(level:LevelConfig, preset:GamePreset):Void {
|
private function start(level:LevelConfig, preset:GamePreset, control:ControlPreset):Void {
|
||||||
state.levelId = level.id;
|
state.levelId = level.id;
|
||||||
state.presetId = preset.id;
|
state.presetId = preset.id;
|
||||||
|
state.controls = control.values;
|
||||||
|
game = new LocalGame(state);
|
||||||
switcher.change(GameFrame.ID);
|
switcher.change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +62,9 @@ import ru.m.tankz.view.popup.LevelPopup;
|
|||||||
levelPopup.setData(
|
levelPopup.setData(
|
||||||
level,
|
level,
|
||||||
state.config.presets,
|
state.config.presets,
|
||||||
|
state.config.controls,
|
||||||
storage.get(state.type)
|
storage.get(state.type)
|
||||||
);
|
);
|
||||||
levelPopup.show().then(function(preset) preset != null ? start(level, preset) : {});
|
levelPopup.show().then(function(result) result != null ? start(level, result.preset, result.control) : {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,3 @@ views:
|
|||||||
factory: $this:levelViewFactory
|
factory: $this:levelViewFactory
|
||||||
+onItemSelect: $this:onLevelSelect
|
+onItemSelect: $this:onLevelSelect
|
||||||
geometry.padding: 10
|
geometry.padding: 10
|
||||||
- $type: haxework.view.HGroupView
|
|
||||||
skinId: panel
|
|
||||||
views:
|
|
||||||
- $type: haxework.view.ButtonView
|
|
||||||
skinId: button.close
|
|
||||||
+onPress: $code:switcher.change('start')
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -25,8 +25,4 @@ import ru.m.tankz.storage.RecordStorage;
|
|||||||
private function onRecordSelect(item:IListItemView<GameRecordInfo>):Void {
|
private function onRecordSelect(item:IListItemView<GameRecordInfo>):Void {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function close() {
|
|
||||||
switcher.change(StartFrame.ID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,3 @@ views:
|
|||||||
scroll:
|
scroll:
|
||||||
$type: haxework.view.list.VScrollBarView
|
$type: haxework.view.list.VScrollBarView
|
||||||
skinId: scroll
|
skinId: scroll
|
||||||
- $type: haxework.view.HGroupView
|
|
||||||
skinId: panel
|
|
||||||
views:
|
|
||||||
- $type: haxework.view.ButtonView
|
|
||||||
skinId: button.close
|
|
||||||
+onPress: $code:close()
|
|
||||||
|
|||||||
@@ -5,41 +5,45 @@ import haxework.view.DataView;
|
|||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
|
import ru.m.tankz.game.GameRunner;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.view.common.LifeView;
|
import ru.m.tankz.view.common.LifeView;
|
||||||
|
|
||||||
@:template class ResultFrame extends VGroupView {
|
@:template class ResultFrame extends VGroupView {
|
||||||
public static var ID(default, never):String = "result";
|
public static var ID(default, never):String = "result";
|
||||||
|
|
||||||
@:view("result") var resultView:DataView<PlayerState, LifeView>;
|
@:view("result") var resultView:DataView<PlayerState, LifeView>;
|
||||||
@:view("next") var nextButton:ButtonView;
|
|
||||||
@:view("level") var levelLabel:LabelView;
|
@:view("level") var levelLabel:LabelView;
|
||||||
|
|
||||||
@:provide var frames:FrameSwitcher;
|
@:provide var frames:FrameSwitcher;
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
@:provide("result") var resultState:GameState;
|
@:provide("next") var nextState:GameState;
|
||||||
|
@:provide var game:IGame;
|
||||||
|
|
||||||
private function playerViewFactory(index:Int, player:PlayerState) {
|
private function playerViewFactory(index:Int, player:PlayerState) {
|
||||||
var view = new LifeView();
|
var view = new LifeView();
|
||||||
var playerConfig = resultState.config.getPlayer(player.id);
|
var playerConfig = state.config.getPlayer(player.id);
|
||||||
var tankType = playerConfig.tanks[0].type;
|
var tankType = playerConfig.tanks[0].type;
|
||||||
var tankConfig = resultState.config.getTank(tankType);
|
var tankConfig = state.config.getTank(tankType);
|
||||||
view.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
view.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||||
view.color = resultState.getPlayerColor(player.id);
|
view.color = state.getPlayerColor(player.id);
|
||||||
view.life = player.frags;
|
view.life = player.frags;
|
||||||
view.score = player.score;
|
view.score = player.score;
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onShow() {
|
public function onShow() {
|
||||||
resultView.data = Lambda.array(resultState.players);
|
resultView.data = Lambda.array(state.players);
|
||||||
levelLabel.text = 'Level ${resultState.levelId}';
|
levelLabel.text = 'Level ${state.levelId}';
|
||||||
nextButton.visible = state != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function next() {
|
private function next() {
|
||||||
|
if (nextState != null) {
|
||||||
|
game = new GameRunner(nextState);
|
||||||
frames.change(GameFrame.ID);
|
frames.change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function close() {
|
private function close() {
|
||||||
frames.change(LevelFrame.ID);
|
frames.change(LevelFrame.ID);
|
||||||
|
|||||||
@@ -14,16 +14,3 @@ views:
|
|||||||
$type: haxework.view.layout.VerticalLayout
|
$type: haxework.view.layout.VerticalLayout
|
||||||
hAlign: right
|
hAlign: right
|
||||||
margin: 10
|
margin: 10
|
||||||
- $type: haxework.view.HGroupView
|
|
||||||
skinId: panel
|
|
||||||
views:
|
|
||||||
- id: close
|
|
||||||
$type: haxework.view.ButtonView
|
|
||||||
skinId: button.close
|
|
||||||
+onPress: $code:close()
|
|
||||||
- $type: haxework.view.SpriteView
|
|
||||||
geometry.size.width: 100%
|
|
||||||
- id: next
|
|
||||||
$type: haxework.view.ButtonView
|
|
||||||
skinId: button.next
|
|
||||||
+onPress: $code:next()
|
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package ru.m.tankz.view;
|
package ru.m.tankz.view;
|
||||||
|
|
||||||
import haxework.view.frame.FrameSwitcher;
|
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
|
|
||||||
@:template class SettingsFrame extends VGroupView {
|
@:template class SettingsFrame extends VGroupView {
|
||||||
|
|
||||||
public static var ID(default, never):String = "settings";
|
public static var ID(default, never):String = "settings";
|
||||||
|
|
||||||
@:provide var switcher:FrameSwitcher;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,3 @@ views:
|
|||||||
controlIndex: 0
|
controlIndex: 0
|
||||||
- $type: ru.m.tankz.view.settings.SettingsEditor
|
- $type: ru.m.tankz.view.settings.SettingsEditor
|
||||||
controlIndex: 1
|
controlIndex: 1
|
||||||
- $type: haxework.view.HGroupView
|
|
||||||
skinId: panel
|
|
||||||
views:
|
|
||||||
- $type: haxework.view.ButtonView
|
|
||||||
skinId: button.close
|
|
||||||
+onPress: $code:switcher.change('start')
|
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ package ru.m.tankz.view;
|
|||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.VGroupView;
|
import haxework.view.VGroupView;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
import ru.m.tankz.Type.GameType;
|
import ru.m.tankz.Type.GameType;
|
||||||
|
import ru.m.tankz.view.network.RoomListFrame;
|
||||||
|
import ru.m.tankz.view.network.RoomFrame;
|
||||||
import ru.m.tankz.view.popup.FontPopup;
|
import ru.m.tankz.view.popup.FontPopup;
|
||||||
|
import ru.m.tankz.view.popup.LoginPopup;
|
||||||
|
|
||||||
@:template class StartFrame extends VGroupView {
|
@:template class StartFrame extends VGroupView {
|
||||||
|
|
||||||
@@ -12,6 +16,7 @@ import ru.m.tankz.view.popup.FontPopup;
|
|||||||
|
|
||||||
@:provide var state:GameState;
|
@:provide var state:GameState;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
|
@:provide var network:NetworkManager;
|
||||||
|
|
||||||
private var fontPopup:FontPopup;
|
private var fontPopup:FontPopup;
|
||||||
|
|
||||||
@@ -20,6 +25,25 @@ import ru.m.tankz.view.popup.FontPopup;
|
|||||||
switcher.change(LevelFrame.ID);
|
switcher.change(LevelFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function startNetwork():Void {
|
||||||
|
switch network.state {
|
||||||
|
case ONLINE(user):
|
||||||
|
if (network.room != null) {
|
||||||
|
switcher.change(RoomFrame.ID);
|
||||||
|
network.joinGame(network.room.game.id, true);
|
||||||
|
} else {
|
||||||
|
switcher.change(RoomListFrame.ID);
|
||||||
|
}
|
||||||
|
case CONNECTED:
|
||||||
|
LoginPopup.instance.show().then(function(user):Void {
|
||||||
|
if (user != null) {
|
||||||
|
switcher.change(RoomListFrame.ID);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function choiceFont():Void {
|
private function choiceFont():Void {
|
||||||
if (fontPopup == null) {
|
if (fontPopup == null) {
|
||||||
fontPopup = new FontPopup();
|
fontPopup = new FontPopup();
|
||||||
|
|||||||
@@ -25,16 +25,17 @@ views:
|
|||||||
skinId: button
|
skinId: button
|
||||||
+onPress: $code:switcher.change('record')
|
+onPress: $code:switcher.change('record')
|
||||||
text: Records
|
text: Records
|
||||||
- $type: haxework.view.HGroupView
|
- id: network
|
||||||
skinId: panel
|
|
||||||
views:
|
|
||||||
- id: settings
|
|
||||||
$type: haxework.view.ButtonView
|
$type: haxework.view.ButtonView
|
||||||
skinId: button.settings
|
skinId: button
|
||||||
+onPress: $code:switcher.change('settings')
|
+onPress: $code:startNetwork()
|
||||||
- $type: haxework.view.SpriteView
|
text: Network
|
||||||
geometry.size.width: 100%
|
#disabled: true
|
||||||
- $type: haxework.view.LabelView
|
- $type: haxework.view.LabelView
|
||||||
|
geometry.hAlign: right
|
||||||
|
geometry.vAlign: top
|
||||||
geometry.padding: [20, 5]
|
geometry.padding: [20, 5]
|
||||||
|
geometry.position: absolute
|
||||||
|
geometry.margin: [0, 20, 20, 0]
|
||||||
skinId: text.box
|
skinId: text.box
|
||||||
text: $r:text:version
|
text: $r:text:version
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.view.common;
|
package ru.m.tankz.view.common;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.record.GamePlayer;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.HGroupView;
|
import haxework.view.HGroupView;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
@@ -18,7 +20,7 @@ import ru.m.tankz.storage.RecordStorage;
|
|||||||
|
|
||||||
@:provide var recordStorage:RecordStorage;
|
@:provide var recordStorage:RecordStorage;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var record:GameRecord;
|
@:provide var game:IGame;
|
||||||
|
|
||||||
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
||||||
if (data != value) {
|
if (data != value) {
|
||||||
@@ -32,7 +34,8 @@ import ru.m.tankz.storage.RecordStorage;
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function play():Void {
|
private function play():Void {
|
||||||
record = recordStorage.read(data.id);
|
var record = recordStorage.read(data.id);
|
||||||
|
game = new GamePlayer(record);
|
||||||
switcher.change(GameFrame.ID);
|
switcher.change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package ru.m.tankz.view.network;
|
|
||||||
|
|
||||||
import haxework.view.HGroupView;
|
|
||||||
import haxework.view.LabelView;
|
|
||||||
import haxework.view.list.ListView;
|
|
||||||
import ru.m.tankz.proto.core.GameInfoProto;
|
|
||||||
|
|
||||||
|
|
||||||
@:template class GameItemView extends HGroupView implements IListItemView<GameInfoProto> {
|
|
||||||
|
|
||||||
public var item_index(default, default):Int;
|
|
||||||
public var data(default, set):GameInfoProto;
|
|
||||||
|
|
||||||
@:view var label(default, null):LabelView;
|
|
||||||
|
|
||||||
private function set_data(value:GameInfoProto):GameInfoProto {
|
|
||||||
data = value;
|
|
||||||
label.text = '${data.type}';
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
50
src/client/haxe/ru/m/tankz/view/network/RoomFrame.hx
Normal file
50
src/client/haxe/ru/m/tankz/view/network/RoomFrame.hx
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package ru.m.tankz.view.network;
|
||||||
|
|
||||||
|
import haxework.view.ButtonView;
|
||||||
|
import haxework.view.frame.FrameSwitcher;
|
||||||
|
import haxework.view.list.VListView;
|
||||||
|
import haxework.view.TextView;
|
||||||
|
import haxework.view.VGroupView;
|
||||||
|
import ru.m.tankz.game.IGame;
|
||||||
|
import ru.m.tankz.game.NetworkGame;
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
|
import ru.m.tankz.proto.room.RoomProto;
|
||||||
|
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||||
|
|
||||||
|
@:template class RoomFrame extends VGroupView {
|
||||||
|
|
||||||
|
public static inline var ID = "room";
|
||||||
|
|
||||||
|
@:view var start:ButtonView;
|
||||||
|
@:view var info:TextView;
|
||||||
|
@:view var slots:VListView<RoomSlotProto>;
|
||||||
|
|
||||||
|
@:provide var switcher:FrameSwitcher;
|
||||||
|
@:provide var network:NetworkManager;
|
||||||
|
@:provide var game:IGame;
|
||||||
|
|
||||||
|
private function refresh(room:RoomProto):Void {
|
||||||
|
if (room != null) {
|
||||||
|
start.visible = room.creator.uuid == network.user.uuid;
|
||||||
|
info.text = '${room.game.type} #${room.game.level}';
|
||||||
|
slots.data = room.slots;
|
||||||
|
if (room.game.started) {
|
||||||
|
if (this.game == null) {
|
||||||
|
this.game = new NetworkGame(network);
|
||||||
|
}
|
||||||
|
switcher.change(GameFrame.ID);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timer.delay(function() switcher.change(RoomListFrame.ID), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onShow():Void {
|
||||||
|
refresh(network.room);
|
||||||
|
network.roomSignal.connect(refresh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onHide():Void {
|
||||||
|
network.roomSignal.disconnect(refresh);
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/client/haxe/ru/m/tankz/view/network/RoomFrame.yaml
Normal file
38
src/client/haxe/ru/m/tankz/view/network/RoomFrame.yaml
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
---
|
||||||
|
views:
|
||||||
|
- $type: haxework.view.VGroupView
|
||||||
|
skinId: container
|
||||||
|
geometry.padding: 20
|
||||||
|
layout.margin: 10
|
||||||
|
views:
|
||||||
|
- id: header
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.margin.bottom: 20
|
||||||
|
skinId: text.header
|
||||||
|
text: Game Room
|
||||||
|
- $type: haxework.view.HGroupView
|
||||||
|
layout.margin: 10
|
||||||
|
views:
|
||||||
|
- id: info
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.padding: [30, 10]
|
||||||
|
skinId: text.box
|
||||||
|
- id: start
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.simple
|
||||||
|
text: Start
|
||||||
|
+onPress: $code:network.startGame()
|
||||||
|
visible: false
|
||||||
|
- id: leave
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.simple
|
||||||
|
text: Leave
|
||||||
|
+onPress: $code:network.leaveGame()
|
||||||
|
- $type: haxework.view.SpriteView
|
||||||
|
geometry.size.width: 100%
|
||||||
|
geometry.size.height: 3
|
||||||
|
skinId: line
|
||||||
|
- id: slots
|
||||||
|
$type: haxework.view.list.VListView
|
||||||
|
geometry.size.stretch: true
|
||||||
|
factory: $code:ru.m.tankz.view.network.SlotItemView.factory
|
||||||
40
src/client/haxe/ru/m/tankz/view/network/RoomItemView.hx
Normal file
40
src/client/haxe/ru/m/tankz/view/network/RoomItemView.hx
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package ru.m.tankz.view.network;
|
||||||
|
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
|
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||||
|
import haxework.view.ButtonView;
|
||||||
|
import haxework.view.HGroupView;
|
||||||
|
import haxework.view.LabelView;
|
||||||
|
import haxework.view.list.ListView;
|
||||||
|
import ru.m.tankz.proto.room.RoomProto;
|
||||||
|
|
||||||
|
@:template class RoomItemView extends HGroupView implements IListItemView<RoomProto> {
|
||||||
|
|
||||||
|
public var item_index(default, default):Int;
|
||||||
|
public var data(default, set):RoomProto;
|
||||||
|
|
||||||
|
@:view("creator") var creatorView:LabelView;
|
||||||
|
@:view("type") var typeView:LabelView;
|
||||||
|
@:view("users") var usersView:LabelView;
|
||||||
|
@:view("join") var joinButton:ButtonView;
|
||||||
|
|
||||||
|
@:provide static var network:NetworkManager;
|
||||||
|
|
||||||
|
private function set_data(value:RoomProto):RoomProto {
|
||||||
|
data = value;
|
||||||
|
creatorView.skinId = value.creator.uuid == network.user.uuid ? "text.box.active" : "text.box";
|
||||||
|
creatorView.text = value.creator.name;
|
||||||
|
typeView.text = '${value.game.type} #${value.game.level}';
|
||||||
|
var used = value.slots.filter(function(slot:RoomSlotProto) return slot.hasUser()).length;
|
||||||
|
usersView.text = value.game.started ? 'started' : '${used}/${value.slots.length}';
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function join():Void {
|
||||||
|
network.joinGame(data.game.id, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function factory():RoomItemView {
|
||||||
|
return new RoomItemView();
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/client/haxe/ru/m/tankz/view/network/RoomItemView.yaml
Normal file
27
src/client/haxe/ru/m/tankz/view/network/RoomItemView.yaml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
---
|
||||||
|
# geometry.size.width: 100%
|
||||||
|
geometry.size.height: 48
|
||||||
|
layout.margin: 10
|
||||||
|
layout.hAlign: center
|
||||||
|
views:
|
||||||
|
- id: creator
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.size.height: 100%
|
||||||
|
geometry.size.width: 200
|
||||||
|
skinId: text.box
|
||||||
|
- id: type
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.size.height: 100%
|
||||||
|
geometry.size.width: 200
|
||||||
|
skinId: text.box
|
||||||
|
- id: users
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.size.height: 100%
|
||||||
|
geometry.size.width: 150
|
||||||
|
skinId: text.box
|
||||||
|
- id: join
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
geometry.size.height: 100%
|
||||||
|
skinId: button.simple
|
||||||
|
text: Join
|
||||||
|
+onPress: $code:join()
|
||||||
47
src/client/haxe/ru/m/tankz/view/network/RoomListFrame.hx
Normal file
47
src/client/haxe/ru/m/tankz/view/network/RoomListFrame.hx
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
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.room.RoomProto;
|
||||||
|
import ru.m.tankz.view.popup.CreateGamePopup;
|
||||||
|
|
||||||
|
@:template class RoomListFrame extends VGroupView {
|
||||||
|
public static inline var ID = "room_list";
|
||||||
|
|
||||||
|
@:view var games:VListView<RoomProto>;
|
||||||
|
|
||||||
|
@:provide var switcher:FrameSwitcher;
|
||||||
|
@:provide var network:NetworkManager;
|
||||||
|
|
||||||
|
public function onShow():Void {
|
||||||
|
network.listRoomSignal.connect(onRoomList);
|
||||||
|
network.roomSignal.connect(onRoom);
|
||||||
|
network.listGame(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onHide():Void {
|
||||||
|
network.listRoomSignal.disconnect(onRoomList);
|
||||||
|
network.roomSignal.disconnect(onRoom);
|
||||||
|
network.listGame(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function create():Void {
|
||||||
|
CreateGamePopup.instance.show().then(function(result) {
|
||||||
|
if (result != null) {
|
||||||
|
network.createGame(result.type, result.level);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onRoomList(data:Array<RoomProto>):Void {
|
||||||
|
games.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onRoom(room:RoomProto):Void {
|
||||||
|
if (room != null) {
|
||||||
|
switcher.change(RoomFrame.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/client/haxe/ru/m/tankz/view/network/RoomListFrame.yaml
Normal file
20
src/client/haxe/ru/m/tankz/view/network/RoomListFrame.yaml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
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.RoomItemView.factory
|
||||||
|
geometry.margin: 10
|
||||||
45
src/client/haxe/ru/m/tankz/view/network/SlotItemView.hx
Normal file
45
src/client/haxe/ru/m/tankz/view/network/SlotItemView.hx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package ru.m.tankz.view.network;
|
||||||
|
|
||||||
|
import haxework.view.ButtonView;
|
||||||
|
import haxework.view.HGroupView;
|
||||||
|
import haxework.view.LabelView;
|
||||||
|
import haxework.view.list.ListView;
|
||||||
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
|
import ru.m.tankz.network.NetworkManager;
|
||||||
|
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||||
|
|
||||||
|
@:template class SlotItemView extends HGroupView implements IListItemView<RoomSlotProto> {
|
||||||
|
|
||||||
|
public var item_index(default, default):Int;
|
||||||
|
public var data(default, set):RoomSlotProto;
|
||||||
|
|
||||||
|
@:view("type") var typeView:LabelView;
|
||||||
|
@:view("user") var userView:LabelView;
|
||||||
|
@:view("select") var selectButton:ButtonView;
|
||||||
|
|
||||||
|
@:provide static var network:NetworkManager;
|
||||||
|
@:provide static var configBundle:IConfigBundle;
|
||||||
|
|
||||||
|
private function set_data(value:RoomSlotProto):RoomSlotProto {
|
||||||
|
data = value;
|
||||||
|
typeView.text = '${value.slot.team} #${value.slot.index}';
|
||||||
|
userView.text = value.hasUser() ? value.user.name : "(NONE)";
|
||||||
|
userView.visible = value.hasUser();
|
||||||
|
selectButton.visible = !value.hasUser();
|
||||||
|
userView.skinId = (value.hasUser() && value.user.uuid == network.user.uuid) ? "text.box.active" : "text.box";
|
||||||
|
var config = configBundle.get(network.room.game.type);
|
||||||
|
var color = config.getColor([value.slot.team, value.slot.index]);
|
||||||
|
if (color != null) {
|
||||||
|
typeView.skin = Style.textBox(color);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function select():Void {
|
||||||
|
network.selectSlot(data.slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function factory():SlotItemView {
|
||||||
|
return new SlotItemView();
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/client/haxe/ru/m/tankz/view/network/SlotItemView.yaml
Normal file
25
src/client/haxe/ru/m/tankz/view/network/SlotItemView.yaml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
---
|
||||||
|
geometry.size.height: 48
|
||||||
|
geometry.padding: 2
|
||||||
|
layout.margin: 10
|
||||||
|
views:
|
||||||
|
- id: type
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
skinId: text.box
|
||||||
|
geometry.size.height: 100%
|
||||||
|
geometry.size.width: 150
|
||||||
|
- $type: haxework.view.HGroupView
|
||||||
|
layout.hAlign: center
|
||||||
|
layout.vAlign: middle
|
||||||
|
geometry.size.height: 100%
|
||||||
|
geometry.size.width: 300
|
||||||
|
views:
|
||||||
|
- id: user
|
||||||
|
$type: haxework.view.LabelView
|
||||||
|
geometry.size.stretch: true
|
||||||
|
skinId: text.box
|
||||||
|
- id: select
|
||||||
|
$type: haxework.view.ButtonView
|
||||||
|
skinId: button.simple
|
||||||
|
text: Select
|
||||||
|
+onPress: $this:select()
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package ru.m.tankz.view.network;
|
|
||||||
|
|
||||||
import haxework.view.HGroupView;
|
|
||||||
import haxework.view.LabelView;
|
|
||||||
import haxework.view.list.ListView;
|
|
||||||
import ru.m.tankz.proto.core.UserProto;
|
|
||||||
|
|
||||||
|
|
||||||
@:template class UserItemView extends HGroupView implements IListItemView<UserProto> {
|
|
||||||
|
|
||||||
public var item_index(default, default):Int;
|
|
||||||
public var data(default, set):UserProto;
|
|
||||||
|
|
||||||
@:view var label(default, null):LabelView;
|
|
||||||
|
|
||||||
private function set_data(value:UserProto):UserProto {
|
|
||||||
data = value;
|
|
||||||
label.text = '${data.uuid} -- ${data.name}';
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
72
src/client/haxe/ru/m/tankz/view/popup/CreateGamePopup.hx
Normal file
72
src/client/haxe/ru/m/tankz/view/popup/CreateGamePopup.hx
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package ru.m.tankz.view.popup;
|
||||||
|
|
||||||
|
import haxework.view.DataView;
|
||||||
|
import haxework.view.popup.PopupView;
|
||||||
|
import haxework.view.ToggleButtonView;
|
||||||
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
private typedef Result = {
|
||||||
|
var type:GameType;
|
||||||
|
var level:LevelId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:template class CreateGamePopup extends PopupView<Result> {
|
||||||
|
|
||||||
|
@:view("type") var typeView:DataView<GameType, ToggleButtonView>;
|
||||||
|
@:view("level") var levelView:DataView<LevelId, ToggleButtonView>;
|
||||||
|
|
||||||
|
@:provide var configBundle:IConfigBundle;
|
||||||
|
|
||||||
|
private var type:GameType;
|
||||||
|
private var level:LevelId;
|
||||||
|
|
||||||
|
override private function onShow():Void {
|
||||||
|
super.onShow();
|
||||||
|
onGameTypeSelect(0, "classic", typeView.dataViews[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function submit():Void {
|
||||||
|
close({type:type, level:level});
|
||||||
|
}
|
||||||
|
|
||||||
|
private function gameTypeViewFactory(index:Int, type:GameType):ToggleButtonView {
|
||||||
|
var result = new ToggleButtonView();
|
||||||
|
result.skinId = "button.simple";
|
||||||
|
result.text = type;
|
||||||
|
result.on = type == this.type;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onGameTypeSelect(index:Int, value:GameType, view:ToggleButtonView):Void {
|
||||||
|
this.type = value;
|
||||||
|
for (v in typeView.dataViews) {
|
||||||
|
v.on = v == view;
|
||||||
|
}
|
||||||
|
levelView.data = [for (i in 0...configBundle.get(type).game.levels) i];
|
||||||
|
onLevelSelect(0, 0, levelView.dataViews[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function levelViewFactory(index:Int, value:LevelId):ToggleButtonView {
|
||||||
|
var result = new ToggleButtonView();
|
||||||
|
result.skinId = "button.level";
|
||||||
|
result.text = Std.string(value);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onLevelSelect(index:Int, level:LevelId, view:ToggleButtonView):Void {
|
||||||
|
this.level = level;
|
||||||
|
for (v in levelView.dataViews) {
|
||||||
|
v.on = v == view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static var instance(get, null):CreateGamePopup;
|
||||||
|
|
||||||
|
private static function get_instance():CreateGamePopup {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new CreateGamePopup();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
68
src/client/haxe/ru/m/tankz/view/popup/CreateGamePopup.yaml
Normal file
68
src/client/haxe/ru/m/tankz/view/popup/CreateGamePopup.yaml
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
---
|
||||||
|
layout.hAlign: center
|
||||||
|
layout.vAlign: middle
|
||||||
|
view:
|
||||||
|
$type: haxework.view.VGroupView
|
||||||
|
layout.hAlign: center
|
||||||
|
geometry.size.width: 600
|
||||||
|
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: New game
|
||||||
|
- $type: haxework.view.ButtonView
|
||||||
|
skinId: window.close
|
||||||
|
+onPress: $code:reject('close')
|
||||||
|
- $type: haxework.view.VGroupView
|
||||||
|
geometry.size.width: 100%
|
||||||
|
geometry.padding: 20
|
||||||
|
layout.margin: 5
|
||||||
|
layout.hAlign: center
|
||||||
|
views:
|
||||||
|
- $type: haxework.view.LabelView
|
||||||
|
geometry.size.width: 100%
|
||||||
|
skinId: text
|
||||||
|
text: Type
|
||||||
|
- id: type
|
||||||
|
$type: haxework.view.DataView
|
||||||
|
layout:
|
||||||
|
$type: haxework.view.layout.HorizontalLayout
|
||||||
|
margin: 10
|
||||||
|
factory: $this:gameTypeViewFactory
|
||||||
|
+onItemSelect: $this:onGameTypeSelect
|
||||||
|
data:
|
||||||
|
- "classic"
|
||||||
|
- "dota"
|
||||||
|
- "death"
|
||||||
|
- $type: haxework.view.LabelView
|
||||||
|
geometry.size.width: 100%
|
||||||
|
skinId: text
|
||||||
|
text: Level
|
||||||
|
- id: level
|
||||||
|
$type: haxework.view.DataView
|
||||||
|
geometry.size.width: 100%
|
||||||
|
layout:
|
||||||
|
$type: haxework.view.layout.TailLayout
|
||||||
|
rowSize: 10
|
||||||
|
margin: 5
|
||||||
|
factory: $this:levelViewFactory
|
||||||
|
+onItemSelect: $this:onLevelSelect
|
||||||
|
geometry.padding: 10
|
||||||
|
- $type: haxework.view.HGroupView
|
||||||
|
layout.hAlign: center
|
||||||
|
layout.margin: 5
|
||||||
|
skinId: panel
|
||||||
|
views:
|
||||||
|
- $type: haxework.view.ButtonView
|
||||||
|
skinId: button.simple
|
||||||
|
text: Create
|
||||||
|
+onPress: $code:submit()
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.view.popup;
|
package ru.m.tankz.view.popup;
|
||||||
|
|
||||||
|
import haxework.view.ToggleButtonView;
|
||||||
import haxework.view.ButtonView;
|
import haxework.view.ButtonView;
|
||||||
import haxework.view.DataView;
|
import haxework.view.DataView;
|
||||||
import haxework.view.LabelView;
|
import haxework.view.LabelView;
|
||||||
@@ -7,19 +8,28 @@ import haxework.view.popup.PopupView;
|
|||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.game.GameProgress;
|
import ru.m.tankz.game.GameProgress;
|
||||||
|
|
||||||
@:template class LevelPopup extends PopupView<GamePreset> {
|
private typedef Result = {
|
||||||
|
var control:ControlPreset;
|
||||||
|
var preset:GamePreset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:template class LevelPopup extends PopupView<Result> {
|
||||||
|
|
||||||
private var level:LevelConfig;
|
private var level:LevelConfig;
|
||||||
private var progress:GameProgress;
|
private var progress:GameProgress;
|
||||||
|
|
||||||
@:view var name:LabelView;
|
@:view var name:LabelView;
|
||||||
@:view("presets") var presetsView:DataView<GamePreset, ButtonView>;
|
@:view("presets") var presetsView:DataView<GamePreset, ButtonView>;
|
||||||
|
@:view("controls") var controlsView:DataView<ControlPreset, ToggleButtonView>;
|
||||||
|
private var control:ControlPreset;
|
||||||
|
|
||||||
public function setData(level:LevelConfig, presets:Array<GamePreset>, progress:GameProgress):Void {
|
public function setData(level:LevelConfig, presets:Array<GamePreset>, controls:Array<ControlPreset>, progress:GameProgress):Void {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.progress = progress;
|
this.progress = progress;
|
||||||
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
||||||
presetsView.data = presets;
|
presetsView.data = presets;
|
||||||
|
control = controls[0];
|
||||||
|
controlsView.data = controls;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function presetViewFactory(index:Int, value:GamePreset):ButtonView {
|
private function presetViewFactory(index:Int, value:GamePreset):ButtonView {
|
||||||
@@ -32,7 +42,22 @@ import ru.m.tankz.game.GameProgress;
|
|||||||
|
|
||||||
private function onPresetSelect(value:GamePreset):Void {
|
private function onPresetSelect(value:GamePreset):Void {
|
||||||
if (progress.isPresetAvailable(level.id, value.id)) {
|
if (progress.isPresetAvailable(level.id, value.id)) {
|
||||||
close(value);
|
close({control: control, preset: value});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function controlViewFactory(index:Int, value:ControlPreset):ToggleButtonView {
|
||||||
|
var result = new ToggleButtonView();
|
||||||
|
result.skinId = 'button.simple.active';
|
||||||
|
result.on = control == value;
|
||||||
|
result.text = value.name;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onControlSelect(index:Int, value:ControlPreset, view:ToggleButtonView):Void {
|
||||||
|
control = value;
|
||||||
|
for (v in controlsView.dataViews) {
|
||||||
|
v.on = v == view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,15 @@ view:
|
|||||||
+onPress: $code:reject('close')
|
+onPress: $code:reject('close')
|
||||||
- $type: haxework.view.SpriteView
|
- $type: haxework.view.SpriteView
|
||||||
geometry.size.height: 100%
|
geometry.size.height: 100%
|
||||||
|
- id: controls
|
||||||
|
$type: haxework.view.DataView
|
||||||
|
factory: $this:controlViewFactory
|
||||||
|
+onItemSelect: $this:onControlSelect
|
||||||
|
layout:
|
||||||
|
$type: haxework.view.layout.HorizontalLayout
|
||||||
|
hAlign: center
|
||||||
|
margin: 5
|
||||||
|
skinId: panel
|
||||||
- id: presets
|
- id: presets
|
||||||
$type: haxework.view.DataView
|
$type: haxework.view.DataView
|
||||||
factory: $this:presetViewFactory
|
factory: $this:presetViewFactory
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
66
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.yaml
Normal file
66
src/client/haxe/ru/m/tankz/view/popup/LoginPopup.yaml
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
---
|
||||||
|
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
|
||||||
|
visible: false
|
||||||
|
- id: password
|
||||||
|
$type: haxework.view.InputView
|
||||||
|
textField.displayAsPassword: true
|
||||||
|
geometry.size.width: 100%
|
||||||
|
geometry.size.height: 28
|
||||||
|
skinId: text.box
|
||||||
|
visible: false
|
||||||
|
- 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(haxework.log.BaseLogger.LoggerUtil.printError(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 protohx.Message;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
|
|
||||||
|
|
||||||
class BaseConnection<O:Message, I:Message> implements IConnection<O, I> {
|
class BaseConnection<O:Message, I:Message> implements IConnection<O, I> {
|
||||||
public var handler(default, null):Signal<ConnectionEvent>;
|
public var handler(default, null):Signal<ConnectionEvent>;
|
||||||
public var sendHandler(default, null):Signal<O>;
|
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>>;
|
private var connectDeferred:Deferred<IConnection<O, I>>;
|
||||||
|
|
||||||
public function new(i:Class<I>) {
|
public function new(inputFactory:Class<I>) {
|
||||||
queue = new PacketQueue<I>(i);
|
queue = new PacketQueue<I>(inputFactory);
|
||||||
handler = new Signal<ConnectionEvent>();
|
handler = new Signal<ConnectionEvent>();
|
||||||
sendHandler = new Signal<O>();
|
sendHandler = new Signal<O>();
|
||||||
receiveHandler = new Signal<I>();
|
receiveHandler = new Signal<I>();
|
||||||
connectDeferred = new Deferred();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect():Promise<IConnection<O, I>> {
|
public function connect():Promise<IConnection<O, I>> {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import haxe.io.Bytes;
|
|||||||
import promhx.Promise;
|
import promhx.Promise;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
|
|
||||||
enum ConnectionEvent {
|
enum ConnectionEvent {
|
||||||
CONNECTED;
|
CONNECTED;
|
||||||
DISCONNECTED;
|
DISCONNECTED;
|
||||||
|
|||||||
@@ -1,21 +1,22 @@
|
|||||||
package ru.m.connect;
|
package ru.m.connect;
|
||||||
|
|
||||||
|
import Reflect;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import haxe.io.BytesBuffer;
|
import haxe.io.BytesBuffer;
|
||||||
import haxe.io.BytesInput;
|
import haxe.io.BytesInput;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
|
|
||||||
class PacketQueue<P:Message> {
|
class PacketQueue<P:Message> {
|
||||||
|
|
||||||
public var packetClass(default, null):Class<P>;
|
public var packetClass(default, null):Class<P>;
|
||||||
|
|
||||||
private var bytesBuff:Bytes;
|
private var buffer:BytesBuffer;
|
||||||
private var msgs:List<P>;
|
private var msgs:List<P>;
|
||||||
|
|
||||||
public function new(packetClass:Class<P>) {
|
public function new(packetClass:Class<P>) {
|
||||||
this.packetClass = packetClass;
|
this.packetClass = packetClass;
|
||||||
msgs = new List<P>();
|
msgs = new List<P>();
|
||||||
|
buffer = new BytesBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public inline function hasMsg():Bool {
|
public inline function hasMsg():Bool {
|
||||||
@@ -30,47 +31,41 @@ class PacketQueue<P:Message> {
|
|||||||
msgs.add(msg);
|
msgs.add(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addBytes(bytes:Bytes):Void {
|
private function readPackage():Null<P> {
|
||||||
if (bytes == null) {
|
var bytes = buffer.getBytes();
|
||||||
return;
|
var input = new BytesInput(bytes);
|
||||||
}
|
input.bigEndian = false;
|
||||||
if (bytesBuff == null) {
|
if (input.length > 1) {
|
||||||
bytesBuff = bytes;
|
var packetSize = input.readUInt16();
|
||||||
} else {
|
if (input.length >= packetSize + 2) {
|
||||||
var buffer = new BytesBuffer();
|
|
||||||
buffer.add(bytesBuff);
|
|
||||||
buffer.add(bytes);
|
|
||||||
bytesBuff = buffer.getBytes();
|
|
||||||
}
|
|
||||||
if (bytesBuff == null || bytesBuff.length < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var available = bytesBuff.length;
|
|
||||||
var bi = new BytesInput(bytesBuff);
|
|
||||||
bi.bigEndian = false;
|
|
||||||
while (available > 0) {
|
|
||||||
var packetSize = bi.readUInt16();
|
|
||||||
available -= 2;
|
|
||||||
if (packetSize <= available) {
|
|
||||||
var msgBytes = bi.read(packetSize);
|
|
||||||
var packet:P = Type.createInstance(packetClass, []);
|
var packet:P = Type.createInstance(packetClass, []);
|
||||||
packet.mergeFrom(msgBytes);
|
try {
|
||||||
addMsg(packet);
|
packet.mergeFrom(input.read(packetSize));
|
||||||
available -= packetSize;
|
buffer = new BytesBuffer();
|
||||||
} else {
|
buffer.add(input.read(input.length - (packetSize + 2)));
|
||||||
break;
|
return packet;
|
||||||
|
} catch (error:Dynamic) {
|
||||||
|
L.w("PacketQueue", "readPackage ", error);
|
||||||
|
buffer = new BytesBuffer();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (available == 0) {
|
|
||||||
bytesBuff = null;
|
|
||||||
} else if (available > 0) {
|
|
||||||
if (bytesBuff.length != available) {
|
|
||||||
var pos = bytesBuff.length - available;
|
|
||||||
bytesBuff = bytesBuff.sub(pos, available);
|
|
||||||
}
|
}
|
||||||
} else {
|
buffer = new BytesBuffer();
|
||||||
throw "Wrong available: " + available;
|
buffer.add(bytes);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addBytes(bytes:Bytes):Void {
|
||||||
|
buffer.add(bytes);
|
||||||
|
var packet = readPackage();
|
||||||
|
while (packet != null) {
|
||||||
|
msgs.add(packet);
|
||||||
|
packet = readPackage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function clean():Void {
|
||||||
|
buffer = new BytesBuffer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
76
src/common/haxe/ru/m/connect/desktop/DesktopConnection.hx
Normal file
76
src/common/haxe/ru/m/connect/desktop/DesktopConnection.hx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
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);
|
||||||
|
handler.emit(ConnectionEvent.CONNECTED);
|
||||||
|
}
|
||||||
|
} catch (error:Dynamic) {
|
||||||
|
handler.emit(ConnectionEvent.ERROR(error));
|
||||||
|
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 promhx.Promise;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
|
|
||||||
class FakeConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
class FakeConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||||
|
|
||||||
override public function connect():Promise<IConnection<O, I>> {
|
override public function connect():Promise<IConnection<O, I>> {
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
package ru.m.connect.flash;
|
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.ErrorEvent;
|
||||||
import flash.events.ProgressEvent;
|
|
||||||
import flash.events.Event;
|
import flash.events.Event;
|
||||||
import flash.events.SecurityErrorEvent;
|
|
||||||
import flash.events.IOErrorEvent;
|
import flash.events.IOErrorEvent;
|
||||||
|
import flash.events.ProgressEvent;
|
||||||
|
import flash.events.SecurityErrorEvent;
|
||||||
import flash.net.Socket;
|
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> {
|
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 port:Int;
|
||||||
private var socket:Socket;
|
private var socket:Socket;
|
||||||
|
|
||||||
public function new(host:String, port:Int, i:Class<I>) {
|
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||||
super(i);
|
super(inputFactory);
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
connected = false;
|
connected = false;
|
||||||
@@ -37,14 +36,15 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
|
|
||||||
override public function connect():Promise<IConnection<O, I>> {
|
override public function connect():Promise<IConnection<O, I>> {
|
||||||
socket.connect(host, port);
|
socket.connect(host, port);
|
||||||
|
connectDeferred = new Deferred();
|
||||||
return connectDeferred.promise();
|
return connectDeferred.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function disconnect():Void {
|
override public function disconnect():Void {
|
||||||
if (socket.connected) {
|
if (socket.connected) {
|
||||||
socket.close();
|
socket.close();
|
||||||
//connected = false;
|
connected = false;
|
||||||
//handler.emit(ConnectionEvent.DISCONNECTED);
|
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,13 +52,19 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
socket.close();
|
socket.close();
|
||||||
connected = false;
|
connected = false;
|
||||||
handler.emit(ConnectionEvent.ERROR(event));
|
handler.emit(ConnectionEvent.ERROR(event));
|
||||||
|
if (connectDeferred != null) {
|
||||||
connectDeferred.throwError(event);
|
connectDeferred.throwError(event);
|
||||||
|
connectDeferred = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onConnect(_):Void {
|
private function onConnect(_):Void {
|
||||||
connected = true;
|
connected = true;
|
||||||
handler.emit(ConnectionEvent.CONNECTED);
|
handler.emit(ConnectionEvent.CONNECTED);
|
||||||
|
if (connectDeferred != null) {
|
||||||
connectDeferred.resolve(this);
|
connectDeferred.resolve(this);
|
||||||
|
connectDeferred = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onClose(_):Void {
|
private function onClose(_):Void {
|
||||||
@@ -68,22 +74,16 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function onSocketData(_):Void {
|
private function onSocketData(_):Void {
|
||||||
try {
|
var data = new flash.utils.ByteArray();
|
||||||
var b = new flash.utils.ByteArray();
|
socket.readBytes(data);
|
||||||
socket.readBytes(b);
|
var bytes = Bytes.ofData(data);
|
||||||
var bs = Bytes.ofData(cast b);
|
pushData(bytes);
|
||||||
pushData(bs);
|
|
||||||
} catch (error:Dynamic) {
|
|
||||||
handler.emit(ConnectionEvent.ERROR(error));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _send(packet:O):Void {
|
private function _send(packet:O):Void {
|
||||||
var out = new BytesOutput();
|
var bytes = PacketUtil.toBytes(packet);
|
||||||
packet.writeTo(out);
|
|
||||||
var bytes = out.getBytes();
|
|
||||||
socket.writeShort(bytes.length);
|
socket.writeShort(bytes.length);
|
||||||
socket.writeBytes(cast bytes.getData());
|
socket.writeBytes(bytes.getData());
|
||||||
socket.flush();
|
socket.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
package ru.m.connect.js;
|
package ru.m.connect.js;
|
||||||
|
|
||||||
|
import haxe.io.Bytes;
|
||||||
import js.Browser;
|
import js.Browser;
|
||||||
|
import js.html.BinaryType;
|
||||||
import js.html.WebSocket;
|
import js.html.WebSocket;
|
||||||
|
import promhx.Deferred;
|
||||||
import promhx.Promise;
|
import promhx.Promise;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.Base64;
|
import ru.m.connect.IConnection;
|
||||||
import ru.m.connect.IConnection.ConnectionEvent;
|
|
||||||
|
|
||||||
|
|
||||||
class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||||
|
|
||||||
@@ -14,40 +15,41 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
private var port:Int;
|
private var port:Int;
|
||||||
private var socket:WebSocket;
|
private var socket:WebSocket;
|
||||||
|
|
||||||
public function new(host:String, port:Int, i:Class<I>) {
|
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||||
super(i);
|
super(inputFactory);
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildSocket(host:String, port:Int):WebSocket {
|
public static function isSecured():Bool {
|
||||||
var protocol = switch Browser.location.protocol {
|
return Browser.location.protocol == "https:";
|
||||||
case 'http:': 'ws:';
|
|
||||||
case 'https:': 'wss:';
|
|
||||||
case _: 'ws:';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function buildSocket(host:String, port:Int):WebSocket {
|
||||||
|
var protocol = isSecured() ? "wss:" : "ws:";
|
||||||
return new WebSocket('$protocol//$host:$port');
|
return new WebSocket('$protocol//$host:$port');
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function connect():Promise<IConnection<O, I>> {
|
override public function connect():Promise<IConnection<O, I>> {
|
||||||
var self = this;
|
var self = this;
|
||||||
var decodeBytes = Base64.decodeBase64;
|
|
||||||
socket = buildSocket(host, port);
|
socket = buildSocket(host, port);
|
||||||
|
socket.binaryType = BinaryType.ARRAYBUFFER;
|
||||||
socket.onopen = this.onConnect;
|
socket.onopen = this.onConnect;
|
||||||
socket.onclose = this.onClose;
|
socket.onclose = this.onClose;
|
||||||
socket.onerror = this.onError;
|
socket.onerror = this.onError;
|
||||||
socket.onmessage = this.onSocketData;
|
socket.onmessage = this.onSocketData;
|
||||||
|
connectDeferred = new Deferred();
|
||||||
return connectDeferred.promise();
|
return connectDeferred.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function disconnect():Void {
|
override public function disconnect():Void {
|
||||||
socket.close();
|
socket.close(1000);
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onError(event:Dynamic):Void {
|
private function onError(event:Dynamic):Void {
|
||||||
socket.close();
|
socket.close(1000);
|
||||||
connected = false;
|
connected = false;
|
||||||
handler.emit(ConnectionEvent.ERROR(event));
|
handler.emit(ConnectionEvent.ERROR(event));
|
||||||
}
|
}
|
||||||
@@ -65,17 +67,25 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function onSocketData(event:Dynamic):Void {
|
private function onSocketData(event:Dynamic):Void {
|
||||||
|
var packet:I = null;
|
||||||
try {
|
try {
|
||||||
var data:String = event.data;
|
var bytes = Bytes.ofData(event.data);
|
||||||
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
packet = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||||
receive(packet);
|
|
||||||
} catch (error:Dynamic) {
|
} catch (error:Dynamic) {
|
||||||
handler.emit(ConnectionEvent.ERROR(event));
|
handler.emit(ConnectionEvent.ERROR(error));
|
||||||
|
}
|
||||||
|
if (packet != null) {
|
||||||
|
receive(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function send(packet:O):Void {
|
override public function send(packet:O):Void {
|
||||||
|
if (connected) {
|
||||||
super.send(packet);
|
super.send(packet);
|
||||||
socket.send(WebSocketTools.packet2string(packet));
|
var bytes = PacketUtil.toBytes(packet);
|
||||||
|
socket.send(bytes.getData());
|
||||||
|
} else {
|
||||||
|
L.w("Connection", "closed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,30 @@
|
|||||||
package ru.m.connect.neko;
|
package ru.m.connect.neko;
|
||||||
|
|
||||||
import haxe.io.BytesOutput;
|
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
|
|
||||||
|
|
||||||
class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||||
|
|
||||||
public var socket(default, null):Socket;
|
public var socket(default, null):Socket;
|
||||||
|
|
||||||
|
private var sendQueue:Array<O>;
|
||||||
|
private var timer:Timer;
|
||||||
|
|
||||||
public function new(socket:Socket, i:Class<I>) {
|
public function new(socket:Socket, i:Class<I>) {
|
||||||
super(i);
|
super(i);
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
socket.setFastSend(true);
|
socket.setFastSend(true);
|
||||||
socket.output.bigEndian = false;
|
socket.output.bigEndian = false;
|
||||||
socket.input.bigEndian = false;
|
socket.input.bigEndian = false;
|
||||||
sendHandler.connect(_send);
|
sendHandler.connect(pushPacket);
|
||||||
|
sendQueue = [];
|
||||||
|
timer = new Timer(1);
|
||||||
|
timer.run = sendRun;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _send(packet:O):Void {
|
private function sendPacket(packet:O):Void {
|
||||||
try {
|
try {
|
||||||
var b = new BytesOutput();
|
var bytes = PacketUtil.toBytes(packet);
|
||||||
packet.writeTo(b);
|
|
||||||
var bytes = b.getBytes();
|
|
||||||
socket.output.writeUInt16(bytes.length);
|
socket.output.writeUInt16(bytes.length);
|
||||||
socket.output.write(bytes);
|
socket.output.write(bytes);
|
||||||
socket.output.flush();
|
socket.output.flush();
|
||||||
@@ -30,4 +32,25 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
L.e('Proto', 'Error send packet: ${packet}', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function sendRun():Void {
|
||||||
|
if (sendQueue.length > 0) {
|
||||||
|
for (packet in sendQueue) {
|
||||||
|
sendPacket(packet);
|
||||||
|
}
|
||||||
|
sendQueue = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function pushPacket(packet:O):Void {
|
||||||
|
sendQueue.push(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
override public function disconnect():Void {
|
||||||
|
if (timer != null) {
|
||||||
|
timer.stop();
|
||||||
|
timer = null;
|
||||||
|
}
|
||||||
|
super.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,12 @@ package ru.m.connect.neko;
|
|||||||
|
|
||||||
import haxe.crypto.BaseCode;
|
import haxe.crypto.BaseCode;
|
||||||
import haxe.crypto.Sha1;
|
import haxe.crypto.Sha1;
|
||||||
import protohx.Message;
|
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
|
import haxe.io.BytesBuffer;
|
||||||
|
import protohx.Message;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
|
|
||||||
|
class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||||
class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|
||||||
|
|
||||||
private var opened:Bool;
|
private var opened:Bool;
|
||||||
|
|
||||||
@@ -16,13 +16,9 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|||||||
opened = false;
|
opened = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
override private function _send(packet:O):Void {
|
override private function sendPacket(packet:O):Void {
|
||||||
try {
|
var data = PacketUtil.toBytes(packet);
|
||||||
var data = WebSocketTools.packet2string(packet);
|
|
||||||
writeData(data, socket);
|
writeData(data, socket);
|
||||||
} catch (error:Dynamic) {
|
|
||||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function pushData(bytes:Bytes):Void {
|
override public function pushData(bytes:Bytes):Void {
|
||||||
@@ -37,13 +33,12 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|||||||
} else {
|
} else {
|
||||||
var data = parseData(bytes);
|
var data = parseData(bytes);
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||||
receive(packet);
|
receive(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function encodeBase64(content:String):String {
|
private function encodeBase64(content:String):String {
|
||||||
var suffix = switch (content.length % 3)
|
var suffix = switch (content.length % 3)
|
||||||
{
|
{
|
||||||
@@ -74,8 +69,8 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|||||||
socket.output.writeString(s);
|
socket.output.writeString(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function writeData(data:String, socket:sys.net.Socket, isServer = true):Void {
|
private function writeData(data:Bytes, socket:sys.net.Socket, isServer = true):Void {
|
||||||
socket.output.writeByte(0x81);
|
socket.output.writeByte(130);
|
||||||
|
|
||||||
var len = 0;
|
var len = 0;
|
||||||
if (data.length < 126) len = data.length;
|
if (data.length < 126) len = data.length;
|
||||||
@@ -98,7 +93,7 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isServer) {
|
if (isServer) {
|
||||||
socket.output.writeString(data);
|
socket.output.writeBytes(data, 0, data.length);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var mask = [ Std.random(256), Std.random(256), Std.random(256), Std.random(256) ];
|
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[1]);
|
||||||
socket.output.writeByte(mask[2]);
|
socket.output.writeByte(mask[2]);
|
||||||
socket.output.writeByte(mask[3]);
|
socket.output.writeByte(mask[3]);
|
||||||
var maskedData = new StringBuf();
|
var maskedData = new BytesBuffer();
|
||||||
for (i in 0...data.length) {
|
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 p = 0;
|
||||||
var opcode = bytes.get(p++);
|
var opcode = bytes.get(p++);
|
||||||
|
|
||||||
if (opcode == 0x00) {
|
if (opcode == 0x00) {
|
||||||
var s = "";
|
var data = new BytesBuffer();
|
||||||
var b:Int;
|
var b:Int;
|
||||||
while ((b = bytes.get(p++)) != 0xFF) {
|
while ((b = bytes.get(p++)) != 0xFF) {
|
||||||
s += String.fromCharCode(b);
|
data.addByte(b);
|
||||||
}
|
}
|
||||||
return s;
|
return data.getBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0x81 = fin & text
|
// 130 = binary data
|
||||||
if (opcode == 0x81) {
|
if (opcode == 130) {
|
||||||
var len = bytes.get(p++);
|
var len = bytes.get(p++);
|
||||||
|
|
||||||
// mask
|
// mask
|
||||||
@@ -159,13 +154,13 @@ class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
|||||||
|
|
||||||
//Lib.println("mask = " + mask);
|
//Lib.println("mask = " + mask);
|
||||||
|
|
||||||
var data = new StringBuf();
|
var data = new BytesBuffer();
|
||||||
for (i in 0...len) {
|
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());
|
//Lib.println("readed = " + data.toString());
|
||||||
return data.toString();
|
return data.getBytes();
|
||||||
} else {
|
} else {
|
||||||
throw "Expected masked data.";
|
throw "Expected masked data.";
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
@@ -12,7 +12,7 @@ class BotControl extends Control {
|
|||||||
private var tank(get, null):Tank;
|
private var tank(get, null):Tank;
|
||||||
|
|
||||||
private inline function get_tank():Tank {
|
private inline function get_tank():Tank {
|
||||||
return handler == null ? null : handler.engine.getEntity(tankId);
|
return handler == null || tankId == -1 ? null : engine.getEntity(tankId);
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function stop():Void {
|
override public function stop():Void {
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import ru.m.geom.Direction;
|
|||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
class BotHelper {
|
class BotHelper {
|
||||||
|
|
||||||
public static function findEagle(team:TeamId, handler:IGame):Null<Eagle> {
|
public static function findEagle(team:TeamId, engine:IEngine):Null<Eagle> {
|
||||||
for (entity in handler.engine.entities) {
|
for (entity in engine.entities) {
|
||||||
switch (EntityTypeResolver.of(entity)) {
|
switch (EntityTypeResolver.of(entity)) {
|
||||||
case EntityType.EAGLE(eagle):
|
case EntityType.EAGLE(eagle):
|
||||||
if (eagle.team != team) {
|
if (eagle.team != team) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
@@ -56,7 +56,7 @@ class HardBotControl extends BotControl {
|
|||||||
}
|
}
|
||||||
var enemy:Tank = null;
|
var enemy:Tank = null;
|
||||||
var distance:Float = Math.POSITIVE_INFINITY;
|
var distance:Float = Math.POSITIVE_INFINITY;
|
||||||
for (entity in handler.engine.entities.iterator()) {
|
for (entity in engine.entities.iterator()) {
|
||||||
switch EntityTypeResolver.of(entity) {
|
switch EntityTypeResolver.of(entity) {
|
||||||
case TANK(t):
|
case TANK(t):
|
||||||
if (t.playerId.team != tank.playerId.team) {
|
if (t.playerId.team != tank.playerId.team) {
|
||||||
@@ -82,7 +82,7 @@ class HardBotControl extends BotControl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function calcTurn():Void {
|
private function calcTurn():Void {
|
||||||
var eagle:Eagle = BotHelper.findEagle(playerId.team, handler);
|
var eagle:Eagle = BotHelper.findEagle(playerId.team, engine);
|
||||||
if (eagle != null && Math.random() > 0.5) {
|
if (eagle != null && Math.random() > 0.5) {
|
||||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.Timer;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
@@ -45,7 +45,7 @@ class StupidBotControl extends BotControl {
|
|||||||
|
|
||||||
private function calcTurn():Void {
|
private function calcTurn():Void {
|
||||||
if (handler == null || tank == null) return;
|
if (handler == null || tank == null) return;
|
||||||
var eagle:Eagle = BotHelper.findEagle(playerId.team, handler);
|
var eagle:Eagle = BotHelper.findEagle(playerId.team, engine);
|
||||||
if (eagle != null && Math.random() > 0.5) {
|
if (eagle != null && Math.random() > 0.5) {
|
||||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,17 +3,6 @@ package ru.m.tankz.bundle;
|
|||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
|
||||||
typedef ConfigSource = {
|
|
||||||
var game:GameConfig;
|
|
||||||
var map: MapConfig;
|
|
||||||
var bricks: Array<BrickConfig>;
|
|
||||||
var presets: Array<GamePreset>;
|
|
||||||
var points: Array<SpawnPoint>;
|
|
||||||
var tanks: Array<TankConfig>;
|
|
||||||
var bonuses: Array<BonusConfig>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IConfigBundle {
|
interface IConfigBundle {
|
||||||
public function get(type:GameType):Config;
|
public function get(type:GameType):Config;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package ru.m.tankz.config;
|
package ru.m.tankz.config;
|
||||||
|
|
||||||
import haxework.color.Color;
|
import haxework.color.Color;
|
||||||
|
import ru.m.tankz.control.Controller;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
typedef CompleteRule = {
|
typedef CompleteRule = {
|
||||||
@@ -50,8 +51,8 @@ typedef TankConfig = {
|
|||||||
var speed:Float;
|
var speed:Float;
|
||||||
var bullet:BulletConfig;
|
var bullet:BulletConfig;
|
||||||
var bullets:Int;
|
var bullets:Int;
|
||||||
var hits:Int;
|
|
||||||
var skin:String;
|
var skin:String;
|
||||||
|
@:optinal var hits:Int;
|
||||||
@:optinal var upgrade:TankType;
|
@:optinal var upgrade:TankType;
|
||||||
@:optinal var downgrade:TankType;
|
@:optinal var downgrade:TankType;
|
||||||
@:optinal var score:Int;
|
@:optinal var score:Int;
|
||||||
@@ -74,7 +75,7 @@ typedef PlayerConfig = {
|
|||||||
@:optional var bonus:Float;
|
@:optional var bonus:Float;
|
||||||
@:optional var protect:Float;
|
@:optional var protect:Float;
|
||||||
@:optional var life:Int;
|
@:optional var life:Int;
|
||||||
@:optional var color:Color;
|
@:optional var color:Null<Int>;
|
||||||
@:optional var control:String;
|
@:optional var control:String;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +89,7 @@ typedef TeamConfig = {
|
|||||||
@:optional var eagle:EagleConfig;
|
@:optional var eagle:EagleConfig;
|
||||||
@:optional var life:Int;
|
@:optional var life:Int;
|
||||||
@:optional var spawnInterval:Int;
|
@:optional var spawnInterval:Int;
|
||||||
@:optional var color:Color;
|
@:optional var color:Null<Int>;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef GamePreset = {
|
typedef GamePreset = {
|
||||||
@@ -104,6 +105,29 @@ typedef LevelConfig = {
|
|||||||
@:optional var points:Array<SpawnPoint>;
|
@:optional var points:Array<SpawnPoint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef PlayerControl = {
|
||||||
|
var playerId:PlayerId;
|
||||||
|
var control:String;
|
||||||
|
@:optional var color:Null<Int>;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef ControlPreset = {
|
||||||
|
var id:Int;
|
||||||
|
var name:String;
|
||||||
|
var values:Array<PlayerControl>;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ConfigSource = {
|
||||||
|
var game:GameConfig;
|
||||||
|
var map: MapConfig;
|
||||||
|
var bricks: Array<BrickConfig>;
|
||||||
|
var presets: Array<GamePreset>;
|
||||||
|
var controls: Array<ControlPreset>;
|
||||||
|
var points: Array<SpawnPoint>;
|
||||||
|
var tanks: Array<TankConfig>;
|
||||||
|
var bonuses: Array<BonusConfig>;
|
||||||
|
}
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public var type(default, null):String;
|
public var type(default, null):String;
|
||||||
public var game(default, null):GameConfig;
|
public var game(default, null):GameConfig;
|
||||||
@@ -111,6 +135,7 @@ class Config {
|
|||||||
public var bricks(default, null):Array<BrickConfig>;
|
public var bricks(default, null):Array<BrickConfig>;
|
||||||
public var tanks(default, null):Array<TankConfig>;
|
public var tanks(default, null):Array<TankConfig>;
|
||||||
public var presets(default, null):Array<GamePreset>;
|
public var presets(default, null):Array<GamePreset>;
|
||||||
|
public var controls(default, null):Array<ControlPreset>;
|
||||||
public var points(default, null):Array<SpawnPoint>;
|
public var points(default, null):Array<SpawnPoint>;
|
||||||
public var bonuses(default, null):Array<BonusConfig>;
|
public var bonuses(default, null):Array<BonusConfig>;
|
||||||
|
|
||||||
@@ -122,12 +147,17 @@ class Config {
|
|||||||
private var teamsMap:Map<TeamId, TeamConfig>;
|
private var teamsMap:Map<TeamId, TeamConfig>;
|
||||||
private var playersMap:Map<String, PlayerConfig>;
|
private var playersMap:Map<String, PlayerConfig>;
|
||||||
|
|
||||||
|
public static function fromSource(type:GameType, source:ConfigSource):Config {
|
||||||
|
return new Config(type, source.game, source.map, source.bricks, source.presets, source.controls, source.points, source.tanks, source.bonuses);
|
||||||
|
}
|
||||||
|
|
||||||
public function new(
|
public function new(
|
||||||
type:String,
|
type:String,
|
||||||
game:GameConfig,
|
game:GameConfig,
|
||||||
map:MapConfig,
|
map:MapConfig,
|
||||||
bricks:Array<BrickConfig>,
|
bricks:Array<BrickConfig>,
|
||||||
presets:Array<GamePreset>,
|
presets:Array<GamePreset>,
|
||||||
|
controls:Array<ControlPreset>,
|
||||||
points:Array<SpawnPoint>,
|
points:Array<SpawnPoint>,
|
||||||
tanks:Array<TankConfig>,
|
tanks:Array<TankConfig>,
|
||||||
bonuses:Array<BonusConfig>
|
bonuses:Array<BonusConfig>
|
||||||
@@ -137,6 +167,7 @@ class Config {
|
|||||||
this.map = map;
|
this.map = map;
|
||||||
this.bricks = bricks;
|
this.bricks = bricks;
|
||||||
this.presets = presets;
|
this.presets = presets;
|
||||||
|
this.controls = controls;
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.tanks = tanks;
|
this.tanks = tanks;
|
||||||
this.bonuses = bonuses;
|
this.bonuses = bonuses;
|
||||||
@@ -202,16 +233,16 @@ class Config {
|
|||||||
return playersMap.get(playerId);
|
return playersMap.get(playerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getColor(playerId:PlayerId):Color {
|
public function getColor(playerId:PlayerId):Null<Color> {
|
||||||
var player = getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
if (player != null && !player.color.zero) {
|
if (player != null && player.color != null) {
|
||||||
return player.color;
|
return player.color;
|
||||||
}
|
}
|
||||||
var team = getTeam(playerId.team);
|
var team = getTeam(playerId.team);
|
||||||
if (team != null) {
|
if (team != null) {
|
||||||
return team.color;
|
return team.color;
|
||||||
}
|
}
|
||||||
return -1;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPoint(teamId:TeamId, type:String, index:Int = -1):SpawnPoint {
|
public function getPoint(teamId:TeamId, type:String, index:Int = -1):SpawnPoint {
|
||||||
@@ -222,4 +253,12 @@ class Config {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isHuman(playerId:PlayerId) {
|
||||||
|
var controller:Controller = AController.fromString(getPlayer(playerId).control);
|
||||||
|
return switch controller {
|
||||||
|
case HUMAN(_): true;
|
||||||
|
case _: false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/common/haxe/ru/m/tankz/control/BaseControlFactory.hx
Normal file
30
src/common/haxe/ru/m/tankz/control/BaseControlFactory.hx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.bot.HardBotControl;
|
||||||
|
import ru.m.tankz.bot.StupidBotControl;
|
||||||
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
class BaseControlFactory implements IControlFactory {
|
||||||
|
|
||||||
|
public function new() {}
|
||||||
|
|
||||||
|
public function build(id:PlayerId, controller:Controller):Control {
|
||||||
|
return switch controller {
|
||||||
|
case Controller.HUMAN(index): buildHuman(id, index);
|
||||||
|
case Controller.BOT(type): buildBot(id, type);
|
||||||
|
case Controller.NONE: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildHuman(id:PlayerId, index:Int):Control {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildBot(id:PlayerId, type:String):Control {
|
||||||
|
return switch type {
|
||||||
|
case StupidBotControl.BOT_TYPE: new StupidBotControl(id);
|
||||||
|
case HardBotControl.BOT_TYPE: new HardBotControl(id);
|
||||||
|
case _: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
package ru.m.tankz.control;
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
import ru.m.tankz.game.GameEvent;
|
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
|
import ru.m.tankz.engine.IEngine;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.IGame;
|
import ru.m.tankz.game.IGame;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
@@ -17,17 +18,20 @@ class Control {
|
|||||||
public var playerId(default, null):PlayerId;
|
public var playerId(default, null):PlayerId;
|
||||||
public var tankId(default, default):Int;
|
public var tankId(default, default):Int;
|
||||||
private var handler:IGame;
|
private var handler:IGame;
|
||||||
|
private var engine:IEngine;
|
||||||
|
|
||||||
public function new(playerId:PlayerId) {
|
public function new(playerId:PlayerId) {
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
|
this.tankId = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bind(handler:IGame):Void {
|
public function bind(handler:IGame, engine:IEngine):Void {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.engine = engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function action(action:TankAction):Void {
|
public function action(action:TankAction):Void {
|
||||||
if (tankId != 0 && handler != null) {
|
if (tankId > -1 && handler != null) {
|
||||||
handler.gameEventSignal.emit(GameEvent.ACTION(tankId, action));
|
handler.gameEventSignal.emit(GameEvent.ACTION(tankId, action));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,5 +45,7 @@ class Control {
|
|||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
stop();
|
stop();
|
||||||
handler = null;
|
handler = null;
|
||||||
|
engine = null;
|
||||||
|
tankId = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package ru.m.tankz.control;
|
|||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
|
||||||
interface IControlFactory {
|
interface IControlFactory {
|
||||||
public function build(id:PlayerId, controller:Controller):Control;
|
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.control.Control;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
|
||||||
class NoneControlFactory implements IControlFactory {
|
class NoneControlFactory implements IControlFactory {
|
||||||
|
|
||||||
public function new() {}
|
public function new() {}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class Eagle extends Entity {
|
|||||||
public var config(default, null):EagleConfig;
|
public var config(default, null):EagleConfig;
|
||||||
public var color(default, default):Color;
|
public var color(default, default):Color;
|
||||||
public var death(default, default):Bool;
|
public var death(default, default):Bool;
|
||||||
public var protect(default, null):Modificator;
|
public var protect(default, default):Bool;
|
||||||
|
|
||||||
public var score(get, null):Int;
|
public var score(get, null):Int;
|
||||||
|
|
||||||
@@ -20,15 +20,10 @@ class Eagle extends Entity {
|
|||||||
this.team = team;
|
this.team = team;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.death = false;
|
this.death = false;
|
||||||
this.protect = new Modificator(id);
|
this.protect = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function get_score():Int {
|
private inline function get_score():Int {
|
||||||
return config != null ? config.score : 0;
|
return config != null ? config.score : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function dispose() {
|
|
||||||
super.dispose();
|
|
||||||
protect.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,4 @@ class Entity {
|
|||||||
public function toString():String {
|
public function toString():String {
|
||||||
return '$type($id)';
|
return '$type($id)';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dispose() {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
package ru.m.tankz.core;
|
|
||||||
|
|
||||||
import haxe.Timer;
|
|
||||||
import haxework.signal.Signal;
|
|
||||||
|
|
||||||
class Modificator extends Signal2<Int, Bool> {
|
|
||||||
|
|
||||||
public var id(default, default):Int;
|
|
||||||
public var active(default, default):Bool;
|
|
||||||
private var timer:Timer;
|
|
||||||
|
|
||||||
public function new(id) {
|
|
||||||
super();
|
|
||||||
this.id = id;
|
|
||||||
active = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
override public function connect(receiver:Int->Bool->Void):Void {
|
|
||||||
super.connect(receiver);
|
|
||||||
receiver(id, active);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function on(seconds:Float):Void {
|
|
||||||
off();
|
|
||||||
active = true;
|
|
||||||
timer = Timer.delay(off, Std.int(seconds * 1000));
|
|
||||||
emit(id, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function off():Void {
|
|
||||||
if (timer != null) {
|
|
||||||
timer.stop();
|
|
||||||
timer = null;
|
|
||||||
emit(id, false);
|
|
||||||
}
|
|
||||||
active = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,13 +12,13 @@ class Tank extends MobileEntity {
|
|||||||
public var color(default, default):Color;
|
public var color(default, default):Color;
|
||||||
public var hits(default, default):Int;
|
public var hits(default, default):Int;
|
||||||
public var bonus(default, default):Bool;
|
public var bonus(default, default):Bool;
|
||||||
public var protect(default, null):Modificator;
|
public var protect(default, default):Bool;
|
||||||
public var freezing(default, null):Modificator;
|
public var freezing(default, default):Bool;
|
||||||
|
|
||||||
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) {
|
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) {
|
||||||
super(id, rect, config.speed, Direction.RIGHT);
|
super(id, rect, config.speed, Direction.RIGHT);
|
||||||
this.protect = new Modificator(id);
|
this.protect = false;
|
||||||
this.freezing = new Modificator(id);
|
this.freezing = false;
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.layer = 1;
|
this.layer = 1;
|
||||||
@@ -35,14 +35,8 @@ class Tank extends MobileEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override public function move(direction:Direction):Void {
|
override public function move(direction:Direction):Void {
|
||||||
if (!freezing.active) {
|
if (!freezing) {
|
||||||
super.move(direction);
|
super.move(direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function dispose() {
|
|
||||||
super.dispose();
|
|
||||||
protect.dispose();
|
|
||||||
freezing.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import ru.m.geom.Direction;
|
|||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.core.Bullet;
|
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.MobileEntity;
|
import ru.m.tankz.core.MobileEntity;
|
||||||
@@ -17,18 +16,17 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
|
|
||||||
public var config(default, default):Config;
|
public var config(default, default):Config;
|
||||||
public var map(default, null):LevelMap;
|
public var map(default, null):LevelMap;
|
||||||
|
public var ticker(default, null):ITicker;
|
||||||
public var allEntities(default, null):Map<Int, Entity>;
|
|
||||||
public var entities(default, null):Map<Int, Entity>;
|
public var entities(default, null):Map<Int, Entity>;
|
||||||
|
|
||||||
private var time:Float;
|
public var allEntities(default, null):Map<Int, Entity>;
|
||||||
|
|
||||||
public function new(config:Config) {
|
public function new(config:Config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
map = new LevelMap(config.map);
|
map = new LevelMap(config.map);
|
||||||
allEntities = new Map();
|
allEntities = new Map();
|
||||||
entities = new Map();
|
entities = new Map();
|
||||||
time = Date.now().getTime();
|
ticker = new Ticker();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEntity<T:Entity>(entityId:Int):T {
|
public function getEntity<T:Entity>(entityId:Int):T {
|
||||||
@@ -68,9 +66,7 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function update():Void {
|
public function update():Void {
|
||||||
var newTime:Float = Date.now().getTime();
|
var d:Float = ticker.tick();
|
||||||
var d:Float = newTime - time;
|
|
||||||
time = newTime;
|
|
||||||
|
|
||||||
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
||||||
var entityType:EntityType = EntityTypeResolver.of(ent);
|
var entityType:EntityType = EntityTypeResolver.of(ent);
|
||||||
@@ -140,9 +136,6 @@ import ru.m.tankz.map.LevelMap;
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
for (entity in allEntities) {
|
|
||||||
entity.dispose();
|
|
||||||
}
|
|
||||||
allEntities = new Map();
|
allEntities = new Map();
|
||||||
entities = new Map();
|
entities = new Map();
|
||||||
//map = null;
|
//map = null;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ interface IEngine {
|
|||||||
public var entities(default, null):Map<Int, Entity>;
|
public var entities(default, null):Map<Int, Entity>;
|
||||||
public var config(default, default):Config;
|
public var config(default, default):Config;
|
||||||
public var map(default, null):LevelMap;
|
public var map(default, null):LevelMap;
|
||||||
|
public var ticker(default, null):ITicker;
|
||||||
|
|
||||||
public var spawnSignal(default, null):Signal1<EntityType>;
|
public var spawnSignal(default, null):Signal1<EntityType>;
|
||||||
public var collisionSignal(default, null):Signal2<EntityType, EntityType>;
|
public var collisionSignal(default, null):Signal2<EntityType, EntityType>;
|
||||||
|
|||||||
11
src/common/haxe/ru/m/tankz/engine/ITicker.hx
Normal file
11
src/common/haxe/ru/m/tankz/engine/ITicker.hx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package ru.m.tankz.engine;
|
||||||
|
|
||||||
|
interface ITicker {
|
||||||
|
public var time(get, null):Int;
|
||||||
|
public var running(default, null):Bool;
|
||||||
|
|
||||||
|
public function start():Void;
|
||||||
|
public function stop():Void;
|
||||||
|
public function tick():Int;
|
||||||
|
public function emit(f:Void->Void, delay:Int):Void;
|
||||||
|
}
|
||||||
74
src/common/haxe/ru/m/tankz/engine/Ticker.hx
Normal file
74
src/common/haxe/ru/m/tankz/engine/Ticker.hx
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package ru.m.tankz.engine;
|
||||||
|
|
||||||
|
typedef Action = {
|
||||||
|
var time:Int;
|
||||||
|
var action:Void->Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ticker implements ITicker {
|
||||||
|
public var time(get, null):Int;
|
||||||
|
public var running(default, null):Bool;
|
||||||
|
|
||||||
|
private var passed:Int;
|
||||||
|
private var last_tick:Int;
|
||||||
|
private var actions:Array<Action>;
|
||||||
|
|
||||||
|
private static var TIME = Timer.stamp();
|
||||||
|
|
||||||
|
private inline static function now():Int {
|
||||||
|
return Std.int((Timer.stamp() - TIME) * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
passed = 0;
|
||||||
|
last_tick = 0;
|
||||||
|
running = false;
|
||||||
|
actions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_time():Int {
|
||||||
|
return passed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start():Void {
|
||||||
|
last_tick = now();
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stop():Void {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tick():Int {
|
||||||
|
if (!running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var now = now();
|
||||||
|
var result = now - last_tick;
|
||||||
|
last_tick = now;
|
||||||
|
passed += result;
|
||||||
|
if (actions.length > 0) {
|
||||||
|
runActions();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function runActions():Void {
|
||||||
|
var it = actions.iterator();
|
||||||
|
var action = it.next();
|
||||||
|
var i = 0;
|
||||||
|
while (action != null && time >= action.time) {
|
||||||
|
action.action();
|
||||||
|
i++;
|
||||||
|
action = it.next();
|
||||||
|
}
|
||||||
|
if (i > 0) {
|
||||||
|
actions = actions.slice(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function emit(action:Void->Void, delay:Int):Void {
|
||||||
|
actions.push({action:action, time:time+delay});
|
||||||
|
actions.sort(function(a, b) return a.time - b.time);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import haxework.color.Color;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
@@ -19,12 +20,14 @@ typedef EntityPoint = {
|
|||||||
class EntityBuilder {
|
class EntityBuilder {
|
||||||
|
|
||||||
private var config:Config;
|
private var config:Config;
|
||||||
|
private var entityId:Int;
|
||||||
|
|
||||||
public function new(config:Config) {
|
public function new(config:Config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.entityId = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
||||||
return new Rectangle(
|
return new Rectangle(
|
||||||
(point.x + 1) * config.map.cellWidth - width / 2,
|
(point.x + 1) * config.map.cellWidth - width / 2,
|
||||||
(point.y + 1) * config.map.cellHeight - height / 2,
|
(point.y + 1) * config.map.cellHeight - height / 2,
|
||||||
@@ -34,35 +37,34 @@ 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 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));
|
eagle.color = config.getColor(new PlayerId(teamId, -1));
|
||||||
return eagle;
|
return eagle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildTank(id:Int, point:EntityPoint, playerId:PlayerId, type:TankType):Tank {
|
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, color:Null<Color>, bonusOff:Bool = false):Tank {
|
||||||
var playerConfig = config.getPlayer(playerId);
|
var playerConfig = config.getPlayer(playerId);
|
||||||
var tankConfig = config.getTank(type);
|
var tankConfig = config.getTank(type);
|
||||||
var tank = new Tank(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.color = color == null || color.zero ? config.getColor(playerId) : color;
|
||||||
|
if (!bonusOff) {
|
||||||
tank.bonus = Math.random() < playerConfig.bonus;
|
tank.bonus = Math.random() < playerConfig.bonus;
|
||||||
if (playerConfig.protect > 0) {
|
|
||||||
tank.protect.on(playerConfig.protect);
|
|
||||||
}
|
}
|
||||||
return tank;
|
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 tankConfig = config.getTank(type);
|
||||||
var bulletConfig = tankConfig.bullet;
|
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;
|
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 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;
|
return bonus;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
73
src/common/haxe/ru/m/tankz/game/EventUtil.hx
Normal file
73
src/common/haxe/ru/m/tankz/game/EventUtil.hx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import ru.m.tankz.core.Bonus;
|
||||||
|
import ru.m.tankz.core.Bullet;
|
||||||
|
import ru.m.tankz.core.Eagle;
|
||||||
|
import ru.m.tankz.core.Entity;
|
||||||
|
import ru.m.tankz.core.EntityType;
|
||||||
|
import ru.m.tankz.core.Tank;
|
||||||
|
import ru.m.tankz.game.GameEvent;
|
||||||
|
import ru.m.tankz.map.Brick;
|
||||||
|
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 SPAWN(BRICK(bricks));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildCellsDestroyed(map:LevelMap):Array<GameEvent> {
|
||||||
|
var result = [];
|
||||||
|
for (brick in map.bricks) {
|
||||||
|
for (cell in brick.cells) {
|
||||||
|
if (cell.destroyed) {
|
||||||
|
result.push(DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, {tankId: -1})));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildEagleSpawn(eagle:Eagle):GameEvent {
|
||||||
|
return SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildTankSpawn(tank:Tank):GameEvent {
|
||||||
|
return SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
||||||
|
type:tank.config.type,
|
||||||
|
hits:tank.hits,
|
||||||
|
bonus:tank.bonus,
|
||||||
|
color:tank.color,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildBonusSpawn(bonus:Bonus):GameEvent {
|
||||||
|
return SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildBulletSpawn(bullet:Bullet):GameEvent {
|
||||||
|
return SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildMove(entity:Entity):GameEvent {
|
||||||
|
return switch EntityTypeResolver.of(entity) {
|
||||||
|
case EAGLE(eagle):
|
||||||
|
MOVE(EAGLE(entity.id, entity.rect.position));
|
||||||
|
case TANK(tank):
|
||||||
|
MOVE(TANK(entity.id, entity.rect.position));
|
||||||
|
case BULLET(bullet):
|
||||||
|
MOVE(BULLET(entity.id, entity.rect.position));
|
||||||
|
case _:
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,11 @@ import ru.m.geom.Point;
|
|||||||
import ru.m.geom.Position;
|
import ru.m.geom.Position;
|
||||||
import ru.m.tankz.bundle.IConfigBundle;
|
import ru.m.tankz.bundle.IConfigBundle;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
|
import ru.m.tankz.control.Controller;
|
||||||
|
import ru.m.tankz.control.IControlFactory;
|
||||||
|
import ru.m.tankz.control.NoneControlFactory;
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.engine.Engine;
|
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.GameState;
|
import ru.m.tankz.game.GameState;
|
||||||
@@ -20,36 +22,34 @@ import ru.m.tankz.Type;
|
|||||||
public var type(default, null):GameType;
|
public var type(default, null):GameType;
|
||||||
public var teams(default, null):Map<TeamId, Team>;
|
public var teams(default, null):Map<TeamId, Team>;
|
||||||
public var config(default, null):Config;
|
public var config(default, null):Config;
|
||||||
public var engine(default, null):IEngine;
|
|
||||||
public var winner(default, null):Null<TeamId>;
|
public var winner(default, null):Null<TeamId>;
|
||||||
|
|
||||||
public var state(default, null):GameState;
|
public var state(default, null):GameState;
|
||||||
|
public var engine(default, null):IEngine;
|
||||||
private var builder:EntityBuilder;
|
public var controlFactory(default, null):IControlFactory;
|
||||||
|
public var pause(default, set):Bool;
|
||||||
|
|
||||||
@:provide var configBundle:IConfigBundle;
|
@:provide var configBundle:IConfigBundle;
|
||||||
|
|
||||||
public function new(state:GameState) {
|
public function new(state:GameState) {
|
||||||
this.type = state.type;
|
this.type = state.type;
|
||||||
|
this.state = state;
|
||||||
this.teams = new Map();
|
this.teams = new Map();
|
||||||
this.config = configBundle.get(type);
|
this.config = configBundle.get(type);
|
||||||
this.engine = new Engine(config);
|
this.controlFactory = new NoneControlFactory();
|
||||||
this.builder = new EntityBuilder(config);
|
this.pause = false;
|
||||||
connect(this);
|
connect(this);
|
||||||
prepare(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepare(state:GameState):Void {
|
private function set_pause(value:Bool):Bool {
|
||||||
var level:LevelConfig = state.level;
|
if (pause != value) {
|
||||||
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
|
pause = value;
|
||||||
engine.map.setData(level.data);
|
changePause(pause);
|
||||||
for (teamConfig in state.preset.teams) {
|
|
||||||
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
|
||||||
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
|
||||||
teams[team.id] = team;
|
|
||||||
}
|
}
|
||||||
|
return pause;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function changePause(value:Bool):Void {}
|
||||||
|
|
||||||
private function applyPosition(entity:Entity, position:Position):Void {
|
private function applyPosition(entity:Entity, position:Position):Void {
|
||||||
entity.rect.center = new Point(position.x, position.y);
|
entity.rect.center = new Point(position.x, position.y);
|
||||||
if (position.direction != null) {
|
if (position.direction != null) {
|
||||||
@@ -67,15 +67,15 @@ import ru.m.tankz.Type;
|
|||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.START(state):
|
case START(state):
|
||||||
this.state = state;
|
this.state = state;
|
||||||
case GameEvent.COMPLETE(state, winnerId):
|
case COMPLETE(state, winnerId):
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.winner = winnerId;
|
this.winner = winnerId;
|
||||||
case GameEvent.SPAWN(EAGLE(id, rect, teamId)):
|
case SPAWN(EAGLE(id, rect, teamId)):
|
||||||
var team = getTeam(teamId);
|
var team = getTeam(teamId);
|
||||||
team.eagleId = id;
|
team.eagleId = id;
|
||||||
case GameEvent.SPAWN(TANK(id, rect, playerId, info)):
|
case SPAWN(TANK(id, rect, playerId, info)):
|
||||||
var player = getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
player.tankId = id;
|
player.tankId = id;
|
||||||
player.state.tank = info.type;
|
player.state.tank = info.type;
|
||||||
@@ -83,8 +83,38 @@ import ru.m.tankz.Type;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function start():Void {
|
||||||
|
var level:LevelConfig = state.level;
|
||||||
|
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
|
||||||
|
for (teamConfig in state.preset.teams) {
|
||||||
|
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
||||||
|
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
||||||
|
teams[team.id] = team;
|
||||||
|
}
|
||||||
|
var controlsById:Map<String, PlayerControl> = new Map();
|
||||||
|
for (control in state.controls) {
|
||||||
|
controlsById[control.playerId] = control;
|
||||||
|
}
|
||||||
|
for (team in teams.iterator()) {
|
||||||
|
for (player in team.players.iterator()) {
|
||||||
|
var playerControl = controlsById.get(player.id);
|
||||||
|
if (playerControl != null && playerControl.color != null) {
|
||||||
|
player.state.color = playerControl.color;
|
||||||
|
}
|
||||||
|
var controlType:Controller = AController.fromString(playerControl != null ? playerControl.control : player.config.control);
|
||||||
|
var control = controlFactory.build(player.id, controlType);
|
||||||
|
if (control != null) {
|
||||||
|
player.control = control;
|
||||||
|
player.control.bind(this, engine);
|
||||||
|
} else {
|
||||||
|
// ToDo: remove player
|
||||||
|
player.state.life = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
gameEventSignal.dispose();
|
gameEventSignal.dispose();
|
||||||
engine.dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
|
import haxework.color.Color;
|
||||||
import ru.m.geom.Position;
|
import ru.m.geom.Position;
|
||||||
import ru.m.geom.Rectangle;
|
import ru.m.geom.Rectangle;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
@@ -9,6 +10,7 @@ typedef TankInfo = {
|
|||||||
var type:TankType;
|
var type:TankType;
|
||||||
var hits:Int;
|
var hits:Int;
|
||||||
var bonus:Bool;
|
var bonus:Bool;
|
||||||
|
var color:Color;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef BrickInfo = {
|
typedef BrickInfo = {
|
||||||
@@ -48,6 +50,7 @@ enum DestroyEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum MoveEvent {
|
enum MoveEvent {
|
||||||
|
EAGLE(id:Int, position:Position);
|
||||||
TANK(id:Int, position:Position);
|
TANK(id:Int, position:Position);
|
||||||
BULLET(id:Int, position:Position);
|
BULLET(id:Int, position:Position);
|
||||||
}
|
}
|
||||||
@@ -65,6 +68,7 @@ enum ChangeEvent {
|
|||||||
PLAYER_LIFE(playerId:PlayerId, value:Int);
|
PLAYER_LIFE(playerId:PlayerId, value:Int);
|
||||||
TEAM_SCORE(teamId:TeamId, value:Int);
|
TEAM_SCORE(teamId:TeamId, value:Int);
|
||||||
TEAM_LIFE(teamId:TeamId, value:Int);
|
TEAM_LIFE(teamId:TeamId, value:Int);
|
||||||
|
BRICK(id:Int, type:BrickType);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum GameEvent {
|
enum GameEvent {
|
||||||
|
|||||||
@@ -1,142 +1,119 @@
|
|||||||
package ru.m.tankz.game;
|
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.Line;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
import ru.m.geom.Position;
|
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.tankz.control.Controller;
|
|
||||||
import ru.m.tankz.control.IControlFactory;
|
|
||||||
import ru.m.tankz.core.Bonus;
|
import ru.m.tankz.core.Bonus;
|
||||||
import ru.m.tankz.core.Bullet;
|
import ru.m.tankz.core.Bullet;
|
||||||
import ru.m.tankz.core.Eagle;
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
|
import ru.m.tankz.engine.Engine;
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.engine.IEngine;
|
||||||
import ru.m.tankz.game.GameEvent;
|
import ru.m.tankz.game.GameEvent;
|
||||||
import ru.m.tankz.game.IGame;
|
|
||||||
import ru.m.tankz.game.Spawner;
|
import ru.m.tankz.game.Spawner;
|
||||||
import ru.m.tankz.map.Brick;
|
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
import ru.m.Timer;
|
||||||
|
|
||||||
class GameRunner implements EngineListener implements GameListener {
|
class GameRunner extends Game implements EngineListener {
|
||||||
@:provide var controlFactory:IControlFactory;
|
|
||||||
|
|
||||||
private var game(default, null):IGame;
|
|
||||||
private var gameEventSignal(get, null):Signal<GameEvent>;
|
|
||||||
private var entityId:Int;
|
|
||||||
private var timer:Timer;
|
private var timer:Timer;
|
||||||
private var builder:EntityBuilder;
|
private var builder:EntityBuilder;
|
||||||
|
|
||||||
public function new(game:IGame) {
|
public function new(state:GameState) {
|
||||||
this.game = game;
|
super(state);
|
||||||
this.builder = new EntityBuilder(this.game.config);
|
this.builder = new EntityBuilder(config);
|
||||||
this.game.connect(this);
|
this.engine = new Engine(config);
|
||||||
this.game.engine.connect(this);
|
this.engine.connect(this);
|
||||||
this.entityId = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function get_gameEventSignal():Signal<GameEvent> {
|
override function changePause(value:Bool):Void {
|
||||||
return game.gameEventSignal;
|
if (engine != null) {
|
||||||
|
if (value) {
|
||||||
|
engine.ticker.stop();
|
||||||
|
} else {
|
||||||
|
engine.ticker.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update():Void {
|
private function update():Void {
|
||||||
game.engine.update();
|
engine.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dispose():Void {
|
override public function dispose():Void {
|
||||||
|
super.dispose();
|
||||||
if (timer != null) {
|
if (timer != null) {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
game.disconnect(this);
|
engine.dispose();
|
||||||
game.engine.disconnect(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function pointToPosition(point:{x:Int, y:Int, direction:String}):Position {
|
override public function start():Void {
|
||||||
return {
|
super.start();
|
||||||
x: (point.x + 1) * game.config.map.cellWidth,
|
engine.map.setData(state.level.data);
|
||||||
y: (point.y + 1) * game.config.map.cellHeight,
|
for (team in teams.iterator()) {
|
||||||
direction: Direction.fromString(point.direction),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function start(state:GameState):Void {
|
|
||||||
for (team in game.teams.iterator()) {
|
|
||||||
for (player in team.players.iterator()) {
|
|
||||||
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
|
|
||||||
if (control != null) {
|
|
||||||
player.control = control;
|
|
||||||
player.control.bind(game);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
team.spawner.runner = spawn;
|
team.spawner.runner = spawn;
|
||||||
for (player in team.players.iterator()) {
|
for (player in team.players.iterator()) {
|
||||||
if (team.tryRespawn(player.id)) {
|
if (team.tryRespawn(player.id)) {
|
||||||
team.spawner.push(player.id);
|
team.spawner.push(engine.ticker, player.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (team.config.eagle != null) {
|
if (team.config.eagle != null) {
|
||||||
var point = game.config.getPoint(team.id, "eagle");
|
var point = team.spawner.getPoint("eagle");
|
||||||
var eagle = builder.buildEagle(++entityId, point, team.id);
|
var eagle = builder.buildEagle(point, team.id);
|
||||||
game.engine.spawn(eagle);
|
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 {
|
gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map));
|
||||||
return {
|
gameEventSignal.emit(START(state));
|
||||||
id: item.id,
|
//for (i in 0...10) spawnBonus();
|
||||||
x: item.cellY,
|
|
||||||
y: item.cellY,
|
|
||||||
rect: item.rect,
|
|
||||||
type: item.config.type,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(BRICK(bricks)));
|
|
||||||
gameEventSignal.emit(GameEvent.START(state));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function next():Option<GameState> {
|
|
||||||
for (rule in game.config.game.complete) {
|
|
||||||
if (rule.team != null && rule.team != game.winner) {
|
|
||||||
return Option.None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var level = game.state.levelId + 1;
|
|
||||||
if (level >= game.config.game.levels) level = 0;
|
|
||||||
return Option.Some(new GameState(game.type, game.state.presetId, level, game.state));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function spawn(task:SpawnTask):Void {
|
private function spawn(task:SpawnTask):Void {
|
||||||
var tank = builder.buildTank(++entityId, task.point, task.playerId, task.tankType);
|
var player = getPlayer(task.playerId);
|
||||||
game.engine.spawn(tank);
|
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color);
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {type:tank.config.type, hits:tank.hits, bonus:tank.bonus})));
|
engine.spawn(tank);
|
||||||
tank.protect.connect(onTankProtectChange);
|
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||||
tank.freezing.connect(onTankFreezingChange);
|
if (player.config.protect > 0) {
|
||||||
|
protectTank(tank, player.config.protect);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onEagleProtectChange(id:Int, state:Bool):Void {
|
private function protectEagle(eagle:Eagle, duration:Float):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(EAGLE_PROTECT(id, state)));
|
eagle.protect = true;
|
||||||
|
gameEventSignal.emit(CHANGE(EAGLE_PROTECT(eagle.id, eagle.protect)));
|
||||||
|
engine.ticker.emit(function() {
|
||||||
|
eagle.protect = false;
|
||||||
|
gameEventSignal.emit(CHANGE(EAGLE_PROTECT(eagle.id, eagle.protect)));
|
||||||
|
}, Std.int(duration * 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onTankProtectChange(id:Int, state:Bool):Void {
|
private function protectTank(tank:Tank, duration:Float):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_PROTECT(id, state)));
|
tank.protect = true;
|
||||||
|
gameEventSignal.emit(CHANGE(TANK_PROTECT(tank.id, tank.protect)));
|
||||||
|
engine.ticker.emit(function() {
|
||||||
|
tank.protect = false;
|
||||||
|
gameEventSignal.emit(CHANGE(TANK_PROTECT(tank.id, tank.protect)));
|
||||||
|
}, Std.int(duration * 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onTankFreezingChange(id:Int, state:Bool):Void {
|
private function freezeTank(tank:Tank, duration:Float):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_FREEZE(id, state)));
|
tank.freezing = true;
|
||||||
|
gameEventSignal.emit(CHANGE(TANK_FREEZE(tank.id, tank.freezing)));
|
||||||
|
engine.ticker.emit(function() {
|
||||||
|
tank.freezing = false;
|
||||||
|
gameEventSignal.emit(CHANGE(TANK_FREEZE(tank.id, tank.freezing)));
|
||||||
|
}, Std.int(duration * 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkComplete():Void {
|
private function checkComplete():Void {
|
||||||
var actives:Array<TeamId> = [];
|
var actives:Array<TeamId> = [];
|
||||||
for (team in game.teams.iterator()) {
|
for (team in teams.iterator()) {
|
||||||
if (team.isAlive) {
|
if (team.isAlive) {
|
||||||
if (team.eagleId > 0) {
|
if (team.eagleId > -1) {
|
||||||
if (!cast(game.engine.entities[team.eagleId], Eagle).death) {
|
if (!cast(engine.entities[team.eagleId], Eagle).death) {
|
||||||
actives.push(team.id);
|
actives.push(team.id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -153,15 +130,17 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function complete(winner:TeamId):Void {
|
private function complete(winner:TeamId):Void {
|
||||||
for (team in game.teams.iterator()) {
|
for (team in teams.iterator()) {
|
||||||
for (player in team.players) {
|
for (player in team.players) {
|
||||||
player.control.action(TankAction.STOP);
|
if (player.control != null) {
|
||||||
|
player.control.action(STOP);
|
||||||
player.control.dispose();
|
player.control.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Timer.delay(function() {
|
Timer.delay(function() {
|
||||||
gameEventSignal.emit(GameEvent.COMPLETE(game.state, winner));
|
gameEventSignal.emit(COMPLETE(state, winner));
|
||||||
}, 5000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onSpawn(entity:EntityType):Void {
|
public function onSpawn(entity:EntityType):Void {
|
||||||
@@ -176,17 +155,17 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline function emitTankMove(tank:Tank):Void {
|
private inline function emitTankMove(tank:Tank):Void {
|
||||||
gameEventSignal.emit(GameEvent.MOVE(TANK(tank.id, {x:tank.rect.x, y:tank.rect.y, direction:tank.rect.direction})));
|
gameEventSignal.emit(MOVE(TANK(tank.id, {x:tank.rect.x, y:tank.rect.y, direction:tank.rect.direction})));
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function emitTankChange(tank:Tank):Void {
|
private inline function emitTankChange(tank:Tank):Void {
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TANK(tank.id, tank.config.type, tank.hits, tank.bonus)));
|
gameEventSignal.emit(CHANGE(TANK(tank.id, tank.config.type, tank.hits, tank.bonus)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onCollision(entity:EntityType, with:EntityType):Void {
|
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||||
switch entity {
|
switch entity {
|
||||||
case EntityType.TANK(tank):
|
case EntityType.TANK(tank):
|
||||||
var control = game.getPlayer(tank.playerId).control;
|
var control = getPlayer(tank.playerId).control;
|
||||||
if (control != null) control.onCollision(with);
|
if (control != null) control.onCollision(with);
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
@@ -201,46 +180,46 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
tank.rect.lean(cell.rect);
|
tank.rect.lean(cell.rect);
|
||||||
emitTankMove(tank);
|
emitTankMove(tank);
|
||||||
case [BULLET(bullet), BULLET(other_bullet)]:
|
case [BULLET(bullet), BULLET(other_bullet)]:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(other_bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(other_bullet.id)));
|
||||||
case [BULLET(bullet), CELL(cell)]:
|
case [BULLET(bullet), CELL(cell)]:
|
||||||
bullet.rect.lean(cell.rect);
|
bullet.rect.lean(cell.rect);
|
||||||
gameEventSignal.emit(GameEvent.HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
gameEventSignal.emit(HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
case [TANK(tank), BONUS(bonus)]:
|
case [TANK(tank), BONUS(bonus)]:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BONUS(bonus.id, {tankId: tank.id, score: bonus.config.score})));
|
gameEventSignal.emit(DESTROY(BONUS(bonus.id, {tankId: tank.id, score: bonus.config.score})));
|
||||||
case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/:
|
case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/:
|
||||||
if (bullet.tankId == tank.id || (!game.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
|
if (bullet.tankId == tank.id || (!config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
|
||||||
// Nothing
|
// Nothing
|
||||||
} else {
|
} else {
|
||||||
if (!tank.protect.active) {
|
if (!tank.protect) {
|
||||||
if (tank.hits > 0) {
|
if (tank.hits > 0) {
|
||||||
tank.hits--;
|
tank.hits--;
|
||||||
if (tank.bonus) {
|
if (tank.bonus) {
|
||||||
tank.bonus = false;
|
tank.bonus = false;
|
||||||
spawnBonus();
|
spawnBonus();
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||||
emitTankChange(tank);
|
emitTankChange(tank);
|
||||||
} else if (tank.config.downgrade != null) {
|
} else if (tank.config.downgrade != null) {
|
||||||
tank.config = game.config.getTank(tank.config.downgrade);
|
tank.config = config.getTank(tank.config.downgrade);
|
||||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||||
emitTankChange(tank);
|
emitTankChange(tank);
|
||||||
} else {
|
} else {
|
||||||
var score = tank.config.score;
|
var score = tank.config.score;
|
||||||
if (tank.playerId.team == bullet.playerId.team) {
|
if (tank.playerId.team == bullet.playerId.team) {
|
||||||
score = Math.round(score * -0.5);
|
score = Math.round(score * -0.5);
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, buildShot(bullet, score))));
|
gameEventSignal.emit(DESTROY(TANK(tank.id, buildShot(bullet, score))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
}
|
}
|
||||||
case [BULLET(bullet), EAGLE(eagle)]:
|
case [BULLET(bullet), EAGLE(eagle)]:
|
||||||
if (!eagle.protect.active) {
|
if (!eagle.protect) {
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
gameEventSignal.emit(DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,7 +229,7 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
case TANK(tank):
|
case TANK(tank):
|
||||||
emitTankMove(tank);
|
emitTankMove(tank);
|
||||||
case BULLET(bullet):
|
case BULLET(bullet):
|
||||||
gameEventSignal.emit(GameEvent.MOVE(BULLET(bullet.id, {x:bullet.rect.x, y:bullet.rect.y, direction:bullet.rect.direction})));
|
gameEventSignal.emit(MOVE(BULLET(bullet.id, {x:bullet.rect.x, y:bullet.rect.y, direction:bullet.rect.direction})));
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,15 +238,15 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function spawnBonus():Void {
|
private function spawnBonus():Void {
|
||||||
var type = game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)].type;
|
var type = config.bonuses[Math.floor(Math.random() * config.bonuses.length)].type;
|
||||||
var point = {
|
var point = {
|
||||||
x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)),
|
x: Math.floor(Math.random() * (engine.map.gridWidth - 1)),
|
||||||
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
|
y: Math.floor(Math.random() * (engine.map.gridHeight - 1)),
|
||||||
direction: "right",
|
direction: "right",
|
||||||
}
|
}
|
||||||
var bonus = builder.buildBonus(++entityId, point, type);
|
var bonus = builder.buildBonus(point, type);
|
||||||
game.engine.spawn(bonus);
|
engine.spawn(bonus);
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
|
gameEventSignal.emit(EventUtil.buildBonusSpawn(bonus));
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function alienTank(team:TeamId):Tank->Bool {
|
private inline function alienTank(team:TeamId):Tank->Bool {
|
||||||
@@ -281,35 +260,35 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
case "star":
|
case "star":
|
||||||
upgradeTank(tank);
|
upgradeTank(tank);
|
||||||
case "grenade":
|
case "grenade":
|
||||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(t.id, {tankId: tank.id})));
|
gameEventSignal.emit(DESTROY(TANK(t.id, {tankId: tank.id})));
|
||||||
}
|
}
|
||||||
case "helmet":
|
case "helmet":
|
||||||
tank.protect.on(bonus.config.duration);
|
protectTank(tank, bonus.config.duration);
|
||||||
case "clock":
|
case "clock":
|
||||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||||
t.freezing.on(bonus.config.duration);
|
freezeTank(t, bonus.config.duration);
|
||||||
t.stop();
|
t.stop();
|
||||||
gameEventSignal.emit(GameEvent.STOP(TANK(t.id)));
|
gameEventSignal.emit(STOP(TANK(t.id)));
|
||||||
}
|
}
|
||||||
case "shovel":
|
case "shovel":
|
||||||
// ToDo: protect eagle/area
|
// ToDo: protect eagle/area
|
||||||
var team:Team = game.teams[tank.playerId.team];
|
var team:Team = teams[tank.playerId.team];
|
||||||
if (team.eagleId > 0) {
|
if (team.eagleId > 0) {
|
||||||
var eagle:Eagle = cast(game.engine.entities[team.eagleId], Eagle);
|
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
|
||||||
eagle.protect.on(bonus.config.duration);
|
protectEagle(eagle, bonus.config.duration);
|
||||||
}
|
}
|
||||||
case "gun":
|
case "gun":
|
||||||
upgradeTank(tank, 5);
|
upgradeTank(tank, 5);
|
||||||
case _:
|
case _:
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, {tankId: tank.id}))); // :-D
|
gameEventSignal.emit(DESTROY(TANK(tank.id, {tankId: tank.id}))); // :-D
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function upgradeTank(tank:Tank, level:Int = 1):Void {
|
private function upgradeTank(tank:Tank, level:Int = 1):Void {
|
||||||
if (tank.config.upgrade != null) {
|
if (tank.config.upgrade != null) {
|
||||||
while (level-- > 0 && tank.config.upgrade != null) {
|
while (level-- > 0 && tank.config.upgrade != null) {
|
||||||
tank.config = game.config.getTank(tank.config.upgrade);
|
tank.config = config.getTank(tank.config.upgrade);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tank.hits++;
|
tank.hits++;
|
||||||
@@ -318,70 +297,84 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function changeScore(playerId:PlayerId, score:Int):Void {
|
private function changeScore(playerId:PlayerId, score:Int):Void {
|
||||||
var player = game.getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
var team = game.getTeam(playerId.team);
|
var team = getTeam(playerId.team);
|
||||||
player.state.score += score;
|
player.state.score += score;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
gameEventSignal.emit(CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TEAM_SCORE(playerId.team, game.state.getTeamScore(team.id))));
|
gameEventSignal.emit(CHANGE(TEAM_SCORE(playerId.team, state.getTeamScore(team.id))));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function changeLife(playerId:PlayerId, life:Int):Void {
|
private function changeLife(playerId:PlayerId, life:Int):Void {
|
||||||
var player = game.getPlayer(playerId);
|
var player = getPlayer(playerId);
|
||||||
player.state.life += life;
|
player.state.life += life;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_LIFE(playerId, player.state.life)));
|
gameEventSignal.emit(CHANGE(PLAYER_LIFE(playerId, player.state.life)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function changeTeamLife(teamId:TeamId, life:Int):Void {
|
private function changeTeamLife(teamId:TeamId, life:Int):Void {
|
||||||
var team = game.getTeam(teamId);
|
var team = getTeam(teamId);
|
||||||
team.state.life += life;
|
team.state.life += life;
|
||||||
gameEventSignal.emit(GameEvent.CHANGE(TEAM_LIFE(teamId, team.state.life)));
|
gameEventSignal.emit(CHANGE(TEAM_LIFE(teamId, team.state.life)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
override public function onGameEvent(event:GameEvent):Void {
|
||||||
|
if (pause) {
|
||||||
switch event {
|
switch event {
|
||||||
case GameEvent.START(_):
|
case ACTION(_): return;
|
||||||
timer = new Timer(10);
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.onGameEvent(event);
|
||||||
|
switch event {
|
||||||
|
case START(_):
|
||||||
|
engine.ticker.start();
|
||||||
|
timer = new Timer(30);
|
||||||
timer.run = update;
|
timer.run = update;
|
||||||
case GameEvent.COMPLETE(_, _):
|
case COMPLETE(_, _):
|
||||||
|
engine.ticker.stop();
|
||||||
if (timer != null) {
|
if (timer != null) {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
case GameEvent.ACTION(tankId, SHOT):
|
case ACTION(tankId, SHOT):
|
||||||
var tank:Tank = cast game.engine.entities.get(tankId);
|
var tank:Tank = cast engine.entities.get(tankId);
|
||||||
var player = game.getPlayer(tank.playerId);
|
var player = getPlayer(tank.playerId);
|
||||||
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
|
if (!tank.freezing && player.bullets < tank.config.bullets) {
|
||||||
var rect = tank.rect;
|
var rect = tank.rect;
|
||||||
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
||||||
var bullet = builder.buildBullet(++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.tank = tank;
|
||||||
bullet.move(bullet.rect.direction);
|
bullet.move(bullet.rect.direction);
|
||||||
game.engine.spawn(bullet);
|
engine.spawn(bullet);
|
||||||
gameEventSignal.emit(GameEvent.SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing)));
|
gameEventSignal.emit(EventUtil.buildBulletSpawn(bullet));
|
||||||
}
|
}
|
||||||
case GameEvent.ACTION(tankId, MOVE(direction)):
|
case ACTION(tankId, MOVE(direction)):
|
||||||
game.engine.move(tankId, direction);
|
engine.move(tankId, direction);
|
||||||
case GameEvent.ACTION(tankId, STOP):
|
case ACTION(tankId, STOP):
|
||||||
gameEventSignal.emit(GameEvent.STOP(TANK(tankId)));
|
gameEventSignal.emit(STOP(TANK(tankId)));
|
||||||
game.engine.stop(tankId);
|
engine.stop(tankId);
|
||||||
case GameEvent.SPAWN(TANK(_, _, playerId, _)):
|
case SPAWN(TANK(_, _, playerId, _)):
|
||||||
game.getPlayer(playerId).control.start();
|
var control = getPlayer(playerId).control;
|
||||||
case GameEvent.SPAWN(BULLET(_, _, playerId, _)):
|
if (control != null) {
|
||||||
game.getPlayer(playerId).bullets++;
|
control.start();
|
||||||
case GameEvent.DESTROY(EAGLE(id, shot)):
|
}
|
||||||
var eagle:Eagle = game.engine.getEntity(id);
|
case SPAWN(BULLET(_, _, playerId, _)):
|
||||||
|
getPlayer(playerId).bullets++;
|
||||||
|
case DESTROY(EAGLE(id, shot)):
|
||||||
|
var eagle:Eagle = engine.getEntity(id);
|
||||||
eagle.death = true;
|
eagle.death = true;
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
var tank:Tank = engine.getEntity(shot.tankId);
|
||||||
changeScore(tank.playerId, shot.score);
|
changeScore(tank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
checkComplete();
|
checkComplete();
|
||||||
case GameEvent.DESTROY(TANK(id, shot)):
|
case DESTROY(TANK(id, shot)):
|
||||||
var tank:Tank = game.engine.getEntity(id);
|
var tank:Tank = engine.getEntity(id);
|
||||||
var team = game.getTeam(tank.playerId.team);
|
var team = getTeam(tank.playerId.team);
|
||||||
var player = game.getPlayer(tank.playerId);
|
var player = getPlayer(tank.playerId);
|
||||||
|
if (player.control != null) {
|
||||||
player.control.stop();
|
player.control.stop();
|
||||||
player.tankId = 0; //ToDo: ?
|
}
|
||||||
|
player.tankId = -1;
|
||||||
team.onDestroy(player.id);
|
team.onDestroy(player.id);
|
||||||
if (player.state.life > 0) {
|
if (player.state.life > 0) {
|
||||||
changeLife(player.id, -1);
|
changeLife(player.id, -1);
|
||||||
@@ -390,7 +383,7 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
}
|
}
|
||||||
var respawn:Bool = team.tryRespawn(player.id);
|
var respawn:Bool = team.tryRespawn(player.id);
|
||||||
if (respawn) {
|
if (respawn) {
|
||||||
team.spawner.push(player.id);
|
team.spawner.push(engine.ticker, player.id);
|
||||||
}
|
}
|
||||||
if (!team.isAlive) {
|
if (!team.isAlive) {
|
||||||
checkComplete();
|
checkComplete();
|
||||||
@@ -399,44 +392,44 @@ class GameRunner implements EngineListener implements GameListener {
|
|||||||
spawnBonus();
|
spawnBonus();
|
||||||
}
|
}
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
var shooterTank:Tank = game.engine.getEntity(shot.tankId);
|
var shooterTank:Tank = engine.getEntity(shot.tankId);
|
||||||
changeScore(shooterTank.playerId, shot.score);
|
changeScore(shooterTank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case GameEvent.DESTROY(BONUS(id, shot)):
|
case DESTROY(BONUS(id, shot)):
|
||||||
var bonus:Bonus = game.engine.getEntity(id);
|
var bonus:Bonus = engine.getEntity(id);
|
||||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
var tank:Tank = engine.getEntity(shot.tankId);
|
||||||
applyBonus(tank, bonus);
|
applyBonus(tank, bonus);
|
||||||
if (shot.score != 0) {
|
if (shot.score != 0) {
|
||||||
changeScore(tank.playerId, shot.score);
|
changeScore(tank.playerId, shot.score);
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case GameEvent.DESTROY(BULLET(id)):
|
case DESTROY(BULLET(id)):
|
||||||
var bullet:Bullet = game.engine.getEntity(id);
|
var bullet:Bullet = engine.getEntity(id);
|
||||||
var player = game.getPlayer(bullet.playerId);
|
var player = getPlayer(bullet.playerId);
|
||||||
player.bullets--;
|
player.bullets--;
|
||||||
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
|
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
|
||||||
// ToDo: move
|
// ToDo: move
|
||||||
new Point(bullet.rect.direction.x * 5, bullet.rect.direction.y * 5)
|
new Point(bullet.rect.direction.x * 5, bullet.rect.direction.y * 5)
|
||||||
);
|
);
|
||||||
var cells = game.engine.map.grid.getCells(side.setLength(game.engine.map.grid.cellWidth * 3));
|
var cells = engine.map.grid.getCells(side.setLength(engine.map.grid.cellWidth * 3));
|
||||||
for (cell in cells) {
|
for (cell in cells) {
|
||||||
if (cell.armor > 0) {
|
if (cell.armor > 0) {
|
||||||
var shot = buildShot(bullet);
|
var shot = buildShot(bullet);
|
||||||
if (cell.armor == bullet.config.piercing) {
|
if (cell.armor == bullet.config.piercing) {
|
||||||
game.engine.destroyCell(cell.cellX, cell.cellY);
|
engine.destroyCell(cell.cellX, cell.cellY);
|
||||||
var brick = game.engine.map.getBrick(cell.position);
|
var brick = engine.map.getBrick(cell.position);
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
gameEventSignal.emit(DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
||||||
} else if (cell.armor < bullet.config.piercing) {
|
} else if (cell.armor < bullet.config.piercing) {
|
||||||
var brick = game.engine.map.getBrick(cell.position);
|
var brick = engine.map.getBrick(cell.position);
|
||||||
for (cell in brick.cells) {
|
for (cell in brick.cells) {
|
||||||
game.engine.destroyCell(cell.cellX, cell.cellY);
|
engine.destroyCell(cell.cellX, cell.cellY);
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(GameEvent.DESTROY(BRICK(brick.id, shot)));
|
gameEventSignal.emit(DESTROY(BRICK(brick.id, shot)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.engine.destroy(id);
|
engine.destroy(id);
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,10 @@ class State {
|
|||||||
public var hits:Int;
|
public var hits:Int;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
|
score = 0;
|
||||||
|
frags = 0;
|
||||||
|
shots = 0;
|
||||||
|
hits = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add(state:State) {
|
public function add(state:State) {
|
||||||
@@ -69,6 +72,7 @@ class GameState {
|
|||||||
public var type:GameType;
|
public var type:GameType;
|
||||||
public var presetId:PresetId;
|
public var presetId:PresetId;
|
||||||
public var levelId:LevelId;
|
public var levelId:LevelId;
|
||||||
|
public var controls:Array<PlayerControl>;
|
||||||
public var players:Map<String, PlayerState>;
|
public var players:Map<String, PlayerState>;
|
||||||
public var teams:Map<TeamId, TeamState>;
|
public var teams:Map<TeamId, TeamState>;
|
||||||
public var preset(get, null):GamePreset;
|
public var preset(get, null):GamePreset;
|
||||||
@@ -78,10 +82,12 @@ class GameState {
|
|||||||
@:provide static private var configBundle:IConfigBundle;
|
@:provide static private var configBundle:IConfigBundle;
|
||||||
@:provide static private var levelBundle:ILevelBundle;
|
@:provide static private var levelBundle:ILevelBundle;
|
||||||
|
|
||||||
public function new(type:GameType, presetId:PresetId = 0, levelId:Int = 0, state:GameState = null) {
|
public function new(type:GameType, presetId:PresetId = 0, levelId:Int = 0, state:GameState = null, controls:Array<PlayerControl> = null) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.presetId = presetId;
|
this.presetId = presetId;
|
||||||
this.levelId = levelId;
|
this.levelId = levelId;
|
||||||
|
//this.controls = controls == null ? config.controls[0].values : controls;
|
||||||
|
this.controls = controls == null ? [] : controls;
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
this.teams = new Map();
|
this.teams = new Map();
|
||||||
this.players = new Map();
|
this.players = new Map();
|
||||||
|
|||||||
@@ -2,16 +2,17 @@ package ru.m.tankz.game;
|
|||||||
|
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.engine.IEngine;
|
import ru.m.tankz.control.IControlFactory;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
interface IGame extends GameListener {
|
interface IGame extends GameListener {
|
||||||
public var type(default, null):GameType;
|
public var type(default, null):GameType;
|
||||||
public var teams(default, null):Map<TeamId, Team>;
|
public var teams(default, null):Map<TeamId, Team>;
|
||||||
public var config(default, null):Config;
|
public var config(default, null):Config;
|
||||||
public var engine(default, null):IEngine;
|
|
||||||
public var winner(default, null):Null<TeamId>;
|
public var winner(default, null):Null<TeamId>;
|
||||||
public var state(default, null):GameState;
|
public var state(default, null):GameState;
|
||||||
|
public var controlFactory(default, null):IControlFactory;
|
||||||
|
public var pause(default, set):Bool;
|
||||||
|
|
||||||
public var gameEventSignal(default, null):Signal<GameEvent>;
|
public var gameEventSignal(default, null):Signal<GameEvent>;
|
||||||
|
|
||||||
@@ -24,6 +25,8 @@ interface IGame extends GameListener {
|
|||||||
public function getTeam(teamId:TeamId):Team;
|
public function getTeam(teamId:TeamId):Team;
|
||||||
|
|
||||||
public function getPlayer(playerId:PlayerId):Player;
|
public function getPlayer(playerId:PlayerId):Player;
|
||||||
|
|
||||||
|
public function start():Void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GameListener {
|
interface GameListener {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ class Player {
|
|||||||
this.state.reset();
|
this.state.reset();
|
||||||
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
||||||
this.bullets = 0;
|
this.bullets = 0;
|
||||||
|
this.tankId = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function set_tankId(value:Int):Int {
|
private function set_tankId(value:Int):Int {
|
||||||
@@ -42,6 +43,6 @@ class Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function get_isAlive():Bool {
|
private function get_isAlive():Bool {
|
||||||
return tankId > 0 || state.life > 0;
|
return tankId > -1 || state.life > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.tankz.engine.ITicker;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
|
|
||||||
|
|
||||||
typedef SpawnTask = {
|
typedef SpawnTask = {
|
||||||
var point:SpawnPoint;
|
var point:SpawnPoint;
|
||||||
var playerId:PlayerId;
|
var playerId:PlayerId;
|
||||||
@@ -19,7 +18,6 @@ class Spawner {
|
|||||||
private var config:TeamConfig;
|
private var config:TeamConfig;
|
||||||
private var points:Array<SpawnPoint>;
|
private var points:Array<SpawnPoint>;
|
||||||
private var queue:Array<SpawnTask>;
|
private var queue:Array<SpawnTask>;
|
||||||
private var timer:Timer;
|
|
||||||
|
|
||||||
private var indexedPoints:Map<Int, SpawnPoint>;
|
private var indexedPoints:Map<Int, SpawnPoint>;
|
||||||
private var anyPoints:Array<SpawnPoint>;
|
private var anyPoints:Array<SpawnPoint>;
|
||||||
@@ -62,7 +60,7 @@ class Spawner {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function push(playerId:PlayerId):Void {
|
public function push(ticker:ITicker, playerId:PlayerId):Void {
|
||||||
var point:SpawnPoint = null;
|
var point:SpawnPoint = null;
|
||||||
if (indexedPoints.exists(playerId.index)) {
|
if (indexedPoints.exists(playerId.index)) {
|
||||||
point = indexedPoints.get(playerId.index);
|
point = indexedPoints.get(playerId.index);
|
||||||
@@ -72,24 +70,16 @@ class Spawner {
|
|||||||
}
|
}
|
||||||
if (point != null) {
|
if (point != null) {
|
||||||
queue.push({playerId:playerId, point:point, tankType:getPlayerTank(playerId)});
|
queue.push({playerId:playerId, point:point, tankType:getPlayerTank(playerId)});
|
||||||
run();
|
if (config.spawnInterval == null) {
|
||||||
|
ticker.emit(spawn, 500);
|
||||||
|
} else {
|
||||||
|
ticker.emit(spawn, 500 + config.spawnInterval * queue.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function run():Void {
|
|
||||||
if (timer == null) {
|
|
||||||
timer = new Timer(config.spawnInterval == null ? 500 : config.spawnInterval);
|
|
||||||
timer.run = spawn;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function spawn():Void {
|
private function spawn():Void {
|
||||||
if (queue.length == 0) {
|
if (queue.length > 0) {
|
||||||
if (timer != null) {
|
|
||||||
timer.stop();
|
|
||||||
timer = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
runner(queue.shift());
|
runner(queue.shift());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user