pw-js-world 0.0.5-dev.24aedef → 0.0.5-dev.46673a2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Block.ts ADDED
@@ -0,0 +1,217 @@
1
+ import { BlockNames } from "pw-js-api";
2
+ import type { BlockArg, Point, SendableBlockPacket } from "./types";
3
+ import BufferReader, { ComponentTypeHeader } from "./BufferReader";
4
+ import { LayerType } from "./Constants";
5
+
6
+ export default class Block {
7
+ bId: number;
8
+ args: BlockArg[] = [];
9
+
10
+ constructor(bId: number | keyof typeof BlockNames, args?: BlockArg[]) {
11
+ if (typeof bId === "number") this.bId = bId;
12
+ else {
13
+ this.bId = BlockNames[bId];
14
+ }
15
+
16
+ if (args) this.args = args;
17
+ }
18
+
19
+ /**
20
+ * I mean... Just use .args.length !== 0 to see if it has args.
21
+ *
22
+ * But anyway, this will return true if there is at least one args, otherwise false.
23
+ */
24
+ hasArgs() : boolean {
25
+ return this.args.length !== 0;
26
+ }
27
+
28
+ /**
29
+ * For helper.
30
+ *
31
+ * This is in Block class for organisation.
32
+ *
33
+ * This will deserialise by using the reader to get the block ID then retrieve the args, if applicable.
34
+ */
35
+ static deserialize(reader: BufferReader) : Block {
36
+ return new Block(reader.readUInt32LE()).deserializeArgs(reader);
37
+ }
38
+
39
+ protected deserializeArgs(reader: BufferReader, flag = false) : this {
40
+ const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[this.name];
41
+
42
+ for (let i = 0; i < (format?.length ?? 0); i++) {
43
+ if (flag) {
44
+ reader.expectUInt8(format[i]);
45
+ }
46
+
47
+ this.args[i] = reader.read(format[i], !flag);
48
+ }
49
+
50
+ return this;
51
+ }
52
+
53
+ /**
54
+ * For helper.
55
+ *
56
+ * This is in Block class for organisation.
57
+ */
58
+ static deserializeArgs(blockId: number, reader: BufferReader, flag = false) : BlockArg[] {
59
+ const format: ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[blockId]];
60
+
61
+ const args = [];
62
+
63
+ for (let i = 0; i < (format?.length ?? 0); i++) {
64
+ if (flag) {
65
+ reader.expectUInt8(format[i]);
66
+ }
67
+
68
+ args[i] = reader.read(format[i], !flag);
69
+ }
70
+
71
+ return args;
72
+ }
73
+
74
+ /**
75
+ * Serializes the block into a buffer. This is used to convert
76
+ * the block into a binary format that can be sent over the game
77
+ * server. As this is static, block id and args are required.
78
+ *
79
+ * - Little Endian
80
+ * - With Id
81
+ * - Type Byte omitted
82
+ */
83
+ public static serializeArgs(bId: number, args: BlockArg[]): Buffer;
84
+
85
+ /**
86
+ * Serializes the block into a buffer. This is used to convert
87
+ * the block into a binary format that can be sent over the game
88
+ * server. As this is static, block id and args are required.
89
+ *
90
+ * - Big Endian
91
+ * - No Id
92
+ * - Type Byte included
93
+ */
94
+ public static serializeArgs(bId: number, args: BlockArg[], options: { endian: "big"; writeId: false; readTypeByte: true }): Buffer;
95
+
96
+ public static serializeArgs(bId: number, args: BlockArg[], options?: { endian: "little" | "big"; writeId: boolean; readTypeByte: boolean }): Buffer {
97
+ options ||= {
98
+ endian: "little",
99
+ writeId: true,
100
+ readTypeByte: false,
101
+ };
102
+
103
+ const buffer: Buffer[] = [];
104
+
105
+ if (options.writeId) {
106
+ const idBuffer = Buffer.alloc(4);
107
+ idBuffer.writeUInt32LE(bId);
108
+ buffer.push(idBuffer);
109
+ }
110
+
111
+ const blockData:ComponentTypeHeader[] = (BlockArgsHeadings as any)[BlockNames[bId]] ?? [];
112
+
113
+ for (let i = 0, len = blockData.length; i < len; i++) {
114
+ const entry = BufferReader.Dynamic(i, args[i]);
115
+ buffer.push(entry);
116
+ }
117
+
118
+ return Buffer.concat(buffer);
119
+ }
120
+
121
+ /**
122
+ *
123
+ * @param pos List of points (X and Y)
124
+ */
125
+ toPacket(pos: Point[], layer: LayerType) : SendableBlockPacket;
126
+ toPacket(x: number, y: number, layer: LayerType) : SendableBlockPacket;
127
+ toPacket(pos: Point[] | number, y: number, layer?: LayerType) : SendableBlockPacket {
128
+ if (typeof pos === "number") {
129
+ pos = [{
130
+ x: pos, y
131
+ }];
132
+
133
+ layer = layer ?? 0;
134
+ } else layer = y ?? 0;
135
+
136
+ return {
137
+ isFillOperation: false,
138
+ blockId: this.bId,
139
+ layer,
140
+ positions: pos,
141
+ extraFields: Block.serializeArgs(this.bId, this.args, { endian: "big", writeId: false, readTypeByte: true })
142
+ } satisfies SendableBlockPacket;
143
+ }
144
+
145
+ /**
146
+ * This will return the block name in UPPER_CASE form.
147
+ *
148
+ * For eg EFFECTS_INVULNERABILITY.
149
+ */
150
+ get name() : keyof typeof BlockNames {
151
+ return BlockNames[this.bId] as keyof typeof BlockNames;
152
+ }
153
+
154
+ /**
155
+ * Returns a copy of the block.
156
+ */
157
+ clone(obj?: false) : Block;
158
+ clone(obj: true) : { bId: number, args: BlockArg[], name: string }
159
+ clone(obj = false) {
160
+ if (obj === true) return { bId: this.bId, args: this.args, name: this.name };
161
+
162
+ return new Block(this.bId, this.args);
163
+ }
164
+ }
165
+
166
+ /**
167
+ * This mapping contains definitions of block data which require additional
168
+ * arguments to be sent or received with.
169
+ */
170
+ export const BlockArgsHeadings = {
171
+ COIN_GOLD_DOOR: [ComponentTypeHeader.Int32],
172
+ COIN_BLUE_DOOR: [ComponentTypeHeader.Int32],
173
+ COIN_GOLD_GATE: [ComponentTypeHeader.Int32],
174
+ COIN_BLUE_GATE: [ComponentTypeHeader.Int32],
175
+
176
+ EFFECTS_JUMP_HEIGHT: [ComponentTypeHeader.Int32],
177
+ EFFECTS_FLY: [ComponentTypeHeader.Boolean],
178
+ EFFECTS_SPEED: [ComponentTypeHeader.Int32],
179
+ EFFECTS_INVULNERABILITY: [ComponentTypeHeader.Boolean],
180
+ EFFECTS_CURSE: [ComponentTypeHeader.Int32],
181
+ EFFECTS_ZOMBIE: [ComponentTypeHeader.Int32],
182
+ EFFECTS_GRAVITYFORCE: [ComponentTypeHeader.Int32],
183
+ EFFECTS_MULTI_JUMP: [ComponentTypeHeader.Int32],
184
+ // gravity effects no data
185
+ // effects off
186
+ // effects zombie
187
+
188
+ TOOL_PORTAL_WORLD_SPAWN: [ComponentTypeHeader.Int32],
189
+
190
+ SIGN_NORMAL: [ComponentTypeHeader.String],
191
+ SIGN_RED: [ComponentTypeHeader.String],
192
+ SIGN_GREEN: [ComponentTypeHeader.String],
193
+ SIGN_BLUE: [ComponentTypeHeader.String],
194
+ SIGN_GOLD: [ComponentTypeHeader.String],
195
+
196
+ PORTAL: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
197
+ PORTAL_INVISIBLE: [ComponentTypeHeader.Int32, ComponentTypeHeader.Int32, ComponentTypeHeader.Int32],
198
+ PORTAL_WORLD: [ComponentTypeHeader.String, ComponentTypeHeader.Int32],
199
+
200
+ SWITCH_LOCAL_TOGGLE: [ComponentTypeHeader.Int32],
201
+ SWITCH_LOCAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
202
+ SWITCH_LOCAL_RESETTER: [ComponentTypeHeader.Boolean],
203
+ SWITCH_LOCAL_DOOR: [ComponentTypeHeader.Int32],
204
+ SWITCH_LOCAL_GATE: [ComponentTypeHeader.Int32],
205
+ SWITCH_GLOBAL_TOGGLE: [ComponentTypeHeader.Int32],
206
+ SWITCH_GLOBAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Boolean],
207
+ SWITCH_GLOBAL_RESETTER: [ComponentTypeHeader.Boolean],
208
+ SWITCH_GLOBAL_DOOR: [ComponentTypeHeader.Int32],
209
+ SWITCH_GLOBAL_GATE: [ComponentTypeHeader.Int32],
210
+
211
+ HAZARD_DEATH_DOOR: [ComponentTypeHeader.Int32],
212
+ HAZARD_DEATH_GATE: [ComponentTypeHeader.Int32],
213
+
214
+ NOTE_DRUM: [ComponentTypeHeader.ByteArray],
215
+ NOTE_PIANO: [ComponentTypeHeader.ByteArray],
216
+ NOTE_GUITAR: [ComponentTypeHeader.ByteArray],
217
+ } as const;