Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/project.js
2941 views
1
var resolution = { width : 1024, height : 768 };
2
3
var newProject = function()
4
{
5
$('#room-accordion').empty();
6
for(var room in editorMapIdRoom)
7
{
8
deleteEditorRoom(room);
9
view_UpdateDataLists('delete', 'rooms', room);
10
editorRoomsCount = 0;
11
editorItemsCount = 0;
12
}
13
$('#character-accordion').empty();
14
for(var character in editorMapIdCharacter)
15
{
16
deleteEditorCharacter(character);
17
}
18
$('#anims-accordion').empty();
19
for(var anim in editorMapIdAnim)
20
{
21
deleteEditorAnim(anim);
22
view_UpdateDataLists('delete', 'anims', anim);
23
}
24
$('#inventory-accordion').empty();
25
for(var invItem in editorMapIdInvItem)
26
{
27
deleteInvItem(invItem);
28
view_UpdateDataLists('delete', 'inventory-items', invItem);
29
editorInvItemCount = 0;
30
}
31
$('#dialog-accordion').empty();
32
for(var dialog in editorMapIdDialog)
33
{
34
deleteDialog(dialog);
35
view_UpdateDataLists('delete', 'dialogs', dialog);
36
editorDialogsCount = 0;
37
}
38
$('#audio-container').empty();
39
for(var audio in editorMapIdAudio)
40
{
41
deleteAudioContent(audio)
42
view_UpdateDataLists('delete', 'audio', audio);
43
}
44
for(var script in editorScriptList)
45
{
46
view_UpdateDataLists('delete', 'scripts', script);
47
}
48
editorScriptList = {};
49
};
50
51
var saveProject = function()
52
{
53
var backupRooms = owl.deepCopy(editorRoomsList);
54
var backupCharacters = owl.deepCopy(editorCharactersList);
55
var backupAnims = owl.deepCopy(editorAnimsList);
56
57
for(var i in backupRooms)
58
{
59
eliminateCycles(backupRooms[i], 'room');
60
delete backupRooms[i].zOrderMap;
61
}
62
for(var i = 0; i < backupAnims.length; i++)
63
eliminateCycles(backupAnims[i], 'anim');
64
65
view_SaveGameVariables();
66
var project = { resolution : JSON.stringify(resolution), rooms : JSON.stringify(backupRooms), actions: JSON.stringify(editorActionsList), characters: JSON.stringify(backupCharacters), scripts: JSON.stringify(editorScriptList), vars : JSON.stringify(editorGameVars), anims: JSON.stringify(backupAnims), inv_items : JSON.stringify(editorInvItemList), dialogs : JSON.stringify(editorMapIdDialog), audio : JSON.stringify(editorMapIdAudio) };
67
download('project.json', JSON.stringify(project));
68
};
69
70
var loadProject = function(event)
71
{
72
newProject();
73
var fileReader = new FileReader();
74
fileReader.onload = function(event)
75
{
76
var s = event.target.result;
77
var project = JSON.parse(s);
78
if(project.resolution)
79
resolution = JSON.parse(project.resolution);
80
editorRoomsList = JSON.parse(project.rooms);
81
editorMapIdRoom = {};
82
editorMapIdItem = {};
83
editorCurrentRoom = null;
84
editorCurrentItem = null;
85
editorRoomsCount = 0;
86
editorItemsCount = 0;
87
editorInvItemList = JSON.parse(project.inv_items);
88
editorInvItemCount = 0;
89
editorCharactersList = JSON.parse(project.characters);
90
editorMapIdCharacter = {};
91
editorAnimsList = JSON.parse(project.anims);
92
editorMapIdAnim = {};
93
editorAnimsCount = 0;
94
editorScriptList = JSON.parse(project.scripts);
95
if(project.dialogs)
96
editorMapIdDialog = JSON.parse(project.dialogs);
97
if(project.audio)
98
editorMapIdAudio = JSON.parse(project.audio);
99
100
if(project.vars) editorGameVars = JSON.parse(project.vars);
101
view_ClearRoomPanels();
102
103
for(var i = 0; i < editorAnimsList.length; i++)
104
{
105
editorAnimsList[i].addFrame = addAnimFrame;
106
editorAnimsList[i].removeFrame = removeAnimFrame;
107
editorAnimsList[i].incrCurrIdx = incrCurrFrame;
108
editorAnimsList[i].play = startRollingFrames;
109
editorAnimsList[i].stop = stopRollingFrames;
110
111
for(var j = 0; j < editorAnimsList[i].frames.length; j++)
112
{
113
var frame = editorAnimsList[i].frames[j];
114
if(frame)
115
{
116
var img = new Image();
117
img.src = frame.img;
118
//var newFrame = new EditorSprite(img);
119
//newFrame.setPosition(frame.boundingBox[1], frame.boundingBox[2]);
120
//newFrame.setSize(frame.boundingBox[3], frame.boundingBox[4]);
121
//editorAnimsList[i].frames[j] = newFrame;
122
editorAnimsList[i].frames[j] = {img: img};
123
}
124
}
125
editorMapIdAnim[editorAnimsList[i].id] = editorAnimsList[i];
126
view_CreateNewAnimPanel(editorAnimsList[i].id);
127
editorAnimsCount++;
128
}
129
130
for(var i = 0; i < editorCharactersList.length; i++)
131
{
132
var currentCharacter = editorCharactersList[i];
133
currentCharacter.getCurrentFrame = getItemCurrentFrame;
134
currentCharacter.setLayer = setItemLayer;
135
for(var j in currentCharacter.sprites)
136
for(var k in currentCharacter.sprites[j])
137
{
138
var src = currentCharacter.sprites[j][k].img;
139
var img = new Image();
140
img.src = src;
141
//var sprite = new EditorSprite(img);
142
//sprite.setPosition(currentCharacter.sprites[j][k].boundingBox[1], currentCharacter.sprites[j][k].boundingBox[2]);
143
//sprite.setSize(currentCharacter.sprites[j][k].boundingBox[3], currentCharacter.sprites[j][k].boundingBox[4]);
144
//currentCharacter.sprites[j][k] = sprite;
145
currentCharacter.sprites[j][k] = {img: img};
146
};
147
148
var hotspot = currentCharacter.hotspot;
149
150
if(hotspot != null)
151
{
152
var hotspotCopy = new Polygon();
153
for (var k = 0; k < hotspot.points.length; k++)
154
hotspotCopy.points[k] = new paper.Point(hotspot.points[k][1], hotspot.points[k][2]);
155
if(hotspot.closed)
156
hotspotCopy.close();
157
editorCharactersList[i].hotspot = hotspotCopy;
158
}
159
editorMapIdCharacter[editorCharactersList[i].id] = editorCharactersList[i];
160
view_CreateNewCharacterPanel(editorCharactersList[i].id);
161
}
162
163
for(var i in editorRoomsList)
164
{
165
editorRoomsList[i].setId = setId;
166
editorRoomsList[i].zOrderMap = {};
167
if(editorRoomsList[i].walkBehinds == undefined) // Project backward compatibility
168
editorRoomsList[i].walkBehinds = [];
169
editorMapIdRoom[editorRoomsList[i].id] = editorRoomsList[i];
170
editorRoomsCount++;
171
172
view_CreateNewRoomPanel(editorRoomsList[i].id);
173
174
if(editorRoomsList[i].walkablePath != null)
175
{
176
var path = editorRoomsList[i].walkablePath;
177
var pathCopy = new Polygon();
178
179
for(var j = 0; j < path.points.length; j++)
180
pathCopy.points[j] = new paper.Point(path.points[j][1], path.points[j][2]);
181
if(path.closed)
182
pathCopy.close();
183
184
if(path.holes.length)
185
{
186
var holesCopy = [];
187
188
for(var j = 0; j < path.holes.length; j++)
189
{
190
var hole = path.holes[j];
191
var newHole = new Polygon();
192
193
for(var k = 0; k < hole.points.length; k++)
194
{
195
newHole.points[k] = new paper.Point(hole.points[k][1], hole.points[k][2]);
196
}
197
holesCopy[j] = newHole;
198
if(hole.closed)
199
newHole.close();
200
}
201
202
pathCopy.holes = holesCopy;
203
}
204
editorRoomsList[i].walkablePath = pathCopy;
205
}
206
var bg = editorRoomsList[i].items[0];
207
if(bg)
208
{
209
var src = bg.img;
210
var img = new Image();
211
img.src = src;
212
editorRoomsList[i].items[0] = {img : img, boundingBox : new paper.Rectangle(bg.boundingBox[1], bg.boundingBox[2], bg.boundingBox[3], bg.boundingBox[4])};
213
}
214
for(var j = 1; j < editorRoomsList[i].items.length; j++)
215
{
216
editorRoomsList[i].items[j].setLayer = setItemLayer;
217
editorRoomsList[i].items[j].getCurrentFrame = getItemCurrentFrame;
218
for(var key in editorRoomsList[i].items[j].defaultAnims)
219
220
//if(animsDefined)
221
//{
222
if (editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer] == undefined)
223
editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer] = [];
224
editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer].push(editorRoomsList[i].items[j].id);
225
//}
226
editorMapIdItem[editorRoomsList[i].items[j].id] = editorRoomsList[i].items[j];
227
view_CreateEditorItemPanel(editorRoomsList[i].items[j].id, editorRoomsList[i].id);
228
editorItemsCount++;
229
230
var hotspot = editorRoomsList[i].items[j].hotspot;
231
232
if(hotspot != null)
233
{
234
var hotspotCopy = new Polygon();
235
for (var k = 0; k < hotspot.points.length; k++)
236
hotspotCopy.points[k] = new paper.Point(hotspot.points[k][1], hotspot.points[k][2]);
237
if(hotspot.closed)
238
hotspotCopy.close();
239
editorRoomsList[i].items[j].hotspot = hotspotCopy;
240
}
241
}
242
for(var j = 0; j < editorRoomsList[i].walkBehinds.length; j++)
243
{
244
var wb = editorRoomsList[i].walkBehinds[j];
245
editorMapIdWb[wb.id] = wb;
246
for(var k = 0; k < wb.poly.points.length; k++)
247
wb.poly.points[k] = new paper.Point(wb.poly.points[k][1], wb.poly.points[k][2]);
248
view_CreateEditorWBPanel(wb.id, editorRoomsList[i].id);
249
editorWbCount++;
250
}
251
for(var j in editorRoomsList[i].walkBoxes)
252
{
253
var wbox = editorRoomsList[i].walkBoxes[j];
254
var polyCopy = new Polygon();
255
for(var k = 0; k < wbox.polygon.points.length; k++)
256
polyCopy.points[k] = new paper.Point(wbox.polygon.points[k][1], wbox.polygon.points[k][2]);
257
if(wbox.polygon.closed)
258
polyCopy.close();
259
/*for(var k = 0; k < wbox.polygon.edges.length; k++)
260
{
261
polyCopy.edges[k] = [ new paper.Point(wbox.polygon.edges[k][0][1], wbox.polygon.edges[k][0][2]),
262
new paper.Point(wbox.polygon.edges[k][1][1], wbox.polygon.edges[k][1][2])];
263
}*/
264
editorRoomsList[i].walkBoxes[j].polygon = polyCopy;
265
view_CreateEditorWalkBoxPanel(wbox.id, editorRoomsList[i].id);
266
//editorWbCount++;
267
}
268
}
269
270
for(var i = 0; i < editorInvItemList.length; i++)
271
{
272
editorMapIdInvItem[editorInvItemList[i].id] = editorInvItemList[i];
273
view_CreateNewInvItemPanel(editorInvItemList[i].id);
274
editorInvItemCount++;
275
}
276
277
for(var key in editorScriptList)
278
{
279
//$('#datalist-game-scripts').append($(document.createElement('option')).append(key));
280
view_UpdateDataLists('add', 'scripts', key);
281
}
282
283
for(var key in editorMapIdDialog)
284
{
285
var d = new Dialog();
286
d.copy(editorMapIdDialog[key]);
287
editorMapIdDialog[key] = d;
288
editorDialogsCount++;
289
view_CreateNewDialogPanel(d.id);
290
}
291
292
view_LoadGameVariables();
293
294
$('#game-resolution')[0].value = resolution.width + 'x' + resolution.height;
295
$('#game-resolution').change();
296
297
for(var key in editorMapIdAudio)
298
{
299
view_AddNewSound(editorMapIdAudio[key].audioData, editorMapIdAudio[key].id);
300
}
301
};
302
303
fileReader.readAsText(event.target.files[0]);
304
};
305
306
var eliminateCycles = function(data, dataType)
307
{
308
switch(dataType)
309
{
310
case 'room':
311
if(data.items[0] != null)
312
data.items[0].img = data.items[0].img.src;
313
break;
314
case 'character':
315
if(data.sprite != null)
316
data.sprite.img = data.sprite.img.src;
317
for(var i in data.sprites)
318
for(var j in data.sprites[i])
319
if(data.sprites[i][j])
320
data.sprites[i][j].img = data.sprites[i][j].img.src;
321
else delete data.sprites[i][j];
322
break;
323
case 'anim':
324
for(var i = 0; i < data.frames.length; i++)
325
if(data.frames[i])
326
data.frames[i].img = data.frames[i].img.src;
327
break;
328
}
329
};
330
331
var download = function (filename, text)
332
{
333
var blob = new Blob([text], {type : 'text/html'});
334
var pom = document.createElement('a');
335
//pom.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(text));
336
pom.setAttribute('href', URL.createObjectURL(blob));
337
pom.setAttribute('download', filename);
338
pom.click();
339
};
340