[common] change game team players from array to map
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.geom.Direction;
|
||||
@@ -12,8 +13,8 @@ class BotControl extends Control {
|
||||
private var turnRandomTimer:Timer;
|
||||
private var turnTimer:Timer;
|
||||
|
||||
public function new(index:Int) {
|
||||
super({type:Control.BOT, index:index});
|
||||
public function new(playerId:PlayerId) {
|
||||
super(playerId);
|
||||
}
|
||||
|
||||
override public function onCollision(with:EntityType):Void {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.geom.Direction;
|
||||
@@ -16,23 +17,17 @@ enum TankAction {
|
||||
typedef ControlType = String;
|
||||
|
||||
|
||||
typedef ControlId = {
|
||||
var type:ControlType;
|
||||
var index:Int;
|
||||
}
|
||||
|
||||
|
||||
class Control {
|
||||
public static var NONE(default, never):ControlType = 'none';
|
||||
public static var HUMAN(default, never):ControlType = 'human';
|
||||
public static var BOT(default, never):ControlType = 'bot';
|
||||
|
||||
public var id:ControlId;
|
||||
public var playerId(default, null):PlayerId;
|
||||
public var tankId(default, default):Int;
|
||||
private var handler:ControlHandler;
|
||||
|
||||
public function new(id:ControlId) {
|
||||
this.id = id;
|
||||
public function new(playerId:PlayerId) {
|
||||
this.playerId = playerId;
|
||||
}
|
||||
|
||||
public function bind(handler:ControlHandler):Void {
|
||||
|
||||
@@ -36,6 +36,8 @@ typedef PlayerId = {
|
||||
|
||||
class Game implements EngineListener {
|
||||
|
||||
private static var TAG(default, never):String = Type.getClassName(Game).split('.').pop();
|
||||
|
||||
public var type(default, null):GameType;
|
||||
public var state(default, null):GameState;
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
@@ -54,7 +56,7 @@ class Game implements EngineListener {
|
||||
}
|
||||
|
||||
public function getPlayer(playerId:PlayerId):Player {
|
||||
return teams.get(playerId.team).players[playerId.index];
|
||||
return teams[playerId.team].players[playerId.index];
|
||||
}
|
||||
|
||||
private function buildTank(playerId:PlayerId, point:SpawnPoint):Tank {
|
||||
@@ -81,33 +83,34 @@ class Game implements EngineListener {
|
||||
var humanControlIndex = 0;
|
||||
for (teamConfig in config.teams) {
|
||||
var team = new Team(teamConfig);
|
||||
for (playerState in state.teams.get(team.id).players) {
|
||||
for (playerState in state.teams[team.id].players) {
|
||||
var player = new Player(playerState.id);
|
||||
team.players.push(player);
|
||||
teams.set(team.id, team);
|
||||
team.players[player.id.index] = player;
|
||||
teams[team.id] = team;
|
||||
if (player.id.type != null) {
|
||||
var control = switch (player.id.type) {
|
||||
case Control.HUMAN: new HumanControl(humanControlIndex++);
|
||||
case Control.BOT: new BotControl(player.id.index);
|
||||
case Control.HUMAN: new HumanControl(player.id, humanControlIndex++);
|
||||
case Control.BOT: new BotControl(player.id);
|
||||
case Control.NONE: null;
|
||||
case _: throw 'Unsupported control type: "${player.id.type}"';
|
||||
}
|
||||
L.d(TAG, 'control(${player.id} - ${control})');
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
player.control.bind(engine);
|
||||
}
|
||||
}
|
||||
}
|
||||
spawners.set(team.id, new Spawner(team.config, spawn));
|
||||
spawners[team.id] = new Spawner(team.config, spawn);
|
||||
}
|
||||
|
||||
for (team in teams) {
|
||||
for (player in team.players) {
|
||||
if (trySpawn(player.id)) {
|
||||
spawners.get(team.id).push(player.id);
|
||||
spawners[team.id].push(player.id);
|
||||
}
|
||||
}
|
||||
var eaglePoint = spawners.get(team.id).getPoint('eagle');
|
||||
var eaglePoint = spawners[team.id].getPoint('eagle');
|
||||
if (eaglePoint != null) {
|
||||
var eagle = new Eagle(team.id);
|
||||
applyPoint(eagle, eaglePoint);
|
||||
@@ -119,6 +122,7 @@ class Game implements EngineListener {
|
||||
}
|
||||
|
||||
private function spawn(task:SpawnTask):Void {
|
||||
L.d(TAG, 'spawn(${task}');
|
||||
getPlayer(task.playerId).tankId = 0;
|
||||
if (trySpawn(task.playerId, true)) {
|
||||
var tank = buildTank(task.playerId, task.point);
|
||||
@@ -145,21 +149,6 @@ class Game implements EngineListener {
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
public function setControl(playerId:PlayerId, control:Control):Void {
|
||||
for (team in teams.iterator()) {
|
||||
if (team.id == playerId.team) {
|
||||
var player = team.players[playerId.index];
|
||||
if (player.control != null) {
|
||||
player.control.dispose();
|
||||
}
|
||||
player.control = control;
|
||||
player.control.bind(engine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function onSpawn(entity:EntityType):Void {
|
||||
switch (entity) {
|
||||
case EntityType.TANK(tank):
|
||||
@@ -220,7 +209,7 @@ class Game implements EngineListener {
|
||||
getPlayer(tank.playerId).tankId = 0; //ToDo: ?
|
||||
var respawn:Bool = trySpawn(tank.playerId);
|
||||
if (respawn) {
|
||||
spawners.get(tank.playerId.team).push(tank.playerId);
|
||||
spawners[tank.playerId.team].push(tank.playerId);
|
||||
}
|
||||
if (!isTeamAlive(tank.playerId.team)) {
|
||||
state.teams[tank.playerId.team].lose = true;
|
||||
|
||||
@@ -9,13 +9,13 @@ class Team {
|
||||
|
||||
public var id(default, null):TeamId;
|
||||
public var config(default, null):TeamConfig;
|
||||
public var players(default, null):Array<Player>;
|
||||
public var players(default, null):Map<Int, Player>;
|
||||
|
||||
private static var i:Int = 0;
|
||||
|
||||
public function new(config:TeamConfig) {
|
||||
this.id = config.id;
|
||||
this.config = config;
|
||||
this.players = [];
|
||||
this.players = new Map();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user