Skip to content

Commit 7027fa2

Browse files
committed
Move auto-serialization into the default input class.
1 parent 10f84d3 commit 7027fa2

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

netplayjs-client/src/defaultinput.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { NetplayInput } from "./netcode/types";
22
import * as utils from "./utils";
33
import { TouchControl } from "./touchcontrols";
44
import { Vec2 } from "./vec2";
5+
import { JsonObject, JsonValue } from "type-fest";
6+
import * as autoserialize from "./serialization/autoserialize";
57

68
export class DefaultInput extends NetplayInput {
79
keysPressed: { [key: string]: boolean } = {};
@@ -29,6 +31,20 @@ export class DefaultInput extends NetplayInput {
2931
(this.keysHeld.s ? -1 : 0) + (this.keysHeld.w ? 1 : 0)
3032
);
3133
}
34+
35+
/**
36+
* By default, use the auto serializer.
37+
*/
38+
serialize(): JsonValue {
39+
return autoserialize.serialize(this);
40+
}
41+
42+
/**
43+
* By default, use the auto deserializer.
44+
*/
45+
deserialize(value: JsonValue): void {
46+
autoserialize.deserialize(value as JsonObject, this);
47+
}
3248
}
3349

3450
export class DefaultInputReader {

netplayjs-client/src/netcode/types.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,11 @@ export abstract class NetplayInput {
4949
}
5050

5151
/**
52-
* By default, use the auto serializer.
53-
*/
54-
serialize(): JsonValue {
55-
return autoserialize.serialize(this);
56-
}
57-
58-
/**
59-
* By default, use the auto deserializer.
52+
* We need to be able to serialize and deserialize the input
53+
* so we can send it across the network.
6054
*/
61-
deserialize(value: JsonValue): void {
62-
autoserialize.deserialize(value as JsonObject, this);
63-
}
55+
abstract serialize(): JsonValue;
56+
abstract deserialize(value: JsonValue);
6457
}
6558

6659
/**
@@ -91,4 +84,4 @@ export class NetplayPlayer {
9184
getID(): number {
9285
return this.id;
9386
}
94-
}
87+
}

0 commit comments

Comments
 (0)