-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.ts
More file actions
121 lines (118 loc) · 5.24 KB
/
serializer.ts
File metadata and controls
121 lines (118 loc) · 5.24 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/// <reference path="primitives.ts" />
/// <reference path="mapobject.ts" />
/// <reference path="wall.ts" />
/// <reference path="structure.ts" />
/// <reference path="object.ts" />
/// <reference path="spawn.ts" />
namespace MapEngine {
export class Serializer {
serialize(objects:MapObjects.MapObject[],is2x:boolean) {
let result = {
version:"1.1",
is2x:is2x,
objects:[]
};
objects.forEach(object => {
if(object instanceof MapObjects.Wall) {
result.objects.push({
type:"wall",
id:object.image.id,
position:{
x:object.position.x,
y:object.position.y
},
rotation:object.rotation
});
} else if(object instanceof MapObjects.Structure) {
let obj = {
type:"structure",
position: {
x:object.position.x,
y:object.position.y
},
elements:[]
};
object.elements.forEach(wall => {
if(wall instanceof MapObjects.Wall) {
obj.elements.push({
type:"wall",
id:wall.image.id,
position:{
x:wall.position.x,
y:wall.position.y
},
rotation:wall.rotation
});
}
});
result.objects.push(obj);
} else if (object instanceof MapObjects.Spawnpoint) {
let obj = {
type: "spawn",
position: {
x: object.position.x,
y: object.position.y
},
rotation: object.rotation,
id: object.image.id
};
result.objects.push(obj);
} else if(object instanceof MapObjects.Object) {
let obj = {
type: "object",
position: {
x: object.position.x,
y: object.position.y
},
rotation: object.rotation,
id: object.image.id
};
result.objects.push(obj);
}
});
return JSON.stringify(result);
}
deserialize(json:string):[MapObjects.MapObject[],boolean] {
let result:MapObjects.MapObject[] = [];
let is2x = false;
let decoded = JSON.parse(json);
if(decoded.version=="1.0" || decoded.version=="1.1") {
if(decoded.version=="1.1") {
is2x = decoded.is2x;
}
decoded.objects.forEach(object => {
if(object.type == "wall") {
let wall = new MapObjects.Wall($("#"+object.id)[0] as HTMLImageElement);
wall.rotation = object.rotation;
wall.position = new MapObjects.Point(object.position.x,object.position.y);
result.push(wall);
} else if(object.type == "structure") {
let structure = new MapObjects.Structure();
structure.position = new MapObjects.Point(object.position.x,object.position.y);
object.elements.forEach(element => {
if(element.type=="wall") {
let wall = new MapObjects.Wall($("#"+element.id)[0] as HTMLImageElement);
wall.position = new MapObjects.Point(element.position.x,element.position.y);
wall.rotation = element.rotation;
structure.elements.push(wall);
}
});
structure.rebuildJoints();
result.push(structure);
} else if(object.type == "object") {
let loaded = new MapObjects.Object($("#"+object.id)[0] as HTMLImageElement);
loaded.rotation = object.rotation;
loaded.position = new MapObjects.Point(object.position.x,object.position.y);
result.push(loaded);
} else if(object.type == "spawn") {
let loaded = new MapObjects.Spawnpoint($("#"+object.id)[0] as HTMLImageElement);
loaded.rotation = object.rotation;
loaded.position = new MapObjects.Point(object.position.x,object.position.y);
result.push(loaded);
}
});
}
return [result,is2x];
}
}
}