-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintercom_outside.ts
More file actions
78 lines (70 loc) · 3.19 KB
/
intercom_outside.ts
File metadata and controls
78 lines (70 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* The intercom_outside.ts is a script that interacts with another room script,
* intercom_inside.ts, to allow someone in the "inside" room to turn on the
* intercom and let the characters speak with one another between the two rooms.
*
* While the intercom is active, all room events are passed to the inside room.
*/
// Post address to the inside room script. Replace this.
// To get a room script's address, type: roomscript <KEYWORD>
const inside = "room.cccccccccccccccccccc#dddddddddddddddddddd"
// Checks if the intercom is active (turned on).
function isActive(): boolean {
// Get the stored "active" value to tell if the intercom is active.
// Null means inactive while anything else means turned active
return Store.getBuffer("active") != null
}
// onActivate is called when the script (not the intercom) is activated.
export function onActivate(): void {
// Start listening to the inside room for when intercom status changes.
Script.listen([inside])
// Start listening to the room if the intercom is active.
if (isActive()) {
Room.listen()
}
// Post a request to the inside room to resend the current intercom status
// just to make sure the rooms are synchronized.
Script.post(inside, "update")
}
// onMessage is called when the inside room script sends a message to this
// script.
export function onMessage(addr: string, topic: string, dta: string): void {
// Get the current active state of the intercom.
const active = isActive()
// If "on" is received, and the intercom was turned off, turn it on.
if (topic == "on" && !active) {
// Store a single byte as a way to tell the intercom is on.
Store.setBuffer("active", new ArrayBuffer(1))
// Start listening to events in this room to send to the inside room.
Room.listen()
// Send a describe to the room to tell the intercom turned on.
Room.describe("A static sound is heard from a speaker as a red light turns on, indicating recording. ((Use `say` to speak with anyone inside.))")
}
// If "off" is received, and the intercom was turned on, turn it off.
if (topic == "off" && active) {
// Delete the stored byte to tell the intercom is turned off.
Store.deleteKey("active")
// Stop listening to events in this room.
Room.unlisten()
// Send a describe to the room to tell the intercom turned off.
Room.describe("The speaker goes silent, and the red light fades out.")
}
// An "event" is sent from the inside room when someone inside said something.
// If the intercom is active, show the event message for the room.
if (topic == "event" && active) {
// Make sure the data is a "say" event. The inside room script
// shouldn't send any other event, but just to make sure.
if (Event.getType(dta) == "say") {
// Parse the json data into a Event.Say object.
let say = JSON.parse<Event.Say>(dta)
// Send a describe to the room with the say message.
Room.describe(`${say.char.name} says through the speaker, "${say.msg}"`)
}
}
}
// onRoomEvent is called when an event occurs in this room while Room.listen is
// called, which is the case when the intercom is active.
export function onRoomEvent(addr: string, evjson: string): void {
// Send the event to the inside room script.
Script.post(inside, "event", evjson)
}