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

@@ -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,51 +0,0 @@
package ru.m.tankz.core;
import flash.geom.Point;
import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent;
import flash.Lib;
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);
this.keyBinding = keyBinding;
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
if (moveQueue.indexOf(event.keyCode) == -1) {
moveQueue.unshift(event.keyCode);
updateMove();
}
case TankAction.SHOT: shot();
}
}
private function onKeyUp(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
moveQueue.remove(event.keyCode);
updateMove();
case _: {};
}
}
private function updateMove():Void {
if (moveQueue.length == 0) {
stop();
} else {
switch (keyBinding.get(moveQueue[0])) {
case TankAction.MOVE(direction): move(direction);
case _: {};
}
}
}
}

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;
}