[client] PlayerTank to PlayerControl change
This commit is contained in:
@@ -2,7 +2,8 @@ set :npm_path, -> { release_path }
|
|||||||
|
|
||||||
namespace :npm do
|
namespace :npm do
|
||||||
task :prepare do
|
task :prepare do
|
||||||
on roles(:all) do
|
npm_role = fetch(:npm_role)
|
||||||
|
on roles(npm_role) do
|
||||||
npm_target_path = fetch(:npm_target_path)
|
npm_target_path = fetch(:npm_target_path)
|
||||||
npm_path = fetch(:npm_path)
|
npm_path = fetch(:npm_path)
|
||||||
execute "mkdir -p #{npm_target_path}"
|
execute "mkdir -p #{npm_target_path}"
|
||||||
@@ -10,7 +11,8 @@ namespace :npm do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
task :link do
|
task :link do
|
||||||
on roles(:all) do
|
npm_role = fetch(:npm_role)
|
||||||
|
on roles(npm_role) do
|
||||||
npm_target_path = fetch(:npm_target_path)
|
npm_target_path = fetch(:npm_target_path)
|
||||||
npm_path = fetch(:npm_path)
|
npm_path = fetch(:npm_path)
|
||||||
execute "ln -s #{npm_target_path}/node_modules #{npm_path}/"
|
execute "ln -s #{npm_target_path}/node_modules #{npm_path}/"
|
||||||
|
|||||||
89
src/client/haxe/ru/m/tankz/core/PlayerControl.hx
Normal file
89
src/client/haxe/ru/m/tankz/core/PlayerControl.hx
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package ru.m.tankz.core;
|
||||||
|
|
||||||
|
import flash.events.FocusEvent;
|
||||||
|
import flash.ui.Keyboard;
|
||||||
|
import ru.m.tankz.engine.IEngine;
|
||||||
|
import ru.m.tankz.core.Tank.TankAction;
|
||||||
|
import flash.events.KeyboardEvent;
|
||||||
|
import flash.Lib;
|
||||||
|
|
||||||
|
class PlayerControl {
|
||||||
|
|
||||||
|
private var engine:IEngine;
|
||||||
|
private var id:Int;
|
||||||
|
|
||||||
|
private var keyBinding:Map<Int, TankAction>;
|
||||||
|
private var moveQueue:Array<Int>;
|
||||||
|
|
||||||
|
public function new(id:Int, engine:IEngine, keyBinding:Map<Int, TankAction>) {
|
||||||
|
this.id = id;
|
||||||
|
this.engine = engine;
|
||||||
|
this.keyBinding = keyBinding;
|
||||||
|
moveQueue = new Array<Int>();
|
||||||
|
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||||
|
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
||||||
|
Lib.current.stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
engine.action(id, TankAction.SHOT);
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 onFocusOut(event:FocusEvent):Void {
|
||||||
|
moveQueue = [];
|
||||||
|
updateMove();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateMove():Void {
|
||||||
|
if (moveQueue.length == 0) {
|
||||||
|
engine.action(id, TankAction.STOP);
|
||||||
|
} else {
|
||||||
|
switch (keyBinding.get(moveQueue[0])) {
|
||||||
|
case TankAction.MOVE(direction):
|
||||||
|
engine.action(id, TankAction.MOVE(direction));
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function forPlayer(index:Int, tankId:Int, engine:IEngine):PlayerControl {
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return new PlayerControl(tankId, engine, [
|
||||||
|
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
|
||||||
|
]);
|
||||||
|
case 1:
|
||||||
|
return new PlayerControl(tankId, engine, [
|
||||||
|
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.NUMPAD_0 => TankAction.SHOT
|
||||||
|
]);
|
||||||
|
case _:
|
||||||
|
throw 'Invalid player index ${index}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
package ru.m.tankz.core;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
class PlayerTank extends Tank {
|
|
||||||
|
|
||||||
private var keyBinding:Map<Int, TankAction>;
|
|
||||||
private var moveQueue:Array<Int>;
|
|
||||||
|
|
||||||
public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction, keyBinding:Map<Int, TankAction>) {
|
|
||||||
super(personId, id, x, y, direction);
|
|
||||||
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:
|
|
||||||
//ToDo:
|
|
||||||
shot();
|
|
||||||
/*Provider.get(IConnection).send(
|
|
||||||
new GameActionRequest()
|
|
||||||
.setType(GameActionType.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();
|
|
||||||
/*Provider.get(IConnection).send(
|
|
||||||
new GameActionRequest()
|
|
||||||
.setType(GameActionType.STOP)
|
|
||||||
);*/
|
|
||||||
} else {
|
|
||||||
switch (keyBinding.get(moveQueue[0])) {
|
|
||||||
case TankAction.MOVE(direction):
|
|
||||||
//ToDo:
|
|
||||||
move(direction);
|
|
||||||
/*Provider.get(IConnection).send(
|
|
||||||
new GameActionRequest()
|
|
||||||
.setType(GameActionType.MOVE)
|
|
||||||
.setDirectionX(direction.x)
|
|
||||||
.setDirectionY(direction.y)
|
|
||||||
);*/
|
|
||||||
case _:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,5 +7,6 @@ import ru.m.tankz.proto.Account;
|
|||||||
class GameData {
|
class GameData {
|
||||||
public var account:Account;
|
public var account:Account;
|
||||||
public var person:Person;
|
public var person:Person;
|
||||||
|
public var players:Array<Person>;
|
||||||
public var game:Game;
|
public var game:Game;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package ru.m.tankz.engine;
|
|
||||||
|
|
||||||
import ru.m.tankz.core.Direction;
|
|
||||||
import ru.m.tankz.core.Tank;
|
|
||||||
import flash.ui.Keyboard;
|
|
||||||
import ru.m.tankz.core.PlayerTank;
|
|
||||||
|
|
||||||
class ClientEngine extends Engine {
|
|
||||||
|
|
||||||
public var personId(default, default):Int;
|
|
||||||
|
|
||||||
public function new() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
override private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
|
|
||||||
return if (this.personId == personId) {
|
|
||||||
new PlayerTank(personId, id, x, y, direction, [
|
|
||||||
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(personId, id, x, y, direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package ru.m.tankz.view.frames;
|
package ru.m.tankz.view.frames;
|
||||||
|
|
||||||
import ru.m.tankz.engine.ClientEngine;
|
import ru.m.tankz.core.PlayerControl;
|
||||||
|
import ru.m.tankz.engine.Engine;
|
||||||
|
import ru.m.tankz.engine.IEngine;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.tankz.proto.GameUpdateResponse;
|
import ru.m.tankz.proto.GameUpdateResponse;
|
||||||
import ru.m.core.connect.IConnection;
|
import ru.m.core.connect.IConnection;
|
||||||
@@ -14,42 +16,46 @@ import haxework.gui.VGroupView;
|
|||||||
@:template("layout/frames/game.json", "layout/styles.json")
|
@:template("layout/frames/game.json", "layout/styles.json")
|
||||||
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
|
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
|
||||||
|
|
||||||
private static inline var TAG = "GameFrame";
|
private static inline var TAG = "GameFrame";
|
||||||
|
|
||||||
public static inline var ID = "game";
|
public static inline var ID = "game";
|
||||||
|
|
||||||
private var engine:ClientEngine;
|
private var engine:IEngine;
|
||||||
|
private var controls:Map<Int, PlayerControl>;
|
||||||
|
|
||||||
public function init():Void {
|
public function init():Void {
|
||||||
engine = new ClientEngine();
|
engine = new Engine();
|
||||||
}
|
controls = new Map<Int, PlayerControl>();
|
||||||
|
}
|
||||||
|
|
||||||
public function onShow():Void {
|
public function onShow():Void {
|
||||||
var person = Provider.get(GameData).person;
|
var data:GameData = Provider.get(GameData);
|
||||||
var persons = Provider.get(GameData).game.persons;
|
engine.init(DEFAULT.CONFIG);
|
||||||
engine.personId = person.id;
|
engine.initTanks(data.game.persons); // ToDo:
|
||||||
engine.init(DEFAULT.CONFIG);
|
content.addEventListener(Event.ENTER_FRAME, updateGame);
|
||||||
engine.initTanks(persons);
|
for (index in 0...data.players.length) {
|
||||||
content.addEventListener(Event.ENTER_FRAME, updateGame);
|
var playerId:Int = data.players[index].id;
|
||||||
Provider.get(IConnection).packetHandler.addListener(this);
|
controls.set(playerId, PlayerControl.forPlayer(index, playerId, engine));
|
||||||
render.draw(engine);
|
}
|
||||||
}
|
Provider.get(IConnection).packetHandler.addListener(this);
|
||||||
|
render.draw(engine);
|
||||||
|
}
|
||||||
|
|
||||||
public function onHide():Void {
|
public function onHide():Void {
|
||||||
Provider.get(IConnection).packetHandler.removeListener(this);
|
Provider.get(IConnection).packetHandler.removeListener(this);
|
||||||
content.removeEventListener(Event.ENTER_FRAME, updateGame);
|
content.removeEventListener(Event.ENTER_FRAME, updateGame);
|
||||||
engine.clear();
|
engine.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateGame(_):Void {
|
private function updateGame(_):Void {
|
||||||
engine.update();
|
engine.update();
|
||||||
render.draw(engine);
|
render.draw(engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
|
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
|
||||||
engine.updateFromChanges(packet.changes);
|
engine.updateFromChanges(packet.changes);
|
||||||
render.draw(engine);
|
render.draw(engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPacket(packet:Message):Void {}
|
public function onPacket(packet:Message):Void {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,28 @@ class StartFrame extends VGroupView implements ViewBuilder {
|
|||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
start_1p.onPress = this;
|
start_1p.onPress = this;
|
||||||
|
start_2p.onPress = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPress(view:ButtonView):Void {
|
public function onPress(view:ButtonView):Void {
|
||||||
switch (view.id) {
|
switch (view.id) {
|
||||||
case "start_1p":
|
case "start_1p":
|
||||||
var player = new Person();
|
startGame(1);
|
||||||
player.id = 1;
|
case "start_2p":
|
||||||
var game = new Game();
|
startGame(2);
|
||||||
game.creator = player;
|
|
||||||
game.persons.push(player);
|
|
||||||
game.id = 1;
|
|
||||||
Provider.get(GameData).person = player;
|
|
||||||
Provider.get(GameData).game = game;
|
|
||||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function startGame(playersCount:Int):Void {
|
||||||
|
var game = new Game();
|
||||||
|
for (i in 0...playersCount) {
|
||||||
|
var player = new Person();
|
||||||
|
player.id = i;
|
||||||
|
game.persons.push(player);
|
||||||
|
}
|
||||||
|
game.id = 1;
|
||||||
|
Provider.get(GameData).players = game.persons;
|
||||||
|
Provider.get(GameData).game = game;
|
||||||
|
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,33 @@
|
|||||||
package ru.m.tankz.core;
|
package ru.m.tankz.core;
|
||||||
|
|
||||||
enum TankAction {
|
enum TankAction {
|
||||||
MOVE(direction:Direction);
|
MOVE(direction:Direction);
|
||||||
SHOT;
|
STOP;
|
||||||
|
SHOT;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Tank extends MobileEntity {
|
class Tank extends MobileEntity {
|
||||||
|
|
||||||
public var personId(default, null):Int;
|
public var personId(default, null):Int;
|
||||||
|
|
||||||
public var bulletsCount:Int = 0;
|
public var bulletsCount:Int = 0;
|
||||||
|
|
||||||
public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction) {
|
public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction) {
|
||||||
super(id, x, y, 4, direction);
|
super(id, x, y, 4, direction);
|
||||||
this.personId = personId;
|
this.personId = personId;
|
||||||
width = 36;
|
width = 36;
|
||||||
height = 36;
|
height = 36;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shot():Null<Bullet> {
|
public function shot():Null<Bullet> {
|
||||||
if (bulletsCount >= 5) return null;
|
if (bulletsCount >= 5) return null;
|
||||||
var bullet = new Bullet(personId, 0, 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.move(direction);
|
bullet.move(direction);
|
||||||
bulletsCount++;
|
bulletsCount++;
|
||||||
return bullet;
|
return bullet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onDestroyBullet():Void {
|
public function onDestroyBullet():Void {
|
||||||
bulletsCount--;
|
bulletsCount--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,107 +15,136 @@ 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):Map<Int, Tank>;
|
public var tanks(default, null):Map<Int, Tank>;
|
||||||
public var mobileEntities(default, null):Map<Int, IMobileEntity>;
|
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;
|
||||||
|
|
||||||
public function new() {}
|
public function new() {}
|
||||||
|
|
||||||
public function clear():Void {
|
public function clear():Void {
|
||||||
tanks = new Map<Int, Tank>();
|
tanks = new Map<Int, Tank>();
|
||||||
}
|
|
||||||
|
|
||||||
private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
|
|
||||||
return new Tank(personId, id, x, y, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function init(config:TankzConfig):Void {
|
|
||||||
this.config = config;
|
|
||||||
map = new TankzMap(config.map);
|
|
||||||
tanks = new Map<Int, Tank>();
|
|
||||||
mobileEntities = new Map<Int, IMobileEntity>();
|
|
||||||
x_limit = map.gridWidth * map.cellWidth;
|
|
||||||
y_limit = map.gridHeight * map.cellHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function initTanks(persons:Array<Person>):Array<GameChange> {
|
|
||||||
var changes = new Array<GameChange>();
|
|
||||||
for (person in persons) {
|
|
||||||
var x = 0;
|
|
||||||
var y = 100 * persons.indexOf(person);
|
|
||||||
var tank = buildTank(person.id, 0, x, y, Direction.BOTTOM);
|
|
||||||
this.tanks.set(tank.personId, tank);
|
|
||||||
this.mobileEntities.set(tank.id, tank);
|
|
||||||
changes.push(new GameChange()
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateFromChanges(changes:Array<GameChange>):Void {
|
private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
|
||||||
for (change in changes) {
|
return new Tank(personId, id, x, y, direction);
|
||||||
switch (change.type) {
|
|
||||||
case GameChangeType.APPEND:
|
|
||||||
switch (change.objectType) {
|
|
||||||
case GameObjectType.TANK:
|
|
||||||
var tank:Tank = buildTank(change.personId, change.objectId, change.x, change.y, Direction.from(change.directionX, change.directionY));
|
|
||||||
mobileEntities.set(tank.id, tank);
|
|
||||||
tanks.set(tank.personId, tank);
|
|
||||||
case GameObjectType.BULLET:
|
|
||||||
var bullet:Bullet = new Bullet(change.personId, change.objectId, change.x, change.y, 0, Direction.from(change.directionX, change.directionY));
|
|
||||||
mobileEntities.set(bullet.id, bullet);
|
|
||||||
}
|
|
||||||
case GameChangeType.DESTROED:
|
|
||||||
mobileEntities.remove(change.objectId);
|
|
||||||
case GameChangeType.DIRECTION:
|
|
||||||
var target = mobileEntities.get(change.objectId);
|
|
||||||
target.direction = Direction.from(change.directionX, change.directionY);
|
|
||||||
case GameChangeType.MOVED:
|
|
||||||
var target = mobileEntities.get(change.objectId);
|
|
||||||
target.x = change.x;
|
|
||||||
target.y = change.y;
|
|
||||||
case GameChangeType.MODIFIED:
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function update():Array<GameChange> {
|
public function init(config:TankzConfig):Void {
|
||||||
var changes = new Array<GameChange>();
|
this.config = config;
|
||||||
|
map = new TankzMap(config.map);
|
||||||
|
tanks = new Map<Int, Tank>();
|
||||||
|
mobileEntities = new Map<Int, IMobileEntity>();
|
||||||
|
x_limit = map.gridWidth * map.cellWidth;
|
||||||
|
y_limit = map.gridHeight * map.cellHeight;
|
||||||
|
}
|
||||||
|
|
||||||
for (entiny in mobileEntities) {
|
public function initTanks(persons:Array<Person>):Array<GameChange> {
|
||||||
var objectType = -1;
|
var changes = new Array<GameChange>();
|
||||||
var personId = Reflect.hasField(entiny, "personId") ? Reflect.field(entiny, "personId") : -1;
|
for (person in persons) {
|
||||||
if (Std.is(entiny, Tank)) objectType = GameObjectType.TANK;
|
var x = 0;
|
||||||
if (Std.is(entiny, Bullet)) objectType = GameObjectType.BULLET;
|
var y = 100 * persons.indexOf(person);
|
||||||
|
var tank = buildTank(person.id, 0, x, y, Direction.BOTTOM);
|
||||||
if (objectType == GameObjectType.TANK) {
|
this.tanks.set(tank.personId, tank);
|
||||||
if (entiny.direction.x != 0) {
|
this.mobileEntities.set(tank.id, tank);
|
||||||
entiny.y = Math.round((entiny.y + entiny.height / 2) / config.map.cellHeight) * config.map.cellHeight - entiny.height / 2;
|
changes.push(new GameChange()
|
||||||
|
.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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (entiny.direction.y != 0) {
|
return changes;
|
||||||
entiny.x = Math.round((entiny.x + entiny.width / 2) / config.map.cellWidth) * config.map.cellWidth - entiny.width / 2;
|
}
|
||||||
|
|
||||||
|
public function action(tankId:Int, action:TankAction):Void {
|
||||||
|
var tank:Tank = tanks.get(tankId);
|
||||||
|
switch (action) {
|
||||||
|
case TankAction.MOVE(direction):
|
||||||
|
tank.move(direction);
|
||||||
|
/*Provider.get(IConnection).send(
|
||||||
|
new GameActionRequest()
|
||||||
|
.setType(GameActionType.MOVE)
|
||||||
|
.setDirectionX(direction.x)
|
||||||
|
.setDirectionY(direction.y)
|
||||||
|
);*/
|
||||||
|
case TankAction.STOP:
|
||||||
|
tank.stop();
|
||||||
|
/*Provider.get(IConnection).send(
|
||||||
|
new GameActionRequest()
|
||||||
|
.setType(GameActionType.STOP)
|
||||||
|
);*/
|
||||||
|
case TankAction.SHOT:
|
||||||
|
var bullet = tank.shot();
|
||||||
|
if (bullet != null) {
|
||||||
|
mobileEntities.set(bullet.id, bullet);
|
||||||
|
}
|
||||||
|
/*Provider.get(IConnection).send(
|
||||||
|
new GameActionRequest()
|
||||||
|
.setType(GameActionType.SHOT)
|
||||||
|
);*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entiny.mx != 0 || entiny.my != 0) {
|
public function updateFromChanges(changes:Array<GameChange>):Void {
|
||||||
entiny.x += entiny.mx;
|
for (change in changes) {
|
||||||
entiny.y += entiny.my;
|
switch (change.type) {
|
||||||
|
case GameChangeType.APPEND:
|
||||||
|
switch (change.objectType) {
|
||||||
|
case GameObjectType.TANK:
|
||||||
|
var tank:Tank = buildTank(change.personId, change.objectId, change.x, change.y, Direction.from(change.directionX, change.directionY));
|
||||||
|
mobileEntities.set(tank.id, tank);
|
||||||
|
tanks.set(tank.personId, tank);
|
||||||
|
case GameObjectType.BULLET:
|
||||||
|
var bullet:Bullet = new Bullet(change.personId, change.objectId, change.x, change.y, 0, Direction.from(change.directionX, change.directionY));
|
||||||
|
mobileEntities.set(bullet.id, bullet);
|
||||||
|
}
|
||||||
|
case GameChangeType.DESTROED:
|
||||||
|
mobileEntities.remove(change.objectId);
|
||||||
|
case GameChangeType.DIRECTION:
|
||||||
|
var target = mobileEntities.get(change.objectId);
|
||||||
|
target.direction = Direction.from(change.directionX, change.directionY);
|
||||||
|
case GameChangeType.MOVED:
|
||||||
|
var target = mobileEntities.get(change.objectId);
|
||||||
|
target.x = change.x;
|
||||||
|
target.y = change.y;
|
||||||
|
case GameChangeType.MODIFIED:
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height);
|
public function update():Array<GameChange> {
|
||||||
|
var changes = new Array<GameChange>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (objectType == GameObjectType.TANK) {
|
||||||
|
if (entiny.direction.x != 0) {
|
||||||
|
entiny.y = Math.round((entiny.y + entiny.height / 2) / config.map.cellHeight) * config.map.cellHeight - entiny.height / 2;
|
||||||
|
}
|
||||||
|
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) {
|
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);
|
||||||
@@ -133,38 +162,38 @@ class Engine implements IEngine {
|
|||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
if (objectType == GameObjectType.TANK) {
|
if (objectType == GameObjectType.TANK) {
|
||||||
if (entiny.x < 0) entiny.x = 0;
|
if (entiny.x < 0) entiny.x = 0;
|
||||||
if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width;
|
if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width;
|
||||||
if (entiny.y < 0) entiny.y = 0;
|
if (entiny.y < 0) entiny.y = 0;
|
||||||
if (entiny.y + entiny.height > y_limit) entiny.y = y_limit - entiny.height;
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return changes;
|
||||||
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return changes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,15 @@ import ru.m.tankz.config.TankzConfig;
|
|||||||
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):Map<Int, Tank>;
|
public var tanks(default, null):Map<Int, Tank>;
|
||||||
public var mobileEntities(default, null):Map<Int, IMobileEntity>;
|
public var mobileEntities(default, null):Map<Int, IMobileEntity>;
|
||||||
|
|
||||||
public function clear():Void;
|
public function clear():Void;
|
||||||
public function init(config:TankzConfig):Void;
|
public function init(config:TankzConfig):Void;
|
||||||
public function initTanks(persons:Array<Person>):Array<GameChange>;
|
public function initTanks(persons:Array<Person>):Array<GameChange>;
|
||||||
public function updateFromChanges(changes:Array<GameChange>):Void;
|
public function action(tankId:Int, action:TankAction):Void;
|
||||||
public function update():Array<GameChange>;
|
public function updateFromChanges(changes:Array<GameChange>):Void;
|
||||||
|
public function update():Array<GameChange>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user