[network] added NetworkFrame
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import ru.m.tankz.storage.UserStorage;
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import flash.events.KeyboardEvent;
|
||||
@@ -70,14 +71,16 @@ class Client implements IConnectionHandler {
|
||||
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection('localhost', 5001));
|
||||
#elseif html5
|
||||
Provider.set(IConnection, new ru.m.connect.js.JsConnection('localhost', 5001));
|
||||
#else
|
||||
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection());
|
||||
#end
|
||||
|
||||
//Provider.get(IConnection).handler.addListener(this);
|
||||
Provider.get(IConnection).handler.addListener(this);
|
||||
|
||||
view = new ClientView();
|
||||
Provider.set(IFrameSwitcher, view.switcher);
|
||||
Root.bind(view);
|
||||
view.content.stage.stageFocusRect = false;
|
||||
//view.logout.onPress = this;
|
||||
|
||||
view.content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||
if (event.keyCode == Keyboard.ESCAPE) {
|
||||
@@ -88,6 +91,7 @@ class Client implements IConnectionHandler {
|
||||
Provider.setFactory(IConfigBundle, ConfigBundle);
|
||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||
Provider.setFactory(SaveStorage, SaveStorage);
|
||||
Provider.setFactory(UserStorage, UserStorage);
|
||||
Provider.setFactory(SoundManager, SoundManager);
|
||||
Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE);
|
||||
Provider.setFactory(Game, DotaGame, DotaGame.TYPE);
|
||||
@@ -110,11 +114,11 @@ class Client implements IConnectionHandler {
|
||||
public function onConnected():Void {}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
//view.switcher.change(AuthFrame.ID);
|
||||
view.switcher.change(StartFrame.ID);
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
L.e(TAG, '', error);
|
||||
//view.switcher.change(AuthFrame.ID);
|
||||
view.switcher.change(StartFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ views:
|
||||
$type: ru.m.tankz.frame.LevelFrame
|
||||
- id: game
|
||||
$type: ru.m.tankz.frame.GameFrame
|
||||
- id: network
|
||||
$type: ru.m.tankz.frame.NetworkFrame
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
inLayout: false
|
||||
|
||||
88
src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx
Normal file
88
src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx
Normal file
@@ -0,0 +1,88 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.InputView;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import haxework.provider.Provider;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.proto.pack.LoginRequest;
|
||||
import ru.m.tankz.proto.pack.LoginResponse;
|
||||
import ru.m.tankz.storage.UserStorage;
|
||||
|
||||
|
||||
interface NetworkFrameLayout {
|
||||
var stateLabel(default, null):LabelView;
|
||||
var nameInput(default, null):InputView;
|
||||
var loginButton(default, null):ButtonView;
|
||||
}
|
||||
|
||||
@:template("ru/m/tankz/frame/NetworkFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class NetworkFrame extends VGroupView implements ViewBuilder implements NetworkFrameLayout implements IPacketHandler implements IConnectionHandler {
|
||||
public static var ID(default, never):String = "network";
|
||||
|
||||
private var state(null, set):String;
|
||||
|
||||
private function set_state(value:String):String {
|
||||
stateLabel.text = value;
|
||||
loginButton.disabled = value != 'online';
|
||||
return value;
|
||||
}
|
||||
|
||||
public function init():Void {
|
||||
loginButton.onPress = this;
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
var user:User = Provider.get(UserStorage).read();
|
||||
nameInput.text = user.name;
|
||||
var connection:IConnection = Provider.get(IConnection);
|
||||
connection.packetHandler.addListener(this);
|
||||
connection.handler.addListener(this);
|
||||
state = connection.connected ? 'online' : 'offline';
|
||||
if (!connection.connected) {
|
||||
connection.connect();
|
||||
}
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
Provider.get(IConnection).packetHandler.removeListener(this);
|
||||
Provider.get(IConnection).handler.removeListener(this);
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
var user:User = Provider.get(UserStorage).read();
|
||||
user.name = nameInput.text;
|
||||
Provider.get(IConnection).send(
|
||||
new LoginRequest()
|
||||
.setUuid(user.uuid)
|
||||
.setName(user.name)
|
||||
);
|
||||
}
|
||||
|
||||
public function onLoginResponse(packet:LoginResponse):Void {
|
||||
var user:User = {
|
||||
uuid: packet.user.uuid,
|
||||
name: packet.user.name,
|
||||
};
|
||||
Provider.get(UserStorage).write(user);
|
||||
//Provider.set(User, user);
|
||||
state = 'logined';
|
||||
}
|
||||
|
||||
public function onPacket(packet:Message):Void {}
|
||||
|
||||
public function onConnected():Void {
|
||||
state = 'online';
|
||||
}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
state = 'offline';
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
state = 'offline';
|
||||
}
|
||||
}
|
||||
23
src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml
Normal file
23
src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
---
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
- id: stateLabel
|
||||
$type: haxework.gui.LabelView
|
||||
$style: label
|
||||
text: offline
|
||||
width: 200
|
||||
height: 50
|
||||
- id: nameInput
|
||||
$type: haxework.gui.InputView
|
||||
$style: label
|
||||
width: 200
|
||||
height: 50
|
||||
text: User
|
||||
skin:
|
||||
$type: haxework.gui.skin.ColorSkin
|
||||
color: '#444444'
|
||||
- id: loginButton
|
||||
$type: haxework.gui.ButtonView
|
||||
$style: button
|
||||
text: Login
|
||||
@@ -19,12 +19,13 @@ interface StartFrameLayout {
|
||||
var dota_1p(default, null):ButtonView;
|
||||
var dota_2p_coop(default, null):ButtonView;
|
||||
var dota_2p_vs(default, null):ButtonView;
|
||||
var network(default, null):ButtonView;
|
||||
}
|
||||
|
||||
@:template("ru/m/tankz/frame/StartFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class StartFrame extends VGroupView implements ViewBuilder implements StartFrameLayout {
|
||||
|
||||
public static inline var ID = "start";
|
||||
public static var ID(default, never):String = "start";
|
||||
|
||||
public function init():Void {
|
||||
classic_1p.onPress = this;
|
||||
@@ -33,6 +34,7 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
||||
dota_1p.onPress = this;
|
||||
dota_2p_coop.onPress = this;
|
||||
dota_2p_vs.onPress = this;
|
||||
network.onPress = this;
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
@@ -59,6 +61,8 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
||||
startGame(DotaGame.TYPE, DotaGame.PLAYER2_COOP);
|
||||
case 'dota_2p_vs':
|
||||
startGame(DotaGame.TYPE, DotaGame.PLAYER2_VS);
|
||||
case 'network':
|
||||
Provider.get(IFrameSwitcher).change(NetworkFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,43 +2,66 @@
|
||||
pWidth: 100
|
||||
pHeight: 100
|
||||
views:
|
||||
# logo
|
||||
- $type: haxework.gui.ImageView
|
||||
image: "@asset:image:resources/image/ui/logo.png"
|
||||
contentSize: true
|
||||
bottomMargin: 15
|
||||
- $type: haxework.gui.HGroupView
|
||||
contentSize: true
|
||||
views:
|
||||
- $type: haxework.gui.VGroupView
|
||||
contentSize: true
|
||||
views:
|
||||
# classic
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
topMargin: 15
|
||||
contentSize: true
|
||||
text: Classic
|
||||
- id: classic_1p
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 1 Player
|
||||
$style: button
|
||||
- id: classic_2p
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 Player
|
||||
$style: button
|
||||
- id: classic_load
|
||||
$type: haxework.gui.ButtonView
|
||||
text: Load
|
||||
$style: button
|
||||
- $type: haxework.gui.VGroupView
|
||||
contentSize: true
|
||||
views:
|
||||
# dota
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
topMargin: 15
|
||||
contentSize: true
|
||||
text: DotA
|
||||
- id: dota_1p
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 1 Player
|
||||
$style: button
|
||||
- id: dota_2p_coop
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 COOP
|
||||
$style: button
|
||||
- id: dota_2p_vs
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 VS
|
||||
$style: button
|
||||
# network
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
topMargin: 15
|
||||
contentSize: true
|
||||
text: Classic
|
||||
- id: classic_1p
|
||||
text: Network
|
||||
- id: network
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 1 Player
|
||||
$style: button
|
||||
- id: classic_2p
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 Player
|
||||
$style: button
|
||||
- id: classic_load
|
||||
$type: haxework.gui.ButtonView
|
||||
text: Load
|
||||
$style: button
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
topMargin: 15
|
||||
contentSize: true
|
||||
text: DotA
|
||||
- id: dota_1p
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 1 Player
|
||||
$style: button
|
||||
- id: dota_2p_coop
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 COOP
|
||||
$style: button
|
||||
- id: dota_2p_vs
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 VS
|
||||
$style: button
|
||||
text: Network
|
||||
$style: button
|
||||
27
src/client/haxe/ru/m/tankz/storage/UserStorage.hx
Normal file
27
src/client/haxe/ru/m/tankz/storage/UserStorage.hx
Normal file
@@ -0,0 +1,27 @@
|
||||
package ru.m.tankz.storage;
|
||||
|
||||
import flash.net.SharedObject;
|
||||
|
||||
|
||||
class UserStorage {
|
||||
|
||||
private static var TAG(default, never):String = 'UserStorage';
|
||||
|
||||
private var so:SharedObject;
|
||||
|
||||
public function new() {
|
||||
so = SharedObject.getLocal('user');
|
||||
}
|
||||
|
||||
public function read():Null<User> {
|
||||
var user:User = Reflect.getProperty(so.data, 'user');
|
||||
L.d(TAG, 'read: ${user}');
|
||||
return user != null ? user : {name: 'User', uuid: null};
|
||||
}
|
||||
|
||||
public function write(user:User):Void {
|
||||
L.d(TAG, 'write: ${user}');
|
||||
so.setProperty('user', user);
|
||||
so.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user