Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/PlayerManager.js
function PlayerManager() {} | function PlayerManager() {} | ||||
PlayerManager.prototype.Schema = | PlayerManager.prototype.Schema = | ||||
"<a:component type='system'/><empty/>"; | "<a:component type='system'/><empty/>"; | ||||
PlayerManager.prototype.Init = function() | PlayerManager.prototype.Init = function() | ||||
{ | { | ||||
this.playerEntities = []; // list of player entity IDs | // List of player entity IDs. | ||||
this.playerEntities = []; | |||||
// Maximum world population (if applicable will be distributed amongst living players). | |||||
this.maxWorldPopulation = undefined; | |||||
}; | }; | ||||
PlayerManager.prototype.AddPlayer = function(ent) | PlayerManager.prototype.AddPlayer = function(ent) | ||||
{ | { | ||||
var id = this.playerEntities.length; | var id = this.playerEntities.length; | ||||
var cmpPlayer = Engine.QueryInterface(ent, IID_Player); | var cmpPlayer = Engine.QueryInterface(ent, IID_Player); | ||||
cmpPlayer.SetPlayerID(id); | cmpPlayer.SetPlayerID(id); | ||||
this.playerEntities.push(ent); | this.playerEntities.push(ent); | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | PlayerManager.prototype.RemoveLastPlayer = function() | ||||
Engine.BroadcastMessage(MT_PlayerEntityChanged, { | Engine.BroadcastMessage(MT_PlayerEntityChanged, { | ||||
"player": this.playerEntities.length + 1, | "player": this.playerEntities.length + 1, | ||||
"from": lastId, | "from": lastId, | ||||
"to": INVALID_ENTITY | "to": INVALID_ENTITY | ||||
}); | }); | ||||
Engine.DestroyEntity(lastId); | Engine.DestroyEntity(lastId); | ||||
}; | }; | ||||
PlayerManager.prototype.SetMaxWorldPopulation = function(max) | |||||
{ | |||||
this.maxWorldPopulation = max; | |||||
Silier: should we here call redistributeworldpopulation right away ? | |||||
}; | |||||
PlayerManager.prototype.GetMaxWorldPopulation = function() | |||||
{ | |||||
return this.maxWorldPopulation; | |||||
}; | |||||
PlayerManager.prototype.RedistributeWorldPopulation = function() | |||||
{ | |||||
let worldPopulation = this.GetMaxWorldPopulation(); | |||||
if (worldPopulation) | |||||
{ | |||||
Done Inline Actionscould you please invert and early return ? Silier: could you please invert and early return ? | |||||
let activePlayers = this.GetActivePlayers(); | |||||
// We need to default to "1" here because when a game is won all players | |||||
// become "inactive" letting an observer see an infinite max pop cap. | |||||
let newMaxPopulation = worldPopulation / (activePlayers.length || 1); | |||||
Done Inline ActionsThis confuses me, if there is no active player, that means activePlayers.length == 0 and because of that activePlayers is empty, so loop cannot run Silier: This confuses me, if there is no active player, that means activePlayers.length == 0 and… | |||||
Done Inline ActionsProbably a previous iteration needed this. Freagarach: Probably a previous iteration needed this. | |||||
for (let playerID of activePlayers) | |||||
{ | |||||
SilierUnsubmitted Done Inline Actionsyou can remove brackets Silier: you can remove brackets | |||||
QueryPlayerIDInterface(playerID).SetMaxPopulation(newMaxPopulation); | |||||
} | |||||
} | |||||
} | |||||
PlayerManager.prototype.OnGlobalPlayerDefeated = function(msg) | |||||
{ | |||||
this.RedistributeWorldPopulation(); | |||||
}; | |||||
Engine.RegisterSystemComponentType(IID_PlayerManager, "PlayerManager", PlayerManager); | Engine.RegisterSystemComponentType(IID_PlayerManager, "PlayerManager", PlayerManager); |
Wildfire Games · Phabricator
should we here call redistributeworldpopulation right away ?