proto update

This commit is contained in:
2015-08-11 12:14:46 +03:00
parent 0f7f97fe81
commit 64e1cfdbfc
16 changed files with 202 additions and 91 deletions

View File

@@ -5,12 +5,6 @@
"id": "name", "@type": "haxework.gui.LabelView",
"pWidth": 100, "height": 25, "text": ""
},
{
"id": "restart", "@type": "haxework.gui.ButtonView",
"width": 100, "height": 45,
"text": "Restart",
"@style": "button_skin"
},
{
"id": "render", "@type": "ru.m.tankz.render.Render",
"contentSize": true

View File

@@ -1,6 +1,9 @@
package ru.m.tankz.core;
import flash.geom.Point;
import ru.m.tankz.proto.GameActionType;
import ru.m.tankz.proto.GameActionRequest;
import ru.m.core.connect.IConnection;
import haxework.provider.Provider;
import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent;
import flash.Lib;
@@ -10,8 +13,8 @@ class PlayerTank extends Tank {
private var keyBinding:Map<Int, TankAction>;
private var moveQueue:Array<Int>;
public function new(position:Point, keyBinding:Map<Int, TankAction>) {
super(position);
public function new(id:Int, x:Float, y:Float, keyBinding:Map<Int, TankAction>) {
super(id, x, y);
this.keyBinding = keyBinding;
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
@@ -25,7 +28,13 @@ class PlayerTank extends Tank {
moveQueue.unshift(event.keyCode);
updateMove();
}
case TankAction.SHOT: shot();
case TankAction.SHOT:
//ToDo:
//shot();
Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.SHOT)
);
}
}
@@ -43,8 +52,16 @@ class PlayerTank extends Tank {
stop();
} else {
switch (keyBinding.get(moveQueue[0])) {
case TankAction.MOVE(direction): move(direction);
case _: {};
case TankAction.MOVE(direction):
//ToDo:
//move(direction);
Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.MOVE)
.setDirectionX(direction.x)
.setDirectionY(direction.y)
);
case _:
}
}
}

View File

@@ -0,0 +1,30 @@
package ru.m.tankz.game;
import ru.m.tankz.core.Direction;
import ru.m.tankz.core.Tank.TankAction;
import flash.ui.Keyboard;
import ru.m.tankz.core.PlayerTank;
import ru.m.tankz.core.ITank;
class ClientTankz extends Tankz {
public var personId(default, default):Int;
public function new() {
super();
}
override private function buildTank(id:Int, x:Float, y:Float):ITank {
return if (id == personId) {
new PlayerTank(id, x, y, [
Keyboard.A => TankAction.MOVE(Direction.LEFT),
Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
Keyboard.W => TankAction.MOVE(Direction.TOP),
Keyboard.D => TankAction.MOVE(Direction.RIGHT),
Keyboard.SPACE => TankAction.SHOT
]);
} else {
super.buildTank(id, x, y);
}
}
}

View File

@@ -1,48 +1,41 @@
package ru.m.tankz.view.frames;
import ru.m.tankz.game.ClientTankz;
import protohx.Message;
import ru.m.tankz.proto.GameUpdateResponse;
import ru.m.core.connect.IConnection;
import haxework.gui.ViewBuilder;
import ru.m.tankz.config.TankzConfig;
import flash.events.Event;
import ru.m.tankz.game.Tankz;
import ru.m.tankz.game.ITankz;
import haxework.gui.LabelView;
import haxework.gui.ButtonView;
import haxework.provider.Provider;
import ru.m.tankz.data.GameData;
import haxework.gui.VGroupView;
@:template("layout/frames/game.json", "layout/styles.json")
class GameFrame extends VGroupView implements ViewBuilder {
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
private static inline var TAG = "GameFrame";
public static inline var ID = "game";
private var game:ITankz;
private var config:TankzConfig;
private var game:ClientTankz;
public function init():Void {
game = new Tankz();
config = {
map: {
cellWidth: 20,
cellHeight: 20,
gridWidth: 26,
gridHeight: 26
}
};
restart.onPress = this;
game = new ClientTankz();
}
public function onShow():Void {
var person = Provider.get(GameData).person;
var persons = Provider.get(GameData).game.persons;
name.text = person.name;
game.init(config);
game.personId = person.id;
game.init(persons, DEFAULT.CONFIG);
content.addEventListener(Event.ENTER_FRAME, updateGame);
Provider.get(IConnection).packetHandler.addListener(this);
}
public function onHide():Void {
Provider.get(IConnection).packetHandler.removeListener(this);
content.removeEventListener(Event.ENTER_FRAME, updateGame);
game.clear();
}
@@ -52,11 +45,9 @@ class GameFrame extends VGroupView implements ViewBuilder {
render.draw(game);
}
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "restart":
game.clear();
game.init(config);
}
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
}
public function onPacket(packet:Message):Void {}
}

