[engine] use signals
This commit is contained in:
19
src/client/haxe/ru/m/tankz/control/ClientNetworkControl.hx
Normal file
19
src/client/haxe/ru/m/tankz/control/ClientNetworkControl.hx
Normal file
@@ -0,0 +1,19 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.control.Control;
|
||||
|
||||
|
||||
class ClientNetworkControl extends HumanControl {
|
||||
|
||||
private var network(get, never):NetworkManager;
|
||||
|
||||
inline private function get_network():NetworkManager {
|
||||
return Provider.get(NetworkManager);
|
||||
}
|
||||
|
||||
override public function action(action:TankAction):Void {
|
||||
network.action(action);
|
||||
}
|
||||
}
|
||||
@@ -42,11 +42,10 @@ class GameFrame extends VGroupView implements ViewBuilder implements GameFrameLa
|
||||
if (game == null) {
|
||||
throw 'Unsupported game type "${save.state.type}"';
|
||||
}
|
||||
game.engine.listeners.push(render);
|
||||
game.engine.listeners.push(Provider.get(SoundManager));
|
||||
game.engine.connect(render);
|
||||
game.engine.connect(Provider.get(SoundManager));
|
||||
game.start(save).then(onGameStateChange).endThen(onGameComplete);
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
//Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(game.engine);
|
||||
timer = new Timer(10);
|
||||
timer.run = updateEngine;
|
||||
@@ -55,7 +54,6 @@ class GameFrame extends VGroupView implements ViewBuilder implements GameFrameLa
|
||||
}
|
||||
|
||||
private function stop():Void {
|
||||
//Provider.get(IConnection).packetHandler.removeListener(this);
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package ru.m.tankz.network;
|
||||
|
||||
import ru.m.tankz.proto.game.GameChange;
|
||||
import ru.m.tankz.proto.game.GameActionType;
|
||||
import ru.m.tankz.proto.pack.GameUpdateRequest;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
||||
@@ -22,6 +26,7 @@ class NetworkManager {
|
||||
public var stateSignal:Signal<String>;
|
||||
public var listGameSignal:Signal<Array<Game>>;
|
||||
public var gameSignal:Signal<Game>;
|
||||
public var gameUpdateSignal:Signal<Array<GameChange>>;
|
||||
public var user(default, null):User;
|
||||
|
||||
private var connection(get, never):ClientConnection;
|
||||
@@ -39,6 +44,7 @@ class NetworkManager {
|
||||
stateSignal = new Signal<String>();
|
||||
listGameSignal = new Signal<Array<Game>>();
|
||||
gameSignal = new Signal<Game>();
|
||||
gameUpdateSignal = new Signal<Array<GameChange>>();
|
||||
updateState('offline');
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.receiveHandler.connect(onResponse);
|
||||
@@ -82,6 +88,18 @@ class NetworkManager {
|
||||
connection.send(new Request().setLeaveGame(new LeaveGameRequest()));
|
||||
}
|
||||
|
||||
public function action(action:TankAction):Void {
|
||||
var update:GameUpdateRequest = switch action {
|
||||
case TankAction.MOVE(direction): new GameUpdateRequest().setType(GameActionType.MOVE).setDirectionX(direction.x).setDirectionY(direction.y);
|
||||
case TankAction.STOP: new GameUpdateRequest().setType(GameActionType.STOP);
|
||||
case TankAction.SHOT: new GameUpdateRequest().setType(GameActionType.SHOT);
|
||||
case _: null;
|
||||
}
|
||||
if (update != null) {
|
||||
connection.send(new Request().setUpdateGame(update));
|
||||
}
|
||||
}
|
||||
|
||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||
L.d('Network', '${event}');
|
||||
updateState(switch (event) {
|
||||
@@ -110,6 +128,8 @@ class NetworkManager {
|
||||
gameSignal.emit(packet.joinGame.game);
|
||||
} else if (packet.hasLeaveGame()) {
|
||||
gameSignal.emit(null);
|
||||
}else if (packet.hasUpdateGame()) {
|
||||
gameUpdateSignal.emit(packet.updateGame.changes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import ru.m.tankz.engine.Engine;
|
||||
import ru.m.tankz.render.RenderItem;
|
||||
|
||||
|
||||
class Render extends SpriteView implements EngineListener {
|
||||
class Render extends SpriteView {
|
||||
|
||||
private var backgroundLayer:Sprite;
|
||||
private var groundLayer:Sprite;
|
||||
@@ -126,7 +126,7 @@ class Render extends SpriteView implements EngineListener {
|
||||
}
|
||||
}
|
||||
|
||||
public function onChange(entity:EntityType, ?change:EntityChange):Void {}
|
||||
public function onChange(entity:EntityType, change:EntityChange):Void {}
|
||||
|
||||
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||
switch [entity, with] {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package ru.m.tankz.sound;
|
||||
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
import openfl.media.Sound;
|
||||
import openfl.utils.Assets;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
|
||||
|
||||
class SoundManager implements EngineListener {
|
||||
class SoundManager {
|
||||
private static var TAG(default, never):String = 'SoundManager';
|
||||
|
||||
#if flash
|
||||
@@ -37,12 +37,12 @@ class SoundManager implements EngineListener {
|
||||
}
|
||||
}
|
||||
|
||||
public function onChange(entity:EntityType, ?change:EntityChange):Void {
|
||||
public function onChange(entity:EntityType, change:EntityChange):Void {
|
||||
switch [entity, change] {
|
||||
case [EntityType.TANK(_), EntityChange.HIT]:
|
||||
play('bullet_hit');
|
||||
case [EntityType.TANK(_), EntityChange.LIVE_UP]:
|
||||
play('live');
|
||||
//case [EntityType.TANK(_), EntityChange.LIVE_UP]:
|
||||
// play('live');
|
||||
case [EntityType.EAGLE(_), EntityChange.DEATH]:
|
||||
play('boom_player');
|
||||
case _:
|
||||
|
||||
Reference in New Issue
Block a user