[network] update

This commit is contained in:
2019-05-31 15:47:16 +03:00
parent 7115ea8ac2
commit fb9a28c126
29 changed files with 274 additions and 221 deletions

View File

@@ -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();

View File

@@ -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");
}
}
}

View File

@@ -48,7 +48,7 @@ class EntityBuilder {
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 ? config.getColor(playerId) : color;
tank.color = color == null || color.zero ? config.getColor(playerId) : color;
if (!bonusOff) {
tank.bonus = Math.random() < playerConfig.bonus;
}

View File

@@ -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,15 +22,27 @@ 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,
@@ -36,14 +50,22 @@ class EventUtil {
}));
}
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;
}

View File

@@ -222,7 +222,7 @@ class GameRunner extends Game implements EngineListener {
}
var bonus = builder.buildBonus(point, type);
engine.spawn(bonus);
gameEventSignal.emit(SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
gameEventSignal.emit(EventUtil.buildBonusSpawn(bonus));
}
private inline function alienTank(team:TeamId):Tank->Bool {
@@ -313,7 +313,7 @@ class GameRunner extends Game implements EngineListener {
bullet.tank = tank;
bullet.move(bullet.rect.direction);
engine.spawn(bullet);
gameEventSignal.emit(SPAWN(BULLET(bullet.id, bullet.rect.clone(), bullet.playerId, bullet.config.piercing)));
gameEventSignal.emit(EventUtil.buildBulletSpawn(bullet));
}
case ACTION(tankId, MOVE(direction)):
engine.move(tankId, direction);

View File

@@ -46,11 +46,13 @@ message CreateGameResponse {
// Join Game
message JoinGameRequest {
int32 game_id = 1;
bool restore = 2;
}
message JoinGameResponse {
ru.m.tankz.proto.core.GameProto game = 1;
ru.m.tankz.proto.core.UserProto user = 2;
repeated string events = 3;
}
// Leave Game