Skip to content

Commit f999ead

Browse files
committed
allow positions to be a tuple
1 parent c54e1ee commit f999ead

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/client/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Buffer } from 'node:buffer'
55
import type { SetOptional } from 'type-fest'
66
import { deserializeMessage, serializeMessage } from '../codec.js'
77
import { Instruction } from '../index.js'
8-
import type { Message, Vector3 } from '../interfaces.js'
8+
import type { Message, Vector3Arg } from '../interfaces.js'
99
import { Replication } from '../worldql-fb/index.js'
1010
import type { ClientEvents as Events, MessagePayload } from './interfaces.js'
1111

@@ -154,7 +154,7 @@ export class Client extends EventEmitter<Events> {
154154
*/
155155
public localMessage(
156156
worldName: string,
157-
position: Readonly<Vector3>,
157+
position: Vector3Arg,
158158
replication = Replication.ExceptSelf,
159159
payload?: Readonly<MessagePayload>
160160
): void {
@@ -196,7 +196,7 @@ export class Client extends EventEmitter<Events> {
196196
* @param worldName World Name
197197
* @param position Area Position
198198
*/
199-
public areaSubscribe(worldName: string, position: Readonly<Vector3>): void {
199+
public areaSubscribe(worldName: string, position: Vector3Arg): void {
200200
this.sendRawMessage({
201201
instruction: Instruction.AreaSubscribe,
202202
worldName,
@@ -209,7 +209,7 @@ export class Client extends EventEmitter<Events> {
209209
* @param worldName World Name
210210
* @param position Area Position
211211
*/
212-
public areaUnsubscribe(worldName: string, position: Readonly<Vector3>): void {
212+
public areaUnsubscribe(worldName: string, position: Vector3Arg): void {
213213
this.sendRawMessage({
214214
instruction: Instruction.AreaUnsubscribe,
215215
worldName,

src/codec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Message,
66
Record,
77
Vector3,
8+
Vector3Arg,
89
} from './interfaces.js'
910
import {
1011
EntityT,
@@ -34,7 +35,13 @@ const decodeFlex: (flex: number[]) => Uint8Array = flex => {
3435
// #endregion
3536

3637
// #region Vector3
37-
const encodeVector3: (vec: Vector3) => Vec3dT = vec => {
38+
const encodeVector3: (vec: Vector3Arg) => Vec3dT = vec => {
39+
// Handle tuples
40+
if (Array.isArray(vec)) {
41+
const [x, y, z] = vec
42+
return new Vec3dT(x, y, z)
43+
}
44+
3845
return new Vec3dT(vec.x, vec.y, vec.z)
3946
}
4047

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export type { ClientOptions } from './client/client.js'
33
export type { ClientEvents } from './client/interfaces.js'
44
export type {
55
Vector3,
6+
Vector3Arg,
7+
Vector3Tuple,
68
Record,
79
Entity,
810
Message,

src/interfaces.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export interface Vector3 {
66
z: number
77
}
88

9+
export type Vector3Tuple = [x: number, y: number, z: number]
10+
export type Vector3Arg = Vector3 | Vector3Tuple
11+
912
export interface Record {
1013
uuid: string
1114
position: Vector3
@@ -29,10 +32,11 @@ export interface Message {
2932
replication: Replication
3033
records?: Record[]
3134
entities?: Entity[]
32-
position?: Vector3
35+
position?: Vector3Arg
3336
flex?: Uint8Array
3437
}
3538

3639
export interface IncomingMessage extends Message {
3740
senderUuid: string
41+
position?: Vector3
3842
}

0 commit comments

Comments
 (0)