-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveLoad.js
More file actions
129 lines (111 loc) · 3.66 KB
/
saveLoad.js
File metadata and controls
129 lines (111 loc) · 3.66 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
122
123
124
125
126
127
128
129
import {Entity, Component, System} from './engine.js';
import {Corn} from "./../corn.js";
import {Cow, Poop} from './../cow.js'
import {Slim,game} from './../SlimReaper.js'
export class SaveComponent extends Component{
constructor(){
super("save");
}
}
export class SaveLoad {
constructor() {
this.saveFileName = "save.json";
this.entities = [];
this.entityConstructors = {};
}
registerEntityConstructor(type, constructor) {
if(!constructor){
console.log("entity type: "+ type +" regestered but missing constructor")
}
this.entityConstructors[type] = constructor;
}
save(saveFileName) {
// Collect the data to save
const saveData = [];
for (const entity of game.entities.values()) {
if(entity.hasComponent("save")){
let entery = {};
if(entity.serialize){
entery = entity.serialize();
}
else{
console.log("Entity has save componet but no serialize function defined. "+entity.type);
}
entery.id = entity.uniqueID;
if(entity.hasComponent("node")){
const node = entity.getComponent("node");
entery.node = {};
entery.node.parent = node.parent?node.parent.uniqueID:false;
entery.node.children = [];
for(const n of node.nodes){
if(n.child){
entery.node.children.push({id:n.child.uniqueID,attachment:n.attachment});
}
else{
entery.node.children.push(false);
}
}
}
saveData.push(entery);
}
}
// Serialize the data
const serializedData = JSON.stringify(saveData);
// Save the data to a file
localStorage.setItem(saveFileName, serializedData);
}
createEntityFromSavedData(savedData) {
let entity = {};//this.entities[savedData.id];
//if (!entity) {
const constructor = this.entityConstructors[savedData.name];
if (!constructor) {
throw new Error(`No constructor registered for entity type ${savedData.name}`);
}
entity = constructor(savedData);
entity.id = savedData.id;
if(savedData.componentData){
for(const componentData of savedData.componentData){
const component = entity.getComponent(componentData.name);
const properties = Object.keys(componentData.properties);
for (const property of properties) {
component[property] = componentData.properties[property];
}
}
}
return entity;
}
load(saveFileName) {
// Load the data from the file
const serializedData = localStorage.getItem(saveFileName);
this.entities = new Map();
// Deserialize the data
if (!serializedData) {
return;
}
const saveData = JSON.parse(serializedData);
// build all the entities
for (const entityData of saveData) {
const entity = this.createEntityFromSavedData(entityData);
this.entities.set(entityData.id, {entity: entity, node:entityData.node});
}
for(const entity of this.entities.values()){
if(entity.node){
const node = entity.entity.getComponent("node");
if(entity.node.parent){
node.parent = this.entities.get(entity.node.parent).entity;
}
for(let i = 0; i < entity.node.children.length; i++){
const child = entity.node.children[i];
if(child){
node.nodes[i].child = this.entities.get(child.id).entity;
node.nodes[i].attachment = child.attachment;
}
}
}
}
// loop through all the entities and add them to the game.
for (const entity of this.entities.values()){
game.addEntity(entity.entity);
}
}
}