1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-02-12 10:59:23 +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
* @param {Vector} tile
* @param {Object} options
* @param {boolean} suppressSound
*/
tryPlaceCurrentBuildingAt(tile, options = {}) {
tryPlaceCurrentBuildingAt(tile, suppressSound = false) {
if (this.root.camera.zoomLevel < globalConfig.mapChunkOverviewMinZoom) {
// Dont allow placing in overview mode
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 { rotation, rotationVariant } = metaBuilding.computeOptimalDirectionAndRotationVariantAtTile(
this.root,
@ -389,7 +392,6 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
originalRotation: this.currentBaseRotation,
building: this.currentMetaBuilding.get(),
variant: this.currentVariant.get(),
sound: options.suppressSound != null ? !options.suppressSound : true,
});
if (entity) {
@ -415,6 +417,12 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
// Stop placement
this.currentMetaBuilding.set(null);
}
// Building has been placed, play sound
if (sound) {
this.root.soundProxy.playUi(metaBuilding.getPlacementSound());
}
return true;
} else {
return false;
@ -449,21 +457,26 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
*/
executeDirectionLockedPlacement() {
const path = this.computeDirectionLockPath();
let needSound = false;
let trySound = false;
this.root.logic.performBulkOperation(() => {
for (let i = 0; i < path.length; ++i) {
const { rotation, tile } = path[i];
this.currentBaseRotation = rotation;
// Add supressSound flag
let addedBuilding = this.tryPlaceCurrentBuildingAt(tile, {
suppressSound: i == 0 || needSound ? false : true,
});
if (!addedBuilding && (i == 0 || needSound)) {
needSound = true;
} else {
needSound = false;
}
/*
Trys to place building.
Includes check to see if we should play sound.
*/
const placedBuilding = this.tryPlaceCurrentBuildingAt(
tile,
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 { createLogger } from "../core/logging";
import { MetaBeltBaseBuilding, arrayBeltVariantToRotation } from "./buildings/belt_base";
import { SOUNDS } from "../platform/sound";
import { round2Digits } from "../core/utils";
const logger = createLogger("ingame/logic");
@ -151,11 +150,9 @@ export class GameLogic {
* @param {number} param0.rotationVariant
* @param {string} param0.variant
* @param {MetaBuilding} param0.building
* @param {boolean} param0.sound
* @returns {Entity}
*/
tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building, sound }) {
sound = sound !== null ? sound : true; // Check for sound argument
tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building }) {
if (this.checkCanPlaceBuilding({ origin, rotation, rotationVariant, variant, building })) {
// Remove any removeable entities below
const checker = new StaticMapEntityComponent({
@ -188,10 +185,6 @@ export class GameLogic {
variant,
});
// Play sound
if (sound) {
this.root.soundProxy.playUi(building.getPlacementSound());
}
return entity;
}
return null;