update
This commit is contained in:
@@ -73,13 +73,29 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
|||||||
break;
|
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:
|
case GameChangeType.APPEND:
|
||||||
switch (change.objectType) {
|
switch (change.objectType) {
|
||||||
case GameObjectType.BULLET:
|
case GameObjectType.BULLET:
|
||||||
for (tank in engine.tanks) {
|
for (tank in engine.tanks) {
|
||||||
if (tank.id == change.parentObjectId) {
|
if (tank.id == change.parentObjectId) {
|
||||||
tank.bullets.push(new MobileEntity(0, change.x, change.y, 0, new Direction(change.directionX, change.directionY)));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.view.frames;
|
package ru.m.tankz.view.frames;
|
||||||
|
|
||||||
|
import ru.m.tankz.proto.LeaveGameResponse;
|
||||||
|
import ru.m.tankz.proto.JoinGameResponse;
|
||||||
import haxework.gui.ViewBuilder;
|
import haxework.gui.ViewBuilder;
|
||||||
import ru.m.tankz.proto.ExitGameResponse;
|
import ru.m.tankz.proto.ExitGameResponse;
|
||||||
import haxework.gui.frame.IFrameSwitcher;
|
import haxework.gui.frame.IFrameSwitcher;
|
||||||
@@ -43,7 +45,18 @@ class GameReadyFrame extends VGroupView implements ViewBuilder implements IPacke
|
|||||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onJoinGameResponse(packet:JoinGameResponse):Void {
|
||||||
|
Provider.get(GameData).game = packet.game;
|
||||||
|
list.data = Provider.get(GameData).game.persons;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onLeaveGameResponse(packet:LeaveGameResponse):Void {
|
||||||
|
Provider.get(GameData).game = packet.game;
|
||||||
|
list.data = Provider.get(GameData).game.persons;
|
||||||
|
}
|
||||||
|
|
||||||
public function onExitGameResponse(packet:ExitGameResponse):Void {
|
public function onExitGameResponse(packet:ExitGameResponse):Void {
|
||||||
|
Provider.get(GameData).game = null;
|
||||||
Provider.get(IFrameSwitcher).change(GameListFrame.ID);
|
Provider.get(IFrameSwitcher).change(GameListFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ enum TankAction {
|
|||||||
|
|
||||||
class Tank extends MobileEntity implements ITank {
|
class Tank extends MobileEntity implements ITank {
|
||||||
|
|
||||||
|
private static var bulletIdCounter:Int = -1;
|
||||||
|
|
||||||
public var bullets:Array<IMobileEntity>;
|
public var bullets:Array<IMobileEntity>;
|
||||||
|
|
||||||
public function new(id:Int, x:Float, y:Float) {
|
public function new(id:Int, x:Float, y:Float) {
|
||||||
@@ -19,7 +21,7 @@ class Tank extends MobileEntity implements ITank {
|
|||||||
|
|
||||||
public function shot():Void {
|
public function shot():Void {
|
||||||
if (bullets.length >= 5) return;
|
if (bullets.length >= 5) return;
|
||||||
var bullet = new MobileEntity(0, x + width / 2 - 5, y + height / 2 - 5, 6, direction);
|
var bullet = new MobileEntity(bulletIdCounter--, x + width / 2 - 5, y + height / 2 - 5, 6, direction);
|
||||||
bullet.width = 10;
|
bullet.width = 10;
|
||||||
bullet.height = 10;
|
bullet.height = 10;
|
||||||
bullet.move(direction);
|
bullet.move(direction);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.server.game;
|
package ru.m.tankz.server.game;
|
||||||
|
|
||||||
|
import ru.m.tankz.proto.GameActionType;
|
||||||
|
import ru.m.tankz.proto.GameActionRequest;
|
||||||
import ru.m.tankz.proto.ExitGameResponse;
|
import ru.m.tankz.proto.ExitGameResponse;
|
||||||
import ru.m.tankz.proto.GameObjectType;
|
import ru.m.tankz.proto.GameObjectType;
|
||||||
import ru.m.tankz.proto.GameChangeType;
|
import ru.m.tankz.proto.GameChangeType;
|
||||||
@@ -56,7 +58,6 @@ class NekoTimer {
|
|||||||
typedef ObjectState = {
|
typedef ObjectState = {
|
||||||
var x:Float;
|
var x:Float;
|
||||||
var y:Float;
|
var y:Float;
|
||||||
var d:Direction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,6 +76,8 @@ class GameManager {
|
|||||||
|
|
||||||
private var timer:NekoTimer;
|
private var timer:NekoTimer;
|
||||||
|
|
||||||
|
private var changes:Array<GameChange> = new Array<GameChange>();
|
||||||
|
|
||||||
public function new(person:Person) {
|
public function new(person:Person) {
|
||||||
game = new Game()
|
game = new Game()
|
||||||
.setId(idCounter++)
|
.setId(idCounter++)
|
||||||
@@ -102,7 +105,7 @@ class GameManager {
|
|||||||
public function leave(person:Person) {
|
public function leave(person:Person) {
|
||||||
game.setPersons(game.persons.filter(function(p) return p.id != person.id));
|
game.setPersons(game.persons.filter(function(p) return p.id != person.id));
|
||||||
byPersonId.remove(person.id);
|
byPersonId.remove(person.id);
|
||||||
if (game.persons.length == 0 || person.id == game.creator.id) {
|
if (game.persons.length == 0/* || person.id == game.creator.id*/) {
|
||||||
stop();
|
stop();
|
||||||
} else {
|
} else {
|
||||||
broadcast(new LeaveGameResponse().setGame(game));
|
broadcast(new LeaveGameResponse().setGame(game));
|
||||||
@@ -129,21 +132,25 @@ class GameManager {
|
|||||||
broadcast(new ExitGameResponse());
|
broadcast(new ExitGameResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update() {
|
public function action(person:Person, action:GameActionRequest) {
|
||||||
var states = new Map<Int, ObjectState>();
|
|
||||||
for (tank in engine.tanks) {
|
for (tank in engine.tanks) {
|
||||||
states.set(tank.id, {
|
if (tank.id == person.id) {
|
||||||
x: tank.x,
|
switch (action.type) {
|
||||||
y: tank.y,
|
case GameActionType.SHOT:
|
||||||
d: tank.direction
|
tank.shot();
|
||||||
});
|
var bullet = tank.bullets.slice(0).pop();
|
||||||
}
|
changes.push(new GameChange()
|
||||||
engine.update();
|
.setType(GameChangeType.APPEND)
|
||||||
var changes = new Array<GameChange>();
|
.setObjectType(GameObjectType.BULLET)
|
||||||
for (tank in engine.tanks) {
|
.setParentObjectId(tank.id)
|
||||||
if (states.exists(tank.id)) {
|
.setObjectId(bullet.id)
|
||||||
var state = states.get(tank.id);
|
.setX(bullet.x)
|
||||||
if (state.d != tank.direction) {
|
.setY(bullet.y)
|
||||||
|
.setDirectionX(bullet.direction.x)
|
||||||
|
.setDirectionY(bullet.direction.y)
|
||||||
|
);
|
||||||
|
case GameActionType.MOVE:
|
||||||
|
tank.move(new Direction(action.directionX, action.directionY));
|
||||||
changes.push(new GameChange()
|
changes.push(new GameChange()
|
||||||
.setType(GameChangeType.DIRECTION)
|
.setType(GameChangeType.DIRECTION)
|
||||||
.setObjectType(GameObjectType.TANK)
|
.setObjectType(GameObjectType.TANK)
|
||||||
@@ -151,7 +158,32 @@ class GameManager {
|
|||||||
.setDirectionX(tank.direction.x)
|
.setDirectionX(tank.direction.x)
|
||||||
.setDirectionY(tank.direction.y)
|
.setDirectionY(tank.direction.y)
|
||||||
);
|
);
|
||||||
|
case GameActionType.STOP:
|
||||||
|
tank.stop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function update() {
|
||||||
|
var states = new Map<Int, ObjectState>();
|
||||||
|
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) {
|
if (state.x != tank.x || state.y != tank.y) {
|
||||||
changes.push(new GameChange()
|
changes.push(new GameChange()
|
||||||
.setType(GameChangeType.MOVED)
|
.setType(GameChangeType.MOVED)
|
||||||
@@ -161,8 +193,25 @@ class GameManager {
|
|||||||
.setY(tank.y)
|
.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);
|
||||||
|
this.changes = [];
|
||||||
if (changes.length > 0) {
|
if (changes.length > 0) {
|
||||||
broadcast(new GameUpdateResponse().setChanges(changes));
|
broadcast(new GameUpdateResponse().setChanges(changes));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,19 +144,7 @@ class Session implements IConnectionHandler implements IPacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onGameActionRequest(packet:GameActionRequest):Void {
|
public function onGameActionRequest(packet:GameActionRequest):Void {
|
||||||
var game:IEngine = GameManager.byPersonId.get(person.id).engine;
|
GameManager.byPersonId.get(person.id).action(person, packet);
|
||||||
for (tank in game.tanks) {
|
|
||||||
if (tank.id == person.id) {
|
|
||||||
switch (packet.type) {
|
|
||||||
case GameActionType.SHOT:
|
|
||||||
tank.shot();
|
|
||||||
case GameActionType.MOVE:
|
|
||||||
tank.move(new Direction(packet.directionX, packet.directionY));
|
|
||||||
case GameActionType.STOP:
|
|
||||||
tank.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPacket(packet:Message):Void {
|
public function onPacket(packet:Message):Void {
|
||||||
|
|||||||
Reference in New Issue
Block a user