Compare commits
22 Commits
0.12.0
...
event_prot
| Author | SHA1 | Date | |
|---|---|---|---|
| 59e9ced333 | |||
| 94b3a94cbb | |||
| caab6ba3a3 | |||
| 7f70c49b87 | |||
| 8f50da64c2 | |||
| 982ca09a23 | |||
| dbf30decae | |||
| abd1b834d7 | |||
| 6ad9076987 | |||
| 78bbf5264e | |||
| 4c8ae66624 | |||
| 5a3d5b974e | |||
| fb9a28c126 | |||
| 7115ea8ac2 | |||
| 01417f5bbd | |||
| abf944418e | |||
| c7946ef1da | |||
| 241499bc2d | |||
| 74297dd9c7 | |||
| 31cb20bf85 | |||
| 1d95de02e1 | |||
| ba13111a8e |
@@ -48,6 +48,9 @@ const config = new Project.Config({
|
||||
assets: [
|
||||
'src/common/resources'
|
||||
],
|
||||
flags: [
|
||||
//'proto_debug',
|
||||
],
|
||||
macros: [
|
||||
`CompilationOption.set('build','${dateformat(new Date(), 'yyyy-mm-dd HH:MM:ss')}')`,
|
||||
]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.12.0",
|
||||
"version": "0.14.2",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"dateformat": "^3.0.3",
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
"protoFiles": [
|
||||
"src/common/proto/core.proto",
|
||||
"src/common/proto/game.proto",
|
||||
"src/common/proto/room.proto",
|
||||
"src/common/proto/pack.proto"
|
||||
],
|
||||
"cleanOut": true,
|
||||
"haxeOut": "src-gen/haxe",
|
||||
"javaOut": null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package ru.m.display;
|
||||
|
||||
import flash.display.DisplayObject;
|
||||
import flash.display.DisplayObjectContainer;
|
||||
|
||||
class DisplayObjectContainerExtender {
|
||||
|
||||
public static function removeChildSafety(self:DisplayObjectContainer, child:DisplayObject):DisplayObject {
|
||||
if (self.contains(child)) {
|
||||
return self.removeChild(child);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -10,14 +10,12 @@ import ru.m.tankz.bundle.ConfigBundle;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.bundle.LevelBundle;
|
||||
import ru.m.tankz.control.ClientControlFactory;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
import ru.m.tankz.storage.NetworkStorage;
|
||||
import ru.m.tankz.storage.RecordStorage;
|
||||
import ru.m.tankz.storage.SettingsStorage;
|
||||
|
||||
@@ -27,12 +25,11 @@ class Init {
|
||||
@:provide static var levelBundle:ILevelBundle;
|
||||
@:provide static var configBundle:IConfigBundle;
|
||||
@:provide static var settingsStorage:SettingsStorage;
|
||||
@:provide static var multiplayerStorage:MultiplayerStorage;
|
||||
@:provide static var multiplayerStorage:NetworkStorage;
|
||||
@:provide static var gameStorage:GameStorage;
|
||||
@:provide static var recordStorage:RecordStorage;
|
||||
@:provide static var soundManager:SoundManager;
|
||||
@:provide static var networkManager:NetworkManager;
|
||||
@:provide static var controlFactory:IControlFactory;
|
||||
@:provide static var popupManager:PopupManager;
|
||||
@:provide static var connection:IConnection<Request, Response>;
|
||||
|
||||
@@ -54,11 +51,10 @@ class Init {
|
||||
levelBundle = new LevelBundle();
|
||||
configBundle = new ConfigBundle();
|
||||
settingsStorage = new SettingsStorage();
|
||||
multiplayerStorage = new MultiplayerStorage();
|
||||
multiplayerStorage = new NetworkStorage();
|
||||
gameStorage = new GameStorage();
|
||||
recordStorage = new RecordStorage();
|
||||
soundManager = new SoundManager();
|
||||
controlFactory = new ClientControlFactory();
|
||||
popupManager = new PopupManager();
|
||||
|
||||
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
|
||||
|
||||
@@ -22,6 +22,7 @@ class Style {
|
||||
public static var darkColor = 0x777564;
|
||||
public static var textColor = 0xE7E0BB;
|
||||
public static var activeColor = 0xFFFF00;
|
||||
public static var borderColor = ColorUtil.multiply(lightColor, 1.5);
|
||||
|
||||
public static var baseFontSize = 18;
|
||||
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 {
|
||||
resources.color.put("light", lightColor);
|
||||
resources.color.put("dark", darkColor);
|
||||
@@ -111,7 +120,7 @@ class Style {
|
||||
Skin.geometry(new Geometry().setPadding([25, 8])),
|
||||
]);
|
||||
resources.skin.put("border", [
|
||||
Skin.border(ColorUtil.multiply(lightColor, 1.5), 1, 2),
|
||||
Skin.border(borderColor, 1, 2),
|
||||
]);
|
||||
resources.skin.put("scroll", [
|
||||
Skin.scrollVertical(lightColor, ColorUtil.diff(lightColor, 128)),
|
||||
@@ -134,13 +143,16 @@ class Style {
|
||||
]);
|
||||
resources.skin.put("window", [
|
||||
Skin.color(darkColor),
|
||||
Skin.border(ColorUtil.multiply(lightColor, 1.5), 1, 2),
|
||||
Skin.border(borderColor, 1, 2),
|
||||
Skin.geometry(new Geometry().setPadding(2)),
|
||||
]);
|
||||
resources.skin.put("window.close", [
|
||||
Skin.size(36, 36),
|
||||
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("close", "times-circle-solid.svg");
|
||||
|
||||
@@ -16,7 +16,7 @@ class ConfigBundle implements IConfigBundle {
|
||||
public function get(type:GameType):Config {
|
||||
if (!_cache.exists(type)) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ActionConfig {
|
||||
}
|
||||
|
||||
public function asKeyBinding():KeyBinding {
|
||||
var result = new Map<Int, TankAction>();
|
||||
var result = new KeyBinding();
|
||||
for (item in data) {
|
||||
result[item.key] = item.action;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package ru.m.tankz.control;
|
||||
import flash.events.FocusEvent;
|
||||
import flash.events.KeyboardEvent;
|
||||
import flash.Lib;
|
||||
import flash.ui.Keyboard;
|
||||
import haxe.Timer;
|
||||
import ru.m.tankz.control.ActionConfig;
|
||||
import ru.m.tankz.control.Control;
|
||||
@@ -21,10 +20,20 @@ class HumanControl extends Control {
|
||||
public function new(playerId:PlayerId, controlIndex:Int) {
|
||||
super(playerId);
|
||||
this.keyBinding = storage.getActionConfig(controlIndex).asKeyBinding();
|
||||
moveQueue = new Array<Int>();
|
||||
moveQueue = [];
|
||||
}
|
||||
|
||||
override public function start():Void {
|
||||
super.start();
|
||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
||||
//Lib.current.stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
|
||||
}
|
||||
|
||||
override public function stop():Void {
|
||||
super.stop();
|
||||
Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||
Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
||||
moveQueue = [];
|
||||
}
|
||||
|
||||
private function onKeyDown(event:KeyboardEvent):Void {
|
||||
|
||||
13
src/client/haxe/ru/m/tankz/local/LocalControlFactory.hx
Normal file
13
src/client/haxe/ru/m/tankz/local/LocalControlFactory.hx
Normal file
@@ -0,0 +1,13 @@
|
||||
package ru.m.tankz.local;
|
||||
|
||||
import ru.m.tankz.control.BaseControlFactory;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.HumanControl;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class LocalControlFactory extends BaseControlFactory {
|
||||
|
||||
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||
return new HumanControl(id, index);
|
||||
}
|
||||
}
|
||||
31
src/client/haxe/ru/m/tankz/local/LocalGame.hx
Normal file
31
src/client/haxe/ru/m/tankz/local/LocalGame.hx
Normal file
@@ -0,0 +1,31 @@
|
||||
package ru.m.tankz.local;
|
||||
|
||||
import ru.m.tankz.local.LocalControlFactory;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
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 _:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
package ru.m.tankz.control;
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.control.HumanControl;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.control.Control;
|
||||
|
||||
class ClientNetworkControl extends HumanControl {
|
||||
class NetworkControl extends HumanControl {
|
||||
|
||||
@:provide private var network:NetworkManager;
|
||||
|
||||
override public function action(action:TankAction):Void {
|
||||
network.action(action);
|
||||
if (tankId > -1) {
|
||||
network.action(tankId, action);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/client/haxe/ru/m/tankz/network/NetworkControlFactory.hx
Normal file
16
src/client/haxe/ru/m/tankz/network/NetworkControlFactory.hx
Normal file
@@ -0,0 +1,16 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.control.BaseControlFactory;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class NetworkControlFactory extends BaseControlFactory {
|
||||
|
||||
override private function buildHuman(id:PlayerId, index:Int):Control {
|
||||
return new NetworkControl(id, index);
|
||||
}
|
||||
|
||||
override private function buildBot(id:PlayerId, type:String):Control {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
54
src/client/haxe/ru/m/tankz/network/NetworkGame.hx
Normal file
54
src/client/haxe/ru/m/tankz/network/NetworkGame.hx
Normal file
@@ -0,0 +1,54 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import haxe.Unserializer;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkControlFactory;
|
||||
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.event.time;
|
||||
var eventStr = game.event.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,21 +1,28 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.game.GameEventProto;
|
||||
import ru.m.tankz.proto.room.SlotProto;
|
||||
import ru.m.tankz.proto.room.SlotRequest;
|
||||
import haxe.Serializer;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.game.GameChangeProto;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||
import ru.m.tankz.proto.pack.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.LogoutRequest;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.pack.StartGameRequest;
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
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.NetworkStorage;
|
||||
|
||||
typedef ClientConnection = IConnection<Request, Response>;
|
||||
|
||||
@@ -31,29 +38,30 @@ enum ConnectionState {
|
||||
class NetworkManager {
|
||||
|
||||
public var state(default, null):ConnectionState;
|
||||
public var game(default, null):GameProto;
|
||||
public var room(default, null):RoomProto;
|
||||
public var user(default, null):UserProto;
|
||||
|
||||
public var stateSignal:Signal<ConnectionState>;
|
||||
public var listGameSignal:Signal<Array<GameProto>>;
|
||||
public var gameSignal:Signal<GameProto>;
|
||||
public var gameUpdateSignal:Signal<Array<GameChangeProto>>;
|
||||
public var listRoomSignal:Signal<Array<RoomProto>>;
|
||||
public var roomSignal:Signal<RoomProto>;
|
||||
public var gameEventSignal:Signal<GameEventResponse>;
|
||||
|
||||
@:provide private var connection:ClientConnection;
|
||||
@:provide private var storage:MultiplayerStorage;
|
||||
@:provide private var storage:NetworkStorage;
|
||||
|
||||
private var reconnectTimer:Timer;
|
||||
private var reconnectDelay:Int;
|
||||
|
||||
public function new() {
|
||||
reconnectDelay = 500;
|
||||
stateSignal = new Signal();
|
||||
listGameSignal = new Signal();
|
||||
gameSignal = new Signal();
|
||||
gameUpdateSignal = new Signal();
|
||||
listRoomSignal = new Signal();
|
||||
roomSignal = new Signal();
|
||||
gameEventSignal = new Signal();
|
||||
updateState(OFFLINE);
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.receiveHandler.connect(onResponse);
|
||||
var user = storage.user;
|
||||
if (user != null) {
|
||||
login(user.name, user.uuid);
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
private function updateState(value:ConnectionState):Void {
|
||||
@@ -62,59 +70,83 @@ class NetworkManager {
|
||||
}
|
||||
|
||||
public function login(name:String, uuid:String = null):Void {
|
||||
updateState(CONNECT);
|
||||
connection.connect().then(function(c:ClientConnection) {
|
||||
updateState(LOGIN);
|
||||
c.send(new Request().setLogin(
|
||||
new LoginRequest()
|
||||
.setUuid(uuid)
|
||||
.setName(name)
|
||||
));
|
||||
}).catchError(function(error) {
|
||||
updateState(ERROR(error));
|
||||
});
|
||||
updateState(LOGIN);
|
||||
connection.send(new Request().setLogin(
|
||||
new LoginRequest()
|
||||
.setUuid(uuid)
|
||||
.setName(name)
|
||||
));
|
||||
}
|
||||
|
||||
public function logout():Void {
|
||||
connection.send(new Request().setLogout(new LogoutRequest()));
|
||||
}
|
||||
|
||||
public function listGame():Void {
|
||||
connection.send(new Request().setListGame(new ListGameRequest()));
|
||||
public function listGame(subscribe:Bool):Void {
|
||||
connection.send(new Request().setRoomList(new RoomListRequest().setSubscribe(subscribe)));
|
||||
}
|
||||
|
||||
public function createGame(type:String, level:Int):Void {
|
||||
connection.send(new Request().setCreateGame(new CreateGameRequest().setType(type).setLevel(level)));
|
||||
connection.send(new Request().setRoom(new RoomRequest().setCreate(new CreateRequest().setType(type).setLevel(level))));
|
||||
}
|
||||
|
||||
public function joinGame(gameId:Int):Void {
|
||||
connection.send(new Request().setJoinGame(new JoinGameRequest().setGameId(gameId)));
|
||||
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 {
|
||||
connection.send(new Request().setLeaveGame(new LeaveGameRequest()));
|
||||
connection.send(new Request().setRoom(new RoomRequest().setLeave(new LeaveRequest())));
|
||||
}
|
||||
|
||||
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 {
|
||||
// ToDo: network
|
||||
public function action(tankId:Int, action:TankAction):Void {
|
||||
connection.send(new Request().setGameEvent(new GameEventRequest().setEvent(
|
||||
new GameEventProto().setTime(0).setEvent(
|
||||
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
||||
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 {
|
||||
updateState(switch event {
|
||||
case CONNECTED:
|
||||
L.d('Network', '$event');
|
||||
CONNECTED;
|
||||
case DISCONNECTED:
|
||||
L.d('Network', '$event');
|
||||
OFFLINE;
|
||||
case ERROR(error):
|
||||
L.e('Network', '$error', error);
|
||||
ERROR(error);
|
||||
case CONNECTED: CONNECTED;
|
||||
case DISCONNECTED: OFFLINE;
|
||||
case ERROR(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 {
|
||||
@@ -129,25 +161,13 @@ class NetworkManager {
|
||||
} else if (packet.hasLogout()) {
|
||||
storage.user = null;
|
||||
updateState(CONNECTED);
|
||||
} else if (packet.hasListGame()) {
|
||||
listGameSignal.emit(packet.listGame.games);
|
||||
} else if (packet.hasCreateGame()) {
|
||||
game = packet.createGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasJoinGame()) {
|
||||
game = packet.joinGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasLeaveGame()) {
|
||||
if (packet.leaveGame.user.uuid == user.uuid) {
|
||||
game = null;
|
||||
gameSignal.emit(null);
|
||||
} else {
|
||||
game = packet.leaveGame.game;
|
||||
gameSignal.emit(game);
|
||||
}
|
||||
} else if (packet.hasStartGame()) {
|
||||
game = packet.startGame.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasRoomList()) {
|
||||
listRoomSignal.emit(packet.roomList.rooms);
|
||||
} else if (packet.hasRoom()) {
|
||||
room = packet.room.room;
|
||||
roomSignal.emit(room);
|
||||
} else if (packet.hasGameEvent()) {
|
||||
gameEventSignal.emit(packet.gameEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import ru.m.tankz.render.item.EagleRenderItem;
|
||||
import ru.m.tankz.render.item.IRenderItem;
|
||||
import ru.m.tankz.render.item.TankRenderItem;
|
||||
import ru.m.tankz.Type;
|
||||
using ru.m.display.DisplayObjectContainerExtender;
|
||||
|
||||
class Render extends SpriteView implements IRender {
|
||||
|
||||
@@ -51,18 +52,18 @@ class Render extends SpriteView implements IRender {
|
||||
}
|
||||
|
||||
private function set_config(value:Config):Config {
|
||||
return config = value;
|
||||
config = value;
|
||||
setContentSize(config.mapWidth, config.mapHeight);
|
||||
drawBackground();
|
||||
return config;
|
||||
}
|
||||
|
||||
private function drawBackground():Void {
|
||||
var width = config.map.cellWidth * config.map.gridWidth;
|
||||
var height = config.map.cellHeight * config.map.gridHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
g.clear();
|
||||
g.beginFill(0x000000);
|
||||
g.drawRect(0, 0, width, height);
|
||||
g.drawRect(0, 0, config.mapWidth, config.mapHeight);
|
||||
g.endFill();
|
||||
setContentSize(width, height);
|
||||
}
|
||||
|
||||
public function draw():Void {
|
||||
@@ -117,10 +118,11 @@ class Render extends SpriteView implements IRender {
|
||||
case SPAWN(TANK(id, rect, playerId, info)):
|
||||
var item = new TankRenderItem(rect);
|
||||
var tankConfig = config.getTank(info.type);
|
||||
item.color = config.getColor(playerId);
|
||||
item.color = info.color;
|
||||
item.skin = tankConfig.skin;
|
||||
item.hits = info.hits;
|
||||
item.bonus = info.bonus;
|
||||
item.name = info.name == null ? "" : info.name;
|
||||
items.set(id, item);
|
||||
entryLayer.addChild(item.view);
|
||||
item.update();
|
||||
@@ -224,6 +226,9 @@ class Render extends SpriteView implements IRender {
|
||||
item.destroy();
|
||||
case _:
|
||||
}
|
||||
#if cpp
|
||||
flash.Lib.current.stage.invalidate();
|
||||
#end
|
||||
}
|
||||
|
||||
private function playAnimate(point:Point, animate:OnceAnimate):Promise<Dynamic> {
|
||||
@@ -233,7 +238,7 @@ class Render extends SpriteView implements IRender {
|
||||
return animate.play().then(function(animate:Animate):Void {
|
||||
// ToDo: clean animates on reset
|
||||
if (upperLayer.contains(animate)) {
|
||||
upperLayer.removeChild(animate);
|
||||
upperLayer.removeChildSafety(animate);
|
||||
}
|
||||
animate.dispose();
|
||||
});
|
||||
@@ -248,6 +253,6 @@ class Render extends SpriteView implements IRender {
|
||||
view.x = point.x - view.width / 2;
|
||||
view.y = point.y - view.height / 2;
|
||||
upperLayer.addChild(view.content);
|
||||
Timer.delay(function() upperLayer.removeChild(view.content), 1000);
|
||||
Timer.delay(function() upperLayer.removeChildSafety(view.content), 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import ru.m.geom.Rectangle;
|
||||
class RenderItem implements IRenderItem {
|
||||
|
||||
public var view(get, null):DisplayObject;
|
||||
public var content(get, null):DisplayObject;
|
||||
|
||||
public var rect(default, null):Rectangle;
|
||||
|
||||
@@ -18,14 +19,24 @@ class RenderItem implements IRenderItem {
|
||||
throw "Not Implemented";
|
||||
}
|
||||
|
||||
private function get_content():DisplayObject {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function move(position:Position):Void {
|
||||
view.x = rect.x = position.x;
|
||||
view.y = rect.y = position.y;
|
||||
if (position.direction != null) {
|
||||
rect.direction = position.direction;
|
||||
view.rotation = rect.direction.angle;
|
||||
view.x = rect.x - rect.width * (rect.direction.x + 1) / 2 + rect.width * (rect.direction.y + 1) / 2 + 0.5 * rect.width;
|
||||
view.y = rect.y - rect.height * (rect.direction.x + 1) / 2 - rect.height * (rect.direction.y + 1) / 2 + 1.5 * rect.height;
|
||||
if (content != null) {
|
||||
content.rotation = rect.direction.angle;
|
||||
content.x = - rect.width * (rect.direction.x + 1) / 2 + rect.width * (rect.direction.y + 1) / 2 + 0.5 * rect.width;
|
||||
content.y = - rect.height * (rect.direction.x + 1) / 2 - rect.height * (rect.direction.y + 1) / 2 + 1.5 * rect.height;
|
||||
} else {
|
||||
view.rotation = rect.direction.angle;
|
||||
view.x = rect.x - rect.width * (rect.direction.x + 1) / 2 + rect.width * (rect.direction.y + 1) / 2 + 0.5 * rect.width;
|
||||
view.y = rect.y - rect.height * (rect.direction.x + 1) / 2 - rect.height * (rect.direction.y + 1) / 2 + 1.5 * rect.height;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package ru.m.tankz.render.item;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
import flash.display.DisplayObject;
|
||||
import flash.display.Sprite;
|
||||
import flash.display.BitmapData;
|
||||
import flash.text.TextField;
|
||||
import flash.text.TextFieldAutoSize;
|
||||
import flash.text.TextFormat;
|
||||
import haxework.color.Color;
|
||||
import haxework.text.BitmapTextField;
|
||||
import haxework.view.utils.BitmapUtil;
|
||||
import openfl.Assets;
|
||||
import ru.m.animate.Animate;
|
||||
@@ -16,11 +20,13 @@ class TankRenderItem extends BitmapRenderItem {
|
||||
public var bonus(default, set):Bool;
|
||||
public var moves(default, set):Bool;
|
||||
public var protect(default, set):Bool;
|
||||
public var name(default, set):String;
|
||||
|
||||
private var container:Sprite;
|
||||
private var images:Array<BitmapData>;
|
||||
private var frame:Int;
|
||||
private var protectView:Animate;
|
||||
private var nameView:TextField;
|
||||
|
||||
public function new(rect:Rectangle) {
|
||||
super(rect);
|
||||
@@ -29,13 +35,29 @@ class TankRenderItem extends BitmapRenderItem {
|
||||
protectView = AnimateBundle.tankProtect();
|
||||
protectView.visible = false;
|
||||
container.addChild(protectView);
|
||||
nameView = buildNameView();
|
||||
container.addChild(nameView);
|
||||
move(rect.position);
|
||||
}
|
||||
|
||||
private function buildNameView():TextField {
|
||||
var result = new BitmapTextField();
|
||||
result.defaultTextFormat = new TextFormat(Style.fontFamily, 10, 0xffffff);
|
||||
result.embedFonts = Style.fontEmbed;
|
||||
result.autoSize = TextFieldAutoSize.LEFT;
|
||||
result.shadowColor = 0x000000;
|
||||
result.stroke = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
override private function get_view():DisplayObject {
|
||||
return container;
|
||||
}
|
||||
|
||||
override private function get_content():DisplayObject {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private function redraw():Void {
|
||||
var image1 = Assets.getBitmapData('resources/image/tank/${skin}-0.png');
|
||||
var image2 = Assets.getBitmapData('resources/image/tank/${skin}-1.png');
|
||||
@@ -105,6 +127,16 @@ class TankRenderItem extends BitmapRenderItem {
|
||||
return moves;
|
||||
}
|
||||
|
||||
private function set_name(value:String):String {
|
||||
if (name != value) {
|
||||
name = value;
|
||||
nameView.text = name;
|
||||
nameView.x = (rect.width - nameView.width) / 2;
|
||||
nameView.y = -nameView.height / 2; //(rect.height - nameView.height) / 2;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
super.update();
|
||||
if (moves) {
|
||||
|
||||
@@ -2,12 +2,12 @@ package ru.m.tankz.storage;
|
||||
|
||||
import haxework.storage.SharedObjectStorage;
|
||||
|
||||
class MultiplayerStorage extends SharedObjectStorage {
|
||||
class NetworkStorage extends SharedObjectStorage {
|
||||
|
||||
public var user(get, set):User;
|
||||
|
||||
public function new() {
|
||||
super("multiplayer");
|
||||
super("network");
|
||||
}
|
||||
|
||||
private inline function get_user():User {
|
||||
@@ -1,22 +1,34 @@
|
||||
package ru.m.tankz.view;
|
||||
|
||||
import ru.m.tankz.game.IGame;
|
||||
import flash.events.KeyboardEvent;
|
||||
import flash.ui.Keyboard;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.view.ButtonView;
|
||||
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.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 switcher:FrameSwitcher;
|
||||
@:provide var soundManager:SoundManager;
|
||||
@:provide var game:IGame;
|
||||
|
||||
public function init():Void {
|
||||
resources.text.put('version', '${Const.VERSION}');
|
||||
switcher = this;
|
||||
onSwitch.connect(onFrameSwitch);
|
||||
switcher = switcherView;
|
||||
network.stateSignal.connect(onConnectionState);
|
||||
onConnectionState(network.state);
|
||||
}
|
||||
|
||||
public function launch():Void {
|
||||
@@ -24,17 +36,50 @@ import ru.m.tankz.sound.SoundManager;
|
||||
content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||
switch event.keyCode {
|
||||
case Keyboard.ESCAPE:
|
||||
change(StartFrame.ID);
|
||||
switcher.change(StartFrame.ID);
|
||||
case Keyboard.M:
|
||||
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 {
|
||||
if (frame.id == StartFrame.ID) {
|
||||
soundManager.stopAll();
|
||||
private function onConnectionState(state:ConnectionState):Void {
|
||||
L.d("ClientView", 'onConnectionState: ${state}');
|
||||
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,21 +1,51 @@
|
||||
---
|
||||
$type: haxework.view.frame.FrameSwitcher
|
||||
geometry.size.stretch: true
|
||||
skinId: dark
|
||||
views:
|
||||
- id: start
|
||||
$type: ru.m.tankz.view.StartFrame
|
||||
- id: level
|
||||
$type: ru.m.tankz.view.LevelFrame
|
||||
- id: game
|
||||
$type: ru.m.tankz.view.GameFrame
|
||||
- id: result
|
||||
$type: ru.m.tankz.view.ResultFrame
|
||||
- id: settings
|
||||
$type: ru.m.tankz.view.SettingsFrame
|
||||
- id: record
|
||||
$type: ru.m.tankz.view.RecordFrame
|
||||
- id: game_list
|
||||
$type: ru.m.tankz.view.network.GameListFrame
|
||||
- id: game_room
|
||||
$type: ru.m.tankz.view.network.GameRoomFrame
|
||||
- $type: haxework.view.frame.FrameSwitcher
|
||||
id: switcher
|
||||
geometry.size.stretch: true
|
||||
skinId: dark
|
||||
views:
|
||||
- id: start
|
||||
$type: ru.m.tankz.view.StartFrame
|
||||
- id: level
|
||||
$type: ru.m.tankz.view.LevelFrame
|
||||
- id: game
|
||||
$type: ru.m.tankz.view.GameFrame
|
||||
- id: result
|
||||
$type: ru.m.tankz.view.ResultFrame
|
||||
- id: settings
|
||||
$type: ru.m.tankz.view.SettingsFrame
|
||||
- id: record
|
||||
$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 haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.record.GamePlayer;
|
||||
import ru.m.tankz.game.record.GameRecord;
|
||||
import ru.m.tankz.game.record.GameRecorder;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.storage.RecordStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.game.GameView;
|
||||
|
||||
@:template class GameFrame extends VGroupView implements GameListener {
|
||||
@@ -28,28 +24,14 @@ import ru.m.tankz.view.game.GameView;
|
||||
@:provide var soundManager:SoundManager;
|
||||
@:provide var state:GameState;
|
||||
@:provide var record:GameRecord;
|
||||
@:provide("result") var result:GameState;
|
||||
@:provide("next") var nextState:GameState;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var gameStorage:GameStorage;
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
|
||||
private var game:IGame;
|
||||
private var runner:GameRunner;
|
||||
private var recorder:GameRecorder;
|
||||
private var player:GamePlayer;
|
||||
@:provide var game:IGame;
|
||||
|
||||
public function onShow():Void {
|
||||
if (record != null) {
|
||||
play(record);
|
||||
record = null;
|
||||
} else {
|
||||
start(state);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildGame(state:GameState):Void {
|
||||
gameView.type = state.type;
|
||||
game = new Game(state);
|
||||
gameView.type = game.type;
|
||||
soundManager.config = game.config;
|
||||
gameView.render.config = game.config;
|
||||
game.connect(gameView.render);
|
||||
@@ -57,28 +39,11 @@ import ru.m.tankz.view.game.GameView;
|
||||
if (gameView.panel != null) {
|
||||
game.connect(gameView.panel);
|
||||
}
|
||||
}
|
||||
|
||||
private function start(state:GameState):Void {
|
||||
buildGame(state);
|
||||
game.connect(this);
|
||||
recorder = new GameRecorder();
|
||||
game.connect(recorder);
|
||||
runner = new GameRunner(game);
|
||||
runner.start(state);
|
||||
}
|
||||
|
||||
private function play(record:GameRecord):Void {
|
||||
buildGame(record.state);
|
||||
player = new GamePlayer(game, record);
|
||||
player.start();
|
||||
game.start();
|
||||
}
|
||||
|
||||
private function stop():Void {
|
||||
if (runner != null) {
|
||||
runner.dispose();
|
||||
runner = null;
|
||||
}
|
||||
if (game != null) {
|
||||
game.dispose();
|
||||
game = null;
|
||||
@@ -88,19 +53,17 @@ import ru.m.tankz.view.game.GameView;
|
||||
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.COMPLETE(state, _):
|
||||
// ToDo:
|
||||
recordStorage.save(recorder.record);
|
||||
result = state;
|
||||
this.state = switch runner.next() {
|
||||
case GameEvent.COMPLETE(state, winner):
|
||||
this.state = state;
|
||||
nextState = switch next(winner) {
|
||||
case Some(s):
|
||||
// ToDo:
|
||||
var progress = gameStorage.get(game.type);
|
||||
progress.completeLevel(result.levelId, result.presetId);
|
||||
progress.completeLevel(state.levelId, state.presetId);
|
||||
gameStorage.set(progress);
|
||||
s;
|
||||
case None:
|
||||
new GameState(state.type, state.presetId, 0);
|
||||
null;
|
||||
}
|
||||
stop();
|
||||
switcher.change(ResultFrame.ID);
|
||||
@@ -108,8 +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 {
|
||||
stop();
|
||||
soundManager.stopAll();
|
||||
}
|
||||
|
||||
public function close():Void {
|
||||
|
||||
@@ -5,10 +5,3 @@ views:
|
||||
views:
|
||||
- id: game
|
||||
$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.config.Config;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.local.LocalGame;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.popup.LevelPopup;
|
||||
@@ -19,6 +21,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
@:view var levels:DataView<LevelId, ButtonView>;
|
||||
|
||||
@:provide var state:GameState;
|
||||
@:provide var game:IGame;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var levelBundle:ILevelBundle;
|
||||
@:provide var storage:GameStorage;
|
||||
@@ -30,9 +33,11 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
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.presetId = preset.id;
|
||||
state.controls = control.values;
|
||||
game = new LocalGame(state);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
|
||||
@@ -57,8 +62,9 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
levelPopup.setData(
|
||||
level,
|
||||
state.config.presets,
|
||||
state.config.controls,
|
||||
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
|
||||
+onItemSelect: $this:onLevelSelect
|
||||
geometry.padding: 10
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:switcher.change('start')
|
||||
|
||||
@@ -25,8 +25,4 @@ import ru.m.tankz.storage.RecordStorage;
|
||||
private function onRecordSelect(item:IListItemView<GameRecordInfo>):Void {
|
||||
|
||||
}
|
||||
|
||||
private function close() {
|
||||
switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,3 @@ views:
|
||||
scroll:
|
||||
$type: haxework.view.list.VScrollBarView
|
||||
skinId: scroll
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:close()
|
||||
|
||||
@@ -5,40 +5,44 @@ import haxework.view.DataView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.view.common.LifeView;
|
||||
|
||||
@:template class ResultFrame extends VGroupView {
|
||||
public static var ID(default, never):String = "result";
|
||||
|
||||
@:view("result") var resultView:DataView<PlayerState, LifeView>;
|
||||
@:view("next") var nextButton:ButtonView;
|
||||
@:view("level") var levelLabel:LabelView;
|
||||
|
||||
@:provide var frames:FrameSwitcher;
|
||||
@:provide var state:GameState;
|
||||
@:provide("result") var resultState:GameState;
|
||||
@:provide("next") var nextState:GameState;
|
||||
@:provide var game:IGame;
|
||||
|
||||
private function playerViewFactory(index:Int, player:PlayerState) {
|
||||
var view = new LifeView();
|
||||
var playerConfig = resultState.config.getPlayer(player.id);
|
||||
var playerConfig = state.config.getPlayer(player.id);
|
||||
var tankType = playerConfig.tanks[0].type;
|
||||
var tankConfig = resultState.config.getTank(tankType);
|
||||
var tankConfig = state.config.getTank(tankType);
|
||||
view.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||
view.color = resultState.getPlayerColor(player.id);
|
||||
view.color = state.getPlayerColor(player.id);
|
||||
view.life = player.frags;
|
||||
view.score = player.score;
|
||||
return view;
|
||||
}
|
||||
|
||||
public function onShow() {
|
||||
resultView.data = Lambda.array(resultState.players);
|
||||
levelLabel.text = 'Level ${resultState.levelId}';
|
||||
nextButton.visible = state != null;
|
||||
resultView.data = Lambda.array(state.players);
|
||||
levelLabel.text = 'Level ${state.levelId}';
|
||||
}
|
||||
|
||||
private function next() {
|
||||
frames.change(GameFrame.ID);
|
||||
if (nextState != null) {
|
||||
game = new GameRunner(nextState);
|
||||
frames.change(GameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function close() {
|
||||
|
||||
@@ -14,16 +14,3 @@ views:
|
||||
$type: haxework.view.layout.VerticalLayout
|
||||
hAlign: right
|
||||
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;
|
||||
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.VGroupView;
|
||||
|
||||
@:template class SettingsFrame extends VGroupView {
|
||||
|
||||
public static var ID(default, never):String = "settings";
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
}
|
||||
|
||||
@@ -13,9 +13,3 @@ views:
|
||||
controlIndex: 0
|
||||
- $type: ru.m.tankz.view.settings.SettingsEditor
|
||||
controlIndex: 1
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:switcher.change('start')
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package ru.m.tankz.view;
|
||||
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.Type.GameType;
|
||||
import ru.m.tankz.view.network.RoomListFrame;
|
||||
import ru.m.tankz.view.network.RoomFrame;
|
||||
import ru.m.tankz.view.popup.FontPopup;
|
||||
import ru.m.tankz.view.popup.LoginPopup;
|
||||
|
||||
@@ -14,54 +14,34 @@ import ru.m.tankz.view.popup.LoginPopup;
|
||||
|
||||
public static var ID(default, never):String = "start";
|
||||
|
||||
@:view var username:LabelView;
|
||||
@:view("login") var loginButton:ButtonView;
|
||||
@:view("logout") var logoutButton:ButtonView;
|
||||
@:view("network") var networkButton:ButtonView;
|
||||
|
||||
@:provide var state:GameState;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
private var fontPopup:FontPopup;
|
||||
|
||||
public function onShow():Void {
|
||||
onConnectionState(network.state);
|
||||
network.stateSignal.connect(onConnectionState);
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.stateSignal.disconnect(onConnectionState);
|
||||
}
|
||||
|
||||
private function onConnectionState(state:ConnectionState):Void {
|
||||
trace("state", state);
|
||||
setUser(switch state {
|
||||
case ONLINE(user): user;
|
||||
case _: null;
|
||||
});
|
||||
}
|
||||
|
||||
private function setUser(value:User):Void {
|
||||
username.text = value == null ? "" : value.name;
|
||||
loginButton.visible = value == null;
|
||||
logoutButton.visible = value != null;
|
||||
networkButton.disabled = value == null;
|
||||
}
|
||||
|
||||
private function startGame(type:GameType):Void {
|
||||
state = new GameState(type);
|
||||
switcher.change(LevelFrame.ID);
|
||||
}
|
||||
|
||||
private function login():Void {
|
||||
LoginPopup.instance.show().then(function(user:User):Void {
|
||||
L.d("Login", 'user: $user');
|
||||
});
|
||||
}
|
||||
|
||||
private function logout():Void {
|
||||
network.logout();
|
||||
private function 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 {
|
||||
|
||||
@@ -28,31 +28,9 @@ views:
|
||||
- id: network
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button
|
||||
+onPress: $code:switcher.change('game_list')
|
||||
+onPress: $code:startNetwork()
|
||||
text: Network
|
||||
disabled: true
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- id: settings
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.settings
|
||||
+onPress: $code:switcher.change('settings')
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.width: 100%
|
||||
- id: username
|
||||
$type: haxework.view.LabelView
|
||||
skinId: text
|
||||
geometry.margin.right: 10
|
||||
- id: login
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.login
|
||||
+onPress: $code:login()
|
||||
- id: logout
|
||||
$type: haxework.view.ButtonView
|
||||
skinId: button.logout
|
||||
+onPress: $code:logout()
|
||||
visible: false
|
||||
#disabled: true
|
||||
- $type: haxework.view.LabelView
|
||||
geometry.hAlign: right
|
||||
geometry.vAlign: top
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.view.common;
|
||||
|
||||
import ru.m.tankz.game.record.GamePlayer;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.LabelView;
|
||||
@@ -18,7 +20,7 @@ import ru.m.tankz.storage.RecordStorage;
|
||||
|
||||
@:provide var recordStorage:RecordStorage;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var record:GameRecord;
|
||||
@:provide var game:IGame;
|
||||
|
||||
private function set_data(value:GameRecordInfo):GameRecordInfo {
|
||||
if (data != value) {
|
||||
@@ -32,7 +34,8 @@ import ru.m.tankz.storage.RecordStorage;
|
||||
}
|
||||
|
||||
private function play():Void {
|
||||
record = recordStorage.read(data.id);
|
||||
var record = recordStorage.read(data.id);
|
||||
game = new GamePlayer(record);
|
||||
switcher.change(GameFrame.ID);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
geometry.padding: 5
|
||||
geometry.size.height: 100%
|
||||
layout.margin: 5
|
||||
layout.hAlign: right
|
||||
layout.hAlign: left
|
||||
views:
|
||||
- id: level
|
||||
$type: haxework.view.LabelView
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.list.ListView;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
@:template class GameItemView extends HGroupView implements IListItemView<GameProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):GameProto;
|
||||
|
||||
@:view var label:LabelView;
|
||||
|
||||
private function set_data(value:GameProto):GameProto {
|
||||
data = value;
|
||||
label.text = '[${value.creator.name}] ${value.type} ${value.level} (${value.players.length})';
|
||||
return data;
|
||||
}
|
||||
|
||||
public static function factory():GameItemView {
|
||||
return new GameItemView();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 48
|
||||
views:
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.stretch: true
|
||||
skinId: text.box
|
||||
@@ -1,54 +0,0 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.list.VListView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
|
||||
@:template class GameListFrame extends VGroupView {
|
||||
public static inline var ID = "game_list";
|
||||
|
||||
@:view var games:VListView<GameProto>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
public function onShow():Void {
|
||||
network.listGameSignal.connect(onGameList);
|
||||
network.gameSignal.connect(onGame);
|
||||
network.stateSignal.connect(onState);
|
||||
network.listGame();
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.listGameSignal.disconnect(onGameList);
|
||||
network.gameSignal.disconnect(onGame);
|
||||
network.stateSignal.disconnect(onState);
|
||||
}
|
||||
|
||||
private function create():Void {
|
||||
network.createGame("classic", 0);
|
||||
}
|
||||
|
||||
private function onState(state:ConnectionState):Void {
|
||||
switch state {
|
||||
case ONLINE(_):
|
||||
case _: switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function onGameList(data:Array<GameProto>):Void {
|
||||
games.data = data;
|
||||
}
|
||||
|
||||
private function onGame(game:GameProto):Void {
|
||||
if (game != null) {
|
||||
switcher.change(GameRoomFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
private function selectGame(game:GameProto):Void {
|
||||
network.joinGame(game.id);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package ru.m.tankz.view.network;
|
||||
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.list.VListView;
|
||||
import haxework.view.TextView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
|
||||
@:template class GameRoomFrame extends VGroupView {
|
||||
|
||||
public static inline var ID = "game_room";
|
||||
|
||||
@:view var info:TextView;
|
||||
@:view var players:VListView<UserProto>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var network:NetworkManager;
|
||||
|
||||
private function refresh(game:GameProto):Void {
|
||||
if (game != null) {
|
||||
info.text = '[${game.creator.name}] ${game.type} ${game.level} (${game.players.length})';
|
||||
players.data = game.players;
|
||||
} else {
|
||||
Timer.delay(function() switcher.change(GameListFrame.ID), 1);
|
||||
}
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
refresh(network.game);
|
||||
network.gameSignal.connect(onGame);
|
||||
network.stateSignal.connect(onState);
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
network.gameSignal.disconnect(onGame);
|
||||
network.stateSignal.disconnect(onState);
|
||||
network.leaveGame();
|
||||
}
|
||||
|
||||
private function onGame(game:GameProto):Void {
|
||||
refresh(game);
|
||||
}
|
||||
|
||||
private function onState(state:ConnectionState):Void {
|
||||
switch state {
|
||||
case ONLINE(_):
|
||||
case _: switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
---
|
||||
views:
|
||||
- $type: haxework.view.VGroupView
|
||||
skinId: container
|
||||
geometry.padding: 20
|
||||
views:
|
||||
- id: header
|
||||
$type: haxework.view.LabelView
|
||||
geometry.margin.bottom: 20
|
||||
skinId: text.header
|
||||
text: Game Room
|
||||
- id: info
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.width: 100%
|
||||
skinId: text.box
|
||||
- id: players
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $code:ru.m.tankz.view.network.PlayerItemView.factory
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:network.leaveGame()
|
||||
@@ -1,24 +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 PlayerItemView extends HGroupView implements IListItemView<UserProto> {
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):UserProto;
|
||||
|
||||
@:view var label:LabelView;
|
||||
|
||||
private function set_data(value:UserProto):UserProto {
|
||||
data = value;
|
||||
label.text = '${value.name}';
|
||||
return data;
|
||||
}
|
||||
|
||||
public static function factory():PlayerItemView {
|
||||
return new PlayerItemView();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
---
|
||||
geometry.size.width: 100%
|
||||
geometry.size.height: 48
|
||||
views:
|
||||
- id: label
|
||||
$type: haxework.view.LabelView
|
||||
geometry.size.stretch: true
|
||||
skinId: text.box
|
||||
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.network.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,5 @@ views:
|
||||
- id: games
|
||||
$type: haxework.view.list.VListView
|
||||
geometry.size.stretch: true
|
||||
factory: $code:ru.m.tankz.view.network.GameItemView.factory
|
||||
factory: $code:ru.m.tankz.view.network.RoomItemView.factory
|
||||
geometry.margin: 10
|
||||
+onItemSelect: $code:function(item) selectGame(item.data)
|
||||
- $type: haxework.view.HGroupView
|
||||
skinId: panel
|
||||
views:
|
||||
- $type: haxework.view.ButtonView
|
||||
skinId: button.close
|
||||
+onPress: $code:switcher.change('start')
|
||||
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()
|
||||
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;
|
||||
|
||||
import haxework.view.ToggleButtonView;
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.DataView;
|
||||
import haxework.view.LabelView;
|
||||
@@ -7,19 +8,28 @@ import haxework.view.popup.PopupView;
|
||||
import ru.m.tankz.config.Config;
|
||||
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 progress:GameProgress;
|
||||
|
||||
@:view var name:LabelView;
|
||||
@: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.progress = progress;
|
||||
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
||||
presetsView.data = presets;
|
||||
control = controls[0];
|
||||
controlsView.data = controls;
|
||||
}
|
||||
|
||||
private function presetViewFactory(index:Int, value:GamePreset):ButtonView {
|
||||
@@ -32,7 +42,22 @@ import ru.m.tankz.game.GameProgress;
|
||||
|
||||
private function onPresetSelect(value:GamePreset):Void {
|
||||
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')
|
||||
- $type: haxework.view.SpriteView
|
||||
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
|
||||
$type: haxework.view.DataView
|
||||
factory: $this:presetViewFactory
|
||||
|
||||
@@ -42,12 +42,14 @@ view:
|
||||
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%
|
||||
|
||||
@@ -20,7 +20,7 @@ class Timer {
|
||||
try {
|
||||
run();
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
trace(haxework.log.BaseLogger.LoggerUtil.printError(error));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package ru.m.connect;
|
||||
|
||||
import Reflect;
|
||||
import haxe.io.Bytes;
|
||||
import haxe.io.BytesBuffer;
|
||||
import haxe.io.BytesInput;
|
||||
import protohx.Message;
|
||||
|
||||
|
||||
class PacketQueue<P:Message> {
|
||||
|
||||
public var packetClass(default, null):Class<P>;
|
||||
|
||||
private var bytesBuff:Bytes;
|
||||
private var buffer:BytesBuffer;
|
||||
private var msgs:List<P>;
|
||||
|
||||
public function new(packetClass:Class<P>) {
|
||||
this.packetClass = packetClass;
|
||||
msgs = new List<P>();
|
||||
buffer = new BytesBuffer();
|
||||
}
|
||||
|
||||
public inline function hasMsg():Bool {
|
||||
@@ -30,47 +31,41 @@ class PacketQueue<P:Message> {
|
||||
msgs.add(msg);
|
||||
}
|
||||
|
||||
public function addBytes(bytes:Bytes):Void {
|
||||
if (bytes == null) {
|
||||
return;
|
||||
}
|
||||
if (bytesBuff == null) {
|
||||
bytesBuff = bytes;
|
||||
} else {
|
||||
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);
|
||||
private function readPackage():Null<P> {
|
||||
var bytes = buffer.getBytes();
|
||||
var input = new BytesInput(bytes);
|
||||
input.bigEndian = false;
|
||||
if (input.length > 1) {
|
||||
var packetSize = input.readUInt16();
|
||||
if (input.length >= packetSize + 2) {
|
||||
var packet:P = Type.createInstance(packetClass, []);
|
||||
packet.mergeFrom(msgBytes);
|
||||
addMsg(packet);
|
||||
available -= packetSize;
|
||||
} else {
|
||||
break;
|
||||
try {
|
||||
packet.mergeFrom(input.read(packetSize));
|
||||
buffer = new BytesBuffer();
|
||||
buffer.add(input.read(input.length - (packetSize + 2)));
|
||||
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 {
|
||||
throw "Wrong available: " + available;
|
||||
buffer = new BytesBuffer();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,10 @@ class DesktopConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
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();
|
||||
|
||||
@@ -74,14 +74,10 @@ class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
}
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
try {
|
||||
var data = new flash.utils.ByteArray();
|
||||
socket.readBytes(data);
|
||||
var bytes = Bytes.ofData(data);
|
||||
pushData(bytes);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
}
|
||||
var data = new flash.utils.ByteArray();
|
||||
socket.readBytes(data);
|
||||
var bytes = Bytes.ofData(data);
|
||||
pushData(bytes);
|
||||
}
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
|
||||
@@ -21,7 +21,7 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
this.port = port;
|
||||
connected = false;
|
||||
}
|
||||
|
||||
|
||||
public static function isSecured():Bool {
|
||||
return Browser.location.protocol == "https:";
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
}
|
||||
|
||||
private function onError(event:Dynamic):Void {
|
||||
socket.close();
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
}
|
||||
@@ -67,18 +67,25 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
}
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
var packet:I = null;
|
||||
try {
|
||||
var bytes = Bytes.ofData(event.data);
|
||||
var packet:I = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
receive(packet);
|
||||
packet = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
} 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 {
|
||||
super.send(packet);
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.send(bytes.getData());
|
||||
if (connected) {
|
||||
super.send(packet);
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.send(bytes.getData());
|
||||
} else {
|
||||
L.w("Connection", "closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,16 +7,22 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
public var socket(default, null):Socket;
|
||||
|
||||
private var sendQueue:Array<O>;
|
||||
private var timer:Timer;
|
||||
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(i);
|
||||
this.socket = socket;
|
||||
socket.setFastSend(true);
|
||||
socket.output.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 {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
@@ -26,4 +32,25 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
opened = false;
|
||||
}
|
||||
|
||||
override private function _send(packet:O):Void {
|
||||
override private function sendPacket(packet:O):Void {
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import haxe.Timer;
|
||||
import ru.m.Timer;
|
||||
import ru.m.geom.Direction;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.core.Tank;
|
||||
@@ -12,7 +12,7 @@ class BotControl extends Control {
|
||||
private var tank(get, null):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 {
|
||||
|
||||
@@ -4,13 +4,13 @@ import ru.m.geom.Direction;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class BotHelper {
|
||||
|
||||
public static function findEagle(team:TeamId, handler:IGame):Null<Eagle> {
|
||||
for (entity in handler.engine.entities) {
|
||||
public static function findEagle(team:TeamId, engine:IEngine):Null<Eagle> {
|
||||
for (entity in engine.entities) {
|
||||
switch (EntityTypeResolver.of(entity)) {
|
||||
case EntityType.EAGLE(eagle):
|
||||
if (eagle.team != team) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import haxe.Timer;
|
||||
import ru.m.Timer;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.core.Tank;
|
||||
@@ -56,7 +56,7 @@ class HardBotControl extends BotControl {
|
||||
}
|
||||
var enemy:Tank = null;
|
||||
var distance:Float = Math.POSITIVE_INFINITY;
|
||||
for (entity in handler.engine.entities.iterator()) {
|
||||
for (entity in engine.entities.iterator()) {
|
||||
switch EntityTypeResolver.of(entity) {
|
||||
case TANK(t):
|
||||
if (t.playerId.team != tank.playerId.team) {
|
||||
@@ -82,7 +82,7 @@ class HardBotControl extends BotControl {
|
||||
}
|
||||
|
||||
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) {
|
||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import haxe.Timer;
|
||||
import ru.m.Timer;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
@@ -45,7 +45,7 @@ class StupidBotControl extends BotControl {
|
||||
|
||||
private function calcTurn():Void {
|
||||
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) {
|
||||
turn(BotHelper.getDirectionTo(tank, eagle));
|
||||
} else {
|
||||
|
||||
@@ -3,17 +3,6 @@ package ru.m.tankz.bundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
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 {
|
||||
public function get(type:GameType):Config;
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ typedef TankConfig = {
|
||||
var speed:Float;
|
||||
var bullet:BulletConfig;
|
||||
var bullets:Int;
|
||||
var hits:Int;
|
||||
var skin:String;
|
||||
@:optinal var hits:Int;
|
||||
@:optinal var upgrade:TankType;
|
||||
@:optinal var downgrade:TankType;
|
||||
@:optinal var score:Int;
|
||||
@@ -75,7 +75,7 @@ typedef PlayerConfig = {
|
||||
@:optional var bonus:Float;
|
||||
@:optional var protect:Float;
|
||||
@:optional var life:Int;
|
||||
@:optional var color:Color;
|
||||
@:optional var color:Null<Int>;
|
||||
@:optional var control:String;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ typedef TeamConfig = {
|
||||
@:optional var eagle:EagleConfig;
|
||||
@:optional var life:Int;
|
||||
@:optional var spawnInterval:Int;
|
||||
@:optional var color:Color;
|
||||
@:optional var color:Null<Int>;
|
||||
}
|
||||
|
||||
typedef GamePreset = {
|
||||
@@ -105,6 +105,30 @@ typedef LevelConfig = {
|
||||
@:optional var points:Array<SpawnPoint>;
|
||||
}
|
||||
|
||||
typedef PlayerControl = {
|
||||
var playerId:PlayerId;
|
||||
var control:String;
|
||||
@:optional var color:Null<Int>;
|
||||
@:optional var name:String;
|
||||
}
|
||||
|
||||
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 {
|
||||
public var type(default, null):String;
|
||||
public var game(default, null):GameConfig;
|
||||
@@ -112,8 +136,11 @@ class Config {
|
||||
public var bricks(default, null):Array<BrickConfig>;
|
||||
public var tanks(default, null):Array<TankConfig>;
|
||||
public var presets(default, null):Array<GamePreset>;
|
||||
public var controls(default, null):Array<ControlPreset>;
|
||||
public var points(default, null):Array<SpawnPoint>;
|
||||
public var bonuses(default, null):Array<BonusConfig>;
|
||||
public var mapWidth(get, null):Float;
|
||||
public var mapHeight(get, null):Float;
|
||||
|
||||
private var brickMap:Map<BrickType, BrickConfig>;
|
||||
private var brickMapByIndex:Map<Int, BrickConfig>;
|
||||
@@ -123,12 +150,17 @@ class Config {
|
||||
private var teamsMap:Map<TeamId, TeamConfig>;
|
||||
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(
|
||||
type:String,
|
||||
game:GameConfig,
|
||||
map:MapConfig,
|
||||
bricks:Array<BrickConfig>,
|
||||
presets:Array<GamePreset>,
|
||||
controls:Array<ControlPreset>,
|
||||
points:Array<SpawnPoint>,
|
||||
tanks:Array<TankConfig>,
|
||||
bonuses:Array<BonusConfig>
|
||||
@@ -138,6 +170,7 @@ class Config {
|
||||
this.map = map;
|
||||
this.bricks = bricks;
|
||||
this.presets = presets;
|
||||
this.controls = controls;
|
||||
this.points = points;
|
||||
this.tanks = tanks;
|
||||
this.bonuses = bonuses;
|
||||
@@ -175,6 +208,14 @@ class Config {
|
||||
}
|
||||
}
|
||||
|
||||
private function get_mapWidth():Float {
|
||||
return map.cellWidth * map.gridWidth;
|
||||
}
|
||||
|
||||
private function get_mapHeight():Float {
|
||||
return map.cellHeight * map.gridHeight;
|
||||
}
|
||||
|
||||
public function getBrick(type:BrickType):BrickConfig {
|
||||
return brickMap.get(type);
|
||||
}
|
||||
@@ -203,16 +244,16 @@ class Config {
|
||||
return playersMap.get(playerId);
|
||||
}
|
||||
|
||||
public function getColor(playerId:PlayerId):Color {
|
||||
public function getColor(playerId:PlayerId):Null<Color> {
|
||||
var player = getPlayer(playerId);
|
||||
if (player != null && !player.color.zero) {
|
||||
if (player != null && player.color != null) {
|
||||
return player.color;
|
||||
}
|
||||
var team = getTeam(playerId.team);
|
||||
if (team != null) {
|
||||
return team.color;
|
||||
}
|
||||
return -1;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getPoint(teamId:TeamId, type:String, index:Int = -1):SpawnPoint {
|
||||
|
||||
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;
|
||||
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.geom.Direction;
|
||||
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.Type;
|
||||
|
||||
@@ -17,17 +18,20 @@ class Control {
|
||||
public var playerId(default, null):PlayerId;
|
||||
public var tankId(default, default):Int;
|
||||
private var handler:IGame;
|
||||
private var engine:IEngine;
|
||||
|
||||
public function new(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.engine = engine;
|
||||
}
|
||||
|
||||
public function action(action:TankAction):Void {
|
||||
if (tankId != 0 && handler != null) {
|
||||
if (tankId > -1 && handler != null) {
|
||||
handler.gameEventSignal.emit(GameEvent.ACTION(tankId, action));
|
||||
}
|
||||
}
|
||||
@@ -41,5 +45,7 @@ class Control {
|
||||
public function dispose():Void {
|
||||
stop();
|
||||
handler = null;
|
||||
engine = null;
|
||||
tankId = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class Eagle extends Entity {
|
||||
public var config(default, null):EagleConfig;
|
||||
public var color(default, default):Color;
|
||||
public var death(default, default):Bool;
|
||||
public var protect(default, null):Modificator;
|
||||
public var protect(default, default):Bool;
|
||||
|
||||
public var score(get, null):Int;
|
||||
|
||||
@@ -20,15 +20,10 @@ class Eagle extends Entity {
|
||||
this.team = team;
|
||||
this.config = config;
|
||||
this.death = false;
|
||||
this.protect = new Modificator(id);
|
||||
this.protect = false;
|
||||
}
|
||||
|
||||
private inline function get_score():Int {
|
||||
return config != null ? config.score : 0;
|
||||
}
|
||||
|
||||
override public function dispose() {
|
||||
super.dispose();
|
||||
protect.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,4 @@ class Entity {
|
||||
public function toString():String {
|
||||
return '$type($id)';
|
||||
}
|
||||
|
||||
public function dispose() {}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package ru.m.tankz.core;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.Timer;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -10,15 +10,16 @@ class Tank extends MobileEntity {
|
||||
public var playerId(default, null):PlayerId;
|
||||
public var config(default, set):TankConfig;
|
||||
public var color(default, default):Color;
|
||||
public var name(default, default):String;
|
||||
public var hits(default, default):Int;
|
||||
public var bonus(default, default):Bool;
|
||||
public var protect(default, null):Modificator;
|
||||
public var freezing(default, null):Modificator;
|
||||
public var protect(default, default):Bool;
|
||||
public var freezing(default, default):Bool;
|
||||
|
||||
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) {
|
||||
super(id, rect, config.speed, Direction.RIGHT);
|
||||
this.protect = new Modificator(id);
|
||||
this.freezing = new Modificator(id);
|
||||
this.protect = false;
|
||||
this.freezing = false;
|
||||
this.playerId = playerId;
|
||||
this.config = config;
|
||||
this.layer = 1;
|
||||
@@ -35,14 +36,8 @@ class Tank extends MobileEntity {
|
||||
}
|
||||
|
||||
override public function move(direction:Direction):Void {
|
||||
if (!freezing.active) {
|
||||
if (!freezing) {
|
||||
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.Point;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.core.Bullet;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.core.MobileEntity;
|
||||
@@ -17,18 +16,17 @@ import ru.m.tankz.map.LevelMap;
|
||||
|
||||
public var config(default, default):Config;
|
||||
public var map(default, null):LevelMap;
|
||||
|
||||
public var allEntities(default, null):Map<Int, Entity>;
|
||||
public var ticker(default, null):ITicker;
|
||||
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) {
|
||||
this.config = config;
|
||||
map = new LevelMap(config.map);
|
||||
allEntities = new Map();
|
||||
entities = new Map();
|
||||
time = Date.now().getTime();
|
||||
ticker = new Ticker();
|
||||
}
|
||||
|
||||
public function getEntity<T:Entity>(entityId:Int):T {
|
||||
@@ -68,9 +66,7 @@ import ru.m.tankz.map.LevelMap;
|
||||
}
|
||||
|
||||
public function update():Void {
|
||||
var newTime:Float = Date.now().getTime();
|
||||
var d:Float = newTime - time;
|
||||
time = newTime;
|
||||
var d:Float = ticker.tick();
|
||||
|
||||
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
||||
var entityType:EntityType = EntityTypeResolver.of(ent);
|
||||
@@ -140,9 +136,6 @@ import ru.m.tankz.map.LevelMap;
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
for (entity in allEntities) {
|
||||
entity.dispose();
|
||||
}
|
||||
allEntities = new Map();
|
||||
entities = new Map();
|
||||
//map = null;
|
||||
|
||||
@@ -12,6 +12,7 @@ interface IEngine {
|
||||
public var entities(default, null):Map<Int, Entity>;
|
||||
public var config(default, default):Config;
|
||||
public var map(default, null):LevelMap;
|
||||
public var ticker(default, null):ITicker;
|
||||
|
||||
public var spawnSignal(default, null):Signal1<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;
|
||||
|
||||
import haxework.color.Color;
|
||||
import ru.m.geom.Direction;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.geom.Rectangle;
|
||||
@@ -23,7 +24,7 @@ class EntityBuilder {
|
||||
|
||||
public function new(config:Config) {
|
||||
this.config = config;
|
||||
this.entityId = 0;
|
||||
this.entityId = 1;
|
||||
}
|
||||
|
||||
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
|
||||
@@ -43,17 +44,15 @@ class EntityBuilder {
|
||||
return eagle;
|
||||
}
|
||||
|
||||
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, bonusOff:Bool = false):Tank {
|
||||
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, color:Null<Color>, name:String, bonusOff:Bool = false):Tank {
|
||||
var playerConfig = config.getPlayer(playerId);
|
||||
var tankConfig = config.getTank(type);
|
||||
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;
|
||||
tank.name = name;
|
||||
if (!bonusOff) {
|
||||
tank.bonus = Math.random() < playerConfig.bonus;
|
||||
}
|
||||
if (playerConfig.protect > 0) {
|
||||
tank.protect.on(playerConfig.protect);
|
||||
}
|
||||
return tank;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.core.EntityType.EntityTypeResolver;
|
||||
import ru.m.tankz.core.Entity;
|
||||
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.map.Brick;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.map.Brick;
|
||||
import ru.m.tankz.map.LevelMap;
|
||||
|
||||
class EventUtil {
|
||||
@@ -20,29 +22,51 @@ class EventUtil {
|
||||
type: item.config.type,
|
||||
}
|
||||
});
|
||||
return GameEvent.SPAWN(BRICK(bricks));
|
||||
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 GameEvent.SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team));
|
||||
return SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team));
|
||||
}
|
||||
|
||||
public static function buildTankSpawn(tank:Tank):GameEvent {
|
||||
return GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
||||
return SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
|
||||
type:tank.config.type,
|
||||
hits:tank.hits,
|
||||
bonus:tank.bonus
|
||||
bonus:tank.bonus,
|
||||
color:tank.color,
|
||||
name:tank.name,
|
||||
}));
|
||||
}
|
||||
|
||||
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):
|
||||
GameEvent.MOVE(EAGLE(entity.id, entity.rect.position));
|
||||
MOVE(EAGLE(entity.id, entity.rect.position));
|
||||
case TANK(tank):
|
||||
GameEvent.MOVE(TANK(entity.id, entity.rect.position));
|
||||
MOVE(TANK(entity.id, entity.rect.position));
|
||||
case BULLET(bullet):
|
||||
GameEvent.MOVE(BULLET(entity.id, entity.rect.position));
|
||||
MOVE(BULLET(entity.id, entity.rect.position));
|
||||
case _:
|
||||
null;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,12 @@ import ru.m.geom.Point;
|
||||
import ru.m.geom.Position;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.control.NoneControlFactory;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
@@ -20,36 +23,37 @@ import ru.m.tankz.Type;
|
||||
public var type(default, null):GameType;
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):IEngine;
|
||||
public var winner(default, null):Null<TeamId>;
|
||||
|
||||
public var state(default, null):GameState;
|
||||
public var engine(default, null):IEngine;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
public var pause(default, set):Bool;
|
||||
|
||||
private var builder:EntityBuilder;
|
||||
private var controls:Map<String, Control>;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
public function new(state:GameState) {
|
||||
this.type = state.type;
|
||||
this.state = state;
|
||||
this.teams = new Map();
|
||||
this.config = configBundle.get(type);
|
||||
this.engine = new Engine(config);
|
||||
this.builder = new EntityBuilder(config);
|
||||
this.controlFactory = new NoneControlFactory();
|
||||
this.pause = false;
|
||||
this.controls = new Map();
|
||||
connect(this);
|
||||
prepare(state);
|
||||
}
|
||||
|
||||
private function prepare(state:GameState):Void {
|
||||
var level:LevelConfig = state.level;
|
||||
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
|
||||
engine.map.setData(level.data);
|
||||
for (teamConfig in state.preset.teams) {
|
||||
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
||||
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
|
||||
teams[team.id] = team;
|
||||
private function set_pause(value:Bool):Bool {
|
||||
if (pause != value) {
|
||||
pause = value;
|
||||
changePause(pause);
|
||||
}
|
||||
return pause;
|
||||
}
|
||||
|
||||
private function changePause(value:Bool):Void {}
|
||||
|
||||
private function applyPosition(entity:Entity, position:Position):Void {
|
||||
entity.rect.center = new Point(position.x, position.y);
|
||||
if (position.direction != null) {
|
||||
@@ -67,24 +71,73 @@ import ru.m.tankz.Type;
|
||||
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.START(state):
|
||||
case START(state):
|
||||
this.state = state;
|
||||
case GameEvent.COMPLETE(state, winnerId):
|
||||
case COMPLETE(state, winnerId):
|
||||
this.state = state;
|
||||
this.winner = winnerId;
|
||||
case GameEvent.SPAWN(EAGLE(id, rect, teamId)):
|
||||
case SPAWN(EAGLE(id, rect, teamId)):
|
||||
var team = getTeam(teamId);
|
||||
team.eagleId = id;
|
||||
case GameEvent.SPAWN(TANK(id, rect, playerId, info)):
|
||||
var player = getPlayer(playerId);
|
||||
player.tankId = id;
|
||||
player.state.tank = info.type;
|
||||
case SPAWN(TANK(id, rect, playerId, info)):
|
||||
if (controls.exists(playerId)) {
|
||||
var control = controls[playerId];
|
||||
control.tankId = id;
|
||||
control.start();
|
||||
}
|
||||
case DESTROY(TANK(id, shot)):
|
||||
for (control in controls) {
|
||||
if (control.tankId == id) {
|
||||
control.stop();
|
||||
control.tankId = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
if (playerControl.color != null) {
|
||||
player.state.color = playerControl.color;
|
||||
}
|
||||
if (playerControl.name != null) {
|
||||
player.state.name = playerControl.name;
|
||||
}
|
||||
}
|
||||
var controlType:Controller = AController.fromString(playerControl != null ? playerControl.control : player.config.control);
|
||||
var control = controlFactory.build(player.id, controlType);
|
||||
if (control != null) {
|
||||
controls[player.id] = control;
|
||||
control.bind(this, engine);
|
||||
} else {
|
||||
// ToDo: remove player
|
||||
player.state.life = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
for (control in controls) {
|
||||
control.dispose();
|
||||
}
|
||||
controls = new Map();
|
||||
gameEventSignal.dispose();
|
||||
engine.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxework.color.Color;
|
||||
import ru.m.geom.Position;
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.tankz.control.Control;
|
||||
@@ -9,6 +10,8 @@ typedef TankInfo = {
|
||||
var type:TankType;
|
||||
var hits:Int;
|
||||
var bonus:Bool;
|
||||
var color:Color;
|
||||
@:optional var name:String;
|
||||
}
|
||||
|
||||
typedef BrickInfo = {
|
||||
|
||||
@@ -1,121 +1,119 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxe.ds.Option;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.core.Bonus;
|
||||
import ru.m.tankz.core.Bullet;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.core.Tank;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.Spawner;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.Timer;
|
||||
|
||||
class GameRunner implements EngineListener implements GameListener {
|
||||
@:provide var controlFactory:IControlFactory;
|
||||
|
||||
private var game(default, null):IGame;
|
||||
private var gameEventSignal(get, null):Signal<GameEvent>;
|
||||
class GameRunner extends Game implements EngineListener {
|
||||
private var timer:Timer;
|
||||
private var builder:EntityBuilder;
|
||||
|
||||
public function new(game:IGame) {
|
||||
this.game = game;
|
||||
this.builder = new EntityBuilder(this.game.config);
|
||||
this.game.connect(this);
|
||||
this.game.engine.connect(this);
|
||||
public function new(state:GameState) {
|
||||
super(state);
|
||||
this.builder = new EntityBuilder(config);
|
||||
this.engine = new Engine(config);
|
||||
this.engine.connect(this);
|
||||
}
|
||||
|
||||
private inline function get_gameEventSignal():Signal<GameEvent> {
|
||||
return game.gameEventSignal;
|
||||
override function changePause(value:Bool):Void {
|
||||
if (engine != null) {
|
||||
if (value) {
|
||||
engine.ticker.stop();
|
||||
} else {
|
||||
engine.ticker.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function update():Void {
|
||||
game.engine.update();
|
||||
engine.update();
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
override public function dispose():Void {
|
||||
super.dispose();
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
game.disconnect(this);
|
||||
game.engine.disconnect(this);
|
||||
engine.dispose();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
override public function start():Void {
|
||||
super.start();
|
||||
engine.map.setData(state.level.data);
|
||||
for (team in teams.iterator()) {
|
||||
team.spawner.runner = spawn;
|
||||
for (player in team.players.iterator()) {
|
||||
if (team.tryRespawn(player.id)) {
|
||||
team.spawner.push(player.id);
|
||||
team.spawner.push(engine.ticker, player.id);
|
||||
}
|
||||
}
|
||||
if (team.config.eagle != null) {
|
||||
var point = team.spawner.getPoint("eagle");
|
||||
var eagle = builder.buildEagle(point, team.id);
|
||||
game.engine.spawn(eagle);
|
||||
engine.spawn(eagle);
|
||||
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
||||
eagle.protect.connect(onEagleProtectChange);
|
||||
}
|
||||
}
|
||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(game.engine.map));
|
||||
gameEventSignal.emit(GameEvent.START(state));
|
||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map));
|
||||
gameEventSignal.emit(START(state));
|
||||
//for (i in 0...10) spawnBonus();
|
||||
}
|
||||
|
||||
public function next():Option<GameState> {
|
||||
for (rule in game.config.game.complete) {
|
||||
if (rule.team != null && rule.team != game.winner) {
|
||||
return Option.None;
|
||||
}
|
||||
}
|
||||
var level = game.state.levelId + 1;
|
||||
if (level >= game.config.game.levels) level = 0;
|
||||
return Option.Some(new GameState(game.type, game.state.presetId, level, game.state));
|
||||
}
|
||||
|
||||
private function spawn(task:SpawnTask):Void {
|
||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType);
|
||||
game.engine.spawn(tank);
|
||||
var player = getPlayer(task.playerId);
|
||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color, player.state.name);
|
||||
engine.spawn(tank);
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
tank.protect.connect(onTankProtectChange);
|
||||
tank.freezing.connect(onTankFreezingChange);
|
||||
if (player.config.protect > 0) {
|
||||
protectTank(tank, player.config.protect);
|
||||
}
|
||||
}
|
||||
|
||||
private function onEagleProtectChange(id:Int, state:Bool):Void {
|
||||
gameEventSignal.emit(GameEvent.CHANGE(EAGLE_PROTECT(id, state)));
|
||||
private function protectEagle(eagle:Eagle, duration:Float):Void {
|
||||
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 {
|
||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_PROTECT(id, state)));
|
||||
private function protectTank(tank:Tank, duration:Float):Void {
|
||||
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 {
|
||||
gameEventSignal.emit(GameEvent.CHANGE(TANK_FREEZE(id, state)));
|
||||
private function freezeTank(tank:Tank, duration:Float):Void {
|
||||
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 {
|
||||
var actives:Array<TeamId> = [];
|
||||
for (team in game.teams.iterator()) {
|
||||
for (team in teams.iterator()) {
|
||||
if (team.isAlive) {
|
||||
if (team.eagleId > 0) {
|
||||
if (!cast(game.engine.entities[team.eagleId], Eagle).death) {
|
||||
if (team.eagleId > -1) {
|
||||
if (!cast(engine.entities[team.eagleId], Eagle).death) {
|
||||
actives.push(team.id);
|
||||
}
|
||||
} else {
|
||||
@@ -132,15 +130,9 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
private function complete(winner:TeamId):Void {
|
||||
for (team in game.teams.iterator()) {
|
||||
for (player in team.players) {
|
||||
player.control.action(TankAction.STOP);
|
||||
player.control.dispose();
|
||||
}
|
||||
}
|
||||
Timer.delay(function() {
|
||||
gameEventSignal.emit(GameEvent.COMPLETE(game.state, winner));
|
||||
}, 5000);
|
||||
gameEventSignal.emit(COMPLETE(state, winner));
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
public function onSpawn(entity:EntityType):Void {
|
||||
@@ -155,18 +147,19 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
switch entity {
|
||||
case EntityType.TANK(tank):
|
||||
var control = game.getPlayer(tank.playerId).control;
|
||||
if (control != null) control.onCollision(with);
|
||||
if (controls.exists(tank.playerId)) {
|
||||
controls[tank.playerId].onCollision(with);
|
||||
}
|
||||
case _:
|
||||
}
|
||||
switch [entity, with] {
|
||||
@@ -180,46 +173,46 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
tank.rect.lean(cell.rect);
|
||||
emitTankMove(tank);
|
||||
case [BULLET(bullet), BULLET(other_bullet)]:
|
||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(other_bullet.id)));
|
||||
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||
gameEventSignal.emit(DESTROY(BULLET(other_bullet.id)));
|
||||
case [BULLET(bullet), CELL(cell)]:
|
||||
bullet.rect.lean(cell.rect);
|
||||
gameEventSignal.emit(GameEvent.HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
||||
gameEventSignal.emit(HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
|
||||
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||
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)]*/:
|
||||
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
|
||||
} else {
|
||||
if (!tank.protect.active) {
|
||||
if (!tank.protect) {
|
||||
if (tank.hits > 0) {
|
||||
tank.hits--;
|
||||
if (tank.bonus) {
|
||||
tank.bonus = false;
|
||||
spawnBonus();
|
||||
}
|
||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
||||
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||
emitTankChange(tank);
|
||||
} else if (tank.config.downgrade != null) {
|
||||
tank.config = game.config.getTank(tank.config.downgrade);
|
||||
gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet))));
|
||||
tank.config = config.getTank(tank.config.downgrade);
|
||||
gameEventSignal.emit(HIT(TANK(tank.id, buildShot(bullet))));
|
||||
emitTankChange(tank);
|
||||
} else {
|
||||
var score = tank.config.score;
|
||||
if (tank.playerId.team == bullet.playerId.team) {
|
||||
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)]:
|
||||
if (!eagle.protect.active) {
|
||||
gameEventSignal.emit(GameEvent.DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
||||
if (!eagle.protect) {
|
||||
gameEventSignal.emit(DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
||||
}
|
||||
gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id)));
|
||||
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||
case _:
|
||||
}
|
||||
}
|
||||
@@ -229,7 +222,7 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
case TANK(tank):
|
||||
emitTankMove(tank);
|
||||
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 _:
|
||||
}
|
||||
}
|
||||
@@ -238,15 +231,15 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
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 = {
|
||||
x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)),
|
||||
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
|
||||
x: Math.floor(Math.random() * (engine.map.gridWidth - 1)),
|
||||
y: Math.floor(Math.random() * (engine.map.gridHeight - 1)),
|
||||
direction: "right",
|
||||
}
|
||||
var bonus = builder.buildBonus(point, type);
|
||||
game.engine.spawn(bonus);
|
||||
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
|
||||
engine.spawn(bonus);
|
||||
gameEventSignal.emit(EventUtil.buildBonusSpawn(bonus));
|
||||
}
|
||||
|
||||
private inline function alienTank(team:TeamId):Tank->Bool {
|
||||
@@ -260,35 +253,35 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
case "star":
|
||||
upgradeTank(tank);
|
||||
case "grenade":
|
||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||
gameEventSignal.emit(GameEvent.DESTROY(TANK(t.id, {tankId: tank.id})));
|
||||
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||
gameEventSignal.emit(DESTROY(TANK(t.id, {tankId: tank.id})));
|
||||
}
|
||||
case "helmet":
|
||||
tank.protect.on(bonus.config.duration);
|
||||
protectTank(tank, bonus.config.duration);
|
||||
case "clock":
|
||||
for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||
t.freezing.on(bonus.config.duration);
|
||||
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||
freezeTank(t, bonus.config.duration);
|
||||
t.stop();
|
||||
gameEventSignal.emit(GameEvent.STOP(TANK(t.id)));
|
||||
gameEventSignal.emit(STOP(TANK(t.id)));
|
||||
}
|
||||
case "shovel":
|
||||
// ToDo: protect eagle/area
|
||||
var team:Team = game.teams[tank.playerId.team];
|
||||
var team:Team = teams[tank.playerId.team];
|
||||
if (team.eagleId > 0) {
|
||||
var eagle:Eagle = cast(game.engine.entities[team.eagleId], Eagle);
|
||||
eagle.protect.on(bonus.config.duration);
|
||||
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
|
||||
protectEagle(eagle, bonus.config.duration);
|
||||
}
|
||||
case "gun":
|
||||
upgradeTank(tank, 5);
|
||||
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 {
|
||||
if (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 {
|
||||
tank.hits++;
|
||||
@@ -297,70 +290,76 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
private function changeScore(playerId:PlayerId, score:Int):Void {
|
||||
var player = game.getPlayer(playerId);
|
||||
var team = game.getTeam(playerId.team);
|
||||
var player = getPlayer(playerId);
|
||||
var team = getTeam(playerId.team);
|
||||
player.state.score += score;
|
||||
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
||||
gameEventSignal.emit(GameEvent.CHANGE(TEAM_SCORE(playerId.team, game.state.getTeamScore(team.id))));
|
||||
gameEventSignal.emit(CHANGE(PLAYER_SCORE(playerId, player.state.score)));
|
||||
gameEventSignal.emit(CHANGE(TEAM_SCORE(playerId.team, state.getTeamScore(team.id))));
|
||||
}
|
||||
|
||||
private function changeLife(playerId:PlayerId, life:Int):Void {
|
||||
var player = game.getPlayer(playerId);
|
||||
var player = getPlayer(playerId);
|
||||
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 {
|
||||
var team = game.getTeam(teamId);
|
||||
var team = getTeam(teamId);
|
||||
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 {
|
||||
case ACTION(_): return;
|
||||
case _:
|
||||
}
|
||||
}
|
||||
super.onGameEvent(event);
|
||||
switch event {
|
||||
case GameEvent.START(_):
|
||||
timer = new Timer(10);
|
||||
case START(_):
|
||||
engine.ticker.start();
|
||||
timer = new Timer(30);
|
||||
timer.run = update;
|
||||
case GameEvent.COMPLETE(_, _):
|
||||
case COMPLETE(_, _):
|
||||
engine.ticker.stop();
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
case GameEvent.ACTION(tankId, SHOT):
|
||||
var tank:Tank = cast game.engine.entities.get(tankId);
|
||||
var player = game.getPlayer(tank.playerId);
|
||||
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
|
||||
case ACTION(tankId, SHOT):
|
||||
var tank:Tank = cast engine.entities.get(tankId);
|
||||
var player = getPlayer(tank.playerId);
|
||||
if (!tank.freezing && player.bullets < tank.config.bullets) {
|
||||
var rect = tank.rect;
|
||||
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
||||
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
|
||||
bullet.tank = tank;
|
||||
bullet.move(bullet.rect.direction);
|
||||
game.engine.spawn(bullet);
|
||||
gameEventSignal.emit(GameEvent.SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing)));
|
||||
engine.spawn(bullet);
|
||||
gameEventSignal.emit(EventUtil.buildBulletSpawn(bullet));
|
||||
}
|
||||
case GameEvent.ACTION(tankId, MOVE(direction)):
|
||||
game.engine.move(tankId, direction);
|
||||
case GameEvent.ACTION(tankId, STOP):
|
||||
gameEventSignal.emit(GameEvent.STOP(TANK(tankId)));
|
||||
game.engine.stop(tankId);
|
||||
case GameEvent.SPAWN(TANK(_, _, playerId, _)):
|
||||
game.getPlayer(playerId).control.start();
|
||||
case GameEvent.SPAWN(BULLET(_, _, playerId, _)):
|
||||
game.getPlayer(playerId).bullets++;
|
||||
case GameEvent.DESTROY(EAGLE(id, shot)):
|
||||
var eagle:Eagle = game.engine.getEntity(id);
|
||||
case ACTION(tankId, MOVE(direction)):
|
||||
engine.move(tankId, direction);
|
||||
case ACTION(tankId, STOP):
|
||||
gameEventSignal.emit(STOP(TANK(tankId)));
|
||||
engine.stop(tankId);
|
||||
case SPAWN(BULLET(_, _, playerId, _)):
|
||||
getPlayer(playerId).bullets++;
|
||||
case DESTROY(EAGLE(id, shot)):
|
||||
var eagle:Eagle = engine.getEntity(id);
|
||||
eagle.death = true;
|
||||
if (shot.score != 0) {
|
||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
||||
var tank:Tank = engine.getEntity(shot.tankId);
|
||||
changeScore(tank.playerId, shot.score);
|
||||
}
|
||||
checkComplete();
|
||||
case GameEvent.DESTROY(TANK(id, shot)):
|
||||
var tank:Tank = game.engine.getEntity(id);
|
||||
var team = game.getTeam(tank.playerId.team);
|
||||
var player = game.getPlayer(tank.playerId);
|
||||
player.control.stop();
|
||||
player.tankId = 0; //ToDo: ?
|
||||
case DESTROY(TANK(id, shot)):
|
||||
var tank:Tank = engine.getEntity(id);
|
||||
var team = getTeam(tank.playerId.team);
|
||||
var player = getPlayer(tank.playerId);
|
||||
player.tankId = -1;
|
||||
team.onDestroy(player.id);
|
||||
if (player.state.life > 0) {
|
||||
changeLife(player.id, -1);
|
||||
@@ -369,7 +368,7 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
var respawn:Bool = team.tryRespawn(player.id);
|
||||
if (respawn) {
|
||||
team.spawner.push(player.id);
|
||||
team.spawner.push(engine.ticker, player.id);
|
||||
}
|
||||
if (!team.isAlive) {
|
||||
checkComplete();
|
||||
@@ -378,44 +377,44 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
spawnBonus();
|
||||
}
|
||||
if (shot.score != 0) {
|
||||
var shooterTank:Tank = game.engine.getEntity(shot.tankId);
|
||||
var shooterTank:Tank = engine.getEntity(shot.tankId);
|
||||
changeScore(shooterTank.playerId, shot.score);
|
||||
}
|
||||
game.engine.destroy(id);
|
||||
case GameEvent.DESTROY(BONUS(id, shot)):
|
||||
var bonus:Bonus = game.engine.getEntity(id);
|
||||
var tank:Tank = game.engine.getEntity(shot.tankId);
|
||||
engine.destroy(id);
|
||||
case DESTROY(BONUS(id, shot)):
|
||||
var bonus:Bonus = engine.getEntity(id);
|
||||
var tank:Tank = engine.getEntity(shot.tankId);
|
||||
applyBonus(tank, bonus);
|
||||
if (shot.score != 0) {
|
||||
changeScore(tank.playerId, shot.score);
|
||||
}
|
||||
game.engine.destroy(id);
|
||||
case GameEvent.DESTROY(BULLET(id)):
|
||||
var bullet:Bullet = game.engine.getEntity(id);
|
||||
var player = game.getPlayer(bullet.playerId);
|
||||
engine.destroy(id);
|
||||
case DESTROY(BULLET(id)):
|
||||
var bullet:Bullet = engine.getEntity(id);
|
||||
var player = getPlayer(bullet.playerId);
|
||||
player.bullets--;
|
||||
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
|
||||
// ToDo: move
|
||||
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) {
|
||||
if (cell.armor > 0) {
|
||||
var shot = buildShot(bullet);
|
||||
if (cell.armor == bullet.config.piercing) {
|
||||
game.engine.destroyCell(cell.cellX, cell.cellY);
|
||||
var brick = game.engine.map.getBrick(cell.position);
|
||||
gameEventSignal.emit(GameEvent.DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
||||
engine.destroyCell(cell.cellX, cell.cellY);
|
||||
var brick = engine.map.getBrick(cell.position);
|
||||
gameEventSignal.emit(DESTROY(CELL(brick.id, cell.cellX - brick.cellX * 2, cell.cellY - brick.cellY * 2, shot)));
|
||||
} 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) {
|
||||
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 _:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ class PlayerState extends State {
|
||||
public var id:PlayerId;
|
||||
public var tank:TankType;
|
||||
public var color:Color;
|
||||
public var name:String;
|
||||
public var life:Int;
|
||||
public var total:State;
|
||||
|
||||
@@ -72,6 +73,7 @@ class GameState {
|
||||
public var type:GameType;
|
||||
public var presetId:PresetId;
|
||||
public var levelId:LevelId;
|
||||
public var controls:Array<PlayerControl>;
|
||||
public var players:Map<String, PlayerState>;
|
||||
public var teams:Map<TeamId, TeamState>;
|
||||
public var preset(get, null):GamePreset;
|
||||
@@ -81,10 +83,12 @@ class GameState {
|
||||
@:provide static private var configBundle:IConfigBundle;
|
||||
@: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.presetId = presetId;
|
||||
this.levelId = levelId;
|
||||
//this.controls = controls == null ? config.controls[0].values : controls;
|
||||
this.controls = controls == null ? [] : controls;
|
||||
if (state == null) {
|
||||
this.teams = new Map();
|
||||
this.players = new Map();
|
||||
|
||||
@@ -2,16 +2,17 @@ package ru.m.tankz.game;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.engine.IEngine;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
interface IGame extends GameListener {
|
||||
public var type(default, null):GameType;
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):IEngine;
|
||||
public var winner(default, null):Null<TeamId>;
|
||||
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>;
|
||||
|
||||
@@ -24,6 +25,8 @@ interface IGame extends GameListener {
|
||||
public function getTeam(teamId:TeamId):Team;
|
||||
|
||||
public function getPlayer(playerId:PlayerId):Player;
|
||||
|
||||
public function start():Void;
|
||||
}
|
||||
|
||||
interface GameListener {
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class Player {
|
||||
public var config(default, null):PlayerConfig;
|
||||
public var id(default, null):PlayerId;
|
||||
public var tankId(default, set):Int;
|
||||
public var control(default, set):Control;
|
||||
public var isAlive(get, null):Bool;
|
||||
public var state(default, default):PlayerState;
|
||||
public var bullets(default, default):Int;
|
||||
@@ -17,31 +15,19 @@ class Player {
|
||||
public function new(teamId:TeamId, config:PlayerConfig, state:PlayerState = null) {
|
||||
this.config = config;
|
||||
this.id = new PlayerId(teamId, config.index);
|
||||
this.control = null;
|
||||
this.state = state == null ? new PlayerState(id) : state;
|
||||
this.state.reset();
|
||||
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
||||
this.bullets = 0;
|
||||
this.tankId = -1;
|
||||
}
|
||||
|
||||
private function set_tankId(value:Int):Int {
|
||||
tankId = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
}
|
||||
return tankId;
|
||||
}
|
||||
|
||||
private function set_control(value:Control):Control {
|
||||
if (control != null) control.dispose();
|
||||
control = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
}
|
||||
return control;
|
||||
}
|
||||
|
||||
private function get_isAlive():Bool {
|
||||
return tankId > 0 || state.life > 0;
|
||||
return tankId > -1 || state.life > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.engine.ITicker;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.Timer;
|
||||
|
||||
typedef SpawnTask = {
|
||||
var point:SpawnPoint;
|
||||
@@ -18,7 +18,6 @@ class Spawner {
|
||||
private var config:TeamConfig;
|
||||
private var points:Array<SpawnPoint>;
|
||||
private var queue:Array<SpawnTask>;
|
||||
private var timer:Timer;
|
||||
|
||||
private var indexedPoints:Map<Int, SpawnPoint>;
|
||||
private var anyPoints:Array<SpawnPoint>;
|
||||
@@ -61,7 +60,7 @@ class Spawner {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function push(playerId:PlayerId):Void {
|
||||
public function push(ticker:ITicker, playerId:PlayerId):Void {
|
||||
var point:SpawnPoint = null;
|
||||
if (indexedPoints.exists(playerId.index)) {
|
||||
point = indexedPoints.get(playerId.index);
|
||||
@@ -71,24 +70,16 @@ class Spawner {
|
||||
}
|
||||
if (point != null) {
|
||||
queue.push({playerId:playerId, point:point, tankType:getPlayerTank(playerId)});
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
private function run():Void {
|
||||
if (timer == null) {
|
||||
timer = new Timer(config.spawnInterval == null ? 500 : config.spawnInterval);
|
||||
timer.run = spawn;
|
||||
if (config.spawnInterval == null) {
|
||||
ticker.emit(spawn, 500);
|
||||
} else {
|
||||
ticker.emit(spawn, 500 + config.spawnInterval * queue.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function spawn():Void {
|
||||
if (queue.length == 0) {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
} else {
|
||||
if (queue.length > 0) {
|
||||
runner(queue.shift());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ class Team {
|
||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, state:TeamState = null) {
|
||||
this.id = config.id;
|
||||
this.config = config;
|
||||
this.active = 0;
|
||||
this.eagleId = -1;
|
||||
this.players = new Map();
|
||||
for (playerConfig in config.players) {
|
||||
var playerState = state == null ? null : state.players[playerConfig.index];
|
||||
|
||||
@@ -2,24 +2,25 @@ package ru.m.tankz.game.record;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.Lib;
|
||||
import ru.m.tankz.engine.Ticker;
|
||||
import ru.m.tankz.game.record.GameRecord;
|
||||
|
||||
class GamePlayer {
|
||||
class GamePlayer extends Game {
|
||||
|
||||
private var frame:Int;
|
||||
private var game:IGame;
|
||||
private var record:GameRecord;
|
||||
private var data:Array<EventItem>;
|
||||
private var ticker:Ticker;
|
||||
|
||||
public function new(game:IGame, record:GameRecord) {
|
||||
frame = 0;
|
||||
this.game = game;
|
||||
public function new(record:GameRecord) {
|
||||
super(record.state);
|
||||
this.record = record;
|
||||
this.data = null;
|
||||
this.ticker = new Ticker();
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
frame = 0;
|
||||
override public function start():Void {
|
||||
super.start();
|
||||
ticker.start();
|
||||
data = record.events.slice(0);
|
||||
Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||
}
|
||||
@@ -29,12 +30,11 @@ class GamePlayer {
|
||||
}
|
||||
|
||||
private function onEnterFrame(event:Event):Void {
|
||||
frame++;
|
||||
var events = 0;
|
||||
for (event in data) {
|
||||
if (event.frame <= frame) {
|
||||
if (event.time <= ticker.time) {
|
||||
events++;
|
||||
game.gameEventSignal.emit(event.event);
|
||||
gameEventSignal.emit(event.event);
|
||||
switch event.event {
|
||||
case GameEvent.COMPLETE(_, _):
|
||||
stop();
|
||||
|
||||
@@ -5,8 +5,8 @@ import com.hurlant.crypto.extra.UUID;
|
||||
import com.hurlant.crypto.prng.Random;
|
||||
|
||||
typedef EventItem = {
|
||||
frame:Int,
|
||||
event:GameEvent
|
||||
var time:Int;
|
||||
var event:GameEvent;
|
||||
}
|
||||
|
||||
class GameRecordInfo {
|
||||
|
||||
@@ -1,44 +1,33 @@
|
||||
package ru.m.tankz.game.record;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.Lib;
|
||||
import ru.m.tankz.engine.ITicker;
|
||||
import ru.m.tankz.engine.Ticker;
|
||||
import ru.m.tankz.game.IGame;
|
||||
|
||||
class GameRecorder implements GameListener {
|
||||
|
||||
private var frame:Int;
|
||||
public var record(default, null):GameRecord;
|
||||
|
||||
private var ticker:ITicker;
|
||||
|
||||
public function new() {
|
||||
frame = 0;
|
||||
ticker = new Ticker();
|
||||
record = new GameRecord();
|
||||
}
|
||||
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.START(state):
|
||||
ticker.start();
|
||||
record.info.type = state.type;
|
||||
record.info.presetId = state.presetId;
|
||||
record.info.levelId = state.levelId;
|
||||
start();
|
||||
record.info.date = Date.now();
|
||||
case GameEvent.COMPLETE(_, _):
|
||||
stop();
|
||||
ticker.stop();
|
||||
case _:
|
||||
}
|
||||
record.events.push({frame: frame, event: event});
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
frame = 0;
|
||||
record.info.date = Date.now();
|
||||
Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||
}
|
||||
|
||||
public function stop():Void {
|
||||
Lib.current.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
|
||||
}
|
||||
|
||||
private function onEnterFrame(event:Event):Void {
|
||||
frame++;
|
||||
ticker.tick();
|
||||
record.events.push({time: ticker.time, event: event});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,23 +2,14 @@ syntax = "proto3";
|
||||
|
||||
package ru.m.tankz.proto.core;
|
||||
|
||||
|
||||
message UserProto {
|
||||
string uuid = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
enum GameStateProto {
|
||||
READY = 0;
|
||||
STARTED = 1;
|
||||
ENDED = 2;
|
||||
}
|
||||
|
||||
message GameProto {
|
||||
int32 id = 1;
|
||||
string type = 2;
|
||||
int32 level = 3;
|
||||
UserProto creator = 4;
|
||||
repeated UserProto players = 5;
|
||||
GameStateProto state = 6;
|
||||
bool started = 4;
|
||||
}
|
||||
|
||||
@@ -2,27 +2,116 @@ syntax = "proto3";
|
||||
|
||||
package ru.m.tankz.proto.game;
|
||||
|
||||
|
||||
enum GameActionTypeProto {
|
||||
MOVE = 0;
|
||||
SHOT = 1;
|
||||
STOP = 2;
|
||||
message PositionProto {
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
}
|
||||
|
||||
enum GameChangeTypeProto {
|
||||
MOVED = 0;
|
||||
DESTROED = 1;
|
||||
MODIFIED = 2;
|
||||
APPEND = 3;
|
||||
DIRECTION = 4;
|
||||
message RectangleProto {
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
float width = 3;
|
||||
float height = 4;
|
||||
}
|
||||
|
||||
message GameChangeProto {
|
||||
GameChangeTypeProto type = 1;
|
||||
string entityType = 2;
|
||||
int32 entityId = 3;
|
||||
float x = 4;
|
||||
float y = 5;
|
||||
int32 directionX = 6;
|
||||
int32 directionY = 7;
|
||||
}
|
||||
message ShotProto {
|
||||
int32 tankId = 1;
|
||||
int32 bulletId = 2;
|
||||
int32 score = 3;
|
||||
}
|
||||
|
||||
message BrickProto {
|
||||
int32 gridX = 1;
|
||||
int32 gridY = 2;
|
||||
string type = 3;
|
||||
}
|
||||
|
||||
message EagleProto {
|
||||
string team = 1;
|
||||
}
|
||||
|
||||
message TankProto {
|
||||
string team = 1;
|
||||
int32 index = 2;
|
||||
string type = 3;
|
||||
int32 hits = 4;
|
||||
bool bonus = 5;
|
||||
int32 color = 6;
|
||||
string name = 7;
|
||||
}
|
||||
|
||||
message BulletProto {
|
||||
string team = 1;
|
||||
int32 index = 2;
|
||||
int32 piercing = 3;
|
||||
}
|
||||
|
||||
message BonusProto {
|
||||
string type = 1;
|
||||
}
|
||||
|
||||
message GameStateProto {
|
||||
string state = 1;
|
||||
}
|
||||
|
||||
message GameEventStartProto {
|
||||
GameStateProto state = 1;
|
||||
}
|
||||
|
||||
message GameEventCompleteProto {
|
||||
GameStateProto state = 1;
|
||||
}
|
||||
|
||||
message GameEventSpawnProto {
|
||||
int32 entityId = 1;
|
||||
RectangleProto rectangle = 2;
|
||||
oneof entity {
|
||||
EagleProto eagle = 3;
|
||||
TankProto tank = 4;
|
||||
BulletProto bullet = 5;
|
||||
BonusProto bonus = 6;
|
||||
BrickProto bricks = 7;
|
||||
}
|
||||
}
|
||||
|
||||
message GameEventMoveProto {
|
||||
int32 entityId = 1;
|
||||
PositionProto position = 2;
|
||||
}
|
||||
|
||||
message GameEventStopProto {
|
||||
int32 entityId = 1;
|
||||
}
|
||||
|
||||
message GameEventHitProto {
|
||||
int32 entityId = 1;
|
||||
ShotProto shot = 2;
|
||||
}
|
||||
|
||||
message GameEventDestroyProto {
|
||||
int32 entityId = 1;
|
||||
ShotProto shot = 2;
|
||||
}
|
||||
|
||||
message GameEventActionProto {
|
||||
int32 entityId = 1;
|
||||
oneof action {
|
||||
PositionProto move = 2;
|
||||
bool stop = 3;
|
||||
bool shot = 4;
|
||||
}
|
||||
}
|
||||
|
||||
message GameEventProto {
|
||||
int32 time = 1;
|
||||
oneof event {
|
||||
GameEventStartProto start = 2;
|
||||
GameEventCompleteProto complete = 3;
|
||||
GameEventSpawnProto spawn = 4;
|
||||
GameEventMoveProto move = 5;
|
||||
GameEventStopProto stop = 6;
|
||||
GameEventHitProto hit = 7;
|
||||
GameEventDestroyProto destroy = 8;
|
||||
GameEventActionProto action = 9;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,15 @@ syntax = "proto3";
|
||||
|
||||
import "core.proto";
|
||||
import "game.proto";
|
||||
import "room.proto";
|
||||
|
||||
package ru.m.tankz.proto.pack;
|
||||
|
||||
|
||||
message ErrorResponse {
|
||||
int32 code = 1;
|
||||
string message = 2;
|
||||
}
|
||||
|
||||
// Login
|
||||
message LoginRequest {
|
||||
string uuid = 1;
|
||||
string name = 2;
|
||||
@@ -21,76 +20,35 @@ message LoginResponse {
|
||||
ru.m.tankz.proto.core.UserProto user = 1;
|
||||
}
|
||||
|
||||
// Logout
|
||||
message LogoutRequest {}
|
||||
|
||||
message LogoutResponse {}
|
||||
|
||||
// List Game
|
||||
message ListGameRequest {}
|
||||
|
||||
message ListGameResponse {
|
||||
repeated ru.m.tankz.proto.core.GameProto games = 1;
|
||||
message GameEventRequest {
|
||||
ru.m.tankz.proto.game.GameEventProto event = 1;
|
||||
}
|
||||
|
||||
// Create Game
|
||||
message CreateGameRequest {
|
||||
string type = 1;
|
||||
int32 level = 2;
|
||||
message GameEventResponse {
|
||||
ru.m.tankz.proto.game.GameEventProto event = 1;
|
||||
}
|
||||
|
||||
message CreateGameResponse {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
}
|
||||
|
||||
// Join Game
|
||||
message JoinGameRequest {
|
||||
int32 game_id = 1;
|
||||
}
|
||||
|
||||
message JoinGameResponse {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
ru.m.tankz.proto.core.UserProto user = 2;
|
||||
}
|
||||
|
||||
// Leave Game
|
||||
message LeaveGameRequest {}
|
||||
|
||||
message LeaveGameResponse {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
ru.m.tankz.proto.core.UserProto user = 2;
|
||||
}
|
||||
|
||||
// Start Game
|
||||
message StartGameRequest {}
|
||||
|
||||
message StartGameResponse {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
}
|
||||
|
||||
// Request
|
||||
message Request {
|
||||
oneof content {
|
||||
LoginRequest login = 1;
|
||||
LogoutRequest logout = 2;
|
||||
ListGameRequest listGame = 3;
|
||||
CreateGameRequest createGame = 4;
|
||||
JoinGameRequest joinGame = 5;
|
||||
LeaveGameRequest leaveGame = 6;
|
||||
StartGameRequest startGame = 7;
|
||||
ru.m.tankz.proto.room.RoomRequest room = 3;
|
||||
ru.m.tankz.proto.room.RoomListRequest roomList = 4;
|
||||
GameEventRequest gameEvent = 6;
|
||||
}
|
||||
}
|
||||
|
||||
// Response
|
||||
message Response {
|
||||
oneof content {
|
||||
LoginResponse login = 1;
|
||||
LogoutResponse logout = 2;
|
||||
ListGameResponse listGame = 3;
|
||||
CreateGameResponse createGame = 4;
|
||||
JoinGameResponse joinGame = 5;
|
||||
LeaveGameResponse leaveGame = 6;
|
||||
StartGameResponse startGame = 7;
|
||||
ru.m.tankz.proto.room.RoomResponse room = 3;
|
||||
ru.m.tankz.proto.room.RoomListResponse roomList = 4;
|
||||
GameEventResponse gameEvent = 6;
|
||||
|
||||
ErrorResponse error = 999;
|
||||
}
|
||||
|
||||
64
src/common/proto/room.proto
Normal file
64
src/common/proto/room.proto
Normal file
@@ -0,0 +1,64 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "core.proto";
|
||||
|
||||
package ru.m.tankz.proto.room;
|
||||
|
||||
message SlotProto {
|
||||
string team = 3;
|
||||
int32 index = 4;
|
||||
}
|
||||
|
||||
message RoomSlotProto {
|
||||
SlotProto slot = 1;
|
||||
ru.m.tankz.proto.core.UserProto user = 2;
|
||||
}
|
||||
|
||||
message RoomProto {
|
||||
ru.m.tankz.proto.core.GameProto game = 1;
|
||||
ru.m.tankz.proto.core.UserProto creator = 2;
|
||||
repeated ru.m.tankz.proto.core.UserProto users = 3;
|
||||
repeated RoomSlotProto slots = 4;
|
||||
}
|
||||
|
||||
message CreateRequest {
|
||||
string type = 2;
|
||||
int32 level = 3;
|
||||
}
|
||||
|
||||
message JoinRequest {
|
||||
int32 gameId = 1;
|
||||
bool restore = 2;
|
||||
}
|
||||
|
||||
message LeaveRequest {
|
||||
}
|
||||
|
||||
message SlotRequest {
|
||||
SlotProto slot = 3;
|
||||
}
|
||||
|
||||
message StartRequest {
|
||||
}
|
||||
|
||||
message RoomRequest {
|
||||
oneof content {
|
||||
CreateRequest create = 1;
|
||||
JoinRequest join = 2;
|
||||
LeaveRequest leave = 3;
|
||||
SlotRequest slot = 4;
|
||||
StartRequest start = 5;
|
||||
}
|
||||
}
|
||||
|
||||
message RoomResponse {
|
||||
RoomProto room = 1;
|
||||
}
|
||||
|
||||
message RoomListRequest {
|
||||
bool subscribe = 1;
|
||||
}
|
||||
|
||||
message RoomListResponse {
|
||||
repeated RoomProto rooms = 1;
|
||||
}
|
||||
@@ -38,14 +38,15 @@ team:
|
||||
human: &team_human
|
||||
id: human
|
||||
players:
|
||||
- {<<: *human, index: 0, color: 0xFFFF00, control: human-0}
|
||||
- {<<: *human, index: 0, color: 0xFFFF00, control: 'null'}
|
||||
- {<<: *human, index: 1, color: 0x15C040, control: 'null'}
|
||||
eagle:
|
||||
score: 0
|
||||
|
||||
points:
|
||||
- {team: human, type: eagle, index: -1, direction: right, x: 12, y: 24}
|
||||
- {team: human, type: tank, index: 0, direction: top, x: 8, y: 24}
|
||||
#- {team: human, type: tank, index: 1, direction: top, x: 16, y: 24}
|
||||
- {team: human, type: tank, index: 1, direction: top, x: 16, y: 24}
|
||||
- {team: bot, type: tank, index: -1, direction: bottom, x: 0, y: 0}
|
||||
- {team: bot, type: tank, index: -2, direction: bottom, x: 12, y: 0}
|
||||
- {team: bot, type: tank, index: -3, direction: bottom, x: 24, y: 0}
|
||||
@@ -195,3 +196,20 @@ presets:
|
||||
- {<<: *bot, index: 3, control: bot-hard}
|
||||
- {<<: *bot, index: 4, control: bot-hard}
|
||||
- {<<: *bot, index: 5, control: bot-hard}
|
||||
|
||||
controls:
|
||||
- id: 0
|
||||
name: 1 Player
|
||||
values:
|
||||
- playerId: [human, 0]
|
||||
control: human-0
|
||||
name: Player 1
|
||||
- id: 1
|
||||
name: 2 Player
|
||||
values:
|
||||
- playerId: [human, 0]
|
||||
control: human-0
|
||||
name: Player 1
|
||||
- playerId: [human, 1]
|
||||
control: human-1
|
||||
name: Player 2
|
||||
|
||||
@@ -37,8 +37,6 @@ presets:
|
||||
teams:
|
||||
- id: alpha
|
||||
color: 0xFF4422
|
||||
players:
|
||||
- {<<: *player, index: 0, control: human-0}
|
||||
<<: *team
|
||||
- id: beta
|
||||
color: 0xFFD000
|
||||
@@ -91,3 +89,20 @@ tanks:
|
||||
skin: pc
|
||||
|
||||
bonuses: []
|
||||
|
||||
controls:
|
||||
- id: 0
|
||||
name: 1 Player
|
||||
values:
|
||||
- playerId: [alpha, 0]
|
||||
control: human-0
|
||||
name: Player 1
|
||||
- id: 1
|
||||
name: 2 Player
|
||||
values:
|
||||
- playerId: [alpha, 0]
|
||||
control: human-0
|
||||
name: Player 1
|
||||
- playerId: [beta, 0]
|
||||
control: human-1
|
||||
name: Player 2
|
||||
|
||||
@@ -41,7 +41,7 @@ team:
|
||||
id: radiant
|
||||
color: 0xff4422
|
||||
players:
|
||||
- {<<: *player-slow, index: 0, control: human-0, color: 0xff8866}
|
||||
- {<<: *player-slow, index: 0, control: bot-hard}
|
||||
- {<<: *player-fast, index: 1, control: bot-hard}
|
||||
- {<<: *player-slow, index: 2, control: bot-hard}
|
||||
- {<<: *player-fast, index: 3, control: bot-hard}
|
||||
@@ -114,3 +114,34 @@ bonuses:
|
||||
- {type: life}
|
||||
- {type: shovel, duration: 10}
|
||||
- {type: star}
|
||||
|
||||
controls:
|
||||
- id: 0
|
||||
name: 1 Player
|
||||
values:
|
||||
- playerId: [radiant, 0]
|
||||
control: human-0
|
||||
color: 0xff8866
|
||||
name: Player 1
|
||||
- id: 1
|
||||
name: 2 Player Coop
|
||||
values:
|
||||
- playerId: [radiant, 0]
|
||||
control: human-0
|
||||
color: 0xff8866
|
||||
name: Player 1
|
||||
- playerId: [radiant, 1]
|
||||
control: human-1
|
||||
color: 0xff8866
|
||||
name: Player 2
|
||||
- id: 2
|
||||
name: 2 Player VS
|
||||
values:
|
||||
- playerId: [radiant, 0]
|
||||
control: human-0
|
||||
color: 0xff8866
|
||||
name: Player 1
|
||||
- playerId: [dire, 0]
|
||||
control: human-1
|
||||
color: 0x4294ff
|
||||
name: Player 2
|
||||
|
||||
@@ -46,17 +46,15 @@ enum Brush {
|
||||
|
||||
override private function drawBackground():Void {
|
||||
super.drawBackground();
|
||||
var mapWidth = map.gridWidth * map.cellWidth;
|
||||
var mapHeight = map.gridHeight * map.cellHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
g.lineStyle(1, 0x007700);
|
||||
for (x in 0...map.gridWidth) {
|
||||
g.moveTo(x * map.cellWidth, 0);
|
||||
g.lineTo(x * map.cellWidth, mapHeight);
|
||||
for (x in 0...config.map.gridWidth) {
|
||||
g.moveTo(x * config.map.cellWidth, 0);
|
||||
g.lineTo(x * config.map.cellWidth, config.mapHeight);
|
||||
}
|
||||
for (y in 0...map.gridHeight) {
|
||||
g.moveTo(0, y * map.cellHeight);
|
||||
g.lineTo(mapWidth, y * map.cellHeight);
|
||||
for (y in 0...config.map.gridHeight) {
|
||||
g.moveTo(0, y * config.map.cellHeight);
|
||||
g.lineTo(config.mapWidth, y * config.map.cellHeight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +115,7 @@ enum Brush {
|
||||
var playerId = new PlayerId(point.team, point.index < 0 ? 0 : point.index);
|
||||
var player = config.getPlayer(playerId);
|
||||
var tankSpawn = player.tanks[0];
|
||||
var tank = builder.buildTank(point, playerId, tankSpawn.type, true);
|
||||
var tank = builder.buildTank(point, playerId, tankSpawn.type, 0, playerId.toString(), true);
|
||||
pointEntities[pointKey(point)] = tank;
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user