Differential D3723 Diff 16625 ps/trunk/binaries/data/mods/public/gui/gamesettings/attributes/PlayerColor.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/gui/gamesettings/attributes/PlayerColor.js
Show First 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | _resize(nb) | ||||
} | } | ||||
} | } | ||||
onMapChange() | onMapChange() | ||||
{ | { | ||||
// Reset. | // Reset. | ||||
this.locked = this.locked.map(x => this.settings.map.type == "scenario"); | this.locked = this.locked.map(x => this.settings.map.type == "scenario"); | ||||
this.trigger("locked"); | this.trigger("locked"); | ||||
if (this.settings.map.type === "scenario") | if (this.settings.map.type === "scenario") | ||||
this._resize(0); | this._resize(0); | ||||
this._updateAvailable(); | this._updateAvailable(); | ||||
this.maybeUpdate(); | this.maybeUpdate(); | ||||
this.maybeUpdate(); | |||||
} | } | ||||
maybeUpdate() | maybeUpdate() | ||||
{ | { | ||||
this._resize(this.settings.playerCount.nbPlayers); | this._resize(this.settings.playerCount.nbPlayers); | ||||
this.values.forEach((c, i) => this._set(i, c)); | this.values.forEach((c, i) => this._set(i, c)); | ||||
this.trigger("values"); | this.trigger("values"); | ||||
} | } | ||||
_set(playerIndex, color) | _set(playerIndex, color) | ||||
{ | { | ||||
let inUse = this.values.findIndex((otherColor, i) => | let inUse = this.values.findIndex((otherColor, i) => | ||||
color && otherColor && | color && otherColor && | ||||
sameColor(color, otherColor)); | sameColor(color, otherColor)); | ||||
if (inUse !== -1 && inUse !== playerIndex) | if (inUse != -1 && inUse != playerIndex) | ||||
{ | |||||
if (sameColor(this.values[playerIndex], this.values[inUse])) | |||||
{ | |||||
this.values[playerIndex] = undefined; | |||||
color = undefined; | |||||
} | |||||
else | |||||
{ | { | ||||
// Swap colors. | // Swap colors. | ||||
let col = this.values[playerIndex]; | let col = this.values[playerIndex]; | ||||
this.values[playerIndex] = undefined; | this.values[playerIndex] = undefined; | ||||
this._set(inUse, col); | this._set(inUse, col); | ||||
} | } | ||||
} | |||||
if (!color || this.available.indexOf(color) == -1) | if (!color || this.available.indexOf(color) == -1) | ||||
{ | { | ||||
this.values[playerIndex] = color ? | this.values[playerIndex] = color ? | ||||
this._findClosestColor(color, this.available) : | this._findClosestColor(color, this.available) : | ||||
this._getUnusedColor(); | this._getUnusedColor(); | ||||
} | } | ||||
else | else | ||||
this.values[playerIndex] = color; | this.values[playerIndex] = color; | ||||
Show All 29 Lines | _getMapData(i) | ||||
return data.settings.PlayerData[i].Color; | return data.settings.PlayerData[i].Color; | ||||
} | } | ||||
_updateAvailable() | _updateAvailable() | ||||
{ | { | ||||
// Pick colors that the map specifies, add most unsimilar default colors | // Pick colors that the map specifies, add most unsimilar default colors | ||||
// Provide the access to g_MaxPlayers different colors, regardless of current playercount. | // Provide the access to g_MaxPlayers different colors, regardless of current playercount. | ||||
let values = []; | let values = []; | ||||
let mapColors = false; | |||||
for (let i = 0; i < g_MaxPlayers; ++i) | for (let i = 0; i < g_MaxPlayers; ++i) | ||||
values.push(this._getMapData(i) || | { | ||||
this.defaultColors[i] || this._findFarthestUnusedColor(values)); | let col = this._getMapData(i); | ||||
if (col) | |||||
mapColors = true; | |||||
if (mapColors) | |||||
values.push(col || this._findFarthestUnusedColor(values)); | |||||
else | |||||
values.push(this.defaultColors[i]); | |||||
} | |||||
this.available = values; | this.available = values; | ||||
} | } | ||||
_findClosestColor(targetColor, colors) | _findClosestColor(targetColor, colors) | ||||
{ | { | ||||
let colorDistances = colors.map(color => colorDistance(color, targetColor)); | let closestColor; | ||||
let closestColorDistance = 0; | |||||
let smallestDistance = colorDistances.find( | for (let color of colors) | ||||
distance => colorDistances.every(distance2 => distance2 >= distance)); | { | ||||
let dist = colorDistance(targetColor, color); | |||||
return colors.find(color => colorDistance(color, targetColor) == smallestDistance); | if (!closestColor || dist < closestColorDistance) | ||||
{ | |||||
closestColor = color; | |||||
closestColorDistance = dist; | |||||
} | |||||
} | |||||
return closestColor; | |||||
} | } | ||||
_findFarthestUnusedColor(values) | _findFarthestUnusedColor(values) | ||||
{ | { | ||||
let farthestColor; | let farthestColor; | ||||
let farthestDistance = 0; | let farthestDistance = 0; | ||||
for (let defaultColor of this.defaultColors) | for (let defaultColor of this.defaultColors) | ||||
Show All 25 Lines |
Wildfire Games · Phabricator