View File

@@ -24,7 +24,6 @@ class GameReadyFrame extends VGroupView implements ViewBuilder implements IPacke
public static inline var ID = "game_ready";
public function init() {
list = findViewById("list");
//list.dispatcher.addListener(this);
findViewById("start", ButtonView).onPress = this;
findViewById("exit", ButtonView).onPress = this;

View File

@@ -1,5 +1,7 @@
package ru.m.tankz;
import ru.m.tankz.proto.GameUpdateResponse;
import ru.m.tankz.proto.GameActionRequest;
import ru.m.core.connect.IConnection;
import protohx.Message;
import ru.m.tankz.proto.LoginRequest;
@@ -41,6 +43,10 @@ class PacketBuilder implements IPacketBuilder {
0x0008 => StartGameResponse,
0x0009 => ExitGameRequest,
0x000a => ExitGameResponse
],
0x03 => [
0x0001 => GameActionRequest,
0x0002 => GameUpdateResponse
]
];

View File

@@ -10,3 +10,14 @@ typedef MapConfig = {
typedef TankzConfig = {
var map:MapConfig;
}
class DEFAULT {
public static var CONFIG:TankzConfig = {
map: {
cellWidth: 20,
cellHeight: 20,
gridWidth: 26,
gridHeight: 26
}
};
}

View File

@@ -1,7 +1,5 @@
package ru.m.tankz.core;
import flash.geom.Point;
class Entity implements IEntity {
public var x(default, default):Float;
@@ -10,8 +8,8 @@ class Entity implements IEntity {
public var width(default, default):Float;
public var height(default, default):Float;
public function new(position:Point) {
x = position.x;
y = position.y;
public function new(x:Float, y:Float) {
this.x = x;
this.y = y;
}
}

View File

@@ -1,6 +1,7 @@
package ru.m.tankz.core;
interface ITank extends IMobileEntity {
public var id(default, null):Int;
public var bullets:Array<IMobileEntity>;
public function shot():Void;

View File

@@ -1,6 +1,5 @@
package ru.m.tankz.core;
import flash.geom.Point;
class MobileEntity extends Entity implements IMobileEntity {
public var mx(default, default):Float;
@@ -9,8 +8,8 @@ class MobileEntity extends Entity implements IMobileEntity {
public var speed(default, null):Float;
public var direction(default, default):Direction;
public function new(position:Point, speed:Float, ?direction:Direction = null) {
super(position);
public function new(x:Float, y:Float, speed:Float, direction:Direction = null) {
super(x, y);
this.speed = speed;
this.direction = direction == null ? Direction.BOTTOM : direction;
}

View File

@@ -1,7 +1,5 @@
package ru.m.tankz.core;
import flash.geom.Point;
enum TankAction {
MOVE(direction:Direction);
SHOT;
@@ -9,10 +7,12 @@ enum TankAction {
class Tank extends MobileEntity implements ITank {
public var id(default, null):Int;
public var bullets:Array<IMobileEntity>;
public function new(position:Point) {
super(position, 4);
public function new(id:Int, x:Float, y:Float) {
super(x, y, 4);
this.id = id;
bullets = new Array<IMobileEntity>();
width = 34;
height = 34;
@@ -20,7 +20,7 @@ class Tank extends MobileEntity implements ITank {
public function shot():Void {
if (bullets.length >= 5) return;
var bullet = new MobileEntity(new Point(x + width / 2 - 5, y + height / 2 - 5), 6, direction);
var bullet = new MobileEntity(x + width / 2 - 5, y + height / 2 - 5, 6, direction);
bullet.width = 10;
bullet.height = 10;
bullet.move(direction);

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.proto.Person;
import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap;
@@ -10,6 +11,6 @@ interface ITankz {
public var tanks(default, null):Array<ITank>;
public function clear():Void;
public function init(config:TankzConfig):Void;
public function init(persons:Array<Person>, config:TankzConfig):Void;
public function update():Void;
}

View File

@@ -1,13 +1,11 @@
package ru.m.tankz.game;
import ru.m.tankz.proto.Person;
import ru.m.tankz.core.Direction;
import ru.m.tankz.core.IMobileEntity;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.geom.Rectangle;
//import flash.geom.Rectangle;
import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.Tank;
import ru.m.tankz.core.PlayerTank;
import ru.m.tankz.map.TankzMap;
import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap;
@@ -27,25 +25,19 @@ class Tankz implements ITankz {
tanks = [];
}
public function init(config:TankzConfig):Void {
private function buildTank(id:Int, x:Float, y:Float):ITank {
return new Tank(id, x, y);
}
public function init(persons:Array<Person>, config:TankzConfig):Void {
this.config = config;
map = new TankzMap(config.map);
tanks = [
new PlayerTank(new Point(0, 0), [
Keyboard.A => TankAction.MOVE(Direction.LEFT),
Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
Keyboard.W => TankAction.MOVE(Direction.TOP),
Keyboard.D => TankAction.MOVE(Direction.RIGHT),
Keyboard.SPACE => TankAction.SHOT
]),
new PlayerTank(new Point(100, 0), [
Keyboard.LEFT => TankAction.MOVE(Direction.LEFT),
Keyboard.DOWN => TankAction.MOVE(Direction.BOTTOM),
Keyboard.UP => TankAction.MOVE(Direction.TOP),
Keyboard.RIGHT => TankAction.MOVE(Direction.RIGHT),
Keyboard.SHIFT => TankAction.SHOT
])
];
tanks = [];
for (person in persons) {
var x = 0;
var y = 100 * persons.indexOf(person);
tanks.push(buildTank(person.id, x, y));
}
x_limit = map.gridWidth * map.cellWidth;
y_limit = map.gridHeight * map.cellHeight;
}
@@ -61,7 +53,7 @@ class Tankz implements ITankz {
tank.x += tank.mx;
tank.y += tank.my;
var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height);
/*var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height);
for (t in tanks) if (t != tank) {
var r = new Rectangle(t.x, t.y, t.width, t.height);
@@ -77,7 +69,7 @@ class Tankz implements ITankz {
if (tank.y < t.y + t.height) tank.y = t.y + t.height;
}
}
}
}*/
if (tank.x < 0) tank.x = 0;
if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width;
@@ -93,7 +85,7 @@ class Tankz implements ITankz {
bullet.x += bullet.mx;
bullet.y += bullet.my;
var bulletR = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.height);
/*var bulletR = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.height);
var i = 0;
while (i < tanks.length) {
@@ -106,7 +98,7 @@ class Tankz implements ITankz {
i--;
}
}
}
}*/
if (bullet.x < 0 || bullet.x + bullet.width > x_limit || bullet.y < 0 || bullet.y + bullet.height > y_limit) {
tank.destroyBullet(bullet);

View File

@@ -85,4 +85,41 @@ message ExitGameRequest {
message ExitGameResponse {
}
/**
Game
*/
enum GameActionType {
MOVE = 1;
SHOT = 2;
}
message GameActionRequest {
required GameActionType type = 1;
optional int32 directionX = 2;
optional int32 directionY = 3;
}
enum GameObjectType {
TANK = 1;
}
enum GameChangeType {
MOVED = 1;
DESTROED = 2;
MODIFIED = 3;
APPEND = 4;
}
message GameChange {
required GameChangeType type = 1;
required GameObjectType objectType = 2;
required int32 objectId = 3;
optional int32 newX = 4;
optional int32 newY = 5;
}
message GameUpdateResponse {
repeated GameChange changes = 1;
}

View File

@@ -4,19 +4,12 @@ import ru.m.tankz.db.Orm;
import ru.m.tankz.proto.Person;
import ru.m.tankz.proto.Account;
typedef DbPerson = {
@:optional var a_id:Int;
var p_id:Int;
var p_name:String;
}
class DbProvider {
class Db {
private var db:orm.Db;
private var orm:Orm;
public function new() {
db = new orm.Db("mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon");
var db = new orm.Db("mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon");
orm = new Orm(db);
}

View File

@@ -1,5 +1,9 @@
package ru.m.tankz.server.session;
import ru.m.tankz.game.Tankz;
import ru.m.tankz.game.ITankz;
import ru.m.tankz.proto.GameActionType;
import ru.m.tankz.proto.GameActionRequest;
import ru.m.tankz.proto.ExitGameResponse;
import ru.m.tankz.proto.ExitGameRequest;
import ru.m.tankz.proto.StartGameRequest;
@@ -18,7 +22,7 @@ import ru.m.tankz.proto.PersonSelectRequest;
import ru.m.tankz.proto.Account;
import ru.m.core.connect.neko.NekoConnection;
import ru.m.tankz.proto.ErrorResponse;
import ru.m.tankz.server.db.Db;
import ru.m.tankz.server.db.DbProvider;
import ru.m.tankz.proto.LoginResponse;
import ru.m.tankz.proto.LoginRequest;
import ru.m.tankz.proto.GamesRequest;
@@ -35,10 +39,13 @@ class GameCenter {
private var created:Map<Int, Int>;
private var persons:Map<Int, Int>;
private var running:Map<Int, ITankz>;
public function new() {
games = new Map<Int, Game>();
created = new Map<Int, Int>();
persons = new Map<Int, Int>();
running = new Map<Int, ITankz>();
}
public function getReadyGames():Array<Game> {
@@ -68,7 +75,7 @@ class GameCenter {
return game;
}
public function exit(personId):Void {
public function exit(personId:Int):Void {
if (persons.exists(personId)) {
var game:Game = games.get(persons.get(personId));
for (person in game.persons) if (person.id == personId) {
@@ -81,12 +88,33 @@ class GameCenter {
persons.remove(personId);
}
}
public function start(gameId:Int):Void {
if (games.exists(gameId)) {
games.get(gameId).setState(GameState.STARTED);
var tankz = new Tankz();
running.set(gameId, tankz);
}
}
public function broadcast(gameId:Int, packet:Message):Void {
var game = games.get(gameId);
if (game != null) {
for (person in game.persons) {
var session = Session.sessions.get(person.id);
if (session != null) {
session.send(packet);
}
}
}
}
}
class Session implements IConnectionHandler implements IPacketHandler {
private static var games:GameCenter = new GameCenter();
public static var sessions:Map<Int, Session> = new Map<Int, Session>();
public var account(default, null):Account;
public var person(default, null):Person;
@@ -99,6 +127,10 @@ class Session implements IConnectionHandler implements IPacketHandler {
this.socket = socket;
}
public function send(packet:Message):Void {
connection.send(packet);
}
public function pushData(bytes:Bytes):Void {
if (connection != null) {
connection.pushData(bytes);
@@ -140,7 +172,7 @@ class Session implements IConnectionHandler implements IPacketHandler {
* Packets handlers
**/
public function onLoginRequest(packet:LoginRequest):Void {
var db = new Db();
var db = new DbProvider();
account = db.getAccount(packet.login, packet.password);
if (account != null) {
connection.send(new LoginResponse().setAccount(account));
@@ -150,10 +182,11 @@ class Session implements IConnectionHandler implements IPacketHandler {
}
public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
var db = new Db();
var db = new DbProvider();
var person = db.getPerson(packet.personId);
if (person != null) {
this.person = person;
sessions.set(person.id, this);
connection.send(new PersonSelectResponse().setPerson(person));
} else {
connection.send(new ErrorResponse().setCode(404).setMessage("Person not found"));
@@ -177,7 +210,7 @@ class Session implements IConnectionHandler implements IPacketHandler {
public function onStartGameRequest(packet:StartGameRequest):Void {
var game:Game = games.getCreatedGame(person.id);
game.setState(GameState.STARTED);
connection.send(new StartGameResponse().setGame(game));
games.broadcast(game.id, new StartGameResponse().setGame(game));
}
public function onExitGameRequest(packet:ExitGameRequest):Void {
@@ -185,6 +218,15 @@ class Session implements IConnectionHandler implements IPacketHandler {
connection.send(new ExitGameResponse());
}
public function onGameActionRequest(packet:GameActionRequest):Void {
switch (packet.type) {
case GameActionType.SHOT:
case GameActionType.MOVE:
}
}
public function onPacket(packet:Message):Void {
trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop());
}