-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft-saves-inventory.js
148 lines (114 loc) · 3.93 KB
/
draft-saves-inventory.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
let INVENTORY = null;
function getInventoryFromPath(path) {
if(typeof path === "string") {
let inventory = INVENTORY;
let idList = path.split("/");
for(let i = 0; i < idList.length; ++i) {
let items = inventory.items;
if(idList[i] !== "") {
if(Array.isArray(items)) {
let next = items.find(function(item) {return item.id === idList[i];});
if(typeof next != "undefined") {
inventory = next;
} else {
return inventory;
}
} else {
return inventory;
}
}
}
return inventory;
}
return null;
}
function getInventoryItemPath(searchItem, inventory = INVENTORY, path = "/") {
let res = null;
for(let i = 0; i < inventory.items.length; ++i) {
const item = inventory.items[i];
if(item === searchItem) {
return path + item.id;
} else if(item instanceof Inventory) {
let r = getInventoryFromPath(searchItem, item, path + item.id + "/");
if(r) {
return r;
}
}
}
return res;
}
function saveGameState() {
let fileName, pathName;
if(typeof saveGameState.fileName === "function") {
fileName = saveGameState.fileName();
} else {
fileName = saveGameState.fileName;
}
// let filename = (new Date()).toJSON() + ".json";
// filename = filename.replace(/:/g, "'");
if(typeof saveGameState.pathName === "function") {
pathName = saveGameState.pathName(fileName);
} else {
pathName = fileName;
}
// pathname = "saves/" + filename;
//// Resetting values ////
saveGameState.outputtedType = null;
saveGameState.resFileName = null;
saveGameState.resPathName = null;
saveGameState.success = false;
//// Trying to save the game state somewhere ////
if(fileSystem_isUsable()) {
fs.writeFileSync(pathName, JSON.stringify(INVENTORY));
saveGameState.outputtedType = "file";
saveGameState.resFileName = fileName;
saveGameState.resPathName = pathName;
saveGameState.success = true;
}
else if(localStorage_isUsable()) {
localStorage.setItem("gameState", JSON.stringify(INVENTORY));
saveGameState.outputtedType = "local storage";
saveGameState.success = true;
}
}
saveGameState.outputtedType = null;
saveGameState.fileName = "gameState.json";
saveGameState.pathName = saveGameState.fileName;
saveGameState.resFileName = null;
saveGameState.resPathName = null;
saveGameState.success = false;
addEventListener("beforeunload", function() {
if(getCurrentChapter().saveOnQuit) {
getCurrentChapter().playerPositionM = PLAYERS[0].entity.getPositionM();
updateCurrentCharacter();
saveMapState();
saveGameState();
updateSaveState({chapterPath : getInventoryItemPath(getCurrentChapter())});
}
});
function inventory_findItem(inventory, predicate) {
for(let i = 0; i < inventory.items.length; ++i) {
const item = inventory.items[i];
if(predicate(item)) {
return item;
}
}
for(let i = 0; i < inventory.items.length; ++i) {
const item = inventory.items[i];
if(item instanceof Inventory) {
const foundItem = inventory_findItem(item, predicate);
if(foundItem !== null) {
return foundItem;
}
}
}
return null;
}
function findItem(predicate) {
return inventory_findItem(INVENTORY, predicate);
}
function findCharacterIdentifier() {
return findItem(function(item) {
return item instanceof CharacterIdentifier;
});
}