1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-02-12 19:09:21 +00:00

Reorganizing Code

Moved some code around to make it a lot simplier.
This commit is contained in:
Triston Stuart 2020-06-24 09:44:18 -07:00
parent 91cbb7f496
commit 1e59a3db1a
2 changed files with 27 additions and 21 deletions

View File

@ -366,14 +366,17 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
/** /**
* Tries to place the current building at the given tile * Tries to place the current building at the given tile
* @param {Vector} tile * @param {Vector} tile
* @param {Object} options * @param {boolean} suppressSound
*/ */
tryPlaceCurrentBuildingAt(tile, options = {}) { tryPlaceCurrentBuildingAt(tile, suppressSound = false) {
if (this.root.camera.zoomLevel < globalConfig.mapChunkOverviewMinZoom) { if (this.root.camera.zoomLevel < globalConfig.mapChunkOverviewMinZoom) {
// Dont allow placing in overview mode // Dont allow placing in overview mode
return; return;
} }
// Set sound to true if no options.suppressSound or set to opposite of options.suppressSound.
const sound = !suppressSound;
const metaBuilding = this.currentMetaBuilding.get(); const metaBuilding = this.currentMetaBuilding.get();
const { rotation, rotationVariant } = metaBuilding.computeOptimalDirectionAndRotationVariantAtTile( const { rotation, rotationVariant } = metaBuilding.computeOptimalDirectionAndRotationVariantAtTile(
this.root, this.root,
@ -389,7 +392,6 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
originalRotation: this.currentBaseRotation, originalRotation: this.currentBaseRotation,
building: this.currentMetaBuilding.get(), building: this.currentMetaBuilding.get(),
variant: this.currentVariant.get(), variant: this.currentVariant.get(),
sound: options.suppressSound != null ? !options.suppressSound : true,
}); });
if (entity) { if (entity) {
@ -415,6 +417,12 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
// Stop placement // Stop placement
this.currentMetaBuilding.set(null); this.currentMetaBuilding.set(null);
} }
// Building has been placed, play sound
if (sound) {
this.root.soundProxy.playUi(metaBuilding.getPlacementSound());
}
return true; return true;
} else { } else {
return false; return false;
@ -449,21 +457,26 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
*/ */
executeDirectionLockedPlacement() { executeDirectionLockedPlacement() {
const path = this.computeDirectionLockPath(); const path = this.computeDirectionLockPath();
let needSound = false; let trySound = false;
this.root.logic.performBulkOperation(() => { this.root.logic.performBulkOperation(() => {
for (let i = 0; i < path.length; ++i) { for (let i = 0; i < path.length; ++i) {
const { rotation, tile } = path[i]; const { rotation, tile } = path[i];
this.currentBaseRotation = rotation; this.currentBaseRotation = rotation;
// Add supressSound flag
let addedBuilding = this.tryPlaceCurrentBuildingAt(tile, { /*
suppressSound: i == 0 || needSound ? false : true, Trys to place building.
}); Includes check to see if we should play sound.
if (!addedBuilding && (i == 0 || needSound)) { */
needSound = true; const placedBuilding = this.tryPlaceCurrentBuildingAt(
} else { tile,
needSound = false; i == 0 || trySound ? false : true
} );
/*
If placedBuilding is false then the building didn't place.
If the sound hasn't played yet we will need to try to play sound again.
*/
trySound = !placedBuilding && (i == 0 || trySound);
} }
}); });
} }

View File

@ -6,7 +6,6 @@ import { StaticMapEntityComponent } from "./components/static_map_entity";
import { Math_abs, performanceNow } from "../core/builtins"; import { Math_abs, performanceNow } from "../core/builtins";
import { createLogger } from "../core/logging"; import { createLogger } from "../core/logging";
import { MetaBeltBaseBuilding, arrayBeltVariantToRotation } from "./buildings/belt_base"; import { MetaBeltBaseBuilding, arrayBeltVariantToRotation } from "./buildings/belt_base";
import { SOUNDS } from "../platform/sound";
import { round2Digits } from "../core/utils"; import { round2Digits } from "../core/utils";
const logger = createLogger("ingame/logic"); const logger = createLogger("ingame/logic");
@ -151,11 +150,9 @@ export class GameLogic {
* @param {number} param0.rotationVariant * @param {number} param0.rotationVariant
* @param {string} param0.variant * @param {string} param0.variant
* @param {MetaBuilding} param0.building * @param {MetaBuilding} param0.building
* @param {boolean} param0.sound
* @returns {Entity} * @returns {Entity}
*/ */
tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building, sound }) { tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building }) {
sound = sound !== null ? sound : true; // Check for sound argument
if (this.checkCanPlaceBuilding({ origin, rotation, rotationVariant, variant, building })) { if (this.checkCanPlaceBuilding({ origin, rotation, rotationVariant, variant, building })) {
// Remove any removeable entities below // Remove any removeable entities below
const checker = new StaticMapEntityComponent({ const checker = new StaticMapEntityComponent({
@ -188,10 +185,6 @@ export class GameLogic {
variant, variant,
}); });
// Play sound
if (sound) {
this.root.soundProxy.playUi(building.getPlacementSound());
}
return entity; return entity;
} }
return null; return null;