This commit is contained in:
2014-08-29 12:37:12 +04:00
parent c605f843b0
commit 17c3db46ba
11 changed files with 277 additions and 135 deletions

View File

@@ -19,4 +19,5 @@
<haxeflag name="-dce" value="no"/> <haxeflag name="-dce" value="no"/>
<haxeflag name="-debug"/> <haxeflag name="-debug"/>
<haxeflag name="--macro" value="Meta.set('0.0.0')"/> <haxeflag name="--macro" value="Meta.set('0.0.0')"/>
<haxeflag name="--macro" value="include('ru.m.tankz')"/>
</project> </project>

View File

@@ -34,10 +34,16 @@ message ErrorResponse {
} }
enum GameState {
READY = 1;
STARTED = 2;
ENDED = 3;
}
message Game { message Game {
required int32 id = 1; required int32 id = 1;
repeated Person persons = 2; repeated Person persons = 2;
required GameState state = 3;
} }
message GamesRequest { message GamesRequest {

View File

@@ -1,10 +1,27 @@
{ {
"type":"haxework.frame.FrameSwitcher", "type": "haxework.gui.VGroupView",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"}, "pWidth": 100, "pHeight": 100,
"views": [
{
"id":"top", "type": "haxework.gui.HGroupView",
"pWidth": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ColorSkin", "color": "0x105000"},
"views": [
{
"id": "logout", "type": "haxework.gui.ButtonView",
"width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Logout"
}
]
},
{
"id": "switcher", "type": "haxework.frame.FrameSwitcher",
"pWidth": 100, "pHeight": 100,
"skin": {"type": "haxework.gui.skin.ColorSkin", "color": "0x50a000"},
"views": [ "views": [
{ {
"id": "auth", "type": "ru.m.tankz.view.frames.AuthFrame", "id": "auth", "type": "ru.m.tankz.view.frames.AuthFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth": 100, "pHeight": 100, "layoutMargin": 3, "pWidth": 100, "pHeight": 100, "layoutMargin": 3,
"views": [ "views": [
{ {
@@ -52,7 +69,6 @@
{ {
"id": "person_list", "type": "ru.m.tankz.view.frames.PersonListFrame", "id": "person_list", "type": "ru.m.tankz.view.frames.PersonListFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth": 100, "pHeight": 100, "layoutMargin": 3, "paddings": 10, "pWidth": 100, "pHeight": 100, "layoutMargin": 3, "paddings": 10,
"views": [ "views": [
{ {
@@ -72,7 +88,6 @@
{ {
"id": "game_list", "type": "ru.m.tankz.view.frames.GameListFrame", "id": "game_list", "type": "ru.m.tankz.view.frames.GameListFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth": 100, "pHeight": 100, "pWidth": 100, "pHeight": 100,
"views": [ "views": [
{ {
@@ -85,12 +100,6 @@
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"}, "skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Create" "text": "Create"
}, },
{
"id":"logout", "type":"haxework.gui.ButtonView",
"width":100, "height":30,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Logout"
},
{ {
"id": "list", "type": "haxework.gui.list.VListView", "id": "list", "type": "haxework.gui.list.VListView",
"factory": { "factory": {
@@ -107,8 +116,7 @@
{ {
"id":"game", "type":"ru.m.tankz.view.frames.GameFrame", "id": "game_ready", "type": "ru.m.tankz.view.frames.GameReadyFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth": 100, "pHeight": 100, "pWidth": 100, "pHeight": 100,
"views": [ "views": [
{ {
@@ -116,10 +124,39 @@
"pWidth": 100, "height": 25, "text": "" "pWidth": 100, "height": 25, "text": ""
}, },
{ {
"id":"logout", "type":"haxework.gui.ButtonView", "id": "start", "type": "haxework.gui.ButtonView",
"width": 100, "height": 30, "width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"}, "skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text":"Logout" "text": "Start"
},
{
"id": "exit", "type": "haxework.gui.ButtonView",
"width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Exit"
},
{
"id": "list", "type": "haxework.gui.list.VListView",
"factory": {
"type": "~ru.m.tankz.view.frames.game.PersonView"
},
"scroll": {
"type": "haxework.gui.list.VScrollView",
"skin": {"type": "haxework.gui.list.VScrollSkin"}
},
"pWidth": 100, "pHeight": 100, "layoutMargin": 5
}
]
},
{
"id": "game", "type": "ru.m.tankz.view.frames.GameFrame",
"pWidth": 100, "pHeight": 100,
"views": [
{
"id": "name", "type": "haxework.gui.LabelView",
"pWidth": 100, "height": 25, "text": ""
}, },
{ {
"id": "restart", "type": "haxework.gui.ButtonView", "id": "restart", "type": "haxework.gui.ButtonView",
@@ -135,3 +172,5 @@
} }
] ]
} }
]
}

View File

@@ -1,5 +1,9 @@
package ru.m.tankz; package ru.m.tankz;
import haxework.gui.ButtonView;
import flash.display.Sprite;
import haxework.gui.IGroupView;
import ru.m.tankz.view.frames.GameReadyFrame;
import ru.m.tankz.PacketBuilder; import ru.m.tankz.PacketBuilder;
import flash.text.TextFieldType; import flash.text.TextFieldType;
import flash.Lib; import flash.Lib;
@@ -31,13 +35,18 @@ class Client implements IConnectionHandler {
} }
private var view:IGroupView<Sprite>;
private var logout:ButtonView;
private var switcher:FrameSwitcher; private var switcher:FrameSwitcher;
public function new() { public function new() {
var bytes = Assets.getBytes("res/layout/main.json"); var bytes = Assets.getBytes("res/layout/main.json");
var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable)); var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable));
switcher = GuiBuilder.build(form, {listener:this}); view = GuiBuilder.build(form, {listener:this});
new Root(switcher); new Root(view);
switcher = view.findViewById("switcher");
logout = view.findViewById("top:logout");
logout.onPress = this;
Provider.setFactory(GameData, GameData); Provider.setFactory(GameData, GameData);
Provider.set(IFrameSwitcher, switcher); Provider.set(IFrameSwitcher, switcher);
@@ -58,6 +67,13 @@ class Client implements IConnectionHandler {
Lib.current.addChild(tf);*/ Lib.current.addChild(tf);*/
} }
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
}
}
public function onConnected():Void {} public function onConnected():Void {}
public function onDisconnected():Void { public function onDisconnected():Void {

View File

@@ -39,7 +39,6 @@ class GameFrame extends VGroupView {
public function init():Void { public function init():Void {
render = findViewById("render"); render = findViewById("render");
findViewById("logout", ButtonView).onPress = this;
findViewById("restart", ButtonView).onPress = this; findViewById("restart", ButtonView).onPress = this;
} }
@@ -62,8 +61,6 @@ class GameFrame extends VGroupView {
public function onPress(view:ButtonView):Void { public function onPress(view:ButtonView):Void {
switch (view.id) { switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
case "restart": case "restart":
game.clear(); game.clear();
game.init(config); game.init(config);

View File

@@ -34,7 +34,6 @@ class GameListFrame extends VGroupView implements IPacketHandler implements List
public function init() { public function init() {
list = findViewById("list"); list = findViewById("list");
list.dispatcher.addListener(this); list.dispatcher.addListener(this);
findViewById("logout", ButtonView).onPress = this;
findViewById("create", ButtonView).onPress = this; findViewById("create", ButtonView).onPress = this;
} }
@@ -54,21 +53,21 @@ class GameListFrame extends VGroupView implements IPacketHandler implements List
} }
public function onCreateGameResponse(packet:CreateGameResponse):Void { public function onCreateGameResponse(packet:CreateGameResponse):Void {
list.data.push(packet.game); //list.data.push(packet.game);
list.update(); //list.update();
Provider.get(GameData).game = packet.game;
Provider.get(IFrameSwitcher).change(GameReadyFrame.ID);
} }
public function onJoinGameResponse(packet:JoinGameResponse):Void { public function onJoinGameResponse(packet:JoinGameResponse):Void {
Provider.get(GameData).game = packet.game; Provider.get(GameData).game = packet.game;
Provider.get(IFrameSwitcher).change(GameFrame.ID); Provider.get(IFrameSwitcher).change(GameReadyFrame.ID);
} }
public function onPacket(packet:Message):Void {} public function onPacket(packet:Message):Void {}
public function onPress(view:ButtonView):Void { public function onPress(view:ButtonView):Void {
switch (view.id) { switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
case "create": case "create":
Provider.get(IConnection).send(new CreateGameRequest()); Provider.get(IConnection).send(new CreateGameRequest());
} }

View File

@@ -0,0 +1,46 @@
package ru.m.tankz.view.frames;
import ru.m.tankz.data.GameData;
import protohx.Message;
import haxework.gui.ButtonView;
import haxework.gui.list.VListView;
import ru.m.tankz.proto.Person;
import haxework.provider.Provider;
import haxework.gui.VGroupView;
import ru.m.core.connect.IConnection;
class GameReadyFrame extends VGroupView implements IPacketHandler {
private static inline var TAG = "GameReadyFrame";
public static inline var ID = "game_ready";
private var list:VListView<Person>;
public function new() {
super();
}
public function init() {
list = findViewById("list");
//list.dispatcher.addListener(this);
findViewById("start", ButtonView).onPress = this;
findViewById("exit", ButtonView).onPress = this;
}
public function onShow() {
Provider.get(IConnection).packetHandler = this;
list.data = Provider.get(GameData).game.persons;
}
public function onPacket(packet:Message):Void {}
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "start":
case "exit":
}
}
}

View File

@@ -0,0 +1,31 @@
package ru.m.tankz.view.frames.game;
import ru.m.tankz.proto.Person;
import haxework.gui.list.ListView.IListItemView;
import haxework.gui.LabelView;
import haxework.gui.skin.ColorSkin;
import haxework.gui.HGroupView;
class PersonView extends HGroupView implements IListItemView<Person> {
public var item_index(default, default):Int;
public var data(default, set):Person;
private var nameLabel:LabelView;
public function new() {
super();
pWidth = 100;
height = 50;
skin = new ColorSkin(0xffffff);
nameLabel = new LabelView();
addView(nameLabel);
}
private function set_data(value:Person):Person {
this.data = value;
nameLabel.text = value.name;
return this.data;
}
}

View File

@@ -43,10 +43,12 @@ class FlashConnection extends BaseConnection {
} }
override public function disconnect():Void { override public function disconnect():Void {
if (socket.connected) {
socket.close(); socket.close();
connected = false; connected = false;
if (handler != null) handler.onDisconnected(); if (handler != null) handler.onDisconnected();
} }
}
private function onError(event:ErrorEvent):Void { private function onError(event:ErrorEvent):Void {
socket.close(); socket.close();

4
src/common/haxe/ru/m/tankz/core/PlayerTank.hx Normal file → Executable file
View File

@@ -19,7 +19,7 @@ class PlayerTank extends Tank {
} }
private function onKeyDown(event:KeyboardEvent):Void { private function onKeyDown(event:KeyboardEvent):Void {
switch (keyBinding.get(event.keyCode)) { if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction): case TankAction.MOVE(direction):
if (moveQueue.indexOf(event.keyCode) == -1) { if (moveQueue.indexOf(event.keyCode) == -1) {
moveQueue.unshift(event.keyCode); moveQueue.unshift(event.keyCode);
@@ -30,7 +30,7 @@ class PlayerTank extends Tank {
} }
private function onKeyUp(event:KeyboardEvent):Void { private function onKeyUp(event:KeyboardEvent):Void {
switch (keyBinding.get(event.keyCode)) { if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction): case TankAction.MOVE(direction):
moveQueue.remove(event.keyCode); moveQueue.remove(event.keyCode);
updateMove(); updateMove();

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.server.session; package ru.m.tankz.server.session;
import ru.m.tankz.proto.GameState;
import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Person;
import ru.m.tankz.proto.JoinGameResponse; import ru.m.tankz.proto.JoinGameResponse;
import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.JoinGameRequest;
@@ -34,13 +35,14 @@ class GameCenter {
persons = new Map<Int, Int>(); persons = new Map<Int, Int>();
} }
public function getGames():Array<Game> { public function getReadyGames():Array<Game> {
return Lambda.array(games); return Lambda.array(games).filter(function(g) return g.state == GameState.READY);
} }
public function createGame():Game { public function createGame(person:Person):Game {
var game:Game = new Game().setId(game_id++); var game:Game = new Game().setId(game_id++).setState(GameState.READY);
games.set(game.id, game); games.set(game.id, game);
join(person, game.id);
return game; return game;
} }
@@ -57,6 +59,9 @@ class GameCenter {
var game:Game = games.get(persons.get(personId)); var game:Game = games.get(persons.get(personId));
for (person in game.persons) if (person.id == personId) { for (person in game.persons) if (person.id == personId) {
game.persons.remove(person); game.persons.remove(person);
if (game.persons.length == 0) {
games.remove(game.id);
}
break; break;
} }
persons.remove(personId); persons.remove(personId);
@@ -140,11 +145,11 @@ class Session implements IConnectionHandler implements IPacketHandler {
} }
public function onGamesRequest(packet:GamesRequest):Void { public function onGamesRequest(packet:GamesRequest):Void {
connection.send(new GamesResponse().setGames(games.getGames())); connection.send(new GamesResponse().setGames(games.getReadyGames()));
} }
public function onCreateGameRequest(packet:CreateGameRequest):Void { public function onCreateGameRequest(packet:CreateGameRequest):Void {
var game:Game = games.createGame(); var game:Game = games.createGame(person);
connection.send(new CreateGameResponse().setGame(game)); connection.send(new CreateGameResponse().setGame(game));
} }