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="-debug"/>
<haxeflag name="--macro" value="Meta.set('0.0.0')"/>
<haxeflag name="--macro" value="include('ru.m.tankz')"/>
</project>

View File

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

View File

@@ -1,135 +1,174 @@
{
"type":"haxework.frame.FrameSwitcher",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"},
"views":[
"type": "haxework.gui.VGroupView",
"pWidth": 100, "pHeight": 100,
"views": [
{
"id":"auth", "type":"ru.m.tankz.view.frames.AuthFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth":100, "pHeight":100, "layoutMargin":3,
"views":[
"id":"top", "type": "haxework.gui.HGroupView",
"pWidth": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ColorSkin", "color": "0x105000"},
"views": [
{
"id":"login", "type":"haxework.gui.HGroupView",
"contentSize":true,
"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": [
{
"id": "auth", "type": "ru.m.tankz.view.frames.AuthFrame",
"pWidth": 100, "pHeight": 100, "layoutMargin": 3,
"views": [
{
"type":"haxework.gui.LabelView",
"width":150, "height":25, "text":"Login"
"id": "login", "type": "haxework.gui.HGroupView",
"contentSize": true,
"views": [
{
"type": "haxework.gui.LabelView",
"width": 150, "height": 25, "text": "Login"
},
{
"id": "input", "type": "haxework.gui.InputView",
"skin": {"type": "haxework.gui.skin.ColorSkin", "color": "0xffffff"},
"width": 200, "height": 25, "text": ""
}
]
},
{
"id":"input", "type":"haxework.gui.InputView",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffffff"},
"width":200, "height":25, "text":""
}
]
},
{
"id":"password", "type":"haxework.gui.HGroupView",
"contentSize":true,
"views":[
{
"type":"haxework.gui.LabelView",
"width":150, "height":25, "text":"Password"
"id": "password", "type": "haxework.gui.HGroupView",
"contentSize": true,
"views": [
{
"type": "haxework.gui.LabelView",
"width": 150, "height": 25, "text": "Password"
},
{
"id": "input", "type": "haxework.gui.InputView",
"skin": {"type": "haxework.gui.skin.ColorSkin", "color": "0xffffff"},
"width": 200, "height": 25, "text": ""
}
]
},
{
"id":"input", "type":"haxework.gui.InputView",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffffff"},
"width":200, "height":25, "text":""
"id": "auth",
"type": "haxework.gui.ButtonView",
"width": 100,
"height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Auth"
}
]
},
{
"id":"auth",
"type":"haxework.gui.ButtonView",
"width":100,
"height":30,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Auth"
}
]
},
{
"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,
"views":[
{
"id":"list", "type":"haxework.gui.list.VListView",
"factory":{
"type":"~ru.m.tankz.view.frames.person.PersonView"
},
"scroll":{
"type":"haxework.gui.list.VScrollView",
"skin":{"type":"haxework.gui.list.VScrollSkin"}
},
"pWidth":100, "pHeight":100, "layoutMargin":5
}
]
},
{
"id":"game_list", "type":"ru.m.tankz.view.frames.GameListFrame",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth":100, "pHeight":100,
"views":[
{
"id":"name", "type":"haxework.gui.LabelView",
"pWidth":100, "height":25, "text":""
},
{
"id":"create", "type":"haxework.gui.ButtonView",
"width":100, "height":30,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"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",
"factory":{
"type":"~ru.m.tankz.view.frames.game.GameView"
},
"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",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
"pWidth":100, "pHeight":100,
"views":[
{
"id":"name", "type":"haxework.gui.LabelView",
"pWidth":100, "height":25, "text":""
"id": "person_list", "type": "ru.m.tankz.view.frames.PersonListFrame",
"pWidth": 100, "pHeight": 100, "layoutMargin": 3, "paddings": 10,
"views": [
{
"id": "list", "type": "haxework.gui.list.VListView",
"factory": {
"type": "~ru.m.tankz.view.frames.person.PersonView"
},
"scroll": {
"type": "haxework.gui.list.VScrollView",
"skin": {"type": "haxework.gui.list.VScrollSkin"}
},
"pWidth": 100, "pHeight": 100, "layoutMargin": 5
}
]
},
{
"id":"logout", "type":"haxework.gui.ButtonView",
"width":100, "height":30,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Logout"
"id": "game_list", "type": "ru.m.tankz.view.frames.GameListFrame",
"pWidth": 100, "pHeight": 100,
"views": [
{
"id": "name", "type": "haxework.gui.LabelView",
"pWidth": 100, "height": 25, "text": ""
},
{
"id": "create", "type": "haxework.gui.ButtonView",
"width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Create"
},
{
"id": "list", "type": "haxework.gui.list.VListView",
"factory": {
"type": "~ru.m.tankz.view.frames.game.GameView"
},
"scroll": {
"type": "haxework.gui.list.VScrollView",
"skin": {"type": "haxework.gui.list.VScrollSkin"}
},
"pWidth": 100, "pHeight": 100, "layoutMargin": 5
}
]
},
{
"id":"restart", "type":"haxework.gui.ButtonView",
"width":100, "height":30,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Restart"
"id": "game_ready", "type": "ru.m.tankz.view.frames.GameReadyFrame",
"pWidth": 100, "pHeight": 100,
"views": [
{
"id": "name", "type": "haxework.gui.LabelView",
"pWidth": 100, "height": 25, "text": ""
},
{
"id": "start", "type": "haxework.gui.ButtonView",
"width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"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":"render", "type":"ru.m.tankz.render.Render",
"contentSize":true
"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",
"width": 100, "height": 30,
"skin": {"type": "haxework.gui.skin.ButtonColorSkin"},
"text": "Restart"
},
{
"id": "render", "type": "ru.m.tankz.render.Render",
"contentSize": true
}
]
}
]
}

View File

@@ -1,5 +1,9 @@
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 flash.text.TextFieldType;
import flash.Lib;
@@ -31,13 +35,18 @@ class Client implements IConnectionHandler {
}
private var view:IGroupView<Sprite>;
private var logout:ButtonView;
private var switcher:FrameSwitcher;
public function new() {
var bytes = Assets.getBytes("res/layout/main.json");
var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable));
switcher = GuiBuilder.build(form, {listener:this});
new Root(switcher);
view = GuiBuilder.build(form, {listener:this});
new Root(view);
switcher = view.findViewById("switcher");
logout = view.findViewById("top:logout");
logout.onPress = this;
Provider.setFactory(GameData, GameData);
Provider.set(IFrameSwitcher, switcher);
@@ -58,6 +67,13 @@ class Client implements IConnectionHandler {
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 onDisconnected():Void {

View File

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

View File

@@ -34,7 +34,6 @@ class GameListFrame extends VGroupView implements IPacketHandler implements List
public function init() {
list = findViewById("list");
list.dispatcher.addListener(this);
findViewById("logout", 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 {
list.data.push(packet.game);
list.update();
//list.data.push(packet.game);
//list.update();
Provider.get(GameData).game = packet.game;
Provider.get(IFrameSwitcher).change(GameReadyFrame.ID);
}
public function onJoinGameResponse(packet:JoinGameResponse):Void {
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 onPress(view:ButtonView):Void {
switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
case "create":
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,9 +43,11 @@ class FlashConnection extends BaseConnection {
}
override public function disconnect():Void {
socket.close();
connected = false;
if (handler != null) handler.onDisconnected();
if (socket.connected) {
socket.close();
connected = false;
if (handler != null) handler.onDisconnected();
}
}
private function onError(event:ErrorEvent):Void {

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 {
switch (keyBinding.get(event.keyCode)) {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
if (moveQueue.indexOf(event.keyCode) == -1) {
moveQueue.unshift(event.keyCode);
@@ -30,7 +30,7 @@ class PlayerTank extends Tank {
}
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):
moveQueue.remove(event.keyCode);
updateMove();

View File

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