Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
fix: fix structure sounds not playing
Browse files Browse the repository at this point in the history
  • Loading branch information
leia-uwu committed Sep 21, 2023
1 parent db9b93a commit b59b71d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
35 changes: 22 additions & 13 deletions src/game/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectKind, CollisionType, type Orientation } from "../utils/constants";
import { log, deepCopy } from "../utils/misc";
import { TypeToId, Debug, Maps, Objects } from "../utils/data";
import { Debug, Maps, Objects } from "../utils/data";
import {
type MinMax,
random,
Expand Down Expand Up @@ -238,9 +238,9 @@ export class Map {
}
} else {
isVisible = object.position.x > minX &&
object.position.x < maxX &&
object.position.y > minY &&
object.position.y < maxY;
object.position.x < maxX &&
object.position.y > minY &&
object.position.y < maxY;
}
if (isVisible) visibleObjects.add(object);
}
Expand All @@ -259,40 +259,48 @@ export class Map {
for (let i = 0; i < count; i++) this.genStructure(type, building);
}

private genStructure (typeString: string, structureData: JSONObjects.Structure, setPosition?: Vec2, setOrientation?: Orientation): void {
private genStructure (typeString: string, structureData: JSONObjects.Structure, setPosition?: Vec2, setOrientation?: Orientation): Structure {
// TODO proper structure bounds, structures are being deleted from the client when they should still be visible
const orientation = setOrientation ?? random(0, 3) as Orientation;
const position = setPosition ?? this.getRandomPositionFor(ObjectKind.Structure, structureData, orientation, 1);

const layerObjIds: number[] = [];
const layerObjs: GameObject[] = [];

if (structureData.layers != null) {
for (let layerId = 0, length = structureData.layers.length; layerId < length; layerId++) {
const layerObj = structureData.layers[layerId];
const layerType = layerObj.type;
const layer = Objects[layerType];
layerObjIds.push(TypeToId[layerType]);

let layerOrientation: Orientation;
if (layerObj.inheritOri === false) layerOrientation = layerObj.ori;
else layerOrientation = addOrientations(layerObj.ori, orientation);
const layerPosition = addAdjust(position, Vec2(layerObj.pos), orientation);

if (layer.type === "structure") {
this.genStructure(layerType, layer, layerPosition, layerOrientation);
const object = this.genStructure(layerType, layer, layerPosition, layerOrientation);
layerObjs.push(object);
} else if (layer.type === "building") {
this.genBuilding(layerType, layer, layerPosition, layerOrientation, layerId);
const object = this.genBuilding(layerType, layer, layerPosition, layerOrientation, layerId);
layerObjs.push(object);
} else {
// console.warn(`Unsupported object type: ${layer.type}`);
}
}
}
this.game.staticObjects.add(new Structure(this.game, typeString, position, orientation, layerObjIds));
const structure = new Structure(this.game, typeString, position, orientation, layerObjs.map(object => object.id));
this.game.staticObjects.add(structure);

for (const object of layerObjs) {
if (object instanceof Building) object.parentStructure = structure;
}

if ("stairs" in structureData && Array.isArray(structureData.stairs)) {
for (const stairData of structureData.stairs) {
if (!stairData.lootOnly) this.game.stairs.add(new Stair(position, orientation ?? 0, stairData));
}
}
return structure;
}

private genBuildings (count: number, type: string, building: JSONObjects.Building): void {
Expand All @@ -308,7 +316,7 @@ export class Map {
setPosition?: Vec2,
setOrientation?: Orientation,
setLayer?: number,
debug = false): void {
debug = false): Building {
const orientation = setOrientation ?? (typeString.startsWith("cache_") ? 0 : random(0, 3) as Orientation);

const layer = setLayer ?? 0;
Expand Down Expand Up @@ -393,8 +401,8 @@ export class Map {
break;
// No such entry exists
// case "ignored":
// Ignored
// break;
// Ignored
// break;
}
}
if (buildingData.mapGroundPatches != null) {
Expand All @@ -421,6 +429,7 @@ export class Map {
}
}
this.game.staticObjects.add(building);
return building;
}

placeDebugMarker (position: Vec2): void {
Expand Down
9 changes: 8 additions & 1 deletion src/game/objects/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { type Game } from "../game";
import { type Obstacle } from "./obstacle";
import { type Player } from "./player";
import { type JSONObjects } from "../../jsonTypings";
import { type Structure } from "./structure";

export class Building extends GameObject {
showOnMap: boolean;
Expand Down Expand Up @@ -59,6 +60,8 @@ export class Building extends GameObject {

puzzlePieces: Obstacle[] = [];

parentStructure?: Structure;

declare kind: ObjectKind.Building;

constructor (game: Game,
Expand Down Expand Up @@ -182,7 +185,7 @@ export class Building extends GameObject {
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
damage (amount: number, source): void {}
damage (amount: number, source): void { }

playerIsOnZoomArea (player: Player): number {
if (this.ceiling.destroyed || !sameLayer(this.layer, player.layer)) return 0;
Expand Down Expand Up @@ -211,6 +214,10 @@ export class Building extends GameObject {
}
}
this.puzzle.solved = true;
if (this.parentStructure) {
this.parentStructure.altSound = true;
this.game.fullDirtyObjects.add(this.parentStructure);
}
setTimeout(this.resetPuzzle.bind(this), this.puzzle.completeOffDelay * 1000);
this.game.partialDirtyObjects.add(this);
} else if (this.puzzle.inputOrder.length >= this.puzzle.order.length) {
Expand Down
8 changes: 5 additions & 3 deletions src/game/objects/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class Structure extends GameObject {

declare kind: ObjectKind.Structure;

altSound = false;

constructor (game: Game,
typeString: string,
position: Vec2,
Expand All @@ -22,18 +24,18 @@ export class Structure extends GameObject {
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
serializePartial (stream: SurvivBitStream): void {}
serializePartial (stream: SurvivBitStream): void { }

serializeFull (stream: SurvivBitStream): void {
stream.writeVec(this.position, 0, 0, 1024, 1024, 16);
stream.writeMapType(this.typeId);
stream.writeBits(this.orientation, 2);
stream.writeBoolean(true); // Interior sound enabled
stream.writeBoolean(false); // Interior sound alt
stream.writeBoolean(this.altSound); // Interior sound alt
stream.writeUint16(this.layerObjIds[0]); // Layer 1 ID
stream.writeUint16(this.layerObjIds[1]); // Layer 2 ID
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
damage (amount: number, source): void {}
damage (amount: number, source): void { }
}

0 comments on commit b59b71d

Please sign in to comment.