1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-02-12 10:59:23 +00:00

simplified cycle algorithms, simplified method names

This commit is contained in:
SStrippel 2020-06-13 09:18:31 +02:00
parent bb781a67b2
commit 6266406a79
2 changed files with 39 additions and 23 deletions

View File

@ -381,7 +381,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
index >= 0, index >= 0,
"Current variant was invalid: " + this.currentVariant.get() + " out of " + availableVariants "Current variant was invalid: " + this.currentVariant.get() + " out of " + availableVariants
); );
const newIndex = this.calculateNewVariantCycleIndex(index, availableVariants.length); const newIndex = this.calculateNewCycleIndex(index, availableVariants.length);
const newVariant = availableVariants[newIndex]; const newVariant = availableVariants[newIndex];
this.currentVariant.set(newVariant); this.currentVariant.set(newVariant);
@ -395,17 +395,17 @@ export class HUDBuildingPlacer extends BaseHUDPart {
* @param {number} availableVariantsLength the number of available variants * @param {number} availableVariantsLength the number of available variants
* @returns {number} index for next variant * @returns {number} index for next variant
*/ */
calculateNewVariantCycleIndex(index, availableVariantsLength) { calculateNewCycleIndex(index, availableVariantsLength) {
let indexModifier = 0; const actionMapper = this.root.keyMapper;
if ( let cycleDirection;
this.root.keyMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()
) { if (actionMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()) {
indexModifier -= 1; cycleDirection = -1;
} else { } else {
indexModifier += 1; cycleDirection = 1;
} }
return (index + indexModifier + availableVariantsLength) % availableVariantsLength; return (index + cycleDirection + availableVariantsLength) % availableVariantsLength;
} }
/** /**

View File

@ -112,27 +112,24 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
const actionMapper = this.root.keyMapper; const actionMapper = this.root.keyMapper;
let newIndex = this.lastSelectedIndex; let newIndex = this.lastSelectedIndex;
if (actionMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()) { for (let i = 0; i < toolbarBuildings.length; ++i) {
for (let i = 0; i < toolbarBuildings.length; --i, --newIndex) { newIndex = this.calculateNewCycleIndex(newIndex, toolbarBuildings.length);
newIndex = (newIndex + toolbarBuildings.length) % toolbarBuildings.length; if (this.isBuildingSelectable(newIndex)) {
if (this.isCycledBuildingSelectable(newIndex)) {
break; break;
} }
} }
} else {
for (let i = 0; i < toolbarBuildings.length; ++i, ++newIndex) {
newIndex %= toolbarBuildings.length;
if (this.isCycledBuildingSelectable(newIndex)) {
break;
}
}
}
const metaBuildingClass = toolbarBuildings[newIndex]; const metaBuildingClass = toolbarBuildings[newIndex];
const metaBuilding = gMetaBuildingRegistry.findByClass(metaBuildingClass); const metaBuilding = gMetaBuildingRegistry.findByClass(metaBuildingClass);
this.selectBuildingForPlacement(metaBuilding); this.selectBuildingForPlacement(metaBuilding);
} }
isCycledBuildingSelectable(toolbarIndex) { /**
*
* @param {number} toolbarIndex The current toolbar index
* @return {boolean} true if building is selectable, otherwise false
*/
isBuildingSelectable(toolbarIndex) {
const metaBuilding = gMetaBuildingRegistry.findByClass(toolbarBuildings[toolbarIndex]); const metaBuilding = gMetaBuildingRegistry.findByClass(toolbarBuildings[toolbarIndex]);
const handle = this.buildingHandles[metaBuilding.id]; const handle = this.buildingHandles[metaBuilding.id];
if (!handle.selected && handle.unlocked) { if (!handle.selected && handle.unlocked) {
@ -142,6 +139,25 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
return false; return false;
} }
/**
*
* @param {number} index the current index
* @param {number} length the number of length of the array
* @returns {number} the next index
*/
calculateNewCycleIndex(index, length) {
const actionMapper = this.root.keyMapper;
let cycleDirection;
if (actionMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()) {
cycleDirection = -1;
} else {
cycleDirection = 1;
}
return (index + cycleDirection + length) % length;
}
/** /**
* @param {MetaBuilding} metaBuilding * @param {MetaBuilding} metaBuilding
*/ */