This commit is contained in:
2015-08-13 14:18:22 +03:00
parent a37712136e
commit f2cbdbf341
16 changed files with 229 additions and 256 deletions

View File

@@ -14,6 +14,7 @@
<window width="0" height="0" if="html5"/> <window width="0" height="0" if="html5"/>
<haxeflag name="-D" value="swf-gpu"/> <haxeflag name="-D" value="swf-gpu"/>
<haxeflag name="-D" value="native-trace"/> <haxeflag name="-D" value="native-trace"/>
<haxeflag name="-D" value="proto_debug"/>
<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')"/>

View File

@@ -6,3 +6,4 @@
-cp src/server/haxe -cp src/server/haxe
-cp src-gen/haxe -cp src-gen/haxe
-neko target/server.n -neko target/server.n
-D proto_debug

View File

@@ -13,8 +13,8 @@ class PlayerTank extends Tank {
private var keyBinding:Map<Int, TankAction>; private var keyBinding:Map<Int, TankAction>;
private var moveQueue:Array<Int>; private var moveQueue:Array<Int>;
public function new(id:Int, x:Float, y:Float, keyBinding:Map<Int, TankAction>) { public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction, keyBinding:Map<Int, TankAction>) {
super(id, x, y); super(personId, id, x, y, direction);
this.keyBinding = keyBinding; this.keyBinding = keyBinding;
moveQueue = new Array<Int>(); moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

View File

@@ -1,10 +1,9 @@
package ru.m.tankz.engine; package ru.m.tankz.engine;
import ru.m.tankz.core.Direction; import ru.m.tankz.core.Direction;
import ru.m.tankz.core.Tank.TankAction; import ru.m.tankz.core.Tank;
import flash.ui.Keyboard; import flash.ui.Keyboard;
import ru.m.tankz.core.PlayerTank; import ru.m.tankz.core.PlayerTank;
import ru.m.tankz.core.ITank;
class ClientEngine extends Engine { class ClientEngine extends Engine {
@@ -14,9 +13,9 @@ class ClientEngine extends Engine {
super(); super();
} }
override private function buildTank(id:Int, x:Float, y:Float):ITank { override private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
return if (id == personId) { return if (this.personId == personId) {
new PlayerTank(id, x, y, [ new PlayerTank(personId, id, x, y, direction, [
Keyboard.A => TankAction.MOVE(Direction.LEFT), Keyboard.A => TankAction.MOVE(Direction.LEFT),
Keyboard.S => TankAction.MOVE(Direction.BOTTOM), Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
Keyboard.W => TankAction.MOVE(Direction.TOP), Keyboard.W => TankAction.MOVE(Direction.TOP),
@@ -24,7 +23,7 @@ class ClientEngine extends Engine {
Keyboard.SPACE => TankAction.SHOT Keyboard.SPACE => TankAction.SHOT
]); ]);
} else { } else {
super.buildTank(id, x, y); super.buildTank(personId, id, x, y, direction);
} }
} }
} }

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.render; package ru.m.tankz.render;
import ru.m.tankz.core.Bullet;
import ru.m.tankz.core.Tank;
import flash.display.Sprite; import flash.display.Sprite;
import flash.display.Graphics; import flash.display.Graphics;
import haxework.gui.SpriteView; import haxework.gui.SpriteView;
@@ -46,22 +48,23 @@ class Render extends SpriteView implements IRender {
var g:Graphics = tankLayer.graphics; var g:Graphics = tankLayer.graphics;
g.clear(); g.clear();
for (tank in game.tanks) {
g.beginFill(0xff0000);
g.drawRect(tank.x, tank.y, tank.width, tank.height);
g.endFill();
g.beginFill(0x990000);
g.drawRect(
tank.x + tank.width / 2 - tank.width / 8 + tank.direction.x * tank.width / 4,
tank.y + tank.height / 2 - tank.height / 8 + tank.direction.y * tank.height / 4,
tank.width / 4,
tank.height / 4
);
g.endFill();
for (bullet in tank.bullets) { for (e in game.mobileEntities) {
if (Std.is(e, Tank)) {
g.beginFill(0xff0000); g.beginFill(0xff0000);
g.drawRect(bullet.x, bullet.y, bullet.width, bullet.height); g.drawRect(e.x, e.y, e.width, e.height);
g.endFill();
g.beginFill(0x990000);
g.drawRect(
e.x + e.width / 2 - e.width / 8 + e.direction.x * e.width / 4,
e.y + e.height / 2 - e.height / 8 + e.direction.y * e.height / 4,
e.width / 4,
e.height / 4
);
g.endFill();
} else if (Std.is(e, Bullet)) {
g.beginFill(0xff0000);
g.drawRect(e.x, e.y, e.width, e.height);
g.endFill(); g.endFill();
} }
} }

View File

@@ -1,9 +1,5 @@
package ru.m.tankz.view.frames; package ru.m.tankz.view.frames;
import ru.m.tankz.core.MobileEntity;
import ru.m.tankz.core.Direction;
import ru.m.tankz.proto.GameObjectType;
import ru.m.tankz.proto.GameChangeType;
import ru.m.tankz.engine.ClientEngine; import ru.m.tankz.engine.ClientEngine;
import protohx.Message; import protohx.Message;
import ru.m.tankz.proto.GameUpdateResponse; import ru.m.tankz.proto.GameUpdateResponse;
@@ -33,7 +29,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
var persons = Provider.get(GameData).game.persons; var persons = Provider.get(GameData).game.persons;
name.text = person.name; name.text = person.name;
engine.personId = person.id; engine.personId = person.id;
engine.init(persons, DEFAULT.CONFIG); engine.init(DEFAULT.CONFIG);
content.addEventListener(Event.ENTER_FRAME, updateGame); content.addEventListener(Event.ENTER_FRAME, updateGame);
Provider.get(IConnection).packetHandler.addListener(this); Provider.get(IConnection).packetHandler.addListener(this);
render.draw(engine); render.draw(engine);
@@ -51,57 +47,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
} }
public function onGameUpdateResponse(packet:GameUpdateResponse):Void { public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
for (change in packet.changes) { engine.updateFromChanges(packet.changes);
switch (change.type) {
case GameChangeType.DIRECTION:
switch (change.objectType) {
case GameObjectType.TANK:
for (tank in engine.tanks) {
if (tank.id == change.objectId) {
tank.direction = new Direction(change.directionX, change.directionY);
break;
}
}
}
case GameChangeType.MOVED:
switch (change.objectType) {
case GameObjectType.TANK:
for (tank in engine.tanks) {
if (tank.id == change.objectId) {
tank.x = change.x;
tank.y = change.y;
break;
}
}
case GameObjectType.BULLET:
for (tank in engine.tanks) {
if (tank.id == change.parentObjectId) {
for (bullet in tank.bullets) {
if (bullet.id == change.objectId) {
bullet.x = change.x;
bullet.y = change.y;
break;
}
}
break;
}
}
}
case GameChangeType.APPEND:
switch (change.objectType) {
case GameObjectType.BULLET:
for (tank in engine.tanks) {
if (tank.id == change.parentObjectId) {
var bullet = new MobileEntity(change.objectId, change.x, change.y, 0, new Direction(change.directionX, change.directionY));
bullet.width = 10;
bullet.height = 10;
tank.bullets.push(bullet);
break;
}
}
}
}
}
render.draw(engine); render.draw(engine);
} }

