[common] change game team players from array to map
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tankz",
|
"name": "tankz",
|
||||||
"version": "0.4.0",
|
"version": "0.4.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ansi-colors": "^1.0.1",
|
"ansi-colors": "^1.0.1",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.control;
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.Game;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import haxe.Timer;
|
import haxe.Timer;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
@@ -18,9 +19,9 @@ class HumanControl extends Control {
|
|||||||
private var moveQueue:Array<Int>;
|
private var moveQueue:Array<Int>;
|
||||||
private var shotTimer:Timer;
|
private var shotTimer:Timer;
|
||||||
|
|
||||||
public function new(index:Int) {
|
public function new(playerId:PlayerId, controlIndex:Int) {
|
||||||
super({type:Control.HUMAN, index:index});
|
super(playerId);
|
||||||
this.keyBinding = resolve(index);
|
this.keyBinding = resolve(controlIndex);
|
||||||
moveQueue = new Array<Int>();
|
moveQueue = new Array<Int>();
|
||||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
||||||
@@ -82,8 +83,8 @@ class HumanControl extends Control {
|
|||||||
action(TankAction.SHOT);
|
action(TankAction.SHOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function resolve(index:Int):KeyBinding {
|
private static function resolve(controlIndex:Int):KeyBinding {
|
||||||
switch (index) {
|
switch (controlIndex) {
|
||||||
case 0:
|
case 0:
|
||||||
return [
|
return [
|
||||||
Keyboard.A => TankAction.MOVE(Direction.LEFT),
|
Keyboard.A => TankAction.MOVE(Direction.LEFT),
|
||||||
@@ -100,8 +101,8 @@ class HumanControl extends Control {
|
|||||||
Keyboard.RIGHT => TankAction.MOVE(Direction.RIGHT),
|
Keyboard.RIGHT => TankAction.MOVE(Direction.RIGHT),
|
||||||
Keyboard.NUMPAD_0 => TankAction.SHOT
|
Keyboard.NUMPAD_0 => TankAction.SHOT
|
||||||
];
|
];
|
||||||
case _:
|
case x:
|
||||||
throw 'Invalid control index ${index}';
|
throw 'Invalid control index ${x}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.bot;
|
package ru.m.tankz.bot;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.Game;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
@@ -12,8 +13,8 @@ class BotControl extends Control {
|
|||||||
private var turnRandomTimer:Timer;
|
private var turnRandomTimer:Timer;
|
||||||
private var turnTimer:Timer;
|
private var turnTimer:Timer;
|
||||||
|
|
||||||
public function new(index:Int) {
|
public function new(playerId:PlayerId) {
|
||||||
super({type:Control.BOT, index:index});
|
super(playerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function onCollision(with:EntityType):Void {
|
override public function onCollision(with:EntityType):Void {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.control;
|
package ru.m.tankz.control;
|
||||||
|
|
||||||
|
import ru.m.tankz.game.Game;
|
||||||
import ru.m.tankz.core.Entity;
|
import ru.m.tankz.core.Entity;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.EntityType;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
@@ -16,23 +17,17 @@ enum TankAction {
|
|||||||
typedef ControlType = String;
|
typedef ControlType = String;
|
||||||
|
|
||||||
|
|
||||||
typedef ControlId = {
|
|
||||||
var type:ControlType;
|
|
||||||
var index:Int;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Control {
|
class Control {
|
||||||
public static var NONE(default, never):ControlType = 'none';
|
public static var NONE(default, never):ControlType = 'none';
|
||||||
public static var HUMAN(default, never):ControlType = 'human';
|
public static var HUMAN(default, never):ControlType = 'human';
|
||||||
public static var BOT(default, never):ControlType = 'bot';
|
public static var BOT(default, never):ControlType = 'bot';
|
||||||
|
|
||||||
public var id:ControlId;
|
public var playerId(default, null):PlayerId;
|
||||||
public var tankId(default, default):Int;
|
public var tankId(default, default):Int;
|
||||||
private var handler:ControlHandler;
|
private var handler:ControlHandler;
|
||||||
|
|
||||||
public function new(id:ControlId) {
|
public function new(playerId:PlayerId) {
|
||||||
this.id = id;
|
this.playerId = playerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bind(handler:ControlHandler):Void {
|
public function bind(handler:ControlHandler):Void {
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ typedef PlayerId = {
|
|||||||
|
|
||||||
class Game implements EngineListener {
|
class Game implements EngineListener {
|
||||||
|
|
||||||
|
private static var TAG(default, never):String = Type.getClassName(Game).split('.').pop();
|
||||||
|
|
||||||
public var type(default, null):GameType;
|
public var type(default, null):GameType;
|
||||||
public var state(default, null):GameState;
|
public var state(default, null):GameState;
|
||||||
public var teams(default, null):Map<TeamId, Team>;
|
public var teams(default, null):Map<TeamId, Team>;
|
||||||
@@ -54,7 +56,7 @@ class Game implements EngineListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getPlayer(playerId:PlayerId):Player {
|
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 {
|
private function buildTank(playerId:PlayerId, point:SpawnPoint):Tank {
|
||||||
@@ -81,33 +83,34 @@ class Game implements EngineListener {
|
|||||||
var humanControlIndex = 0;
|
var humanControlIndex = 0;
|
||||||
for (teamConfig in config.teams) {
|
for (teamConfig in config.teams) {
|
||||||
var team = new Team(teamConfig);
|
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);
|
var player = new Player(playerState.id);
|
||||||
team.players.push(player);
|
team.players[player.id.index] = player;
|
||||||
teams.set(team.id, team);
|
teams[team.id] = team;
|
||||||
if (player.id.type != null) {
|
if (player.id.type != null) {
|
||||||
var control = switch (player.id.type) {
|
var control = switch (player.id.type) {
|
||||||
case Control.HUMAN: new HumanControl(humanControlIndex++);
|
case Control.HUMAN: new HumanControl(player.id, humanControlIndex++);
|
||||||
case Control.BOT: new BotControl(player.id.index);
|
case Control.BOT: new BotControl(player.id);
|
||||||
case Control.NONE: null;
|
case Control.NONE: null;
|
||||||
case _: throw 'Unsupported control type: "${player.id.type}"';
|
case _: throw 'Unsupported control type: "${player.id.type}"';
|
||||||
}
|
}
|
||||||
|
L.d(TAG, 'control(${player.id} - ${control})');
|
||||||
if (control != null) {
|
if (control != null) {
|
||||||
player.control = control;
|
player.control = control;
|
||||||
player.control.bind(engine);
|
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 (team in teams) {
|
||||||
for (player in team.players) {
|
for (player in team.players) {
|
||||||
if (trySpawn(player.id)) {
|
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) {
|
if (eaglePoint != null) {
|
||||||
var eagle = new Eagle(team.id);
|
var eagle = new Eagle(team.id);
|
||||||
applyPoint(eagle, eaglePoint);
|
applyPoint(eagle, eaglePoint);
|
||||||
@@ -119,6 +122,7 @@ class Game implements EngineListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function spawn(task:SpawnTask):Void {
|
private function spawn(task:SpawnTask):Void {
|
||||||
|
L.d(TAG, 'spawn(${task}');
|
||||||
getPlayer(task.playerId).tankId = 0;
|
getPlayer(task.playerId).tankId = 0;
|
||||||
if (trySpawn(task.playerId, true)) {
|
if (trySpawn(task.playerId, true)) {
|
||||||
var tank = buildTank(task.playerId, task.point);
|
var tank = buildTank(task.playerId, task.point);
|
||||||
@@ -145,21 +149,6 @@ class Game implements EngineListener {
|
|||||||
}, 5000);
|
}, 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 {
|
public function onSpawn(entity:EntityType):Void {
|
||||||
switch (entity) {
|
switch (entity) {
|
||||||
case EntityType.TANK(tank):
|
case EntityType.TANK(tank):
|
||||||
@@ -220,7 +209,7 @@ class Game implements EngineListener {
|
|||||||
getPlayer(tank.playerId).tankId = 0; //ToDo: ?
|
getPlayer(tank.playerId).tankId = 0; //ToDo: ?
|
||||||
var respawn:Bool = trySpawn(tank.playerId);
|
var respawn:Bool = trySpawn(tank.playerId);
|
||||||
if (respawn) {
|
if (respawn) {
|
||||||
spawners.get(tank.playerId.team).push(tank.playerId);
|
spawners[tank.playerId.team].push(tank.playerId);
|
||||||
}
|
}
|
||||||
if (!isTeamAlive(tank.playerId.team)) {
|
if (!isTeamAlive(tank.playerId.team)) {
|
||||||
state.teams[tank.playerId.team].lose = true;
|
state.teams[tank.playerId.team].lose = true;
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ class Team {
|
|||||||
|
|
||||||
public var id(default, null):TeamId;
|
public var id(default, null):TeamId;
|
||||||
public var config(default, null):TeamConfig;
|
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;
|
private static var i:Int = 0;
|
||||||
|
|
||||||
public function new(config:TeamConfig) {
|
public function new(config:TeamConfig) {
|
||||||
this.id = config.id;
|
this.id = config.id;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.players = [];
|
this.players = new Map();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user