Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/room-manager.js
2941 views
1
var EditorRoom = function(id)
2
{
3
this.id = id;
4
this.items = [];
5
this.walkBoxes = {};
6
this.zOrderMap = {};
7
this.setId = setId;
8
this.walkablePath = null;
9
this.walkBehinds = [];
10
this.onEnterScript = null;
11
this.onExitScript = null;
12
};
13
14
var setId = function(newId)
15
{
16
for(var i = 0; i < this.items.length; i++)
17
this.items[i].parentRoomId = newId;
18
19
for(var i = 0; i < editorCharactersList.length; i++)
20
if(editorCharactersList[i].parentRoomId == this.id)
21
editorCharactersList[i].parentRoomId = newId;
22
23
this.id = newId;
24
};
25
26
var EditorItem = function(id, type, pRoomId)
27
{
28
this.id = id;
29
this.type = type;
30
this.walkspot = { x : null, y : null};
31
this.hotspot = null;
32
this.visible = true;
33
this.layer = 2;
34
this.description = '';
35
this.parentRoomId = pRoomId;
36
this.setLayer = setItemLayer;
37
this.position = { x : null, y : null };
38
this.anim_state = null;
39
this.dir = null;
40
this.defaultAnims = {};
41
this.customAnims = {};
42
this.speechColor = '#ffffff';
43
this.faceDir = 'Left';
44
this.getCurrentFrame = getItemCurrentFrame;
45
this.maxScaleFactor = 100;
46
this.exitTo = { 'room' : null, xPos : null, yPos : null };
47
48
switch(type)
49
{
50
case 'character':
51
var initAnims = function() { return { 'FL' : null, 'FR' : null, 'FF' : null, 'FB' : null, 'FFL' : null, 'FFR' : null, 'FBL' : null, 'FBR' : null }; };
52
this.defaultAnims.stand = initAnims();
53
this.defaultAnims.walk = initAnims();
54
this.defaultAnims.talk = initAnims();
55
this.anim_state = 'stand';
56
this.dir = 'FL';
57
break;
58
case 'object':
59
this.defaultAnims.default = null;
60
this.anim_state = 'default';
61
break;
62
default:
63
throw 'Error: invalid object type';
64
break;
65
}
66
};
67
68
var getItemPlaceHolder = function(item)
69
{
70
var anim;
71
72
switch(item.type)
73
{
74
case 'object':
75
anim = editorMapIdAnim[item.defaultAnims.default];
76
break;
77
case 'character':
78
anim = editorMapIdAnim[item.defaultAnims.stand.FL];
79
break;
80
}
81
if(anim)
82
return anim.frames[anim.start_idx];
83
return null;
84
};
85
86
var setItemLayer = function(l, which)
87
{
88
l = parseInt(l);
89
90
var parentRoom;
91
92
if(!which)
93
parentRoom = editorMapIdRoom[this.parentRoomId];
94
else
95
parentRoom = testMapIdRoom[this.parentRoomId];
96
if(parentRoom.zOrderMap[this.layer])
97
{
98
var index = parentRoom.zOrderMap[this.layer].indexOf(this.id);
99
if(index !== -1)
100
{
101
parentRoom.zOrderMap[this.layer].splice(index, 1);
102
if (parentRoom.zOrderMap[this.layer].length == 0)
103
delete parentRoom.zOrderMap[this.layer];
104
}
105
}
106
if(parentRoom.zOrderMap[l] == undefined)
107
parentRoom.zOrderMap[l] = [];
108
parentRoom.zOrderMap[l].push(this.id);
109
110
this.layer = l;
111
};
112
113
var getItemCurrentFrame = function()
114
{
115
var anim;
116
117
switch(this.type)
118
{
119
case 'object':
120
anim = testMapIdAnim[this.defaultAnims[this.anim_state][this.dir]];
121
break;
122
case 'character':
123
if(this.anim_state in this.defaultAnims)
124
try {anim = testMapIdAnim[this.defaultAnims[this.anim_state][this.dir]];}
125
catch(err) { alert(err); return; }
126
else if(this.anim_state in this.customAnims)
127
try {anim = testMapIdAnim[this.customAnims[this.anim_state]];}
128
catch(err) { alert(err); return; }
129
130
break;
131
}
132
if(anim)
133
return anim.frames[anim.start_idx];
134
return null;
135
};
136
137
var EditorSprite = function(image)
138
{
139
this.img = image;
140
this.centralPerspectiveWalkBehind = null;
141
//this.boundingBox = new paper.Rectangle(0, 0, image.width, image.height);
142
this.boundingBox = new paper.Rectangle();
143
this.setPosition = function setPosition(left, top)
144
{
145
//this.boundingBox = new paper.Rectangle(left, top, this.boundingBox.width, this.boundingBox.height);
146
this.boundingBox.setTopLeft(left, top);
147
};
148
this.setSize = function setSize(width, height)
149
{
150
this.boundingBox.setSize(width, height);
151
};
152
//this.setPosition(0, 0);
153
};
154
155
var editorRoomsList = [];
156
var editorRoomsCount = 0;
157
var editorItemsCount = 0;
158
var editorMapIdRoom = {};
159
var editorMapIdItem = {};
160
var editorCurrentRoom = null;
161
var editorCurrentItem = null;
162
var editorCurrentWalkBox = null;
163
var editorMapIdWb = {};
164
var editorWbCount = 0;
165
166
var createNewEditorRoom = function(id)
167
{
168
var newEditorRoom = new EditorRoom(id);
169
editorRoomsList.push(newEditorRoom);
170
editorMapIdRoom[id] = newEditorRoom;
171
};
172
173
var setEditorRoomBackground = function(event)
174
{
175
if(!event.target.files[0].type.match(/image.*/))
176
{
177
alert("Please choose an image file!");
178
return;
179
}
180
$('.previous-file').hide();
181
var newEditorItem;
182
var fileReader = new FileReader();
183
fileReader.onload = function(event){
184
var img = new Image();
185
img.onload = function(){
186
var bg = new EditorSprite(img);
187
bg.setPosition(0, 0);
188
bg.setSize(img.width, img.height);
189
editorCurrentRoom.items[0] = bg;
190
//editorCurrentRoom.zOrderMap[0] = newEditorItem;
191
updateCanvas(editorCurrentRoom, 'room');
192
$('.item').show();
193
};
194
img.src = event.target.result;
195
img.draggable="true";
196
};
197
fileReader.readAsDataURL(event.target.files[0]);
198
};
199
200
var setEditorItemSprite = function(event, dfd)
201
{
202
if(!event.target.files[0].type.match(/image.*/))
203
{
204
alert("Please choose an image file!");
205
return;
206
}
207
$('.previous-file').hide();
208
var fileReader = new FileReader();
209
fileReader.onload = function(event)
210
{
211
var img = new Image();
212
img.onload = function()
213
{
214
editorMapIdItem[editorCurrentItem.id].sprite = new EditorSprite(img);
215
editorMapIdItem[editorCurrentItem.id].sprite.setPosition(-img.width, -img.height);
216
editorMapIdItem[editorCurrentItem.id].sprite.setSize(img.width, img.height);
217
if(editorCurrentRoom.zOrderMap[editorCurrentItem.layer] == undefined)
218
editorCurrentRoom.zOrderMap[editorCurrentItem.layer] = [];
219
editorCurrentRoom.zOrderMap[editorCurrentItem.layer].push(editorCurrentItem.id);
220
dfd.resolve();
221
};
222
img.src = event.target.result;
223
img.draggable="true";
224
};
225
fileReader.readAsDataURL(event.target.files[0]);
226
};
227
228
var createNewEditorItem = function(id, parentRoomId)
229
{
230
var newEditorItem = new EditorItem(id, 'object', parentRoomId);
231
editorCurrentRoom.items.push(newEditorItem);
232
editorMapIdItem[id] = newEditorItem;
233
if(editorCurrentRoom.zOrderMap[newEditorItem.layer] == undefined)
234
editorCurrentRoom.zOrderMap[newEditorItem.layer] = [];
235
editorCurrentRoom.zOrderMap[newEditorItem.layer].push(newEditorItem.id);
236
};
237
238
var deleteEditorRoom = function(roomId)
239
{
240
var room = editorMapIdRoom[roomId];
241
for(var i = 1; i < room.items.length; i++)
242
{
243
delete editorMapIdItem[room.items[i].id];
244
}
245
editorRoomsList.splice(editorRoomsList.indexOf(room), 1);
246
delete editorMapIdRoom[room.id];
247
};
248
249
var deleteEditorItem = function(itemId)
250
{
251
var item = editorMapIdItem[itemId];
252
var room = editorMapIdRoom[item.parentRoomId];
253
254
delete editorMapIdItem[itemId];
255
256
room.zOrderMap[item.layer].splice(room.zOrderMap[item.layer].indexOf(item.id));
257
room.items.splice(room.items.indexOf(item), 1);
258
};
259
260
var checkIdUniqueness = function(id, type)
261
{
262
switch(type)
263
{
264
case 'room': return !(id in editorMapIdRoom);
265
case 'item':
266
for(var i = 0; i < editorRoomsList.length; i++)
267
for(var j = 1; j < editorRoomsList[i].items.length; j++)
268
if(editorRoomsList[i].items[j].id == id)
269
return false;
270
return true;
271
case 'action': return !(id in editorMapIdAction);
272
case 'character': return !(id in editorMapIdCharacter);
273
case 'anim': return !(id in editorMapIdAnim);
274
case 'inventory-item': return !(id in editorMapIdInvItem);
275
case 'dialog' : return !(id in editorMapIdDialog);
276
}
277
};
278
279
var createNewEditorWalkBehind = function(id, parentRoomId)
280
{
281
editorMapIdRoom[parentRoomId].walkBehinds.push({ id : id, image : null, poly: null, position : null, centralPerspectiveWalkBehind : null });
282
editorMapIdWb[id] = editorMapIdRoom[parentRoomId].walkBehinds[editorMapIdRoom[parentRoomId].walkBehinds.length - 1];
283
};
284
285
286