Skip to content

Commit

Permalink
fix: Tiled does not support properties with same name
Browse files Browse the repository at this point in the history
So it is pointless to support those in this package.
Removes support for properties.getMany
Now, the "openLayer" and "closeLayer" properties accept several layers separated by a carriage return.
  • Loading branch information
moufmouf committed Aug 18, 2021
1 parent e7f4cdb commit 2f6533d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 69 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Now, add 2 properties to the variable:
- `openLayer`: this will contain the name of the layer that has the opened door
- `closeLayer`: this will contain the name of the layer that has the closed door

Note: for both `openLayer` and `closeLayer`, you can input several layer names separated by a new line character.

Whenever the value of the variable switches from true to false (or the opposite), the door will open or close.

#### Door sound
Expand Down
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@semantic-release/git": "^9.0.0",
"@semantic-release/npm": "^7.1.0",
"@tsconfig/svelte": "^1.0.10",
"@types/copy-webpack-plugin": "^8.0.1",
"@types/jest": "^26.0.22",
"@types/mini-css-extract-plugin": "^1.4.3",
"@types/webpack-dev-server": "^3.11.4",
Expand Down
74 changes: 36 additions & 38 deletions src/Features/doors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,34 @@ let playerY = 0;
*/
function updateDoorLayers(variable: VariableDescriptor): void {
if (WA.state[variable.name]) {
let layers = variable.properties.getMany("openLayer");
for (const layer of layers) {
WA.room.showLayer(layer as string);
let layers = variable.properties.mustGetString("openLayer");
for (const layer of layers.split("\n")) {
WA.room.showLayer(layer);
}

layers = variable.properties.getMany("closeLayer");
for (const layer of layers) {
WA.room.hideLayer(layer as string);
layers = variable.properties.mustGetString("closeLayer");
for (const layer of layers.split("\n")) {
WA.room.hideLayer(layer);
}
} else {
let layers = variable.properties.getMany("openLayer");
for (const layer of layers) {
WA.room.hideLayer(layer as string);
let layers = variable.properties.mustGetString("openLayer");
for (const layer of layers.split("\n")) {
WA.room.hideLayer(layer);
}

layers = variable.properties.getMany("closeLayer");
for (const layer of layers) {
WA.room.showLayer(layer as string);
layers = variable.properties.mustGetString("closeLayer");
for (const layer of layers.split("\n")) {
WA.room.showLayer(layer);
}
}
}

function playOpenSound(variable: VariableDescriptor): void {
const url = variable.properties.getOneString("openSound");
const radius = variable.properties.getOneNumber("soundRadius");
const url = variable.properties.getString("openSound");
const radius = variable.properties.getNumber("soundRadius");
let volume = 1;
if (radius) {
const distance = getDistance(variable.properties.getMany("openLayer") as string[]);
const distance = getDistance(variable.properties.mustGetString("openLayer").split("\n"));
if (distance > radius) {
return;
}
Expand All @@ -59,11 +59,11 @@ function playOpenSound(variable: VariableDescriptor): void {
}

function playCloseSound(variable: VariableDescriptor): void {
const url = variable.properties.getOneString("closeSound");
const radius = variable.properties.getOneNumber("soundRadius");
const url = variable.properties.getString("closeSound");
const radius = variable.properties.getNumber("soundRadius");
let volume = 1;
if (radius) {
const distance = getDistance(variable.properties.getMany("closeLayer") as string[]);
const distance = getDistance(variable.properties.mustGetString("closeLayer").split("\n"));
if (distance > radius) {
return;
}
Expand Down Expand Up @@ -114,12 +114,12 @@ function initDoorstep(
let keypadWebsite: EmbeddedWebsite | undefined = undefined;
let inZone = false;

const zoneName = properties.getOneString("zone");
const zoneName = properties.getString("zone");
if (!zoneName) {
throw new Error('Missing "zone" property on doorstep layer "' + name + '"');
}

const tag = properties.getOneString("tag");
const tag = properties.getString("tag");
let allowed = true;
if (tag && !WA.player.tags.includes(tag)) {
allowed = false;
Expand All @@ -132,8 +132,7 @@ function initDoorstep(
}

actionMessage = WA.ui.displayActionMessage({
message:
properties.getOneString("closeTriggerMessage") ?? "Press SPACE to close the door",
message: properties.getString("closeTriggerMessage") ?? "Press SPACE to close the door",
callback: () => {
WA.state[doorVariable] = false;
displayOpenDoorMessage();
Expand All @@ -146,8 +145,7 @@ function initDoorstep(
actionMessage.remove();
}
actionMessage = WA.ui.displayActionMessage({
message:
properties.getOneString("openTriggerMessage") ?? "Press SPACE to open the door",
message: properties.getString("openTriggerMessage") ?? "Press SPACE to open the door",
callback: () => {
WA.state[doorVariable] = true;
displayCloseDoorMessage();
Expand Down Expand Up @@ -180,15 +178,15 @@ function initDoorstep(

WA.room.onEnterZone(zoneName, () => {
inZone = true;
if (properties.getOneBoolean("autoOpen") && allowed) {
if (properties.getBoolean("autoOpen") && allowed) {
WA.state[doorVariable] = true;
return;
}

if (
!WA.state[doorVariable] &&
((accessRestricted && !allowed) || !accessRestricted) && // Do not display code if user is allowed by tag
(properties.getOneString("code") || properties.getOneString("codeVariable"))
(properties.getString("code") || properties.getString("codeVariable"))
) {
openKeypad(name);
return;
Expand All @@ -207,7 +205,7 @@ function initDoorstep(

WA.room.onLeaveZone(zoneName, () => {
inZone = false;
if (properties.getOneBoolean("autoClose")) {
if (properties.getBoolean("autoClose")) {
WA.state[doorVariable] = false;
}

Expand All @@ -219,24 +217,24 @@ function initDoorstep(

WA.state.onVariableChange(doorVariable).subscribe(() => {
if (inZone) {
if (!properties.getOneBoolean("autoClose") && WA.state[doorVariable] === true) {
if (!properties.getBoolean("autoClose") && WA.state[doorVariable] === true) {
displayCloseDoorMessage();
}

if (keypadWebsite && WA.state[doorVariable] === true) {
closeKeypad();
}

if (!properties.getOneBoolean("autoOpen") && WA.state[doorVariable] === false) {
if (!properties.getBoolean("autoOpen") && WA.state[doorVariable] === false) {
displayOpenDoorMessage();
}
}
});
}

function playBellSound(variable: VariableDescriptor): void {
const url = variable.properties.mustGetOneString("bellSound");
const radius = variable.properties.getOneNumber("soundRadius");
const url = variable.properties.mustGetString("bellSound");
const radius = variable.properties.getNumber("soundRadius");
let volume = 1;
if (radius) {
const distance = Math.sqrt(
Expand Down Expand Up @@ -268,17 +266,17 @@ function initBell(variable: VariableDescriptor): void {
function initBellLayer(bellVariable: string, properties: Properties): void {
let popup: Popup | undefined = undefined;

const zoneName = properties.mustGetOneString("zone");
const zoneName = properties.mustGetString("zone");

const bellPopupName = properties.getOneString("bellPopup");
const bellPopupName = properties.getString("bellPopup");

WA.room.onEnterZone(zoneName, () => {
if (!bellPopupName) {
WA.state[bellVariable] = (WA.state[bellVariable] as number) + 1;
} else {
popup = WA.ui.openPopup(bellPopupName, "", [
{
label: properties.getOneString("bellButtonText") ?? "Ring",
label: properties.getString("bellButtonText") ?? "Ring",
callback: () => {
WA.state[bellVariable] = (WA.state[bellVariable] as number) + 1;
},
Expand All @@ -300,22 +298,22 @@ export async function initDoors(): Promise<void> {
layersMap = await getLayersMap();

for (const variable of variables.values()) {
if (variable.properties.getOne("door")) {
if (variable.properties.get("door")) {
initDoor(variable);
}

if (variable.properties.getOne("bell")) {
if (variable.properties.get("bell")) {
initBell(variable);
}
}

for (const layer of layersMap.values()) {
const properties = new Properties(layer.properties);
const doorVariable = properties.getOneString("doorVariable");
const doorVariable = properties.getString("doorVariable");
if (doorVariable && layer.type === "tilelayer") {
initDoorstep(layer, doorVariable, properties);
}
const bellVariable = properties.getOneString("bellVariable");
const bellVariable = properties.getString("bellVariable");
if (bellVariable) {
initBellLayer(bellVariable, properties);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Features/variable_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export async function initVariableActions(): Promise<void> {

for (const layer of layers.values()) {
const properties = new Properties(layer.properties);
const variableName = properties.getOneString("bindVariable");
const variableName = properties.getString("bindVariable");
if (variableName) {
const zone = properties.getOneString("zone");
const zone = properties.getString("zone");
if (!zone) {
throw new Error(
'A layer with a "bindVariable" property must ALSO have a "zone" property.',
);
}
const enterValue = properties.getOne("enterValue");
const leaveValue = properties.getOne("leaveValue");
const triggerMessage = properties.getOneString("triggerMessage");
const tag = properties.getOneString("tag");
const enterValue = properties.get("enterValue");
const leaveValue = properties.get("leaveValue");
const triggerMessage = properties.getString("triggerMessage");
const tag = properties.getString("tag");

initVariableActionLayer(
variableName,
Expand Down
6 changes: 3 additions & 3 deletions src/Iframes/Keypad/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ WA.onInit().then(async () => {
}

const properties = new Properties(layer.properties);
const tmpCode = properties.getOneString("code");
const codeVariable = properties.getOneString("codeVariable");
const tmpCode = properties.getString("code");
const codeVariable = properties.getString("codeVariable");

if (tmpCode === undefined && codeVariable === undefined) {
throw new Error('Missing "code" or "codeVariable" for layer "' + layerName + '".');
Expand All @@ -40,7 +40,7 @@ WA.onInit().then(async () => {
code = tmpCode as string;
}

const doorVariableVal = properties.getOneString("doorVariable");
const doorVariableVal = properties.getString("doorVariable");

if (doorVariableVal === undefined) {
throw new Error('Missing "doorVariable" for layer "' + layerName + '".');
Expand Down
40 changes: 18 additions & 22 deletions src/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ export class Properties {
this.properties = properties ?? [];
}

public getMany(name: string): (string | boolean | number | undefined)[] {
return this.properties
public get(name: string): string | boolean | number | undefined {
const values = this.properties
.filter((property) => property.name === name)
.map((property) => property.value);
}

public getOne(name: string): string | boolean | number | undefined {
const values = this.getMany(name);
if (values.length > 1) {
throw new Error('Expected only one property to be named "' + name + '"');
}
Expand All @@ -24,23 +20,23 @@ export class Properties {
return values[0];
}

public getOneString(name: string): string | undefined {
return this.getOneByType(name, "string") as string | undefined;
public getString(name: string): string | undefined {
return this.getByType(name, "string") as string | undefined;
}

public getOneNumber(name: string): number | undefined {
return this.getOneByType(name, "number") as number | undefined;
public getNumber(name: string): number | undefined {
return this.getByType(name, "number") as number | undefined;
}

public getOneBoolean(name: string): boolean | undefined {
return this.getOneByType(name, "boolean") as boolean | undefined;
public getBoolean(name: string): boolean | undefined {
return this.getByType(name, "boolean") as boolean | undefined;
}

private getOneByType(
private getByType(
name: string,
type: "string" | "number" | "boolean",
): string | boolean | number | undefined {
const value = this.getOne(name);
const value = this.get(name);
if (value === undefined) {
return undefined;
}
Expand All @@ -50,23 +46,23 @@ export class Properties {
return value;
}

public mustGetOneString(name: string): string {
return this.mustGetOneByType(name, "string") as string;
public mustGetString(name: string): string {
return this.mustGetByType(name, "string") as string;
}

public mustGetOneNumber(name: string): number {
return this.mustGetOneByType(name, "number") as number;
public mustGetNumber(name: string): number {
return this.mustGetByType(name, "number") as number;
}

public mustGetOneBoolean(name: string): boolean {
return this.mustGetOneByType(name, "boolean") as boolean;
public mustGetBoolean(name: string): boolean {
return this.mustGetByType(name, "boolean") as boolean;
}

private mustGetOneByType(
private mustGetByType(
name: string,
type: "string" | "number" | "boolean",
): string | boolean | number | undefined {
const value = this.getOne(name);
const value = this.get(name);
if (value === undefined) {
throw new Error('Property "' + name + '" is missing');
}
Expand Down

0 comments on commit 2f6533d

Please sign in to comment.