View File

@@ -42,11 +42,11 @@ class BaseConnection implements IConnection {
} }
} }
public function send(packet:Message):Void { public function send(packet:Message):Void {
//L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end
} }
public function receive(packet:Message):Void { public function receive(packet:Message):Void {
//L.d("Receive", Type.getClassName(Type.getClass(packet)).split(".").pop()); #if proto_debug L.d("Receive", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end
var name = "on" + Type.getClassName(Type.getClass(packet)).split(".").pop(); var name = "on" + Type.getClassName(Type.getClass(packet)).split(".").pop();
packetHandler.dispatch(function(h) { packetHandler.dispatch(function(h) {
var method = Reflect.field(h, name); var method = Reflect.field(h, name);

View File

@@ -17,7 +17,7 @@ class NekoWebConnection extends NekoConnection {
} }
override public function send(packet:Message):Void { override public function send(packet:Message):Void {
//L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end
try { try {
var data = WebSocketTools.packet2string(packet, builder); var data = WebSocketTools.packet2string(packet, builder);
writeData(data, socket); writeData(data, socket);

View File

@@ -0,0 +1,13 @@
package ru.m.tankz.core;
class Bullet extends MobileEntity {
public var personId(default, null):Int;
public function new(personId:Int, id:Int, x:Float, y:Float, speed:Float, direction:Direction) {
super(id, x, y, speed, direction);
this.personId = personId;
this.width = 10;
this.height = 10;
}
}

View File

@@ -1,8 +0,0 @@
package ru.m.tankz.core;
interface ITank extends IMobileEntity {
public var bullets:Array<IMobileEntity>;
public function shot():Void;
public function destroyBullet(bullet:IMobileEntity):Void;
}

View File

@@ -1,6 +1,8 @@
package ru.m.tankz.core; package ru.m.tankz.core;
class MobileEntity extends Entity implements IMobileEntity { class MobileEntity extends Entity implements IMobileEntity {
private static var idCounter:Int = 0;
public var id(default, null):Int; public var id(default, null):Int;
public var mx(default, default):Float = 0; public var mx(default, default):Float = 0;
@@ -9,9 +11,9 @@ class MobileEntity extends Entity implements IMobileEntity {
public var speed(default, null):Float = 0; public var speed(default, null):Float = 0;
public var direction(default, default):Direction; public var direction(default, default):Direction;
public function new(id:Int, x:Float, y:Float, speed:Float, direction:Direction = null) { public function new(id:Int = 0, x:Float = 0, y:Float = 0, speed:Float = 0, direction:Direction = null) {
super(x, y); super(x, y);
this.id = id; this.id = id == 0 ? ++idCounter : id;
this.speed = speed; this.speed = speed;
this.direction = direction == null ? Direction.BOTTOM : direction; this.direction = direction == null ? Direction.BOTTOM : direction;
} }

View File

@@ -5,30 +5,28 @@ enum TankAction {
SHOT; SHOT;
} }
class Tank extends MobileEntity implements ITank { class Tank extends MobileEntity {
private static var bulletIdCounter:Int = -1; public var personId(default, null):Int;
public var bullets:Array<IMobileEntity>; public var bulletsCount:Int = 0;
public function new(id:Int, x:Float, y:Float) { public function new(personId:Int, id:Int, x:Float, y:Float, direction:Directiondo apt-g) {
super(id, x, y, 4); super(id, x, y, 4, direction);
this.id = id; this.personId = personId;
bullets = new Array<IMobileEntity>();
width = 34; width = 34;
height = 34; height = 34;
} }
public function shot():Void { public function shot():Null<Bullet> {
if (bullets.length >= 5) return; if (bulletsCount >= 5) return null;
var bullet = new MobileEntity(bulletIdCounter--, x + width / 2 - 5, y + height / 2 - 5, 6, direction); var bullet = new Bullet(personId, 0, x + width / 2 - 5, y + height / 2 - 5, 6, direction);
bullet.width = 10;
bullet.height = 10;
bullet.move(direction); bullet.move(direction);
bullets.push(bullet); bulletsCount++;
return bullet;
} }
public function destroyBullet(bullet:IMobileEntity):Void { public function onDestroyBullet():Void {
bullets.remove(bullet); bulletsCount--;
} }
} }

View File

@@ -1,5 +1,9 @@
package ru.m.tankz.engine; package ru.m.tankz.engine;
import ru.m.tankz.core.Bullet;
import ru.m.tankz.proto.GameObjectType;
import ru.m.tankz.proto.GameChangeType;
import ru.m.tankz.proto.GameChange;
import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Person;
import ru.m.tankz.core.Direction; import ru.m.tankz.core.Direction;
import ru.m.tankz.core.IMobileEntity; import ru.m.tankz.core.IMobileEntity;
@@ -7,14 +11,15 @@ import ru.m.tankz.core.IMobileEntity;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.map.TankzMap; import ru.m.tankz.map.TankzMap;
import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ITankzMap;
class Engine implements IEngine { class Engine implements IEngine {
public var config(default, default):TankzConfig; public var config(default, default):TankzConfig;
public var map(default, null):ITankzMap; public var map(default, null):ITankzMap;
public var tanks(default, null):Array<ITank>;
public var tanks(default, null):Map<Int, Tank>;
public var mobileEntities(default, null):Map<Int, IMobileEntity>;
private var x_limit:Float; private var x_limit:Float;
private var y_limit:Float; private var y_limit:Float;
@@ -22,87 +27,144 @@ class Engine implements IEngine {
public function new() {} public function new() {}
public function clear():Void { public function clear():Void {
tanks = []; tanks = new Map<Int, Tank>();
} }
private function buildTank(id:Int, x:Float, y:Float):ITank { private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
return new Tank(id, x, y); return new Tank(personId, id, x, y, direction);
} }
public function init(persons:Array<Person>, config:TankzConfig):Void { public function init(config:TankzConfig):Void {
this.config = config; this.config = config;
map = new TankzMap(config.map); map = new TankzMap(config.map);
tanks = []; tanks = new Map<Int, Tank>();
for (person in persons) { mobileEntities = new Map<Int, IMobileEntity>();
var x = 0;
var y = 100 * persons.indexOf(person);
tanks.push(buildTank(person.id, x, y));
}
x_limit = map.gridWidth * map.cellWidth; x_limit = map.gridWidth * map.cellWidth;
y_limit = map.gridHeight * map.cellHeight; y_limit = map.gridHeight * map.cellHeight;
} }
public function update():Void { public function initTanks(persons:Array<Person>):Array<GameChange> {
for (tank in tanks) { var changes = new Array<GameChange>();
if (tank.direction.x != 0) { for (person in persons) {
tank.y = Math.round((tank.y + tank.height / 2) / config.map.cellHeight) * config.map.cellHeight - tank.height / 2; var x = 0;
} var y = 100 * persons.indexOf(person);
if (tank.direction.y != 0) { var tank = buildTank(person.id, 0, x, y, Direction.BOTTOM);
tank.x = Math.round((tank.x + tank.width / 2) / config.map.cellWidth) * config.map.cellWidth - tank.width / 2; this.tanks.set(tank.personId, tank);
} this.mobileEntities.set(tank.id, tank);
tank.x += tank.mx; changes.push(new GameChange()
tank.y += tank.my; .setType(GameChangeType.APPEND)
.setObjectType(GameObjectType.TANK)
.setPersonId(tank.personId)
.setObjectId(tank.id)
.setX(tank.x)
.setY(tank.y)
.setDirectionX(tank.direction.x)
.setDirectionY(tank.direction.y)
);
}
return changes;
}
/*var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height); public function updateFromChanges(changes:Array<GameChange>):Void {
for (change in changes) {
for (t in tanks) if (t != tank) { switch (change.type) {
var r = new Rectangle(t.x, t.y, t.width, t.height); case GameChangeType.APPEND:
if (tankR.intersects(r)) { switch (change.objectType) {
if (tank.direction.x > 0) { case GameObjectType.TANK:
if (tank.x + tank.width > t.x) tank.x = t.x - tank.width; var tank:Tank = buildTank(change.personId, change.objectId, change.x, change.y, new Direction(change.directionY, change.directionY));
} else if (tank.direction.x < 0) { mobileEntities.set(tank.id, tank);
if (tank.x < t.x + t.width) tank.x = t.x + t.width; tanks.set(tank.personId, tank);
case GameObjectType.BULLET:
var bullet:Bullet = new Bullet(change.personId, change.objectId, change.x, change.y, 0, new Direction(change.directionY, change.directionY));
mobileEntities.set(bullet.id, bullet);
} }
if (tank.direction.y > 0) { case GameChangeType.DESTROED:
if (tank.y + tank.height > t.y) tank.y = t.y - tank.height; mobileEntities.remove(change.objectId);
} else if (tank.direction.y < 0) { case GameChangeType.DIRECTION:
if (tank.y < t.y + t.height) tank.y = t.y + t.height; var target = mobileEntities.get(change.objectId);
} target.direction = new Direction(change.directionX, change.directionY);
} case GameChangeType.MOVED:
}*/ var target = mobileEntities.get(change.objectId);
target.x = change.x;
if (tank.x < 0) tank.x = 0; target.y = change.y;
if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width; case GameChangeType.MODIFIED:
if (tank.y < 0) tank.y = 0; //
if (tank.y + tank.height > y_limit) tank.y = y_limit - tank.height; }
updateBullets(tank);
} }
} }
private function updateBullets(tank:ITank):Void { public function update():Array<GameChange> {
for (bullet in tank.bullets) { var changes = new Array<GameChange>();
bullet.x += bullet.mx;
bullet.y += bullet.my;
/*var bulletR = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.height); for (entiny in mobileEntities) {
var objectType = -1;
var personId = Reflect.hasField(entiny, "personId") ? Reflect.field(entiny, "personId") : -1;
if (Std.is(entiny, Tank)) objectType = GameObjectType.TANK;
if (Std.is(entiny, Bullet)) objectType = GameObjectType.BULLET;
var i = 0; if (objectType == GameObjectType.TANK) {
while (i < tanks.length) { if (entiny.direction.x != 0) {
var t = tanks[i++]; entiny.y = Math.round((entiny.y + entiny.height / 2) / config.map.cellHeight) * config.map.cellHeight - entiny.height / 2;
if (t != tank) { }
if (entiny.direction.y != 0) {
entiny.x = Math.round((entiny.x + entiny.width / 2) / config.map.cellWidth) * config.map.cellWidth - entiny.width / 2;
}
}
if (entiny.mx != 0 || entiny.my != 0) {
entiny.x += entiny.mx;
entiny.y += entiny.my;
/*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); var r = new Rectangle(t.x, t.y, t.width, t.height);
if (bulletR.intersects(r)) { if (tankR.intersects(r)) {
tank.destroyBullet(bullet); if (tank.direction.x > 0) {
tanks.remove(t); if (tank.x + tank.width > t.x) tank.x = t.x - tank.width;
i--; } else if (tank.direction.x < 0) {
if (tank.x < t.x + t.width) tank.x = t.x + t.width;
}
if (tank.direction.y > 0) {
if (tank.y + tank.height > t.y) tank.y = t.y - tank.height;
} else if (tank.direction.y < 0) {
if (tank.y < t.y + t.height) tank.y = t.y + t.height;
}
}
}*/
if (objectType == GameObjectType.TANK) {
if (entiny.x < 0) entiny.x = 0;
if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width;
if (entiny.y < 0) entiny.y = 0;
if (entiny.y + entiny.height > y_limit) entiny.y = y_limit - entiny.height;
}
changes.push(new GameChange()
.setType(GameChangeType.MOVED)
.setObjectType(objectType)
.setPersonId(personId)
.setObjectId(entiny.id)
.setX(entiny.x)
.setY(entiny.y)
);
if (objectType == GameObjectType.BULLET) {
if (entiny.x < 0 || entiny.x + entiny.width > x_limit || entiny.y < 0 || entiny.y + entiny.height > y_limit) {
mobileEntities.remove(entiny.id);
var tank = tanks.get(personId);
tank.onDestroyBullet();
changes.push(new GameChange()
.setType(GameChangeType.DESTROED)
.setObjectType(objectType)
.setPersonId(personId)
.setObjectId(entiny.id)
);
} }
} }
}*/
if (bullet.x < 0 || bullet.x + bullet.width > x_limit || bullet.y < 0 || bullet.y + bullet.height > y_limit) {
tank.destroyBullet(bullet);
} }
} }
return changes;
} }
} }

View File

@@ -1,16 +1,21 @@
package ru.m.tankz.engine; package ru.m.tankz.engine;
import ru.m.tankz.proto.GameChange;
import ru.m.tankz.core.IMobileEntity;
import ru.m.tankz.core.Tank;
import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Person;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ITankzMap;
interface IEngine { interface IEngine {
public var config(default, default):TankzConfig; public var config(default, default):TankzConfig;
public var map(default, null):ITankzMap; public var map(default, null):ITankzMap;
public var tanks(default, null):Array<ITank>; public var tanks(default, null):Map<Int, Tank>;
public var mobileEntities(default, null):Map<Int, IMobileEntity>;
public function clear():Void; public function clear():Void;
public function init(persons:Array<Person>, config:TankzConfig):Void; public function init(config:TankzConfig):Void;
public function update():Void; public function initTanks(persons:Array<Person>):Array<GameChange>;
public function updateFromChanges(changes:Array<GameChange>):Void;
public function update():Array<GameChange>;
} }

View File

@@ -126,8 +126,8 @@ enum GameChangeType {
message GameChange { message GameChange {
required GameChangeType type = 1; required GameChangeType type = 1;
required GameObjectType objectType = 2; required GameObjectType objectType = 2;
required int32 objectId = 3; optional int32 personId = 3;
optional int32 parentObjectId = 4; required int32 objectId = 4;
optional float x = 5; optional float x = 5;
optional float y = 6; optional float y = 6;
optional int32 directionX = 7; optional int32 directionX = 7;

View File

@@ -52,14 +52,6 @@ class NekoTimer {
} }
} }
/**
*
**/
typedef ObjectState = {
var x:Float;
var y:Float;
}
/** /**
* *
**/ **/
@@ -115,10 +107,12 @@ class GameManager {
public function start() { public function start() {
game.setState(GameState.STARTED); game.setState(GameState.STARTED);
engine = new Engine(); engine = new Engine();
engine.init(game.persons, DEFAULT.CONFIG); engine.init(DEFAULT.CONFIG);
var changes = engine.initTanks(game.persons);
timer = new NekoTimer(30); timer = new NekoTimer(30);
timer.run = update; timer.run = update;
broadcast(new StartGameResponse().setGame(game)); broadcast(new StartGameResponse().setGame(game));
broadcast(new GameUpdateResponse().setChanges(changes));
} }
public function stop() { public function stop() {
@@ -133,83 +127,40 @@ class GameManager {
} }
public function action(person:Person, action:GameActionRequest) { public function action(person:Person, action:GameActionRequest) {
for (tank in engine.tanks) { var tank = engine.tanks.get(person.id);
if (tank.id == person.id) { switch (action.type) {
switch (action.type) { case GameActionType.SHOT:
case GameActionType.SHOT: var bullet = tank.shot();
tank.shot(); if (bullet != null) {
var bullet = tank.bullets.slice(0).pop(); engine.mobileEntities.set(bullet.id, bullet);
changes.push(new GameChange() changes.push(new GameChange()
.setType(GameChangeType.APPEND) .setType(GameChangeType.APPEND)
.setObjectType(GameObjectType.BULLET) .setObjectType(GameObjectType.BULLET)
.setParentObjectId(tank.id) .setPersonId(bullet.personId)
.setObjectId(bullet.id) .setObjectId(bullet.id)
.setX(bullet.x) .setX(bullet.x)
.setY(bullet.y) .setY(bullet.y)
.setDirectionX(bullet.direction.x) .setDirectionX(bullet.direction.x)
.setDirectionY(bullet.direction.y) .setDirectionY(bullet.direction.y)
); );
case GameActionType.MOVE:
tank.move(new Direction(action.directionX, action.directionY));
changes.push(new GameChange()
.setType(GameChangeType.DIRECTION)
.setObjectType(GameObjectType.TANK)
.setObjectId(tank.id)
.setDirectionX(tank.direction.x)
.setDirectionY(tank.direction.y)
);
case GameActionType.STOP:
tank.stop();
} }
} case GameActionType.MOVE:
tank.move(new Direction(action.directionX, action.directionY));
changes.push(new GameChange()
.setType(GameChangeType.DIRECTION)
.setObjectType(GameObjectType.TANK)
.setPersonId(tank.personId)
.setObjectId(tank.id)
.setDirectionX(tank.direction.x)
.setDirectionY(tank.direction.y)
);
case GameActionType.STOP:
tank.stop();
} }
} }
private function update() { private function update() {
var states = new Map<Int, ObjectState>(); var changes = engine.update();
for (tank in engine.tanks) {
states.set(tank.id, {
x: tank.x,
y: tank.y
});
for (bullet in tank.bullets) {
states.set(bullet.id, {
x: bullet.x,
y: bullet.y
});
}
}
engine.update();
var changes = new Array<GameChange>();
for (tank in engine.tanks) {
if (states.exists(tank.id)) {
var state = states.get(tank.id);
if (state.x != tank.x || state.y != tank.y) {
changes.push(new GameChange()
.setType(GameChangeType.MOVED)
.setObjectType(GameObjectType.TANK)
.setObjectId(tank.id)
.setX(tank.x)
.setY(tank.y)
);
}
for (bullet in tank.bullets) {
if (states.exists(bullet.id)) {
var state = states.get(bullet.id);
if (state.x != bullet.x || state.y != bullet.y) {
changes.push(new GameChange()
.setType(GameChangeType.MOVED)
.setObjectType(GameObjectType.BULLET)
.setParentObjectId(tank.id)
.setObjectId(bullet.id)
.setX(bullet.x)
.setY(bullet.y)
);
}
}
}
}
}
changes = this.changes.concat(changes); changes = this.changes.concat(changes);
this.changes = []; this.changes = [];
if (changes.length > 0) { if (changes.length > 0) {