0ad/binaries/data/mods/public/simulation/components/PlayerManager.js
leper 354bd8b088 Only delete/create players until the needed number is reached. Patch by trompetin17. Fixes #1953.
Previously we removed all players and created them anew. This caused
issues in Atlas,
as some components were recreated, but did not get informed about
already existing
entities (eg cmpTechnologyManager). By only creating/deleting players
until we obtain
the new number of players we do not have this issue.

This was SVN commit r16080.
2014-12-29 17:33:39 +00:00

78 lines
1.9 KiB
JavaScript

function PlayerManager() {}
PlayerManager.prototype.Schema =
"<a:component type='system'/><empty/>";
PlayerManager.prototype.Init = function()
{
this.playerEntities = []; // list of player entity IDs
};
PlayerManager.prototype.AddPlayer = function(ent)
{
var id = this.playerEntities.length;
var cmpPlayer = Engine.QueryInterface(ent, IID_Player)
cmpPlayer.SetPlayerID(id);
this.playerEntities.push(ent);
// initialize / update the diplomacy arrays
var newDiplo = [];
for (var i = 0; i < id; i++)
{
var cmpOtherPlayer = Engine.QueryInterface(this.GetPlayerByID(i), IID_Player);
cmpOtherPlayer.diplomacy[id] = -1;
newDiplo[i] = -1;
}
newDiplo[id] = 1;
cmpPlayer.SetDiplomacy(newDiplo);
return id;
};
/**
* Returns the player entity ID for the given player ID.
* The player ID must be valid (else there will be an error message).
*/
PlayerManager.prototype.GetPlayerByID = function(id)
{
if (id in this.playerEntities)
return this.playerEntities[id];
// All players at or below ID 0 get gaia-level data. (Observers for example)
if (id <= 0)
return this.playerEntities[0];
var stack = new Error().stack.trimRight().replace(/^/mg, ' '); // indent each line
warn("GetPlayerByID: no player defined for id '"+id+"'\n"+stack);
return INVALID_ENTITY;
};
PlayerManager.prototype.GetNumPlayers = function()
{
return this.playerEntities.length;
};
PlayerManager.prototype.RemoveAllPlayers = function()
{
// Destroy existing player entities
for each (var id in this.playerEntities)
Engine.DestroyEntity(id);
this.playerEntities = [];
};
PlayerManager.prototype.RemoveLastPlayer = function()
{
if (this.playerEntities.length == 0)
return;
var lastId = this.playerEntities.pop();
Engine.DestroyEntity(lastId);
};
PlayerManager.prototype.GetAllPlayerEntities = function()
{
return this.playerEntities;
};
Engine.RegisterSystemComponentType(IID_PlayerManager, "PlayerManager", PlayerManager);