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:
parent
bb781a67b2
commit
6266406a79
@ -381,7 +381,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
|
||||
index >= 0,
|
||||
"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];
|
||||
this.currentVariant.set(newVariant);
|
||||
|
||||
@ -395,17 +395,17 @@ export class HUDBuildingPlacer extends BaseHUDPart {
|
||||
* @param {number} availableVariantsLength the number of available variants
|
||||
* @returns {number} index for next variant
|
||||
*/
|
||||
calculateNewVariantCycleIndex(index, availableVariantsLength) {
|
||||
let indexModifier = 0;
|
||||
if (
|
||||
this.root.keyMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()
|
||||
) {
|
||||
indexModifier -= 1;
|
||||
calculateNewCycleIndex(index, availableVariantsLength) {
|
||||
const actionMapper = this.root.keyMapper;
|
||||
let cycleDirection;
|
||||
|
||||
if (actionMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()) {
|
||||
cycleDirection = -1;
|
||||
} else {
|
||||
indexModifier += 1;
|
||||
cycleDirection = 1;
|
||||
}
|
||||
|
||||
return (index + indexModifier + availableVariantsLength) % availableVariantsLength;
|
||||
return (index + cycleDirection + availableVariantsLength) % availableVariantsLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -112,27 +112,24 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
|
||||
const actionMapper = this.root.keyMapper;
|
||||
let newIndex = this.lastSelectedIndex;
|
||||
|
||||
if (actionMapper.getBinding(KEYMAPPINGS.placementModifiers.cycleInverse).isCurrentlyPressed()) {
|
||||
for (let i = 0; i < toolbarBuildings.length; --i, --newIndex) {
|
||||
newIndex = (newIndex + toolbarBuildings.length) % toolbarBuildings.length;
|
||||
if (this.isCycledBuildingSelectable(newIndex)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < toolbarBuildings.length; ++i, ++newIndex) {
|
||||
newIndex %= toolbarBuildings.length;
|
||||
if (this.isCycledBuildingSelectable(newIndex)) {
|
||||
break;
|
||||
}
|
||||
for (let i = 0; i < toolbarBuildings.length; ++i) {
|
||||
newIndex = this.calculateNewCycleIndex(newIndex, toolbarBuildings.length);
|
||||
if (this.isBuildingSelectable(newIndex)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const metaBuildingClass = toolbarBuildings[newIndex];
|
||||
const metaBuilding = gMetaBuildingRegistry.findByClass(metaBuildingClass);
|
||||
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 handle = this.buildingHandles[metaBuilding.id];
|
||||
if (!handle.selected && handle.unlocked) {
|
||||
@ -142,6 +139,25 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
|
||||
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
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user