[client] fix control
This commit is contained in:
@@ -4,6 +4,7 @@ import ru.m.geom.Point;
|
||||
import ru.m.geom.Position;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
import ru.m.tankz.control.NoneControlFactory;
|
||||
@@ -28,6 +29,8 @@ import ru.m.tankz.Type;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
public var pause(default, set):Bool;
|
||||
|
||||
private var controls:Map<String, Control>;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
public function new(state:GameState) {
|
||||
@@ -37,6 +40,7 @@ import ru.m.tankz.Type;
|
||||
this.config = configBundle.get(type);
|
||||
this.controlFactory = new NoneControlFactory();
|
||||
this.pause = false;
|
||||
this.controls = new Map();
|
||||
connect(this);
|
||||
}
|
||||
|
||||
@@ -76,9 +80,19 @@ import ru.m.tankz.Type;
|
||||
var team = getTeam(teamId);
|
||||
team.eagleId = id;
|
||||
case SPAWN(TANK(id, rect, playerId, info)):
|
||||
var player = getPlayer(playerId);
|
||||
player.tankId = id;
|
||||
player.state.tank = info.type;
|
||||
if (controls.exists(playerId)) {
|
||||
var control = controls[playerId];
|
||||
control.tankId = id;
|
||||
control.start();
|
||||
}
|
||||
case DESTROY(TANK(id, shot)):
|
||||
for (control in controls) {
|
||||
if (control.tankId == id) {
|
||||
control.stop();
|
||||
control.tankId = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
case _:
|
||||
}
|
||||
}
|
||||
@@ -104,8 +118,8 @@ import ru.m.tankz.Type;
|
||||
var controlType:Controller = AController.fromString(playerControl != null ? playerControl.control : player.config.control);
|
||||
var control = controlFactory.build(player.id, controlType);
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
player.control.bind(this, engine);
|
||||
controls[player.id] = control;
|
||||
control.bind(this, engine);
|
||||
} else {
|
||||
// ToDo: remove player
|
||||
player.state.life = 0;
|
||||
@@ -115,6 +129,10 @@ import ru.m.tankz.Type;
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
for (control in controls) {
|
||||
control.dispose();
|
||||
}
|
||||
controls = new Map();
|
||||
gameEventSignal.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,14 +130,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
}
|
||||
|
||||
private function complete(winner:TeamId):Void {
|
||||
for (team in teams.iterator()) {
|
||||
for (player in team.players) {
|
||||
if (player.control != null) {
|
||||
player.control.action(STOP);
|
||||
player.control.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Timer.delay(function() {
|
||||
gameEventSignal.emit(COMPLETE(state, winner));
|
||||
}, 3000);
|
||||
@@ -165,8 +157,9 @@ class GameRunner extends Game implements EngineListener {
|
||||
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||
switch entity {
|
||||
case EntityType.TANK(tank):
|
||||
var control = getPlayer(tank.playerId).control;
|
||||
if (control != null) control.onCollision(with);
|
||||
if (controls.exists(tank.playerId)) {
|
||||
controls[tank.playerId].onCollision(with);
|
||||
}
|
||||
case _:
|
||||
}
|
||||
switch [entity, with] {
|
||||
@@ -352,11 +345,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
case ACTION(tankId, STOP):
|
||||
gameEventSignal.emit(STOP(TANK(tankId)));
|
||||
engine.stop(tankId);
|
||||
case SPAWN(TANK(_, _, playerId, _)):
|
||||
var control = getPlayer(playerId).control;
|
||||
if (control != null) {
|
||||
control.start();
|
||||
}
|
||||
case SPAWN(BULLET(_, _, playerId, _)):
|
||||
getPlayer(playerId).bullets++;
|
||||
case DESTROY(EAGLE(id, shot)):
|
||||
@@ -371,9 +359,6 @@ class GameRunner extends Game implements EngineListener {
|
||||
var tank:Tank = engine.getEntity(id);
|
||||
var team = getTeam(tank.playerId.team);
|
||||
var player = getPlayer(tank.playerId);
|
||||
if (player.control != null) {
|
||||
player.control.stop();
|
||||
}
|
||||
player.tankId = -1;
|
||||
team.onDestroy(player.id);
|
||||
if (player.state.life > 0) {
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class Player {
|
||||
public var config(default, null):PlayerConfig;
|
||||
public var id(default, null):PlayerId;
|
||||
public var tankId(default, set):Int;
|
||||
public var control(default, set):Control;
|
||||
public var isAlive(get, null):Bool;
|
||||
public var state(default, default):PlayerState;
|
||||
public var bullets(default, default):Int;
|
||||
@@ -17,7 +15,6 @@ class Player {
|
||||
public function new(teamId:TeamId, config:PlayerConfig, state:PlayerState = null) {
|
||||
this.config = config;
|
||||
this.id = new PlayerId(teamId, config.index);
|
||||
this.control = null;
|
||||
this.state = state == null ? new PlayerState(id) : state;
|
||||
this.state.reset();
|
||||
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
|
||||
@@ -27,21 +24,9 @@ class Player {
|
||||
|
||||
private function set_tankId(value:Int):Int {
|
||||
tankId = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
}
|
||||
return tankId;
|
||||
}
|
||||
|
||||
private function set_control(value:Control):Control {
|
||||
if (control != null) control.dispose();
|
||||
control = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
}
|
||||
return control;
|
||||
}
|
||||
|
||||
private function get_isAlive():Bool {
|
||||
return tankId > -1 || state.life > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user