Compare commits
7 Commits
0.14.0
...
event_prot
| Author | SHA1 | Date | |
|---|---|---|---|
| 59e9ced333 | |||
| 94b3a94cbb | |||
| caab6ba3a3 | |||
| 7f70c49b87 | |||
| 8f50da64c2 | |||
| 982ca09a23 | |||
| dbf30decae |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.14.0",
|
||||
"version": "0.14.2",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"dateformat": "^3.0.3",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ 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;
|
||||
|
||||
@@ -25,7 +25,7 @@ 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;
|
||||
@@ -51,7 +51,7 @@ 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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package ru.m.tankz.control;
|
||||
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 {
|
||||
@@ -1,7 +1,9 @@
|
||||
package ru.m.tankz.game;
|
||||
package ru.m.tankz.local;
|
||||
|
||||
import ru.m.tankz.control.LocalControlFactory;
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.control;
|
||||
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 {
|
||||
@@ -7,4 +9,8 @@ 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;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package ru.m.tankz.game;
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import haxe.Unserializer;
|
||||
import ru.m.tankz.control.NetworkControlFactory;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.network.NetworkControlFactory;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.pack.GameEventResponse;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
@@ -22,8 +22,8 @@ class NetworkGame extends Game {
|
||||
}
|
||||
|
||||
private function onGameEventProto(game:GameEventResponse):Void {
|
||||
var time = game.time;
|
||||
var eventStr = game.event;
|
||||
var time = game.event.time;
|
||||
var eventStr = game.event.event;
|
||||
var event:GameEvent = Unserializer.run(eventStr);
|
||||
gameEventSignal.emit(event);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.proto.game.GameEventProto;
|
||||
import ru.m.tankz.proto.room.SlotProto;
|
||||
import ru.m.tankz.proto.room.SlotRequest;
|
||||
import haxe.Serializer;
|
||||
@@ -21,7 +22,7 @@ import ru.m.tankz.proto.room.RoomListRequest;
|
||||
import ru.m.tankz.proto.room.RoomProto;
|
||||
import ru.m.tankz.proto.room.RoomRequest;
|
||||
import ru.m.tankz.proto.room.StartRequest;
|
||||
import ru.m.tankz.storage.MultiplayerStorage;
|
||||
import ru.m.tankz.storage.NetworkStorage;
|
||||
|
||||
typedef ClientConnection = IConnection<Request, Response>;
|
||||
|
||||
@@ -46,7 +47,7 @@ class NetworkManager {
|
||||
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;
|
||||
@@ -106,9 +107,11 @@ class NetworkManager {
|
||||
}
|
||||
|
||||
public function action(tankId:Int, action:TankAction):Void {
|
||||
connection.send(new Request().setGameEvent(new GameEventRequest().setTime(0).setEvent(
|
||||
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||
)));
|
||||
connection.send(new Request().setGameEvent(new GameEventRequest().setEvent(
|
||||
new GameEventProto().setTime(0).setEvent(
|
||||
Serializer.run(GameEvent.ACTION(tankId, action))
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
||||
private function connect():Void {
|
||||
|
||||
@@ -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 {
|
||||
@@ -121,6 +122,7 @@ class Render extends SpriteView implements IRender {
|
||||
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();
|
||||
@@ -236,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();
|
||||
});
|
||||
@@ -251,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,10 +1,8 @@
|
||||
---
|
||||
layout.overflow: true
|
||||
views:
|
||||
- $type: haxework.view.frame.FrameSwitcher
|
||||
id: switcher
|
||||
geometry.size.stretch: true
|
||||
layout.overflow: true
|
||||
skinId: dark
|
||||
views:
|
||||
- id: start
|
||||
|
||||
@@ -9,7 +9,7 @@ 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.game.LocalGame;
|
||||
import ru.m.tankz.local.LocalGame;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.popup.LevelPopup;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,7 @@ import haxework.view.list.VListView;
|
||||
import haxework.view.TextView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.game.NetworkGame;
|
||||
import ru.m.tankz.network.NetworkGame;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.room.RoomProto;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
|
||||
@@ -109,6 +109,7 @@ typedef PlayerControl = {
|
||||
var playerId:PlayerId;
|
||||
var control:String;
|
||||
@:optional var color:Null<Int>;
|
||||
@:optional var name:String;
|
||||
}
|
||||
|
||||
typedef ControlPreset = {
|
||||
@@ -138,6 +139,8 @@ class Config {
|
||||
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>;
|
||||
@@ -205,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);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ 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, default):Bool;
|
||||
|
||||
@@ -44,11 +44,12 @@ class EntityBuilder {
|
||||
return eagle;
|
||||
}
|
||||
|
||||
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, color:Null<Color>, 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 = color == null || color.zero ? config.getColor(playerId) : color;
|
||||
tank.name = name;
|
||||
if (!bonusOff) {
|
||||
tank.bonus = Math.random() < playerConfig.bonus;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ class EventUtil {
|
||||
hits:tank.hits,
|
||||
bonus:tank.bonus,
|
||||
color:tank.color,
|
||||
name:tank.name,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -28,6 +29,8 @@ import ru.m.tankz.Type;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
public var pause(default, set):Bool;
|
||||
|
||||
private var controls:Map<String, Control>;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
public function new(state:GameState) {
|
||||
@@ -37,6 +40,7 @@ import ru.m.tankz.Type;
|
||||
this.config = configBundle.get(type);
|
||||
this.controlFactory = new NoneControlFactory();
|
||||
this.pause = false;
|
||||
this.controls = new Map();
|
||||
connect(this);
|
||||
}
|
||||
|
||||
@@ -76,9 +80,19 @@ import ru.m.tankz.Type;
|
||||
var team = getTeam(teamId);
|
||||
team.eagleId = id;
|
||||
case SPAWN(TANK(id, rect, playerId, info)):
|
||||
var player = getPlayer(playerId);
|
||||
player.tankId = id;
|
||||
player.state.tank = info.type;
|
||||
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 _:
|
||||
}
|
||||
}
|
||||
@@ -98,14 +112,19 @@ import ru.m.tankz.Type;
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players.iterator()) {
|
||||
var playerControl = controlsById.get(player.id);
|
||||
if (playerControl != null && playerControl.color != null) {
|
||||
player.state.color = playerControl.color;
|
||||
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) {
|
||||
player.control = control;
|
||||
player.control.bind(this, engine);
|
||||
controls[player.id] = control;
|
||||
control.bind(this, engine);
|
||||
} else {
|
||||
// ToDo: remove player
|
||||
player.state.life = 0;
|
||||
@@ -115,6 +134,10 @@ import ru.m.tankz.Type;
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
for (control in controls) {
|
||||
control.dispose();
|
||||
}
|
||||
controls = new Map();
|
||||
gameEventSignal.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ typedef TankInfo = {
|
||||
var hits:Int;
|
||||
var bonus:Bool;
|
||||
var color:Color;
|
||||
@:optional var name:String;
|
||||
}
|
||||
|
||||
typedef BrickInfo = {
|
||||
|
||||
@@ -73,7 +73,7 @@ class GameRunner extends Game implements EngineListener {
|
||||
|
||||
private function spawn(task:SpawnTask):Void {
|
||||
var player = getPlayer(task.playerId);
|
||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color);
|
||||
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color, player.state.name);
|
||||
engine.spawn(tank);
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
if (player.config.protect > 0) {
|
||||
@@ -130,14 +130,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
}
|
||||
|
||||
private function complete(winner:TeamId):Void {
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players) {
|
||||
if (player.control != null) {
|
||||
player.control.action(STOP);
|
||||
player.control.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Timer.delay(function() {
|
||||
gameEventSignal.emit(COMPLETE(state, winner));
|
||||
}, 3000);
|
||||
@@ -165,8 +157,9 @@ class GameRunner extends Game implements EngineListener {
|
||||
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||
switch entity {
|
||||
case EntityType.TANK(tank):
|
||||
var control = getPlayer(tank.playerId).control;
|
||||
if (control != null) control.onCollision(with);
|
||||
if (controls.exists(tank.playerId)) {
|
||||
controls[tank.playerId].onCollision(with);
|
||||
}
|
||||
case _:
|
||||
}
|
||||
switch [entity, with] {
|
||||
@@ -352,11 +345,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
case ACTION(tankId, STOP):
|
||||
gameEventSignal.emit(STOP(TANK(tankId)));
|
||||
engine.stop(tankId);
|
||||
case SPAWN(TANK(_, _, playerId, _)):
|
||||
var control = getPlayer(playerId).control;
|
||||
if (control != null) {
|
||||
control.start();
|
||||
}
|
||||
case SPAWN(BULLET(_, _, playerId, _)):
|
||||
getPlayer(playerId).bullets++;
|
||||
case DESTROY(EAGLE(id, shot)):
|
||||
@@ -371,9 +359,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
var tank:Tank = engine.getEntity(id);
|
||||
var team = getTeam(tank.playerId.team);
|
||||
var player = getPlayer(tank.playerId);
|
||||
if (player.control != null) {
|
||||
player.control.stop();
|
||||
}
|
||||
player.tankId = -1;
|
||||
team.onDestroy(player.id);
|
||||
if (player.state.life > 0) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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,7 +15,6 @@ 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;
|
||||
@@ -27,21 +24,9 @@ class Player {
|
||||
|
||||
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 > -1 || state.life > 0;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,117 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package ru.m.tankz.proto.game;
|
||||
|
||||
message PositionProto {
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
}
|
||||
|
||||
message RectangleProto {
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
float width = 3;
|
||||
float height = 4;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,13 +25,11 @@ message LogoutRequest {}
|
||||
message LogoutResponse {}
|
||||
|
||||
message GameEventRequest {
|
||||
int32 time = 1;
|
||||
string event = 2;
|
||||
ru.m.tankz.proto.game.GameEventProto event = 1;
|
||||
}
|
||||
|
||||
message GameEventResponse {
|
||||
int32 time = 1;
|
||||
string event = 2;
|
||||
ru.m.tankz.proto.game.GameEventProto event = 1;
|
||||
}
|
||||
|
||||
message Request {
|
||||
|
||||
@@ -203,10 +203,13 @@ controls:
|
||||
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
|
||||
|
||||
@@ -96,10 +96,13 @@ controls:
|
||||
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
|
||||
|
||||
@@ -122,21 +122,26 @@ controls:
|
||||
- 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, 0, true);
|
||||
var tank = builder.buildTank(point, playerId, tankSpawn.type, 0, playerId.toString(), true);
|
||||
pointEntities[pointKey(point)] = tank;
|
||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package ru.m.tankz.server.game;
|
||||
|
||||
import ru.m.tankz.config.Config.PlayerControl;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
import ru.m.tankz.proto.room.SlotProto;
|
||||
import ru.m.tankz.proto.room.RoomProto;
|
||||
import ru.m.tankz.game.GameEvent;
|
||||
import ru.m.tankz.game.IGame.GameListener;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.proto.core.GameProto;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.room.RoomProto;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
import ru.m.tankz.proto.room.SlotProto;
|
||||
import ru.m.tankz.server.game.IGameManager;
|
||||
|
||||
class _GameListener implements GameListener {
|
||||
|
||||
@@ -23,6 +23,7 @@ interface IGameManager {
|
||||
public var games(default, null):Array<ServerGame>;
|
||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
||||
public var gamesByCreator(default, null):Map<String, ServerGame>;
|
||||
public var gamesByUser(default, null):Map<String, ServerGame>;
|
||||
|
||||
private var createSignal(default, null):Signal<ServerGame>;
|
||||
private var changeSignal(default, null):Signal2<ServerGame, GameChange>;
|
||||
|
||||
@@ -28,12 +28,23 @@ class ServerGame extends GameRunner {
|
||||
return room.game.id;
|
||||
}
|
||||
|
||||
public function contains(user:UserProto):Bool {
|
||||
for (slot in room.slots) {
|
||||
if (slot.hasUser() && slot.user.uuid == user.uuid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function join(user:UserProto):Void {
|
||||
leave(user);
|
||||
room.users.push(user);
|
||||
if (!contains(user)) {
|
||||
room.users.push(user);
|
||||
}
|
||||
}
|
||||
|
||||
public function slot(user:UserProto, slot:SlotProto):Void {
|
||||
join(user);
|
||||
for (s in room.slots) {
|
||||
if (s.hasUser() && s.user.uuid == user.uuid) {
|
||||
s.clearUser();
|
||||
@@ -64,6 +75,7 @@ class ServerGame extends GameRunner {
|
||||
.map(function(slot:RoomSlotProto):PlayerControl return {
|
||||
playerId: [slot.slot.team, slot.slot.index],
|
||||
control: "human-0",
|
||||
name: slot.user.name,
|
||||
});
|
||||
super.start();
|
||||
}
|
||||
@@ -72,6 +84,7 @@ class ServerGame extends GameRunner {
|
||||
var result = [];
|
||||
result.push(EventUtil.buildBricksSpawn(engine.map));
|
||||
result = result.concat(EventUtil.buildCellsDestroyed(engine.map));
|
||||
result.push(START(state));
|
||||
for (entity in engine.entities) {
|
||||
switch EntityTypeResolver.of(entity) {
|
||||
case EAGLE(eagle): result.push(EventUtil.buildEagleSpawn(eagle));
|
||||
@@ -81,7 +94,6 @@ class ServerGame extends GameRunner {
|
||||
case CELL(_):
|
||||
}
|
||||
}
|
||||
result.push(START(state));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.server.session;
|
||||
|
||||
import ru.m.tankz.proto.game.GameEventProto;
|
||||
import com.hurlant.crypto.extra.UUID;
|
||||
import com.hurlant.crypto.prng.Random;
|
||||
import haxe.Serializer;
|
||||
@@ -57,15 +58,26 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
}
|
||||
}
|
||||
|
||||
private function logout():Void {
|
||||
private function logout(leave:Bool = true):Void {
|
||||
gameId = -1;
|
||||
gameManager.disconnect(this);
|
||||
if (user != null) {
|
||||
if (user != null && leave) {
|
||||
gameManager.leave(user);
|
||||
user = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function join(gameId:Int, restore:Bool):Void {
|
||||
this.gameId = gameId;
|
||||
gameManager.join(gameId, user);
|
||||
var game = gameManager.gamesById[gameId];
|
||||
if (restore && game.room.game.started) {
|
||||
for (event in game.restore()) {
|
||||
send(new Response().setGameEvent(new GameEventResponse().setEvent(new GameEventProto().setTime(0).setEvent(Serializer.run(event)))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override private function onRequest(request:Request):Void {
|
||||
#if proto_debug L.d(TAG, '$tag onRequest: ${request}'); #end
|
||||
try {
|
||||
@@ -79,6 +91,9 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
.setName(request.login.name);
|
||||
gameManager.connect(this);
|
||||
send(new Response().setLogin(new LoginResponse().setUser(user)));
|
||||
if (gameManager.gamesByUser.exists(user.uuid)) {
|
||||
join(gameManager.gamesByUser[user.uuid].id, false);
|
||||
}
|
||||
// logout
|
||||
} else if (request.hasLogout()) {
|
||||
logout();
|
||||
@@ -90,14 +105,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
gameId = game.id;
|
||||
send(new Response().setRoom(new RoomResponse().setRoom(game.room)));
|
||||
} else if (request.room.hasJoin()) {
|
||||
gameId = request.room.join.gameId;
|
||||
gameManager.join(gameId, user);
|
||||
var game = gameManager.gamesById[gameId];
|
||||
if (request.room.join.restore && game.room.game.started) {
|
||||
for (event in game.restore()) {
|
||||
send(new Response().setGameEvent(new GameEventResponse().setTime(0).setEvent(Serializer.run(event))));
|
||||
}
|
||||
}
|
||||
join(request.room.join.gameId, request.room.join.restore);
|
||||
} else if (request.room.hasLeave()) {
|
||||
gameManager.leave(user);
|
||||
} else if (request.room.hasSlot()) {
|
||||
@@ -113,7 +121,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
}
|
||||
} else if (request.hasGameEvent()) {
|
||||
if (gameManager.gamesById.exists(gameId)) {
|
||||
var event:GameEvent = Unserializer.run(request.gameEvent.event);
|
||||
var event:GameEvent = Unserializer.run(request.gameEvent.event.event);
|
||||
gameManager.gamesById[gameId].gameEventSignal.emit(event);
|
||||
}
|
||||
}
|
||||
@@ -125,7 +133,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
|
||||
override public function disconnect():Void {
|
||||
L.d(TAG, '$tag disconnect');
|
||||
logout();
|
||||
logout(false);
|
||||
super.disconnect();
|
||||
}
|
||||
|
||||
@@ -165,7 +173,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
||||
|
||||
public function onEvent(game:ServerGame, event:GameEvent):Void {
|
||||
if (gameId == game.id) {
|
||||
send(new Response().setGameEvent(new GameEventResponse().setTime(0).setEvent(Serializer.run(event))));
|
||||
send(new Response().setGameEvent(new GameEventResponse().setEvent(new GameEventProto().setTime(0).setEvent(Serializer.run(event)))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user